Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
46e1544
Init traffic light rework
Grufoony Nov 27, 2024
19c30b6
Bugfix and add another test
Grufoony Nov 27, 2024
354ac24
Another one bites the test
Grufoony Nov 27, 2024
b551685
Add utility function for creating cycles
Grufoony Nov 27, 2024
f6cb061
Update stalingrado simulation
Grufoony Nov 27, 2024
3545361
Update slow_charge_tl simulation
Grufoony Nov 27, 2024
2ac1991
Formatting
Grufoony Nov 27, 2024
ff95162
I pay for the whole RAM, I use the whole RAM
Grufoony Nov 28, 2024
d0d4c86
Small bugfix
Grufoony Nov 28, 2024
4cb0434
Enhancements
Grufoony Nov 28, 2024
58da6a8
Merge branch 'main' into rework_TrafficLights
Grufoony Nov 28, 2024
b9fd6cd
Fix typo in error msg
Grufoony Nov 29, 2024
f0ea4dd
Evolve optimization
Grufoony Nov 29, 2024
c367cdf
Add parallel flag
Grufoony Nov 29, 2024
d050ee0
Revert: no buono
Grufoony Nov 29, 2024
85c0bc5
Revert priority functionality
Grufoony Dec 2, 2024
a84514c
Add enum ofr traffic light optimization level
Grufoony Dec 2, 2024
36818cc
Add functions to dynamically edit traffic lights
Grufoony Dec 2, 2024
4b93837
Re-implement traffic light optimization algorithm
Grufoony Dec 2, 2024
88ff07a
Formatting
Grufoony Dec 2, 2024
3f7edda
Fix
Grufoony Dec 2, 2024
e6bfb9a
Small fix
Grufoony Dec 2, 2024
3f31b14
Renamed algorithm versions
Grufoony Dec 3, 2024
be636cf
Remove some this-> calls
Grufoony Dec 3, 2024
dabb105
Add some docs
Grufoony Dec 3, 2024
8ad5d75
Bugfix in single-tail tl optimization
Grufoony Dec 4, 2024
4217832
Merge branch 'main' into rework_TrafficLights
Grufoony Dec 4, 2024
2f9a022
Merge branch 'main' into rework_TrafficLights
Grufoony Dec 5, 2024
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
16 changes: 11 additions & 5 deletions examples/slow_charge_tl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
using Dynamics = dsm::FirstOrderDynamics<Delay>;
using Street = dsm::Street;
using SpireStreet = dsm::SpireStreet;
using TrafficLight = dsm::TrafficLight<Delay>;
using TrafficLight = dsm::TrafficLight;

void printLoadingBar(int const i, int const n) {
std::cout << "Loading: " << std::setprecision(2) << std::fixed << (i * 100. / n) << "%"
Expand Down Expand Up @@ -149,7 +149,7 @@

std::cout << "Traffic Lightning the simulation...\n";
for (Unit i{0}; i < graph.nodeSet().size(); ++i) {
graph.makeTrafficLight<Delay>(i);
graph.makeTrafficLight(i, 120);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
}
std::cout << "Making every street a spire...\n";
for (const auto& [id, street] : graph.streetSet()) {
Expand Down Expand Up @@ -179,7 +179,6 @@
while (value < 0.) {
value = random();
}
tl.setDelay(static_cast<Delay>(value));
const auto& col = adj.getCol(nodeId, true);
std::set<Unit> streets;
const auto id = col.begin();
Expand All @@ -199,7 +198,14 @@
streets.emplace(c);
}
}
tl.setStreetPriorities(streets);
for (auto const& streetId : streets) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 10.4 rule Note

