Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions examples/stalingrado.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ int main() {
graph.makeSpireStreet(19);
auto& spire = dynamic_cast<SpireStreet&>(*graph.street(19));

std::cout << "Intersections: " << graph.nNodes() << '\n';
std::cout << "Streets: " << graph.nEdges() << '\n';
dsm::logger.info(std::format("Intersections: {}", graph.nNodes()));
dsm::logger.info(std::format("Streets: {}", graph.nEdges()));

// Create the dynamics
Dynamics dynamics{graph, 69, 0.95};
Expand Down
2 changes: 1 addition & 1 deletion src/dsm/dsm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

static constexpr uint8_t DSM_VERSION_MAJOR = 2;
static constexpr uint8_t DSM_VERSION_MINOR = 3;
static constexpr uint8_t DSM_VERSION_PATCH = 6;
static constexpr uint8_t DSM_VERSION_PATCH = 7;

static auto const DSM_VERSION =
std::format("{}.{}.{}", DSM_VERSION_MAJOR, DSM_VERSION_MINOR, DSM_VERSION_PATCH);
Expand Down
20 changes: 13 additions & 7 deletions src/dsm/headers/Agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ namespace dsm {
requires(is_numeric_v<delay_t>)
void Agent<delay_t>::setSpeed(double speed) {
if (speed < 0) {
throw std::invalid_argument(buildLog("Speed must be positive"));
logger.error(std::format("Speed ({}) of agent {} must be positive", speed, m_id));
}
m_speed = speed;
}
Expand All @@ -184,23 +184,26 @@ namespace dsm {
requires(is_numeric_v<delay_t>)
void Agent<delay_t>::incrementDelay() {
if (m_delay == std::numeric_limits<delay_t>::max()) {
throw std::overflow_error(buildLog("delay_t has reached its maximum value"));
throw std::overflow_error(
logger.buildExceptionMessage("delay_t has reached its maximum value"));
}
++m_delay;
}
template <typename delay_t>
requires(is_numeric_v<delay_t>)
void Agent<delay_t>::incrementDelay(delay_t const delay) {
if (m_delay + delay < m_delay) {
throw std::overflow_error(buildLog("delay_t has reached its maximum value"));
throw std::overflow_error(
logger.buildExceptionMessage("delay_t has reached its maximum value"));
}
m_delay += delay;
}
template <typename delay_t>
requires(is_numeric_v<delay_t>)
void Agent<delay_t>::decrementDelay() {
if (m_delay == 0) {
throw std::underflow_error(buildLog("delay_t has reached its minimum value"));
throw std::underflow_error(
logger.buildExceptionMessage("delay_t has reached its minimum value"));
}
--m_delay;
}
Expand All @@ -209,7 +212,8 @@ namespace dsm {
requires(is_numeric_v<delay_t>)
void Agent<delay_t>::incrementDistance(double distance) {
if (distance < 0) {
throw std::invalid_argument(buildLog("Distance travelled must be positive"));
logger.error(std::format(
"Distance travelled ({}) by agent {} must be positive", distance, m_id));
}
m_distance += distance;
}
Expand All @@ -218,15 +222,17 @@ namespace dsm {
requires(is_numeric_v<delay_t>)
void Agent<delay_t>::incrementTime() {
if (m_time == std::numeric_limits<unsigned int>::max()) {
throw std::overflow_error(buildLog("Time has reached its maximum value"));
throw std::overflow_error(
logger.buildExceptionMessage("Time has reached its maximum value"));
}
++m_time;
}
template <typename delay_t>
requires(is_numeric_v<delay_t>)
void Agent<delay_t>::incrementTime(unsigned int const time) {
if (m_time + time < m_time) {
throw std::overflow_error(buildLog("Time has reached its maximum value"));
throw std::overflow_error(
logger.buildExceptionMessage("Time has reached its maximum value"));
}
m_time += time;
}
Expand Down
31 changes: 11 additions & 20 deletions src/dsm/headers/Dynamics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,17 @@ namespace dsm {
path.insert(nodeId, nextNodeId, true);
}
} else if ((nextNodeId != destinationID)) {
std::cerr << std::format(
"\033[38;2;130;30;180mWARNING ({}:{}): No "
"path found "
"from node {} "
"to node {}\033[0m",
__FILE__,
__LINE__,
nextNodeId,
destinationID)
<< std::endl;
logger.warning(std::format(
"No path found from node {} to node {}", nextNodeId, destinationID));
}
}
}
if (path.size() == 0) {
throw std::runtime_error(
buildLog(std::format("Path with id {} and destination {} is empty. Please "
"check the adjacency matrix.",
pItinerary->id(),
pItinerary->destination())));
logger.error(
std::format("Path with id {} and destination {} is empty. Please "
"check the adjacency matrix.",
pItinerary->id(),
pItinerary->destination()));
}
pItinerary->setPath(path);
}
Expand Down Expand Up @@ -307,8 +299,7 @@ namespace dsm {
bool updatePaths) {
for (const auto& nodeId : destinationNodes) {
if (!m_graph.nodeSet().contains(nodeId)) {
throw std::invalid_argument(
buildLog(std::format("Node with id {} not found", nodeId)));
logger.error(std::format("Node with id {} not found", nodeId));
}
this->addItinerary(Itinerary{nodeId, nodeId});
}
Expand All @@ -320,13 +311,13 @@ namespace dsm {
template <typename agent_t>
void Dynamics<agent_t>::addAgent(std::unique_ptr<agent_t> agent) {
if (m_agents.size() + 1 > m_graph.maxCapacity()) {
throw std::overflow_error(buildLog(
throw std::overflow_error(logger.buildExceptionMessage(
std::format("Graph is already holding the max possible number of agents ({})",
m_graph.maxCapacity())));
}
if (m_agents.contains(agent->id())) {
throw std::invalid_argument(
buildLog(std::format("Agent with id {} already exists.", agent->id())));
throw std::invalid_argument(logger.buildExceptionMessage(
std::format("Agent with id {} already exists.", agent->id())));
}
m_agents.emplace(agent->id(), std::move(agent));
}
Expand Down
18 changes: 8 additions & 10 deletions src/dsm/headers/Edge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,28 @@ namespace dsm {
m_transportCapacity{transportCapacity},
m_angle{angle} {
if (capacity < 1) {
throw std::invalid_argument(
buildLog(std::format("Edge capacity ({}) must be greater than 0.", capacity)));
logger.error(std::format("Edge capacity ({}) must be greater than 0.", capacity));
}
if (transportCapacity < 1) {
throw std::invalid_argument(buildLog(std::format(
"Edge transport capacity ({}) must be greater than 0.", transportCapacity)));
logger.error(std::format("Edge transport capacity ({}) must be greater than 0.",
transportCapacity));
}
if (std::abs(angle) > 2 * std::numbers::pi) {
throw std::invalid_argument(buildLog(
std::format("Edge angle ({}) must be in the range [-2pi, 2pi].", angle)));
logger.error(
std::format("Edge angle ({}) must be in the range [-2pi, 2pi].", angle));
}
}

void Edge::setCapacity(int capacity) {
if (capacity < 1) {
throw std::invalid_argument(
buildLog(std::format("Edge capacity ({}) must be greater than 0.", capacity)));
logger.error(std::format("Edge capacity ({}) must be greater than 0.", capacity));
}
m_capacity = capacity;
}
void Edge::setTransportCapacity(int capacity) {
if (capacity < 1) {
throw std::invalid_argument(buildLog(
std::format("Edge transport capacity ({}) must be greater than 0.", capacity)));
logger.error(
std::format("Edge transport capacity ({}) must be greater than 0.", capacity));
}
m_transportCapacity = capacity;
}
Expand Down
22 changes: 10 additions & 12 deletions src/dsm/headers/FirstOrderDynamics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ namespace dsm {
FirstOrderDynamics::FirstOrderDynamics(Graph& graph,
std::optional<unsigned int> seed,
double alpha)
: RoadDynamics<Delay>(graph, seed), m_alpha{0.}, m_speedFluctuationSTD{0.} {
: RoadDynamics<Delay>(graph, seed), m_alpha{alpha}, m_speedFluctuationSTD{0.} {
if (alpha < 0. || alpha > 1.) {
throw std::invalid_argument(buildLog(std::format(
"The minimum speed rateo must be between 0 and 1, but it is {}", alpha)));
} else {
m_alpha = alpha;
logger.error(std::format("The minimum speed rateo ({}) must be in [0, 1[", alpha));
}
double globMaxTimePenalty{0.};
for (const auto& [streetId, street] : this->m_graph.streetSet()) {
Expand All @@ -18,11 +15,11 @@ namespace dsm {
std::ceil(street->length() / ((1. - m_alpha) * street->maxSpeed())));
}
if (globMaxTimePenalty > static_cast<double>(std::numeric_limits<Delay>::max())) {
throw std::overflow_error(
buildLog(std::format("The maximum time penalty ({}) is greater than the "
"maximum value of delay_t ({})",
globMaxTimePenalty,
std::numeric_limits<Delay>::max())));
throw std::overflow_error(logger.buildExceptionMessage(
std::format("The maximum time penalty ({}) is greater than the "
"maximum value of delay_t ({})",
globMaxTimePenalty,
std::numeric_limits<Delay>::max())));
}
}

Expand All @@ -40,8 +37,9 @@ namespace dsm {

void FirstOrderDynamics::setSpeedFluctuationSTD(double speedFluctuationSTD) {
if (speedFluctuationSTD < 0.) {
throw std::invalid_argument(
buildLog("The speed fluctuation standard deviation must be positive."));
logger.error(
std::format("The speed fluctuation standard deviation ({}) must be positive",
speedFluctuationSTD));
}
m_speedFluctuationSTD = speedFluctuationSTD;
}
Expand Down
Loading
Loading