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
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 = 2;
static constexpr uint8_t DSM_VERSION_PATCH = 3;

static auto const DSM_VERSION =
std::format("{}.{}.{}", DSM_VERSION_MAJOR, DSM_VERSION_MINOR, DSM_VERSION_PATCH);
Expand Down
8 changes: 6 additions & 2 deletions src/dsm/headers/Dynamics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@
class Dynamics {
private:
std::map<Id, std::unique_ptr<agent_t>> m_agents;
std::unordered_map<Id, std::unique_ptr<Itinerary>> m_itineraries;

protected:
std::unordered_map<Id, std::unique_ptr<Itinerary>> m_itineraries;
Graph m_graph;
Time m_time, m_previousSpireTime;
std::mt19937_64 m_generator;
Expand Down Expand Up @@ -113,8 +113,12 @@
}
} else if ((nextNodeId != destinationID)) {
std::cerr << std::format(
"\033[38;2;130;30;180mWARNING: No path found from node {} "
"\033[38;2;130;30;180mWARNING ({}:{}): No "

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 4.1 rule Note

MISRA 4.1 rule
"path found "
"from node {} "
"to node {}\033[0m",
__FILE__,
__LINE__,
nextNodeId,
destinationID)
<< std::endl;
Expand Down
85 changes: 43 additions & 42 deletions src/dsm/headers/Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@
const auto& oldStreetPriorities{intersection.streetPriorities()};
std::set<Id> newStreetPriorities;
for (const auto streetId : oldStreetPriorities) {
newStreetPriorities.emplace(newStreetIds[streetId]);
newStreetPriorities.emplace(newStreetIds.at(streetId));
}
intersection.setStreetPriorities(newStreetPriorities);
}
if (node->isTrafficLight()) {
auto& trafficLight = dynamic_cast<TrafficLight&>(*node);
std::unordered_map<Id, std::vector<TrafficLightCycle>> newCycles;
for (auto const& [streetId, cycles] : trafficLight.cycles()) {
newCycles.emplace(newStreetIds[streetId], std::move(cycles));
newCycles.emplace(newStreetIds.at(streetId), std::move(cycles));
}
trafficLight.setCycles(newCycles);
}
Expand All @@ -95,8 +95,8 @@

