Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# vs configuration code
.vscode/

# Temporary files
*~*
*#*
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 $<TARGET_FILE:abmtest>)
Expand Down
74 changes: 74 additions & 0 deletions include/agent.h
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} {};
Copy link
Contributor

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

Copy link
Author

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_; } in graph.h

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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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()};
Copy link
Contributor

Choose a reason for hiding this comment

The 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_
66 changes: 66 additions & 0 deletions src/agent.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// #include <vector>
Copy link
Contributor

Choose a reason for hiding this comment

The 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();
// }
// }
37 changes: 37 additions & 0 deletions tests/agent_test.cc
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);
}
}
}