-
Notifications
You must be signed in to change notification settings - Fork 1
Add agent class #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Add agent class #10
Changes from all commits
15efe8b
c2611f5
aabb754
a891ed7
dc5378a
e2198fe
b799c90
39ec986
0b3c1f8
4acb509
ba9352b
2a05853
7828d84
2e6429c
e819d1e
f2917dc
8ddbb09
c04af1f
9324477
0e872b3
751d46a
a1df3db
b613103
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| # vs configuration code | ||
| .vscode/ | ||
|
|
||
| # Temporary files | ||
| *~* | ||
| *#* | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| #ifndef _ABM_AGENT_H_ | ||
| #define _ABM_AGENT_H_ | ||
|
|
||
| #include <memory> | ||
| #include <vector> | ||
|
|
||
| #include "config.h" | ||
| #include "graph.h" | ||
|
|
||
| namespace abm { | ||
|
|
||
| //! status class | ||
| enum class Status { YETTODEPART, ENROUTE, ARRIVED }; | ||
|
|
||
| //! Agent class that has id, origin, destination, departure time, current node | ||
| //! and compute path from current node to destination. | ||
| class Agent { | ||
| public: | ||
| //! Construct an agent class from either an id, or id, origin and destination | ||
| //! \param[in] id Agent id | ||
| //! \param[in] origin Agent origin | ||
| //! \param[in] destination Agent destination | ||
| explicit Agent(const graph::vertex_t& id) : id_{id} {}; | ||
| explicit Agent(const graph::vertex_t& id, const graph::vertex_t& origin, | ||
| const graph::vertex_t& destination) | ||
| : id_{id}, origin_{origin}, destination_{destination} {}; | ||
|
|
||
| //! Return agent id | ||
| graph::vertex_t id() const { return id_; } | ||
|
|
||
| //! Return origin id | ||
| graph::vertex_t origin() const { return origin_; } | ||
|
|
||
| //! Return destination id | ||
| graph::vertex_t destination() const { return destination_; } | ||
|
|
||
| //! Return departure time | ||
| double departure_time() const { return departure_time_; } | ||
| //! Assign departure time | ||
| void departure_time(double departure_time) { | ||
| departure_time_ = departure_time; | ||
| } | ||
|
|
||
| // Return current node id | ||
| graph::vertex_t current_node() const { return current_node_; } | ||
|
|
||
| //! Return agent status | ||
| //! \retval Status of the agent. | ||
| //! destination. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does it say destination? |
||
| Status status() const { return status_; } | ||
|
|
||
| private: | ||
| //! Agent id | ||
| graph::vertex_t id_{std::numeric_limits<graph::vertex_t>::max()}; | ||
|
|
||
| //! Agent origin vertex | ||
| graph::vertex_t origin_{std::numeric_limits<graph::vertex_t>::max()}; | ||
|
|
||
| //! Agent destination vertex | ||
| graph::vertex_t destination_{std::numeric_limits<graph::vertex_t>::max()}; | ||
|
|
||
| //! Agent departure time | ||
| graph::weight_t departure_time_{std::numeric_limits<graph::weight_t>::max()}; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a double |
||
|
|
||
| //! Agent current node | ||
| graph::vertex_t current_node_{std::numeric_limits<graph::vertex_t>::max()}; | ||
|
|
||
| //! Agent status | ||
| Status status_{Status::YETTODEPART}; | ||
| }; | ||
|
|
||
| } // namespace abm | ||
|
|
||
| #endif // _ABM_AGENT_H_ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // #include <vector> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove these if you are not using these |
||
|
|
||
| // #include "agent.h" | ||
| // #include "config.h" | ||
| // #include "graph.h" | ||
|
|
||
| // // Compute path from the current_node_ to destination_ | ||
| // void abm::Agent::compute_path() { | ||
| // // dijkstra function returns a list of vertices | ||
| // const auto path = graph_->dijkstra(current_node_, destination_); | ||
| // this->path_ = path; | ||
| // }; | ||
|
|
||
| // // Print path in the format of (start_of_edge, end_of_edge, | ||
| // w=weight_of_edge), | ||
| // // (s, e, w=), ... | ||
| // void abm::Agent::print_path() { | ||
| // if (path_.size() > 0) { | ||
| // for (auto itr = path_.begin(); itr != path_.end() - 1; ++itr) { | ||
| // auto nitr = itr + 1; | ||
| // auto weight = | ||
| // graph_->get_edge_cost(static_cast<abm::graph::vertex_t>(*itr), | ||
| // static_cast<abm::graph::vertex_t>(*nitr)); | ||
| // std::cout << "(" << *itr << ", " << *nitr << ", w=" << weight << ") "; | ||
| // } | ||
| // } else { | ||
| // std::cout << "path size is zero" << std::endl; | ||
| // } | ||
| // } | ||
|
|
||
| // // Move agent | ||
| // void abm::Agent::move_agent(graph::weight_t time_limit) { | ||
| // if (path_.size() > 0) { | ||
| // // Keep record of cumulated edge cost (cumulative travel time) not to | ||
| // exceed | ||
| // // the time_limit | ||
| // abm::graph::weight_t agent_time = 0; | ||
| // // Change/Keep agent status to 1 "enroute" | ||
| // this->status_ = 1; | ||
| // for (auto itr = path_.begin(); itr != path_.end() - 1; ++itr) { | ||
| // auto nitr = itr + 1; | ||
| // if (agent_time < time_limit) { | ||
| // // Add the cost of the next edge | ||
| // agent_time += | ||
| // graph_->get_edge_cost(static_cast<abm::graph::vertex_t>(*itr), | ||
| // static_cast<abm::graph::vertex_t>(*nitr)); | ||
| // this->current_node_ = static_cast<abm::graph::vertex_t>(*nitr); | ||
| // if (agent_time >= time_limit) { | ||
| // // If cumulative cost exceed the time_limit, make agent stops there | ||
| // // (even though it does not finishing the current link) | ||
| // this->current_node_ = static_cast<abm::graph::vertex_t>(*itr); | ||
| // // Update path, set the beginning of the path to the current node | ||
| // this->path_.erase(path_.begin(), itr); | ||
| // break; | ||
| // } | ||
| // } | ||
| // } | ||
| // } | ||
| // // When agent reachs destination, update its status to 2 "arrival" and | ||
| // delete | ||
| // // the path | ||
| // if (current_node_ == destination_) { | ||
| // this->status_ = 2; | ||
| // this->path_.clear(); | ||
| // } | ||
| // } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #include "catch.hpp" | ||
|
|
||
| #include "agent.h" | ||
|
|
||
| // Check agent class | ||
| TEST_CASE("Agent class is checked", "[agent]") { | ||
| // Tolerance | ||
| const double Tolerance = 1.E-7; | ||
|
|
||
| SECTION("Test agent initialization") { | ||
|
|
||
| // Parameters to be assigned to the test agent | ||
| abm::graph::vertex_t agent_id = 100; | ||
| abm::graph::vertex_t origin = 1; | ||
| abm::graph::vertex_t destination = 3; | ||
| abm::graph::weight_t departure_time = 0; | ||
|
|
||
| SECTION("Initialization with id") { | ||
| // Create test agent | ||
| auto test_agent = std::make_unique<abm::Agent>(agent_id); | ||
|
|
||
| // Check assigned values | ||
| REQUIRE(test_agent->id() == agent_id); | ||
| } | ||
|
|
||
| SECTION("Initialization with id, origin and destination") { | ||
| // Create test agent | ||
| auto test_agent = | ||
| std::make_unique<abm::Agent>(agent_id, origin, destination); | ||
|
|
||
| // Check assigned values | ||
| REQUIRE(test_agent->id() == agent_id); | ||
| REQUIRE(test_agent->origin() == origin); | ||
| REQUIRE(test_agent->destination() == destination); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please only declare functions in the header, do not define them. Please define functions in the implementation .cc file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I see
{ return nvertices_; }ingraph.h