MISRA 10.4 rule
tl.setCycle(streetId, dsm::Direction::ANY, {static_cast<dsm::Delay>(value), 0});

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
}
for (const auto& [c, value] : col) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
if (streets.find(c) == streets.end()) {
tl.setComplementaryCycle(c, *streets.begin());
}
}
++sda[streets.size() - 1];
// std::cout << "Node id: " << nodeId << " has " << streets.size()
// << "streets.\n";
Expand Down Expand Up @@ -313,7 +319,7 @@
}
dynamics.evolve(false);
if (OPTIMIZE && (dynamics.time() % 420 == 0)) {
dynamics.optimizeTrafficLights(std::floor(420. / 60), 0.15, 3. / 10);
dynamics.optimizeTrafficLights(0.15, 3. / 10);
}
if (dynamics.time() % 2400 == 0 && nAgents > 0) {
// auto meanDelta = std::accumulate(deltas.begin(), deltas.end(), 0) /
Expand Down
23 changes: 10 additions & 13 deletions examples/stalingrado.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ using Itinerary = dsm::Itinerary;
using Dynamics = dsm::FirstOrderDynamics<Delay>;
using Street = dsm::Street;
using SpireStreet = dsm::SpireStreet;
using TrafficLight = dsm::TrafficLight<Delay>;
using TrafficLight = dsm::TrafficLight;

void printLoadingBar(int const i, int const n) {
std::cout << "Loading: " << std::setprecision(2) << std::fixed << (i * 100. / n) << "%"
Expand All @@ -46,29 +46,28 @@ int main() {
const auto MAX_TIME{static_cast<Unit>(timeUnit * vehiclesToInsert.size())};

// Create the graph
TrafficLight tl1{1}, tl2{2}, tl3{3}, tl4{4};

// Street(StreetId, Capacity, Length, vMax, (from, to))
Street s01{1, 2281 / 8, 2281., 13.9, std::make_pair(0, 1)};
Street s12{7, 118 / 8, 118., 13.9, std::make_pair(1, 2)};
Street s23{13, 222 / 8, 222., 13.9, std::make_pair(2, 3)};
Street s34{19, 651 / 4, 651., 13.9, std::make_pair(3, 4)};
// Viale Aldo Moro
tl1.setDelay(std::make_pair(62, 70)); // 40, 70
TrafficLight tl1{1, 132};
tl1.setCycle(s01.id(), dsm::Direction::ANY, {62, 0});
tl1.setCapacity(1);
tl1.addStreetPriority(s01.id());
// Via Donato Creti
tl2.setDelay(std::make_pair(72, 69)); // 50, 75
TrafficLight tl2{2, 141};
tl2.setCycle(s12.id(), dsm::Direction::ANY, {72, 0});
tl2.setCapacity(1);
tl2.addStreetPriority(s12.id());
// Via del Lavoro
tl3.setDelay(std::make_pair(88, 50)); // 40, 70
TrafficLight tl3{3, 138};
tl3.setCycle(s23.id(), dsm::Direction::ANY, {88, 0});
tl3.setCapacity(1);
tl3.addStreetPriority(s23.id());
// Viali
tl4.setDelay(std::make_pair(81, 50)); // 38, 106 = 144
TrafficLight tl4{4, 131};
tl4.setCycle(s34.id(), dsm::Direction::ANY, {81, 0});
tl4.setCapacity(1);
tl4.addStreetPriority(s34.id());

Graph graph;
graph.addNode(std::make_unique<TrafficLight>(tl1));
Expand All @@ -77,7 +76,7 @@ int main() {
graph.addNode(std::make_unique<TrafficLight>(tl4));
graph.addStreets(s01, s12, s23, s34);
graph.buildAdj();
graph.makeSpireStreet(19);
auto& spire = graph.makeSpireStreet(19);

std::cout << "Intersections: " << graph.nodeSet().size() << '\n';
std::cout << "Streets: " << graph.streetSet().size() << '\n';
Expand All @@ -91,8 +90,6 @@ int main() {
dynamics.addItinerary(itinerary);
dynamics.updatePaths();

auto& spire = dynamic_cast<SpireStreet&>(*dynamics.graph().streetSet().at(19));

// lauch progress bar
std::jthread t([MAX_TIME]() {
while (progress < MAX_TIME) {
Expand Down
4 changes: 2 additions & 2 deletions src/dsm/dsm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <format>

static constexpr uint8_t DSM_VERSION_MAJOR = 2;
static constexpr uint8_t DSM_VERSION_MINOR = 1;
static constexpr uint8_t DSM_VERSION_PATCH = 10;
static constexpr uint8_t DSM_VERSION_MINOR = 2;
static constexpr uint8_t DSM_VERSION_PATCH = 0;

static auto const DSM_VERSION =
std::format("{}.{}.{}", DSM_VERSION_MAJOR, DSM_VERSION_MINOR, DSM_VERSION_PATCH);
Expand Down
76 changes: 38 additions & 38 deletions src/dsm/headers/Agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/// @details This file contains the definition of the Agent class.
/// The Agent class represents an agent in the network. It is templated by the type
/// of the agent's id and the size of agents, which must both be unsigned integrals.
/// It is also templated by the Delay type, which must be a numeric (see utility/TypeTraits/is_numeric.hpp)
/// It is also templated by the delay_t type, which must be a numeric (see utility/TypeTraits/is_numeric.hpp)
/// and represents the spatial or temporal (depending on the type of the template) distance
/// between the agent and the one in front of it.

Expand All @@ -25,16 +25,16 @@
/// @brief The Agent class represents an agent in the network.
/// @tparam Id, The type of the agent's id. It must be an unsigned integral type.
/// @tparam Size, The type of the size of a street. It must be an unsigned integral type.
/// @tparam Delay, The type of the agent's delay. It must be a numeric type (see utility/TypeTraits/is_numeric.hpp).
template <typename Delay>
requires(is_numeric_v<Delay>)
/// @tparam delay_t, The type of the agent's delay. It must be a numeric type (see utility/TypeTraits/is_numeric.hpp).
template <typename delay_t>
requires(is_numeric_v<delay_t>)
class Agent {
private:
Id m_id;
Id m_itineraryId;
std::optional<Id> m_streetId;
std::optional<Id> m_srcNodeId;
Delay m_delay;
delay_t m_delay;
double m_speed;
double m_distance; // Travelled distance
unsigned int m_time; // Travelled time
Expand Down Expand Up @@ -68,7 +68,7 @@
/// @brief Increment the agent's delay by a given value
/// @param delay The agent's delay
/// @throw std::overflow_error, if delay has reached its maximum value
void incrementDelay(Delay const delay);
void incrementDelay(delay_t const delay);
/// @brief Decrement the agent's delay by 1
/// @throw std::underflow_error, if delay has reached its minimum value
void decrementDelay();
Expand Down Expand Up @@ -105,7 +105,7 @@
double speed() const { return m_speed; }
/// @brief Get the agent's delay
/// @return The agent's delay
Delay delay() const { return m_delay; }
delay_t delay() const { return m_delay; }
/// @brief Get the agent's travelled distance
/// @return The agent's travelled distance
double distance() const { return m_distance; }
Expand All @@ -114,19 +114,19 @@
unsigned int time() const { return m_time; }
};

template <typename Delay>
requires(is_numeric_v<Delay>)
Agent<Delay>::Agent(Id id, Id itineraryId)
template <typename delay_t>
requires(is_numeric_v<delay_t>)
Agent<delay_t>::Agent(Id id, Id itineraryId)
: m_id{id},
m_itineraryId{itineraryId},
m_delay{0},
m_speed{0.},
m_distance{0.},
m_time{0} {}

template <typename Delay>
requires(is_numeric_v<Delay>)
Agent<Delay>::Agent(Id id, Id itineraryId, Id srcNodeId)
template <typename delay_t>
requires(is_numeric_v<delay_t>)
Agent<delay_t>::Agent(Id id, Id itineraryId, Id srcNodeId)
: m_id{id},
m_itineraryId{itineraryId},
m_srcNodeId{srcNodeId},
Expand All @@ -135,59 +135,59 @@
m_distance{0.},
m_time{0} {}

template <typename Delay>
requires(is_numeric_v<Delay>)
void Agent<Delay>::setSpeed(double speed) {
template <typename delay_t>
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"));
}
m_speed = speed;
}
template <typename Delay>
requires(is_numeric_v<Delay>)
void Agent<Delay>::incrementDelay() {
if (m_delay == std::numeric_limits<Delay>::max()) {
throw std::overflow_error(buildLog("Delay has reached its maximum value"));
template <typename delay_t>
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"));
}
++m_delay;
}
template <typename Delay>
requires(is_numeric_v<Delay>)
void Agent<Delay>::incrementDelay(Delay const 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 has reached its maximum value"));
throw std::overflow_error(buildLog("delay_t has reached its maximum value"));
}
m_delay += delay;
}
template <typename Delay>
requires(is_numeric_v<Delay>)
void Agent<Delay>::decrementDelay() {
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 has reached its minimum value"));
throw std::underflow_error(buildLog("delay_t has reached its minimum value"));
}
--m_delay;
}

template <typename Delay>
requires(is_numeric_v<Delay>)
void Agent<Delay>::incrementDistance(double distance) {
template <typename delay_t>
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"));
}
m_distance += distance;
}

template <typename Delay>
requires(is_numeric_v<Delay>)
void Agent<Delay>::incrementTime() {
template <typename delay_t>
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"));
}
++m_time;
}
template <typename Delay>
requires(is_numeric_v<Delay>)
void Agent<Delay>::incrementTime(unsigned int const time) {
template <typename delay_t>
requires(is_numeric_v<delay_t>)
void Agent<delay_t>::incrementTime(unsigned int const time) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

time is Y2038-unsafe Note

time is Y2038-unsafe
if (m_time + time < m_time) {
throw std::overflow_error(buildLog("Time has reached its maximum value"));
}
Expand Down
Loading
Loading