From 15efe8bf9565c12225360ed30676862a5ad3ab25 Mon Sep 17 00:00:00 2001 From: bz247 Date: Wed, 18 Mar 2020 21:23:36 -0700 Subject: [PATCH 01/20] format agent unit test --- CMakeLists.txt | 2 ++ include/agent.h | 78 +++++++++++++++++++++++++++++++++++++++++++++ include/graph.h | 7 ++++ src/agent.cc | 53 ++++++++++++++++++++++++++++++ tests/agent_test.cc | 57 +++++++++++++++++++++++++++++++++ 5 files changed, 197 insertions(+) create mode 100644 include/agent.h create mode 100644 src/agent.cc create mode 100644 tests/agent_test.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 50e1e4a..035bcdb 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,6 +60,7 @@ include_directories(BEFORE SET(abm_src ${abm_SOURCE_DIR}/src/graph.cc ${abm_SOURCE_DIR}/src/router.cc + ${abm_SOURCE_DIR}/src/agent.cc ) add_executable(abm ${abm_src} ${abm_SOURCE_DIR}/src/main.cc) @@ -70,6 +71,7 @@ if(ABM_BUILD_TESTING) ${abm_SOURCE_DIR}/tests/test_main.cc ${abm_SOURCE_DIR}/tests/graph_test.cc ${abm_SOURCE_DIR}/tests/router_test.cc + ${abm_SOURCE_DIR}/tests/agent_test.cc ) add_executable(abmtest ${abm_src} ${test_src}) add_test(NAME abmtest COMMAND $) diff --git a/include/agent.h b/include/agent.h new file mode 100644 index 0000000..e6ccf3a --- /dev/null +++ b/include/agent.h @@ -0,0 +1,78 @@ +#ifndef _ABM_AGENT_H_ +#define _ABM_AGENT_H_ + +#include +#include + +#include "config.h" +#include "graph.h" +#include "agent.h" + +namespace abm { + +// Agent class with origin, destination, departure time. Computes current node and path. +class Agent { + public: + + // Construct an agent class + explicit Agent(const std::shared_ptr& graph) : graph_{graph} {}; + + // Set and get origin + graph::vertex_t get_origin() const { return origin_; } + void set_origin(graph::vertex_t origin) { + this->origin_ = origin; + this->current_node_ = origin; + } + + // Set and get destination + graph::vertex_t get_destination() const { return destination_; } + void set_destination(graph::vertex_t destination) { this->destination_ = destination; } + + // Set and get departure_time + graph::weight_t get_departure_time() const { return departure_time_; } + void set_departure_time(graph::weight_t departure_time) { this->departure_time_ = departure_time; } + + // Get current node + graph::vertex_t get_current_node() const { return current_node_; } + + // Get status + int get_status() const { return status_;} + + // Compute path + void compute_agent_path (); + + // Get path + //std::vector get_agent_path () { return path_; } + + // Print path + void print_agent_path (); + + // Move agent + void move_agent(graph::weight_t time_limit); + + private: + // origin vertex + graph::vertex_t origin_{0}; + + // destination vertex + graph::vertex_t destination_{0}; + + // departure time + graph::weight_t departure_time_{0}; + + // departure node + graph::vertex_t current_node_{origin_}; + + // graph + std::shared_ptr graph_; + + // path + std::vector path_; + + // status: 0: haven't started routing. 1: en_route. 2: arrived. + int status_{0}; +}; + +} + +#endif // _ABM_AGENT_H_ \ No newline at end of file diff --git a/include/graph.h b/include/graph.h index e719396..4f606f5 100644 --- a/include/graph.h +++ b/include/graph.h @@ -110,6 +110,13 @@ class Graph { //! \param[in] path Edges of the path from source to destination //! \retval cost Cost of traversed path abm::graph::weight_t path_cost(const std::vector& path); + + //! Edge cost + //! \param[in] v0 Start vertex of an edge + //! \param[in] v1 End vertex of the same edge + //! \retval cost Weight of an edge + abm::graph::weight_t get_edge_cost(graph::vertex_t v0, graph::vertex_t v1) { + return (this->edges_.at(std::make_tuple(v0, v1)))->second; } private: //! Assign number of vertices diff --git a/src/agent.cc b/src/agent.cc new file mode 100644 index 0000000..c581fdc --- /dev/null +++ b/src/agent.cc @@ -0,0 +1,53 @@ +#include +#include + +#include "config.h" +#include "graph.h" +#include "agent.h" + +// Compute path +void abm::Agent::compute_agent_path () { + const auto path = graph_ -> dijkstra(current_node_, destination_); + this -> path_ = path; +}; + +// Print path +void abm::Agent::print_agent_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(*itr), + static_cast(*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) { + abm::graph::weight_t agent_time = 0; // keep record of cumulated edge cost + this -> status_ = 1; // agent is now enroute + for (auto itr = path_.begin(); itr != path_.end() - 1; ++itr) { + auto nitr = itr + 1; + if (agent_time < time_limit) { + agent_time += graph_->get_edge_cost( + static_cast(*itr), + static_cast(*nitr)); + this -> current_node_ = static_cast(*nitr); + if (agent_time >= time_limit) { + this -> current_node_ = static_cast(*itr); + this -> path_.erase(path_.begin(), itr); + break; + } + } + } + } + // reach destination + if (current_node_ == destination_) { + this -> status_ = 2; + this -> path_.clear(); + } +}; \ No newline at end of file diff --git a/tests/agent_test.cc b/tests/agent_test.cc new file mode 100644 index 0000000..dc56701 --- /dev/null +++ b/tests/agent_test.cc @@ -0,0 +1,57 @@ +#include + +#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, parameter assignment and routing") { + // Set graph properties + const bool directed = true; + // Create simple graph object for the test agent + auto graph = std::make_shared(directed); + graph -> generate_simple_graph(); + + // Create test agent + auto test_agent = std::make_unique(graph); + + // Parameters to be assigned to the test agent + abm::graph::vertex_t origin = 1; + abm::graph::vertex_t destination = 3; + abm::graph::weight_t departure_time = 0; + + // Assign agent parameters + test_agent -> set_origin(origin); + test_agent -> set_destination(destination); + test_agent -> set_departure_time(departure_time); + + // Check assigned values + REQUIRE(test_agent -> get_origin() == origin); + REQUIRE(test_agent -> get_destination() == destination); + REQUIRE(test_agent -> get_departure_time() == departure_time); + + // Check current nodes is initialized to the origin + REQUIRE(test_agent -> get_current_node() == origin); + + // Test agent routing + // Compute shortest path between curret node and destination node + REQUIRE(test_agent -> get_current_node() == origin); // At first, the current node is the origin + REQUIRE(test_agent -> get_status() == 0); // At first, the agent status is 0 "waiting to depart" + test_agent -> compute_agent_path(); + // test_agent -> print_agent_path(); // (1, 2, w=1.5) (2, 4, w=5.5) (4, 3, w=0.2) + + // Check agent position after 2 units of cost + test_agent -> move_agent(2); + REQUIRE(test_agent -> get_current_node() == 2); // After 2 units of cost, agent can reach vertex 2 + REQUIRE(test_agent -> get_status() == 1); // At this time, agent status is 1 "enroute" + + // Check agent position after 10 units of cost + test_agent -> move_agent(10); + REQUIRE(test_agent -> get_current_node() == 3); // After 10 unit of cost, agent can reach vertex 3 + REQUIRE(test_agent -> get_status() == 2); // At this time, agent status is 2 "reached destination" + } +} \ No newline at end of file From c2611f5df9e53d939ace64c29abebf685897c1c7 Mon Sep 17 00:00:00 2001 From: bz247 Date: Wed, 18 Mar 2020 21:37:50 -0700 Subject: [PATCH 02/20] annotate agent class header --- include/agent.h | 55 ++++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/include/agent.h b/include/agent.h index e6ccf3a..dc16bfe 100644 --- a/include/agent.h +++ b/include/agent.h @@ -10,69 +10,78 @@ namespace abm { -// Agent class with origin, destination, departure time. Computes current node and path. +//! Agent class that has origin, destination, departure time, current node and compute path from current node to destination. class Agent { public: - - // Construct an agent class + //! Construct an agent class + //! \param[in] graph Defines the graph that the agent does routing on explicit Agent(const std::shared_ptr& graph) : graph_{graph} {}; - // Set and get origin - graph::vertex_t get_origin() const { return origin_; } + //! Set origin and initialize current node to origin + //! \param[in] origin Origin vertex assigned to agent void set_origin(graph::vertex_t origin) { this->origin_ = origin; this->current_node_ = origin; } + //! Get origin + graph::vertex_t get_origin() const { return origin_; } - // Set and get destination - graph::vertex_t get_destination() const { return destination_; } + //! Set destination + //! \param[in] destination Destination vertex assigned to agent void set_destination(graph::vertex_t destination) { this->destination_ = destination; } + //! Get destination + graph::vertex_t get_destination() const { return destination_; } - // Set and get departure_time - graph::weight_t get_departure_time() const { return departure_time_; } + //! Set departure_time + //! \param[in] departure_time Departure time of an agent void set_departure_time(graph::weight_t departure_time) { this->departure_time_ = departure_time; } + //! Get departure time + graph::weight_t get_departure_time() const { return departure_time_; } // Get current node graph::vertex_t get_current_node() const { return current_node_; } - // Get status + //! Get status + //! \retval Status of the agent. 0: waiting to depart; 1: enroute; 2: reached destination. int get_status() const { return status_;} - // Compute path + //! Compute path based on current node and destination void compute_agent_path (); - // Get path - //std::vector get_agent_path () { return path_; } + //! Get path + //! \retval Return path from current node to destination as a list of vertices + std::vector get_agent_path () { return path_; } - // Print path + //! Print path void print_agent_path (); - // Move agent + //! Move agent, update current_node_, path_ and status_ of the agent object + //! \param[in] time_limit The time that sets the limit on the total weights of a sub route that an agent can cover in the current time step void move_agent(graph::weight_t time_limit); private: - // origin vertex + //! Agent origin vertex graph::vertex_t origin_{0}; - // destination vertex + //! Agent destination vertex graph::vertex_t destination_{0}; - // departure time + //! Agent departure time graph::weight_t departure_time_{0}; - // departure node + //! Agent current node graph::vertex_t current_node_{origin_}; - // graph + //! Graph that agent travels on std::shared_ptr graph_; - // path + //! Path from current node to destination std::vector path_; - // status: 0: haven't started routing. 1: en_route. 2: arrived. + //! status: 0: haven't started routing. 1: en_route. 2: arrived. int status_{0}; }; -} +} // namespace abm #endif // _ABM_AGENT_H_ \ No newline at end of file From aabb754b3c88ced0333da5b4ba30df79eeec7e38 Mon Sep 17 00:00:00 2001 From: bz247 Date: Wed, 18 Mar 2020 22:01:23 -0700 Subject: [PATCH 03/20] annotate agent.cc --- include/agent.h | 2 +- src/agent.cc | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/include/agent.h b/include/agent.h index dc16bfe..755acf0 100644 --- a/include/agent.h +++ b/include/agent.h @@ -14,7 +14,7 @@ namespace abm { class Agent { public: //! Construct an agent class - //! \param[in] graph Defines the graph that the agent does routing on + //! \param[in] graph Pass a reference of the graph that the agent does routing on explicit Agent(const std::shared_ptr& graph) : graph_{graph} {}; //! Set origin and initialize current node to origin diff --git a/src/agent.cc b/src/agent.cc index c581fdc..3fbdfb6 100644 --- a/src/agent.cc +++ b/src/agent.cc @@ -1,17 +1,17 @@ -#include #include #include "config.h" #include "graph.h" #include "agent.h" -// Compute path +// Compute path from the current_node_ to destination_ void abm::Agent::compute_agent_path () { + // dijkstra function returns a list of vertices const auto path = graph_ -> dijkstra(current_node_, destination_); this -> path_ = path; }; -// Print path +// Print path in the format of (start_of_edge, end_of_edge, w=weight_of_edge), (s, e, w=), ... void abm::Agent::print_agent_path () { if (path_.size()>0) { for (auto itr = path_.begin(); itr != path_.end() - 1; ++itr) { @@ -28,24 +28,29 @@ void abm::Agent::print_agent_path () { // Move agent void abm::Agent::move_agent(graph::weight_t time_limit) { if (path_.size() > 0) { - abm::graph::weight_t agent_time = 0; // keep record of cumulated edge cost - this -> status_ = 1; // agent is now enroute + // 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(*itr), static_cast(*nitr)); this -> current_node_ = static_cast(*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(*itr); + // Update path, set the beginning of the path to the current node this -> path_.erase(path_.begin(), itr); break; } } } } - // reach destination + // When agent reachs destination, update its status to 2 "arrival" and delete the path if (current_node_ == destination_) { this -> status_ = 2; this -> path_.clear(); From a891ed7e3ee87527da139b48275a966c23cbd242 Mon Sep 17 00:00:00 2001 From: bz247 Date: Wed, 18 Mar 2020 22:13:17 -0700 Subject: [PATCH 04/20] remove inside a for loop --- src/graph.cc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/graph.cc b/src/graph.cc index 78bb362..afb3107 100644 --- a/src/graph.cc +++ b/src/graph.cc @@ -275,9 +275,7 @@ std::vector abm::Graph::dijkstra_vertices_ual( std::vector route_vertices; if (path.size() > 0) { for (auto itr = path.begin(); itr != path.end(); ++itr) { - if (itr != path.end()) - route_vertices.emplace_back(static_cast(*itr)); - } + route_vertices.emplace_back(static_cast(*itr)); route_vertices.emplace_back(-1); } return route_vertices; From e2198fe32dc45fb10a5d93b6fc17b4526abb6eea Mon Sep 17 00:00:00 2001 From: bz247 Date: Wed, 18 Mar 2020 22:22:38 -0700 Subject: [PATCH 05/20] add agent id --- include/agent.h | 11 ++++++++++- src/graph.cc | 1 + tests/agent_test.cc | 3 +++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/agent.h b/include/agent.h index 755acf0..fbbb6a1 100644 --- a/include/agent.h +++ b/include/agent.h @@ -10,13 +10,19 @@ namespace abm { -//! Agent class that has origin, destination, departure time, current node and compute path from current node to destination. +//! 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 //! \param[in] graph Pass a reference of the graph that the agent does routing on explicit Agent(const std::shared_ptr& graph) : graph_{graph} {}; + //! Set id + //! \param[in] agent_id Agent id assigned to the agent object + void set_id(graph::vertex_t agent_id) { this->agent_id_ = agent_id; } + //! Get id + graph::vertex_t get_id() const { return agent_id_; } + //! Set origin and initialize current node to origin //! \param[in] origin Origin vertex assigned to agent void set_origin(graph::vertex_t origin) { @@ -60,6 +66,9 @@ class Agent { void move_agent(graph::weight_t time_limit); private: + //! Agent id + graph::vertex_t agent_id_{0}; + //! Agent origin vertex graph::vertex_t origin_{0}; diff --git a/src/graph.cc b/src/graph.cc index afb3107..02a883b 100644 --- a/src/graph.cc +++ b/src/graph.cc @@ -276,6 +276,7 @@ std::vector abm::Graph::dijkstra_vertices_ual( if (path.size() > 0) { for (auto itr = path.begin(); itr != path.end(); ++itr) { route_vertices.emplace_back(static_cast(*itr)); + } route_vertices.emplace_back(-1); } return route_vertices; diff --git a/tests/agent_test.cc b/tests/agent_test.cc index dc56701..bfd5bda 100644 --- a/tests/agent_test.cc +++ b/tests/agent_test.cc @@ -20,16 +20,19 @@ TEST_CASE("Agent class is checked", "[agent]") { auto test_agent = std::make_unique(graph); // 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; // Assign agent parameters + test_agent -> set_id(agent_id); test_agent -> set_origin(origin); test_agent -> set_destination(destination); test_agent -> set_departure_time(departure_time); // Check assigned values + REQUIRE(test_agent -> get_id() == agent_id); REQUIRE(test_agent -> get_origin() == origin); REQUIRE(test_agent -> get_destination() == destination); REQUIRE(test_agent -> get_departure_time() == departure_time); From 39ec9866ac1cbeb03b299863429eba270082b61e Mon Sep 17 00:00:00 2001 From: bz247 Date: Wed, 18 Mar 2020 22:34:26 -0700 Subject: [PATCH 06/20] pull and push changes --- src/graph.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/graph.cc b/src/graph.cc index b40afc4..8565386 100644 --- a/src/graph.cc +++ b/src/graph.cc @@ -277,7 +277,7 @@ std::vector abm::Graph::dijkstra_vertices_ual( std::vector route_vertices; if (path.size() > 0) { - for (auto itr = path.begin(); itr != path.end(); ++itr) + for (auto itr = path.begin(); itr != path.end(); ++itr) { route_vertices.emplace_back(static_cast(*itr)); } route_vertices.emplace_back(-1); From 0b3c1f8d06ec91e55f7b5200cbfd5d7ca1149b42 Mon Sep 17 00:00:00 2001 From: Krishna Kumar Date: Thu, 19 Mar 2020 07:43:55 -0500 Subject: [PATCH 07/20] :pencil: Run clang-format on code --- include/agent.h | 42 ++++++++++++++++++++--------------- include/graph.h | 7 +++--- src/agent.cc | 53 +++++++++++++++++++++++++-------------------- tests/agent_test.cc | 51 ++++++++++++++++++++++++------------------- 4 files changed, 87 insertions(+), 66 deletions(-) diff --git a/include/agent.h b/include/agent.h index fbbb6a1..33548ab 100644 --- a/include/agent.h +++ b/include/agent.h @@ -4,20 +4,22 @@ #include #include +#include "agent.h" #include "config.h" #include "graph.h" -#include "agent.h" namespace abm { -//! Agent class that has id, origin, destination, departure time, current node and compute path from current node to destination. +//! 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 - //! \param[in] graph Pass a reference of the graph that the agent does routing on + //! \param[in] graph Pass a reference of the graph that the agent does routing + //! on explicit Agent(const std::shared_ptr& graph) : graph_{graph} {}; - //! Set id + //! Set id //! \param[in] agent_id Agent id assigned to the agent object void set_id(graph::vertex_t agent_id) { this->agent_id_ = agent_id; } //! Get id @@ -25,7 +27,7 @@ class Agent { //! Set origin and initialize current node to origin //! \param[in] origin Origin vertex assigned to agent - void set_origin(graph::vertex_t origin) { + void set_origin(graph::vertex_t origin) { this->origin_ = origin; this->current_node_ = origin; } @@ -34,13 +36,17 @@ class Agent { //! Set destination //! \param[in] destination Destination vertex assigned to agent - void set_destination(graph::vertex_t destination) { this->destination_ = destination; } + void set_destination(graph::vertex_t destination) { + this->destination_ = destination; + } //! Get destination graph::vertex_t get_destination() const { return destination_; } //! Set departure_time //! \param[in] departure_time Departure time of an agent - void set_departure_time(graph::weight_t departure_time) { this->departure_time_ = departure_time; } + void set_departure_time(graph::weight_t departure_time) { + this->departure_time_ = departure_time; + } //! Get departure time graph::weight_t get_departure_time() const { return departure_time_; } @@ -48,21 +54,23 @@ class Agent { graph::vertex_t get_current_node() const { return current_node_; } //! Get status - //! \retval Status of the agent. 0: waiting to depart; 1: enroute; 2: reached destination. - int get_status() const { return status_;} + //! \retval Status of the agent. 0: waiting to depart; 1: enroute; 2: reached + //! destination. + int get_status() const { return status_; } //! Compute path based on current node and destination - void compute_agent_path (); - + void compute_agent_path(); + //! Get path //! \retval Return path from current node to destination as a list of vertices - std::vector get_agent_path () { return path_; } - + std::vector get_agent_path() { return path_; } + //! Print path - void print_agent_path (); + void print_agent_path(); //! Move agent, update current_node_, path_ and status_ of the agent object - //! \param[in] time_limit The time that sets the limit on the total weights of a sub route that an agent can cover in the current time step + //! \param[in] time_limit The time that sets the limit on the total weights of + //! a sub route that an agent can cover in the current time step void move_agent(graph::weight_t time_limit); private: @@ -88,9 +96,9 @@ class Agent { std::vector path_; //! status: 0: haven't started routing. 1: en_route. 2: arrived. - int status_{0}; + int status_{0}; }; -} // namespace abm +} // namespace abm #endif // _ABM_AGENT_H_ \ No newline at end of file diff --git a/include/graph.h b/include/graph.h index 92bc07c..ed9596a 100644 --- a/include/graph.h +++ b/include/graph.h @@ -111,13 +111,14 @@ class Graph { //! \param[in] path Edges of the path from source to destination //! \retval cost Cost of traversed path abm::graph::weight_t path_cost(const std::vector& path); - + //! Edge cost //! \param[in] v0 Start vertex of an edge //! \param[in] v1 End vertex of the same edge //! \retval cost Weight of an edge - abm::graph::weight_t get_edge_cost(graph::vertex_t v0, graph::vertex_t v1) { - return (this->edges_.at(std::make_tuple(v0, v1)))->second; } + abm::graph::weight_t get_edge_cost(graph::vertex_t v0, graph::vertex_t v1) { + return (this->edges_.at(std::make_tuple(v0, v1)))->second; + } private: //! Assign number of vertices diff --git a/src/agent.cc b/src/agent.cc index 3fbdfb6..351625c 100644 --- a/src/agent.cc +++ b/src/agent.cc @@ -1,58 +1,63 @@ #include +#include "agent.h" #include "config.h" #include "graph.h" -#include "agent.h" // Compute path from the current_node_ to destination_ -void abm::Agent::compute_agent_path () { +void abm::Agent::compute_agent_path() { // dijkstra function returns a list of vertices - const auto path = graph_ -> dijkstra(current_node_, destination_); - this -> path_ = path; + 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_agent_path () { - if (path_.size()>0) { +// Print path in the format of (start_of_edge, end_of_edge, w=weight_of_edge), +// (s, e, w=), ... +void abm::Agent::print_agent_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(*itr), - static_cast(*nitr)); + auto weight = + graph_->get_edge_cost(static_cast(*itr), + static_cast(*nitr)); std::cout << "(" << *itr << ", " << *nitr << ", w=" << weight << ") "; } + } else { + std::cout << "path size is zero" << std::endl; } - 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 + // 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) { + 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(*itr), - static_cast(*nitr)); - this -> current_node_ = static_cast(*nitr); + agent_time += + graph_->get_edge_cost(static_cast(*itr), + static_cast(*nitr)); + this->current_node_ = static_cast(*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(*itr); + // 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(*itr); // Update path, set the beginning of the path to the current node - this -> path_.erase(path_.begin(), itr); + this->path_.erase(path_.begin(), itr); break; } } } } - // When agent reachs destination, update its status to 2 "arrival" and delete the path + // When agent reachs destination, update its status to 2 "arrival" and delete + // the path if (current_node_ == destination_) { - this -> status_ = 2; - this -> path_.clear(); + this->status_ = 2; + this->path_.clear(); } }; \ No newline at end of file diff --git a/tests/agent_test.cc b/tests/agent_test.cc index bfd5bda..9e8ded1 100644 --- a/tests/agent_test.cc +++ b/tests/agent_test.cc @@ -14,7 +14,7 @@ TEST_CASE("Agent class is checked", "[agent]") { const bool directed = true; // Create simple graph object for the test agent auto graph = std::make_shared(directed); - graph -> generate_simple_graph(); + graph->generate_simple_graph(); // Create test agent auto test_agent = std::make_unique(graph); @@ -26,35 +26,42 @@ TEST_CASE("Agent class is checked", "[agent]") { abm::graph::weight_t departure_time = 0; // Assign agent parameters - test_agent -> set_id(agent_id); - test_agent -> set_origin(origin); - test_agent -> set_destination(destination); - test_agent -> set_departure_time(departure_time); + test_agent->set_id(agent_id); + test_agent->set_origin(origin); + test_agent->set_destination(destination); + test_agent->set_departure_time(departure_time); // Check assigned values - REQUIRE(test_agent -> get_id() == agent_id); - REQUIRE(test_agent -> get_origin() == origin); - REQUIRE(test_agent -> get_destination() == destination); - REQUIRE(test_agent -> get_departure_time() == departure_time); + REQUIRE(test_agent->get_id() == agent_id); + REQUIRE(test_agent->get_origin() == origin); + REQUIRE(test_agent->get_destination() == destination); + REQUIRE(test_agent->get_departure_time() == departure_time); // Check current nodes is initialized to the origin - REQUIRE(test_agent -> get_current_node() == origin); + REQUIRE(test_agent->get_current_node() == origin); // Test agent routing // Compute shortest path between curret node and destination node - REQUIRE(test_agent -> get_current_node() == origin); // At first, the current node is the origin - REQUIRE(test_agent -> get_status() == 0); // At first, the agent status is 0 "waiting to depart" - test_agent -> compute_agent_path(); - // test_agent -> print_agent_path(); // (1, 2, w=1.5) (2, 4, w=5.5) (4, 3, w=0.2) - + REQUIRE(test_agent->get_current_node() == origin); // At first, the current + // node is the origin + REQUIRE(test_agent->get_status() == 0); // At first, the agent status is 0 + // "waiting to depart" + test_agent->compute_agent_path(); + // test_agent -> print_agent_path(); // (1, 2, w=1.5) (2, 4, w=5.5) (4, 3, + // w=0.2) + // Check agent position after 2 units of cost - test_agent -> move_agent(2); - REQUIRE(test_agent -> get_current_node() == 2); // After 2 units of cost, agent can reach vertex 2 - REQUIRE(test_agent -> get_status() == 1); // At this time, agent status is 1 "enroute" - + test_agent->move_agent(2); + REQUIRE(test_agent->get_current_node() == 2); // After 2 units of cost, + // agent can reach vertex 2 + REQUIRE(test_agent->get_status() == 1); // At this time, agent status is 1 + // "enroute" + // Check agent position after 10 units of cost - test_agent -> move_agent(10); - REQUIRE(test_agent -> get_current_node() == 3); // After 10 unit of cost, agent can reach vertex 3 - REQUIRE(test_agent -> get_status() == 2); // At this time, agent status is 2 "reached destination" + test_agent->move_agent(10); + REQUIRE(test_agent->get_current_node() == 3); // After 10 unit of cost, + // agent can reach vertex 3 + REQUIRE(test_agent->get_status() == 2); // At this time, agent status is 2 + // "reached destination" } } \ No newline at end of file From 4acb509613e8a49b5fc326c154d014de0553b9f9 Mon Sep 17 00:00:00 2001 From: bz247 Date: Thu, 19 Mar 2020 08:50:41 -0700 Subject: [PATCH 08/20] initialize agent start value to std::numeric_limits::max() --- .clang-format | 0 include/agent.h | 25 +++++++++++++------------ 2 files changed, 13 insertions(+), 12 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..e69de29 diff --git a/include/agent.h b/include/agent.h index fbbb6a1..8cf3fbc 100644 --- a/include/agent.h +++ b/include/agent.h @@ -19,28 +19,29 @@ class Agent { //! Set id //! \param[in] agent_id Agent id assigned to the agent object - void set_id(graph::vertex_t agent_id) { this->agent_id_ = agent_id; } + void set_id(graph::vertex_t agent_id) { agent_id_ = agent_id; } //! Get id graph::vertex_t get_id() const { return agent_id_; } - //! Set origin and initialize current node to origin + //! Set origin and initialize status to wait for departure, current node to origin //! \param[in] origin Origin vertex assigned to agent void set_origin(graph::vertex_t origin) { - this->origin_ = origin; - this->current_node_ = origin; + origin_ = origin; + status_ = 0; + current_node_ = origin; } //! Get origin graph::vertex_t get_origin() const { return origin_; } //! Set destination //! \param[in] destination Destination vertex assigned to agent - void set_destination(graph::vertex_t destination) { this->destination_ = destination; } + void set_destination(graph::vertex_t destination) { destination_ = destination; } //! Get destination graph::vertex_t get_destination() const { return destination_; } //! Set departure_time //! \param[in] departure_time Departure time of an agent - void set_departure_time(graph::weight_t departure_time) { this->departure_time_ = departure_time; } + void set_departure_time(graph::weight_t departure_time) { departure_time_ = departure_time; } //! Get departure time graph::weight_t get_departure_time() const { return departure_time_; } @@ -67,19 +68,19 @@ class Agent { private: //! Agent id - graph::vertex_t agent_id_{0}; + graph::vertex_t agent_id_{std::numeric_limits::max()}; //! Agent origin vertex - graph::vertex_t origin_{0}; + graph::vertex_t origin_{std::numeric_limits::max()}; //! Agent destination vertex - graph::vertex_t destination_{0}; + graph::vertex_t destination_{std::numeric_limits::max()}; //! Agent departure time - graph::weight_t departure_time_{0}; + graph::weight_t departure_time_{std::numeric_limits::max()}; //! Agent current node - graph::vertex_t current_node_{origin_}; + graph::vertex_t current_node_{std::numeric_limits::max()}; //! Graph that agent travels on std::shared_ptr graph_; @@ -88,7 +89,7 @@ class Agent { std::vector path_; //! status: 0: haven't started routing. 1: en_route. 2: arrived. - int status_{0}; + int status_{std::numeric_limits::max()}; }; } // namespace abm From 2a05853cc0ceaa2ec5755235119e40cca0957ec3 Mon Sep 17 00:00:00 2001 From: bz247 Date: Thu, 19 Mar 2020 08:58:59 -0700 Subject: [PATCH 09/20] remove in class members and fix numeric limit bug --- include/agent.h | 18 +++++++++--------- src/agent.cc | 4 ++-- tests/agent_test.cc | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/include/agent.h b/include/agent.h index 309d058..9997759 100644 --- a/include/agent.h +++ b/include/agent.h @@ -20,10 +20,10 @@ class Agent { explicit Agent(const std::shared_ptr& graph) : graph_{graph} {}; //! Set id - //! \param[in] agent_id Agent id assigned to the agent object - void set_id(graph::vertex_t agent_id) { agent_id_ = agent_id; } + //! \param[in] id Agent id assigned to the agent object + void set_id(graph::vertex_t id) { id_ = id; } //! Get id - graph::vertex_t get_id() const { return agent_id_; } + graph::vertex_t get_id() const { return id_; } //! Set origin and initialize status to wait for departure, current node to origin //! \param[in] origin Origin vertex assigned to agent @@ -56,14 +56,14 @@ class Agent { int get_status() const { return status_; } //! Compute path based on current node and destination - void compute_agent_path(); + void compute_path(); //! Get path //! \retval Return path from current node to destination as a list of vertices - std::vector get_agent_path() { return path_; } + std::vector get_path() { return path_; } //! Print path - void print_agent_path(); + void print_path(); //! Move agent, update current_node_, path_ and status_ of the agent object //! \param[in] time_limit The time that sets the limit on the total weights of @@ -72,7 +72,7 @@ class Agent { private: //! Agent id - graph::vertex_t agent_id_{std::numeric_limits::max()}; + graph::vertex_t id_{std::numeric_limits::max()}; //! Agent origin vertex graph::vertex_t origin_{std::numeric_limits::max()}; @@ -81,7 +81,7 @@ class Agent { graph::vertex_t destination_{std::numeric_limits::max()}; //! Agent departure time - graph::weight_t departure_time_{std::numeric_limits::max()}; + graph::weight_t departure_time_{std::numeric_limits::max()}; //! Agent current node graph::vertex_t current_node_{std::numeric_limits::max()}; @@ -93,7 +93,7 @@ class Agent { std::vector path_; //! status: 0: haven't started routing. 1: en_route. 2: arrived. - int status_{std::numeric_limits::max()}; + int status_{std::numeric_limits::max()}; }; } // namespace abm diff --git a/src/agent.cc b/src/agent.cc index 351625c..a143224 100644 --- a/src/agent.cc +++ b/src/agent.cc @@ -5,7 +5,7 @@ #include "graph.h" // Compute path from the current_node_ to destination_ -void abm::Agent::compute_agent_path() { +void abm::Agent::compute_path() { // dijkstra function returns a list of vertices const auto path = graph_->dijkstra(current_node_, destination_); this->path_ = path; @@ -13,7 +13,7 @@ void abm::Agent::compute_agent_path() { // Print path in the format of (start_of_edge, end_of_edge, w=weight_of_edge), // (s, e, w=), ... -void abm::Agent::print_agent_path() { +void abm::Agent::print_path() { if (path_.size() > 0) { for (auto itr = path_.begin(); itr != path_.end() - 1; ++itr) { auto nitr = itr + 1; diff --git a/tests/agent_test.cc b/tests/agent_test.cc index 9e8ded1..3c665d2 100644 --- a/tests/agent_test.cc +++ b/tests/agent_test.cc @@ -46,8 +46,8 @@ TEST_CASE("Agent class is checked", "[agent]") { // node is the origin REQUIRE(test_agent->get_status() == 0); // At first, the agent status is 0 // "waiting to depart" - test_agent->compute_agent_path(); - // test_agent -> print_agent_path(); // (1, 2, w=1.5) (2, 4, w=5.5) (4, 3, + test_agent->compute_path(); + // test_agent -> print_path(); // (1, 2, w=1.5) (2, 4, w=5.5) (4, 3, // w=0.2) // Check agent position after 2 units of cost From 7828d848ec5987c1a0fed8933cfb97d63bf665e7 Mon Sep 17 00:00:00 2001 From: bz247 Date: Thu, 19 Mar 2020 11:52:04 -0700 Subject: [PATCH 10/20] run clang-format --- .vscode/settings.json | 3 +++ include/agent.h | 18 +++++++++++------- src/agent.cc | 2 +- tests/agent_test.cc | 2 +- 4 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..cad7657 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "cmake.configureOnOpen": false +} \ No newline at end of file diff --git a/include/agent.h b/include/agent.h index 9997759..64b6f43 100644 --- a/include/agent.h +++ b/include/agent.h @@ -25,9 +25,9 @@ class Agent { //! Get id graph::vertex_t get_id() const { return id_; } - //! Set origin and initialize status to wait for departure, current node to origin - //! \param[in] origin Origin vertex assigned to agent - void set_origin(graph::vertex_t origin) { + //! Set origin and initialize status to wait for departure, current node to + //! origin \param[in] origin Origin vertex assigned to agent + void set_origin(graph::vertex_t origin) { origin_ = origin; status_ = 0; current_node_ = origin; @@ -37,13 +37,17 @@ class Agent { //! Set destination //! \param[in] destination Destination vertex assigned to agent - void set_destination(graph::vertex_t destination) { destination_ = destination; } + void set_destination(graph::vertex_t destination) { + destination_ = destination; + } //! Get destination graph::vertex_t get_destination() const { return destination_; } //! Set departure_time //! \param[in] departure_time Departure time of an agent - void set_departure_time(graph::weight_t departure_time) { departure_time_ = departure_time; } + void set_departure_time(graph::weight_t departure_time) { + departure_time_ = departure_time; + } //! Get departure time graph::weight_t get_departure_time() const { return departure_time_; } @@ -93,9 +97,9 @@ class Agent { std::vector path_; //! status: 0: haven't started routing. 1: en_route. 2: arrived. - int status_{std::numeric_limits::max()}; + int status_{std::numeric_limits::max()}; }; } // namespace abm -#endif // _ABM_AGENT_H_ \ No newline at end of file +#endif // _ABM_AGENT_H_ diff --git a/src/agent.cc b/src/agent.cc index a143224..7f0153e 100644 --- a/src/agent.cc +++ b/src/agent.cc @@ -60,4 +60,4 @@ void abm::Agent::move_agent(graph::weight_t time_limit) { this->status_ = 2; this->path_.clear(); } -}; \ No newline at end of file +}; diff --git a/tests/agent_test.cc b/tests/agent_test.cc index 3c665d2..f101172 100644 --- a/tests/agent_test.cc +++ b/tests/agent_test.cc @@ -64,4 +64,4 @@ TEST_CASE("Agent class is checked", "[agent]") { REQUIRE(test_agent->get_status() == 2); // At this time, agent status is 2 // "reached destination" } -} \ No newline at end of file +} From 2e6429c0f0a563bc41e5f8dc172be9ef23840424 Mon Sep 17 00:00:00 2001 From: bz247 Date: Thu, 19 Mar 2020 13:43:36 -0700 Subject: [PATCH 11/20] stop tracking .vscode/ --- .gitignore | 3 +++ .vscode/settings.json | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index 380488f..a35ee31 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# vs configuration code +.vscode/ + # Temporary files *~* *#* diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index cad7657..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "cmake.configureOnOpen": false -} \ No newline at end of file From e819d1e250add070027fd173b9bcfcb50e88d4a4 Mon Sep 17 00:00:00 2001 From: bz247 Date: Thu, 19 Mar 2020 14:11:09 -0700 Subject: [PATCH 12/20] remove graph and path related functions from agent class --- include/agent.h | 83 +++++++++---------------------- src/agent.cc | 118 ++++++++++++++++++++++---------------------- tests/agent_test.cc | 65 +++++++----------------- 3 files changed, 101 insertions(+), 165 deletions(-) diff --git a/include/agent.h b/include/agent.h index 64b6f43..cc6926a 100644 --- a/include/agent.h +++ b/include/agent.h @@ -14,65 +14,36 @@ namespace abm { //! and compute path from current node to destination. class Agent { public: - //! Construct an agent class - //! \param[in] graph Pass a reference of the graph that the agent does routing - //! on - explicit Agent(const std::shared_ptr& graph) : graph_{graph} {}; - - //! Set id - //! \param[in] id Agent id assigned to the agent object - void set_id(graph::vertex_t id) { id_ = id; } - //! Get id - graph::vertex_t get_id() const { return id_; } - - //! Set origin and initialize status to wait for departure, current node to - //! origin \param[in] origin Origin vertex assigned to agent - void set_origin(graph::vertex_t origin) { - origin_ = origin; - status_ = 0; - current_node_ = origin; - } - //! Get origin - graph::vertex_t get_origin() const { return origin_; } - - //! Set destination - //! \param[in] destination Destination vertex assigned to agent - void set_destination(graph::vertex_t destination) { - destination_ = destination; - } - //! Get destination - graph::vertex_t get_destination() const { return destination_; } - - //! Set departure_time - //! \param[in] departure_time Departure time of an agent - void set_departure_time(graph::weight_t departure_time) { - departure_time_ = departure_time; - } - //! Get departure time - graph::weight_t get_departure_time() const { return departure_time_; } + //! 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(graph::vertex_t id) : id_{id} {}; + explicit Agent(graph::vertex_t id, graph::vertex_t origin, graph::vertex_t destination) : id_{id}, origin_{origin}, destination_{destination} {}; - // Get current node - graph::vertex_t get_current_node() const { return current_node_; } + //! Get and set id + graph::vertex_t id() const { return id_; } + graph::vertex_t id(graph::vertex_t id) { id_ = id; } - //! Get status - //! \retval Status of the agent. 0: waiting to depart; 1: enroute; 2: reached - //! destination. - int get_status() const { return status_; } + //! Get and set origin + graph::vertex_t origin() const { return origin_; } + graph::vertex_t origin(graph::vertex_t origin) { origin_ = origin; } - //! Compute path based on current node and destination - void compute_path(); + //! Get and set destination + graph::vertex_t destination() const { return destination_; } + graph::vertex_t destination(graph::vertex_t destination) { destination_ = destination; } - //! Get path - //! \retval Return path from current node to destination as a list of vertices - std::vector get_path() { return path_; } + //! Get and set departure time + double departure_time() const { return departure_time_; } + double departure_time(double departure_time) { departure_time_ = departure_time; } - //! Print path - void print_path(); + // Get current node + graph::vertex_t current_node() const { return current_node_; } - //! Move agent, update current_node_, path_ and status_ of the agent object - //! \param[in] time_limit The time that sets the limit on the total weights of - //! a sub route that an agent can cover in the current time step - void move_agent(graph::weight_t time_limit); + //! Get status + //! \retval Status of the agent. 0: waiting to depart; 1: enroute; 2: reached + //! destination. + int status() const { return status_; } private: //! Agent id @@ -90,12 +61,6 @@ class Agent { //! Agent current node graph::vertex_t current_node_{std::numeric_limits::max()}; - //! Graph that agent travels on - std::shared_ptr graph_; - - //! Path from current node to destination - std::vector path_; - //! status: 0: haven't started routing. 1: en_route. 2: arrived. int status_{std::numeric_limits::max()}; }; diff --git a/src/agent.cc b/src/agent.cc index 7f0153e..bcb0563 100644 --- a/src/agent.cc +++ b/src/agent.cc @@ -1,63 +1,63 @@ -#include +// #include -#include "agent.h" -#include "config.h" -#include "graph.h" +// #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; -}; +// // 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(*itr), - static_cast(*nitr)); - std::cout << "(" << *itr << ", " << *nitr << ", w=" << weight << ") "; - } - } else { - std::cout << "path size is zero" << std::endl; - } -} +// // 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(*itr), +// static_cast(*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(*itr), - static_cast(*nitr)); - this->current_node_ = static_cast(*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(*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(); - } -}; +// // 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(*itr), +// static_cast(*nitr)); +// this->current_node_ = static_cast(*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(*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(); +// } +// }; diff --git a/tests/agent_test.cc b/tests/agent_test.cc index f101172..100f7d1 100644 --- a/tests/agent_test.cc +++ b/tests/agent_test.cc @@ -9,15 +9,7 @@ TEST_CASE("Agent class is checked", "[agent]") { // Tolerance const double Tolerance = 1.E-7; - SECTION("Test agent initialization, parameter assignment and routing") { - // Set graph properties - const bool directed = true; - // Create simple graph object for the test agent - auto graph = std::make_shared(directed); - graph->generate_simple_graph(); - - // Create test agent - auto test_agent = std::make_unique(graph); + SECTION("Test agent initialization") { // Parameters to be assigned to the test agent abm::graph::vertex_t agent_id = 100; @@ -25,43 +17,22 @@ TEST_CASE("Agent class is checked", "[agent]") { abm::graph::vertex_t destination = 3; abm::graph::weight_t departure_time = 0; - // Assign agent parameters - test_agent->set_id(agent_id); - test_agent->set_origin(origin); - test_agent->set_destination(destination); - test_agent->set_departure_time(departure_time); - - // Check assigned values - REQUIRE(test_agent->get_id() == agent_id); - REQUIRE(test_agent->get_origin() == origin); - REQUIRE(test_agent->get_destination() == destination); - REQUIRE(test_agent->get_departure_time() == departure_time); - - // Check current nodes is initialized to the origin - REQUIRE(test_agent->get_current_node() == origin); - - // Test agent routing - // Compute shortest path between curret node and destination node - REQUIRE(test_agent->get_current_node() == origin); // At first, the current - // node is the origin - REQUIRE(test_agent->get_status() == 0); // At first, the agent status is 0 - // "waiting to depart" - test_agent->compute_path(); - // test_agent -> print_path(); // (1, 2, w=1.5) (2, 4, w=5.5) (4, 3, - // w=0.2) - - // Check agent position after 2 units of cost - test_agent->move_agent(2); - REQUIRE(test_agent->get_current_node() == 2); // After 2 units of cost, - // agent can reach vertex 2 - REQUIRE(test_agent->get_status() == 1); // At this time, agent status is 1 - // "enroute" - - // Check agent position after 10 units of cost - test_agent->move_agent(10); - REQUIRE(test_agent->get_current_node() == 3); // After 10 unit of cost, - // agent can reach vertex 3 - REQUIRE(test_agent->get_status() == 2); // At this time, agent status is 2 - // "reached destination" + SECTION("Initialization with id") { + // Create test agent + auto test_agent = std::make_unique(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(agent_id, origin, destination); + + // Check assigned values + REQUIRE(test_agent->id() == agent_id); + REQUIRE(test_agent->origin() == origin); + REQUIRE(test_agent->destination() == destination); + } } } From f2917dcab188307cee4d33eb705063ed362fff22 Mon Sep 17 00:00:00 2001 From: bz247 Date: Thu, 19 Mar 2020 14:11:44 -0700 Subject: [PATCH 13/20] clang format --- include/agent.h | 12 +++++++++--- src/agent.cc | 9 ++++++--- tests/agent_test.cc | 5 +++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/include/agent.h b/include/agent.h index cc6926a..2062b2a 100644 --- a/include/agent.h +++ b/include/agent.h @@ -19,7 +19,9 @@ class Agent { //! \param[in] origin Agent origin //! \param[in] destination Agent destination explicit Agent(graph::vertex_t id) : id_{id} {}; - explicit Agent(graph::vertex_t id, graph::vertex_t origin, graph::vertex_t destination) : id_{id}, origin_{origin}, destination_{destination} {}; + explicit Agent(graph::vertex_t id, graph::vertex_t origin, + graph::vertex_t destination) + : id_{id}, origin_{origin}, destination_{destination} {}; //! Get and set id graph::vertex_t id() const { return id_; } @@ -31,11 +33,15 @@ class Agent { //! Get and set destination graph::vertex_t destination() const { return destination_; } - graph::vertex_t destination(graph::vertex_t destination) { destination_ = destination; } + graph::vertex_t destination(graph::vertex_t destination) { + destination_ = destination; + } //! Get and set departure time double departure_time() const { return departure_time_; } - double departure_time(double departure_time) { departure_time_ = departure_time; } + double departure_time(double departure_time) { + departure_time_ = departure_time; + } // Get current node graph::vertex_t current_node() const { return current_node_; } diff --git a/src/agent.cc b/src/agent.cc index bcb0563..d6de354 100644 --- a/src/agent.cc +++ b/src/agent.cc @@ -11,7 +11,8 @@ // this->path_ = path; // }; -// // Print path in the format of (start_of_edge, end_of_edge, w=weight_of_edge), +// // 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) { @@ -30,7 +31,8 @@ // // 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 +// // 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" @@ -54,7 +56,8 @@ // } // } // } -// // When agent reachs destination, update its status to 2 "arrival" and delete +// // When agent reachs destination, update its status to 2 "arrival" and +// delete // // the path // if (current_node_ == destination_) { // this->status_ = 2; diff --git a/tests/agent_test.cc b/tests/agent_test.cc index 100f7d1..172f630 100644 --- a/tests/agent_test.cc +++ b/tests/agent_test.cc @@ -24,10 +24,11 @@ TEST_CASE("Agent class is checked", "[agent]") { // 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(agent_id, origin, destination); + auto test_agent = + std::make_unique(agent_id, origin, destination); // Check assigned values REQUIRE(test_agent->id() == agent_id); From 8ddbb098c17ed01aa483c109909e79a77f88d673 Mon Sep 17 00:00:00 2001 From: bz247 Date: Thu, 19 Mar 2020 14:23:34 -0700 Subject: [PATCH 14/20] remove function in Graph class to access individual edge weight --- include/agent.h | 12 ++++++------ include/graph.h | 8 -------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/include/agent.h b/include/agent.h index 2062b2a..f8ff0f3 100644 --- a/include/agent.h +++ b/include/agent.h @@ -18,22 +18,22 @@ class Agent { //! \param[in] id Agent id //! \param[in] origin Agent origin //! \param[in] destination Agent destination - explicit Agent(graph::vertex_t id) : id_{id} {}; - explicit Agent(graph::vertex_t id, graph::vertex_t origin, - graph::vertex_t destination) + explicit Agent(graph::vertex_t& id) : id_{id} {}; + explicit Agent(graph::vertex_t& id, graph::vertex_t& origin, + graph::vertex_t& destination) : id_{id}, origin_{origin}, destination_{destination} {}; //! Get and set id graph::vertex_t id() const { return id_; } - graph::vertex_t id(graph::vertex_t id) { id_ = id; } + graph::vertex_t id(graph::vertex_t& id) { id_ = id; } //! Get and set origin graph::vertex_t origin() const { return origin_; } - graph::vertex_t origin(graph::vertex_t origin) { origin_ = origin; } + graph::vertex_t origin(graph::vertex_t& origin) { origin_ = origin; } //! Get and set destination graph::vertex_t destination() const { return destination_; } - graph::vertex_t destination(graph::vertex_t destination) { + graph::vertex_t destination(graph::vertex_t& destination) { destination_ = destination; } diff --git a/include/graph.h b/include/graph.h index ed9596a..b303169 100644 --- a/include/graph.h +++ b/include/graph.h @@ -112,14 +112,6 @@ class Graph { //! \retval cost Cost of traversed path abm::graph::weight_t path_cost(const std::vector& path); - //! Edge cost - //! \param[in] v0 Start vertex of an edge - //! \param[in] v1 End vertex of the same edge - //! \retval cost Weight of an edge - abm::graph::weight_t get_edge_cost(graph::vertex_t v0, graph::vertex_t v1) { - return (this->edges_.at(std::make_tuple(v0, v1)))->second; - } - private: //! Assign number of vertices //! \param[in] nvertices Number of vertices in graph From c04af1f46f984d5f76cdba9913cd589f5ca7afa9 Mon Sep 17 00:00:00 2001 From: bz247 Date: Thu, 19 Mar 2020 14:35:23 -0700 Subject: [PATCH 15/20] pass constant reference to constructor --- include/agent.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/agent.h b/include/agent.h index f8ff0f3..0dd6743 100644 --- a/include/agent.h +++ b/include/agent.h @@ -18,28 +18,28 @@ class Agent { //! \param[in] id Agent id //! \param[in] origin Agent origin //! \param[in] destination Agent destination - explicit Agent(graph::vertex_t& id) : id_{id} {}; - explicit Agent(graph::vertex_t& id, graph::vertex_t& origin, - graph::vertex_t& 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} {}; //! Get and set id graph::vertex_t id() const { return id_; } - graph::vertex_t id(graph::vertex_t& id) { id_ = id; } + graph::vertex_t id(const graph::vertex_t& id) { id_ = id; } //! Get and set origin graph::vertex_t origin() const { return origin_; } - graph::vertex_t origin(graph::vertex_t& origin) { origin_ = origin; } + graph::vertex_t origin(const graph::vertex_t& origin) { origin_ = origin; } //! Get and set destination graph::vertex_t destination() const { return destination_; } - graph::vertex_t destination(graph::vertex_t& destination) { + graph::vertex_t destination(const graph::vertex_t& destination) { destination_ = destination; } //! Get and set departure time double departure_time() const { return departure_time_; } - double departure_time(double departure_time) { + double departure_time(const double departure_time) { departure_time_ = departure_time; } From 9324477ca685eeb73b06664936cb9b86b857eb91 Mon Sep 17 00:00:00 2001 From: bz247 Date: Thu, 19 Mar 2020 14:52:52 -0700 Subject: [PATCH 16/20] reset changes of graph.cc by checking out the develop branch --- include/agent.h | 7 +------ src/agent.cc | 2 +- src/graph.cc | 10 ++++------ tests/agent_test.cc | 2 -- 4 files changed, 6 insertions(+), 15 deletions(-) diff --git a/include/agent.h b/include/agent.h index 0dd6743..28b4268 100644 --- a/include/agent.h +++ b/include/agent.h @@ -25,21 +25,16 @@ class Agent { //! Get and set id graph::vertex_t id() const { return id_; } - graph::vertex_t id(const graph::vertex_t& id) { id_ = id; } //! Get and set origin graph::vertex_t origin() const { return origin_; } - graph::vertex_t origin(const graph::vertex_t& origin) { origin_ = origin; } //! Get and set destination graph::vertex_t destination() const { return destination_; } - graph::vertex_t destination(const graph::vertex_t& destination) { - destination_ = destination; - } //! Get and set departure time double departure_time() const { return departure_time_; } - double departure_time(const double departure_time) { + void departure_time(const double departure_time) { departure_time_ = departure_time; } diff --git a/src/agent.cc b/src/agent.cc index d6de354..baefd16 100644 --- a/src/agent.cc +++ b/src/agent.cc @@ -63,4 +63,4 @@ // this->status_ = 2; // this->path_.clear(); // } -// }; +// } diff --git a/src/graph.cc b/src/graph.cc index 8565386..78bb362 100644 --- a/src/graph.cc +++ b/src/graph.cc @@ -130,14 +130,11 @@ bool abm::Graph::read_graph_osm(const std::string& filename) { in.read_header(io::ignore_extra_column, "uniqueid", "u", "v", "length"); abm::graph::vertex_t edgeid, v1, v2; abm::graph::weight_t weight; - unsigned nvertices = 0; - std::set vertices; + abm::graph::vertex_t nvertices = 0; while (in.read_row(edgeid, v1, v2, weight)) { this->add_edge(v1, v2, weight, edgeid); - vertices.insert(v1); - vertices.insert(v2); + ++nvertices; } - nvertices = vertices.size(); this->assign_nvertices(nvertices); std::cout << "Graph summary #edges: " << this->edges_.size() << " #vertices: " << this->nvertices_ << "\n"; @@ -278,7 +275,8 @@ std::vector abm::Graph::dijkstra_vertices_ual( std::vector route_vertices; if (path.size() > 0) { for (auto itr = path.begin(); itr != path.end(); ++itr) { - route_vertices.emplace_back(static_cast(*itr)); + if (itr != path.end()) + route_vertices.emplace_back(static_cast(*itr)); } route_vertices.emplace_back(-1); } diff --git a/tests/agent_test.cc b/tests/agent_test.cc index 172f630..90f2d12 100644 --- a/tests/agent_test.cc +++ b/tests/agent_test.cc @@ -1,5 +1,3 @@ -#include - #include "catch.hpp" #include "agent.h" From 0e872b3059c7260ae9426bab686a9853323de39f Mon Sep 17 00:00:00 2001 From: bz247 Date: Thu, 19 Mar 2020 14:58:37 -0700 Subject: [PATCH 17/20] remove .clang-format --- .clang-format | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .clang-format diff --git a/.clang-format b/.clang-format deleted file mode 100644 index e69de29..0000000 From 751d46a48ecaee20c186f0139a52913f510e7c93 Mon Sep 17 00:00:00 2001 From: bz247 Date: Thu, 19 Mar 2020 15:24:57 -0700 Subject: [PATCH 18/20] change agent status to enum class --- include/agent.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/agent.h b/include/agent.h index 28b4268..4aef5ee 100644 --- a/include/agent.h +++ b/include/agent.h @@ -41,10 +41,12 @@ class Agent { // Get current node graph::vertex_t current_node() const { return current_node_; } + //! status: 0: haven't started routing. 1: en_route. 2: arrived. + enum class Status { PREDEPART, ENROUTE, ARRIVE }; //! Get status //! \retval Status of the agent. 0: waiting to depart; 1: enroute; 2: reached //! destination. - int status() const { return status_; } + Status status() const { return status_; } private: //! Agent id @@ -63,7 +65,7 @@ class Agent { graph::vertex_t current_node_{std::numeric_limits::max()}; //! status: 0: haven't started routing. 1: en_route. 2: arrived. - int status_{std::numeric_limits::max()}; + Status status_; }; } // namespace abm From a1df3dbd8cee3d6e980910a3506bfdf97b2fbdcc Mon Sep 17 00:00:00 2001 From: bz247 Date: Thu, 19 Mar 2020 16:56:33 -0700 Subject: [PATCH 19/20] checkout origin/develop src/graph.cc that fixes the CircleCI --- src/graph.cc | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/graph.cc b/src/graph.cc index 78bb362..f08e4c1 100644 --- a/src/graph.cc +++ b/src/graph.cc @@ -130,11 +130,14 @@ bool abm::Graph::read_graph_osm(const std::string& filename) { in.read_header(io::ignore_extra_column, "uniqueid", "u", "v", "length"); abm::graph::vertex_t edgeid, v1, v2; abm::graph::weight_t weight; - abm::graph::vertex_t nvertices = 0; + unsigned nvertices = 0; + std::set vertices; while (in.read_row(edgeid, v1, v2, weight)) { this->add_edge(v1, v2, weight, edgeid); - ++nvertices; + vertices.insert(v1); + vertices.insert(v2); } + nvertices = vertices.size(); this->assign_nvertices(nvertices); std::cout << "Graph summary #edges: " << this->edges_.size() << " #vertices: " << this->nvertices_ << "\n"; @@ -274,10 +277,9 @@ std::vector abm::Graph::dijkstra_vertices_ual( std::vector route_vertices; if (path.size() > 0) { - for (auto itr = path.begin(); itr != path.end(); ++itr) { - if (itr != path.end()) - route_vertices.emplace_back(static_cast(*itr)); - } + for (auto itr = path.begin(); itr != path.end(); ++itr) + route_vertices.emplace_back(static_cast(*itr)); + route_vertices.emplace_back(-1); } return route_vertices; From b61310363a19c6712042e0f65b49308414d8998a Mon Sep 17 00:00:00 2001 From: bz247 Date: Thu, 19 Mar 2020 17:22:29 -0700 Subject: [PATCH 20/20] enum outside of Agent class --- include/agent.h | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/include/agent.h b/include/agent.h index 4aef5ee..fcf08e0 100644 --- a/include/agent.h +++ b/include/agent.h @@ -4,12 +4,14 @@ #include #include -#include "agent.h" #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 { @@ -23,28 +25,27 @@ class Agent { const graph::vertex_t& destination) : id_{id}, origin_{origin}, destination_{destination} {}; - //! Get and set id + //! Return agent id graph::vertex_t id() const { return id_; } - //! Get and set origin + //! Return origin id graph::vertex_t origin() const { return origin_; } - //! Get and set destination + //! Return destination id graph::vertex_t destination() const { return destination_; } - //! Get and set departure time + //! Return departure time double departure_time() const { return departure_time_; } - void departure_time(const double departure_time) { + //! Assign departure time + void departure_time(double departure_time) { departure_time_ = departure_time; } - // Get current node + // Return current node id graph::vertex_t current_node() const { return current_node_; } - //! status: 0: haven't started routing. 1: en_route. 2: arrived. - enum class Status { PREDEPART, ENROUTE, ARRIVE }; - //! Get status - //! \retval Status of the agent. 0: waiting to depart; 1: enroute; 2: reached + //! Return agent status + //! \retval Status of the agent. //! destination. Status status() const { return status_; } @@ -64,8 +65,8 @@ class Agent { //! Agent current node graph::vertex_t current_node_{std::numeric_limits::max()}; - //! status: 0: haven't started routing. 1: en_route. 2: arrived. - Status status_; + //! Agent status + Status status_{Status::YETTODEPART}; }; } // namespace abm