void Graph::m_setStreetAngles() {
for (const auto& [streetId, street] : m_streets) {
const auto& srcNode{m_nodes[street->nodePair().first]};
const auto& dstNode{m_nodes[street->nodePair().second]};
const auto& srcNode{m_nodes.at(street->u())};
const auto& dstNode{m_nodes.at(street->v())};
if (srcNode->coords().has_value() && dstNode->coords().has_value()) {
street->setAngle(srcNode->coords().value(), dstNode->coords().value());
}
Expand All @@ -117,9 +117,9 @@
}

void Graph::buildStreetAngles() {
for (const auto& street : m_streets) {
const auto& node1{m_nodes[street.second->nodePair().first]};
const auto& node2{m_nodes[street.second->nodePair().second]};
for (auto const& street : m_streets) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 10.4 rule Note

MISRA 10.4 rule
const auto& node1{m_nodes.at(street.second->u())};
const auto& node2{m_nodes.at(street.second->v())};
street.second->setAngle(node1->coords().value(), node2->coords().value());
}
}
Expand All @@ -129,16 +129,19 @@
for (Id nodeId = 0; nodeId < m_nodes.size(); ++nodeId) {
value = 0;
for (const auto& [streetId, _] : m_adjacency.getCol(nodeId, true)) {
value += m_streets[streetId]->nLanes() * m_streets[streetId]->transportCapacity();
auto const& pStreet{m_streets.at(streetId)};
value += pStreet->nLanes() * pStreet->transportCapacity();
}
m_nodes[nodeId]->setCapacity(value);
auto const& pNode{m_nodes.at(nodeId)};
pNode->setCapacity(value);
value = 0;
for (const auto& [streetId, _] : m_adjacency.getRow(nodeId, true)) {
value += m_streets[streetId]->nLanes() * m_streets[streetId]->transportCapacity();
auto const& pStreet{m_streets.at(streetId)};
value += pStreet->nLanes() * pStreet->transportCapacity();
}
m_nodes[nodeId]->setTransportCapacity(value == 0 ? 1 : value);
if (m_nodes[nodeId]->capacity() == 0) {
m_nodes[nodeId]->setCapacity(value);
pNode->setTransportCapacity(value == 0 ? 1 : value);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.1 rule Note

MISRA 12.1 rule
if (pNode->capacity() == 0) {
pNode->setCapacity(value);
}
}
}
Expand Down Expand Up @@ -178,7 +181,7 @@
} else {
addEdge<Street>(index, std::make_pair(srcId, dstId), val);
}
m_streets[index]->setMaxSpeed(defaultSpeed);
m_streets.at(index)->setMaxSpeed(defaultSpeed);
}
} else {
// default case: read the file as a matrix with the first two elements being the number of rows and columns and
Expand Down Expand Up @@ -224,7 +227,7 @@
} else {
addEdge<Street>(index, std::make_pair(srcId, dstId), value);
}
m_streets[index]->setMaxSpeed(defaultSpeed);
m_streets.at(index)->setMaxSpeed(defaultSpeed);
}
++index;
}
Expand All @@ -246,8 +249,17 @@
double lat, lon;
for (Size i{0}; i < n; ++i) {
file >> lat >> lon;
if (m_nodes.contains(i)) {
m_nodes[i]->setCoords(std::make_pair(lat, lon));
auto const& it{m_nodes.find(i)};
if (it != m_nodes.cend()) {
it->second->setCoords(std::make_pair(lat, lon));
} else {
std::cerr << std::format(
"\033[38;2;130;30;180mWARNING ({}:{}): Node with id {} not "

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 4.1 rule Note

MISRA 4.1 rule
"found.\033[0m",

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 4.1 rule Note

MISRA 4.1 rule
__FILE__,
__LINE__,
i)
<< std::endl;
}
}
} else if (fileExt == "csv") {
Expand All @@ -274,10 +286,16 @@
std::getline(iss, lon, '\n');
dLat = lat == "Nan" ? 0. : std::stod(lat);
dLon = lon == "Nan" ? 0. : std::stod(lon);
if (m_nodes.contains(std::stoul(nodeId))) {
m_nodes[std::stoul(nodeId)]->setCoords(std::make_pair(dLat, dLon));
auto const& it{m_nodes.find(std::stoul(nodeId))};
if (it != m_nodes.cend()) {
it->second->setCoords(std::make_pair(dLat, dLon));
} else {
std::cerr << std::format("WARNING: Node with id {} not found.", nodeId)
std::cerr << std::format(
"\033[38;2;130;30;180mWARNING ({}:{}): Node with id {} not "

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 4.1 rule Note

MISRA 4.1 rule
"found.\033[0m",

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 4.1 rule Note

MISRA 4.1 rule
__FILE__,
__LINE__,
nodeId)
<< std::endl;
}
}
Expand Down Expand Up @@ -430,46 +448,29 @@
TrafficLight& Graph::makeTrafficLight(Id const nodeId,
Delay const cycleTime,
Delay const counter) {
if (!m_nodes.contains(nodeId)) {
throw std::invalid_argument(buildLog("Node does not exist."));
}
auto& pNode = m_nodes[nodeId];
auto& pNode = m_nodes.at(nodeId);
pNode = std::make_unique<TrafficLight>(*pNode, cycleTime, counter);
return dynamic_cast<TrafficLight&>(*pNode);
}

Roundabout& Graph::makeRoundabout(Id nodeId) {
if (!m_nodes.contains(nodeId)) {
throw std::invalid_argument(buildLog("Node does not exist."));
}
auto& pNode = m_nodes[nodeId];
auto& pNode = m_nodes.at(nodeId);
pNode = std::make_unique<Roundabout>(*pNode);
return dynamic_cast<Roundabout&>(*pNode);
}

Station& Graph::makeStation(Id nodeId, const unsigned int managementTime) {
if (!m_nodes.contains(nodeId)) {
throw std::invalid_argument(buildLog("Node does not exist."));
}
auto& pNode = m_nodes[nodeId];
auto& pNode = m_nodes.at(nodeId);
pNode = std::make_unique<Station>(*pNode, managementTime);
return dynamic_cast<Station&>(*pNode);
}
StochasticStreet& Graph::makeStochasticStreet(Id streetId, double const flowRate) {
if (!m_streets.contains(streetId)) {
throw std::invalid_argument(
buildLog(std::format("Street with id {} does not exist.", streetId)));
}
auto& pStreet = m_streets[streetId];
auto& pStreet = m_streets.at(streetId);
pStreet = std::make_unique<StochasticStreet>(pStreet->id(), *pStreet, flowRate);
return dynamic_cast<StochasticStreet&>(*pStreet);
}
void Graph::makeSpireStreet(Id streetId) {
if (!m_streets.contains(streetId)) {
throw std::invalid_argument(
buildLog(std::format("Street with id {} does not exist.", streetId)));
}
auto& pStreet = m_streets[streetId];
auto& pStreet = m_streets.at(streetId);
if (pStreet->isStochastic()) {
pStreet = std::make_unique<StochasticSpireStreet>(
pStreet->id(), *pStreet, dynamic_cast<StochasticStreet&>(*pStreet).flowRate());
Expand Down
27 changes: 14 additions & 13 deletions src/dsm/headers/RoadDynamics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@
if (!(this->itineraries().empty())) {
if (!(m_errorProbability.has_value() &&
uniformDist(this->m_generator) < m_errorProbability)) {
const auto& it = this->m_itineraries[pAgent->itineraryId()];
const auto& it = this->itineraries().at(pAgent->itineraryId());
if (it->destination() != nodeId) {
possibleMoves = it->path().getRow(nodeId, true);
}
Expand Down Expand Up @@ -296,7 +296,7 @@
}
if (!pAgent->isRandom()) {
if (destinationNode->id() ==
this->m_itineraries[pAgent->itineraryId()]->destination()) {
this->itineraries().at(pAgent->itineraryId())->destination()) {
bArrived = true;
}
}
Expand Down Expand Up @@ -414,11 +414,11 @@
auto const nLanes = street->nLanes();
bool bArrived{false};
if (!agent->isRandom()) {
if (this->m_itineraries[agent->itineraryId()]->destination() ==
if (this->itineraries().at(agent->itineraryId())->destination() ==
street->nodePair().second) {
agent->updateItinerary();
}
if (this->m_itineraries[agent->itineraryId()]->destination() ==
if (this->itineraries().at(agent->itineraryId())->destination() ==
street->nodePair().second) {
bArrived = true;
}
Expand Down Expand Up @@ -525,7 +525,7 @@
requires(is_numeric_v<delay_t>)
void RoadDynamics<delay_t>::addAgentsUniformly(Size nAgents,
std::optional<Id> optItineraryId) {
if (this->m_itineraries.empty()) {
if (this->itineraries().empty()) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 14.4 rule Note

MISRA 14.4 rule
// TODO: make this possible for random agents
throw std::invalid_argument(
buildLog("It is not possible to add random agents without itineraries."));
Expand All @@ -536,17 +536,17 @@
itineraryId = optItineraryId.value();
}
std::uniform_int_distribution<Size> itineraryDist{
0, static_cast<Size>(this->m_itineraries.size() - 1)};
0, static_cast<Size>(this->itineraries().size() - 1)};
std::uniform_int_distribution<Size> streetDist{
0, static_cast<Size>(this->m_graph.nEdges() - 1)};
for (Size i{0}; i < nAgents; ++i) {
if (randomItinerary) {
auto itineraryIt{this->m_itineraries.begin()};
auto itineraryIt{this->itineraries().cbegin()};
std::advance(itineraryIt, itineraryDist(this->m_generator));
itineraryId = itineraryIt->first;
}
Id agentId{0};
if (!this->agents().empty()) {
if (!(this->agents().empty())) {
agentId = this->agents().rbegin()->first + 1;
}
Id streetId{0};
Expand All @@ -559,9 +559,10 @@
this->nAgents() < this->m_graph.maxCapacity());
const auto& street{this->m_graph.streetSet()[streetId]};
this->addAgent(agentId, itineraryId, street->nodePair().first);
this->agents().at(agentId)->setStreetId(streetId);
auto const& pAgent{this->agents().at(agentId)};
pAgent->setStreetId(streetId);
this->setAgentSpeed(agentId);
this->agents().at(agentId)->incrementDelay(
pAgent->incrementDelay(
std::ceil(street->length() / this->agents().at(agentId)->speed()));
street->addAgent(agentId);
++agentId;
Expand Down Expand Up @@ -650,12 +651,12 @@
}
}
// find the itinerary with the given destination as destination
auto itineraryIt{std::find_if(this->m_itineraries.begin(),
this->m_itineraries.end(),
auto itineraryIt{std::find_if(this->itineraries().cbegin(),
this->itineraries().cend(),
[dstId](const auto& itinerary) {
return itinerary.second->destination() == dstId;
})};
if (itineraryIt == this->m_itineraries.end()) {
if (itineraryIt == this->itineraries().cend()) {
throw std::invalid_argument(
buildLog(std::format("Itinerary with destination {} not found.", dstId)));
}
Expand Down
16 changes: 8 additions & 8 deletions src/dsm/headers/TrafficLight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ namespace dsm {
}
switch (direction) {
case Direction::RIGHTANDSTRAIGHT:
m_cycles[streetId][Direction::RIGHT] = cycle;
m_cycles[streetId][Direction::STRAIGHT] = cycle;
m_cycles.at(streetId)[Direction::RIGHT] = cycle;
m_cycles.at(streetId)[Direction::STRAIGHT] = cycle;
break;
case Direction::LEFTANDSTRAIGHT:
m_cycles[streetId][Direction::LEFT] = cycle;
m_cycles[streetId][Direction::STRAIGHT] = cycle;
m_cycles.at(streetId)[Direction::LEFT] = cycle;
m_cycles.at(streetId)[Direction::STRAIGHT] = cycle;
break;
case Direction::ANY:
m_cycles[streetId][Direction::RIGHT] = cycle;
m_cycles[streetId][Direction::STRAIGHT] = cycle;
m_cycles[streetId][Direction::LEFT] = cycle;
m_cycles.at(streetId)[Direction::RIGHT] = cycle;
m_cycles.at(streetId)[Direction::STRAIGHT] = cycle;
m_cycles.at(streetId)[Direction::LEFT] = cycle;
break;
default:
m_cycles[streetId][direction] = cycle;
m_cycles.at(streetId)[direction] = cycle;
break;
}
}
Expand Down
Loading