Skip to content

Commit 9eb43b4

Browse files
committed
Add minGreenTime attribute to initTrafficLights function + bugfixes
1 parent 7d22932 commit 9eb43b4

File tree

6 files changed

+86
-48
lines changed

6 files changed

+86
-48
lines changed

src/dsm/dsm.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
static constexpr uint8_t DSM_VERSION_MAJOR = 2;
88
static constexpr uint8_t DSM_VERSION_MINOR = 6;
9-
static constexpr uint8_t DSM_VERSION_PATCH = 20;
9+
static constexpr uint8_t DSM_VERSION_PATCH = 21;
1010

1111
static auto const DSM_VERSION =
1212
std::format("{}.{}.{}", DSM_VERSION_MAJOR, DSM_VERSION_MINOR, DSM_VERSION_PATCH);

src/dsm/headers/RoadNetwork.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,11 @@ namespace dsm {
8181
/// @details The nodes' capacity is adjusted using the graph's streets transport capacity, which may vary basing on the number of lanes. The node capacity will be set to the sum of the incoming streets' transport capacity.
8282
void adjustNodeCapacities();
8383
/// @brief Initialize the traffic lights with random parameters
84+
/// @param minGreenTime The minimum green time for the traffic lights cycles (default is 30)
8485
/// @details Traffic Lights with no parameters set are initialized with random parameters.
8586
/// Street priorities are assigned considering the number of lanes and the speed limit.
8687
/// Traffic Lights with an input degree lower than 3 are converted to standard intersections.
87-
void initTrafficLights();
88+
void initTrafficLights(Delay const minGreenTime = 30);
8889
/// @brief Automatically re-maps street lanes basing on network's topology
8990
/// @details For example, if one street has the right turn forbidden, then the right lane becomes a straight one
9091
void autoMapStreetLanes();

src/dsm/sources/Intersection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace dsm {
1717

1818
void Intersection::addAgent(double angle, std::unique_ptr<Agent> pAgent) {
1919
assert(!isFull());
20-
Logger::debug(std::format("Agente nell'intersezione {}", this->id()));
20+
// Logger::debug(std::format("Agente nell'intersezione {}", this->id()));
2121
auto iAngle{static_cast<int16_t>(angle * 100)};
2222
m_agents.emplace(iAngle, std::move(pAgent));
2323
++m_agentCounter;

src/dsm/sources/RoadNetwork.cpp

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ namespace dsm {
101101
}
102102
}
103103

104-
void RoadNetwork::initTrafficLights() {
104+
void RoadNetwork::initTrafficLights(Delay const minGreenTime) {
105105
for (auto const& [id, pNode] : m_nodes) {
106106
if (!pNode->isTrafficLight()) {
107107
continue;
@@ -137,7 +137,7 @@ namespace dsm {
137137
double const speed{pStreet->maxSpeed()};
138138
int const nLan{pStreet->nLanes()};
139139
auto const cap{pStreet->capacity()};
140-
Logger::debug(std::format("Street {} with capacity {}", streetId, cap));
140+
// Logger::debug(std::format("Street {} with capacity {}", streetId, cap));
141141
capacities.emplace(streetId, cap);
142142
streetAngles.emplace(streetId, pStreet->angle());
143143

@@ -228,6 +228,15 @@ namespace dsm {
228228
greenTimes = std::make_pair(static_cast<Delay>(capPriority * tl.cycleTime()),
229229
static_cast<Delay>(capNoPriority * tl.cycleTime()));
230230
}
231+
// if one of green times is less than 20, set it to 20 and refactor the other to have the sum to 120
232+
if (greenTimes.first < minGreenTime) {
233+
greenTimes.first = minGreenTime;
234+
greenTimes.second = tl.cycleTime() - minGreenTime;
235+
}
236+
if (greenTimes.second < minGreenTime) {
237+
greenTimes.second = minGreenTime;
238+
greenTimes.first = tl.cycleTime() - minGreenTime;
239+
}
231240
std::for_each(inNeighbours.begin(), inNeighbours.end(), [&](Id const inId) {
232241
auto const streetId{inId * nNodes() + id};
233242
auto const nLane{nLanes.at(streetId)};
@@ -389,6 +398,28 @@ namespace dsm {
389398
}
390399
}
391400
}
401+
if (allowedTurns.size() > static_cast<size_t>(nLanes)) {
402+
// if one is duplicate, remove it
403+
std::set<Direction> uniqueDirections;
404+
std::copy(allowedTurns.begin(),
405+
allowedTurns.end(),
406+
std::inserter(uniqueDirections, uniqueDirections.begin()));
407+
allowedTurns.clear();
408+
std::copy(uniqueDirections.begin(),
409+
uniqueDirections.end(),
410+
std::inserter(allowedTurns, allowedTurns.begin()));
411+
}
412+
while (allowedTurns.size() < static_cast<size_t>(nLanes)) {
413+
if (allowedTurns.contains(Direction::STRAIGHT)) {
414+
allowedTurns.emplace(Direction::STRAIGHT);
415+
} else if (allowedTurns.contains(Direction::RIGHT)) {
416+
allowedTurns.emplace(Direction::RIGHT);
417+
} else if (allowedTurns.contains(Direction::LEFT)) {
418+
allowedTurns.emplace(Direction::LEFT);
419+
} else {
420+
allowedTurns.emplace(Direction::ANY);
421+
}
422+
}
392423
switch (nLanes) {
393424
case 1:
394425
// Leaving Direction::ANY for one lane streets is the less painful option
@@ -428,6 +459,12 @@ namespace dsm {
428459
}
429460
[[fallthrough]];
430461
default:
462+
// Logger::info(std::format(
463+
// "Street {}->{} with {} lanes and {} allowed turns",
464+
// pInStreet->source(),
465+
// pInStreet->target(),
466+
// nLanes,
467+
// allowedTurns.size()));
431468
assert(allowedTurns.size() == static_cast<size_t>(nLanes));
432469
// Logger::info(
433470
// std::format("Street {}->{} with {} lanes and {} allowed turns",

src/dsm/sources/Street.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,27 +75,27 @@ namespace dsm {
7575
void Street::enqueue(size_t const& queueId) {
7676
assert(!m_movingAgents.empty());
7777
m_movingAgents.top()->incrementDistance(m_length);
78-
Logger::debug("Pusing into queue");
78+
// Logger::debug("Pusing into queue");
7979
m_exitQueues[queueId].push(
8080
std::move(const_cast<std::unique_ptr<Agent>&>(m_movingAgents.top())));
8181
m_movingAgents.pop();
82-
Logger::debug("Popped from moving queue");
82+
// Logger::debug("Popped from moving queue");
8383
}
8484
std::unique_ptr<Agent> Street::dequeue(size_t index) {
8585
assert(!m_exitQueues[index].empty());
86-
Logger::debug("Dequeueing from queue");
86+
// Logger::debug("Dequeueing from queue");
8787
auto pAgent{std::move(m_exitQueues[index].front())};
8888
m_exitQueues[index].pop();
8989
return pAgent;
9090
}
9191

9292
int Street::nAgents() const {
9393
auto nAgents{static_cast<int>(m_movingAgents.size())};
94-
Logger::debug(std::format("Number of moving agents street {}: {}", id(), nAgents));
94+
// Logger::debug(std::format("Number of moving agents street {}: {}", id(), nAgents));
9595
for (const auto& queue : m_exitQueues) {
9696
nAgents += queue.size();
9797
}
98-
Logger::debug(std::format("Number of agents street {}: {}", id(), nAgents));
98+
// Logger::debug(std::format("Number of agents street {}: {}", id(), nAgents));
9999
return nAgents;
100100
}
101101

src/dsm/sources/TrafficLight.cpp

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -163,48 +163,48 @@ namespace dsm {
163163
std::format("Street id {} is not valid for node {}.", streetId, id())));
164164
}
165165
if (!m_cycles.at(streetId).contains(direction)) {
166-
switch (direction) {
167-
case Direction::RIGHT:
168-
if (m_cycles.at(streetId).contains(Direction::RIGHTANDSTRAIGHT)) {
169-
direction = Direction::RIGHTANDSTRAIGHT;
170-
} else if (m_cycles.at(streetId).contains(Direction::ANY)) {
171-
direction = Direction::ANY;
172-
}
173-
break;
174-
case Direction::LEFT:
175-
if (m_cycles.at(streetId).contains(Direction::LEFTANDSTRAIGHT)) {
176-
direction = Direction::LEFTANDSTRAIGHT;
177-
} else if (m_cycles.at(streetId).contains(Direction::ANY)) {
178-
direction = Direction::ANY;
179-
}
180-
break;
181-
case Direction::STRAIGHT:
182-
if (m_cycles.at(streetId).contains(Direction::RIGHTANDSTRAIGHT)) {
183-
direction = Direction::RIGHTANDSTRAIGHT;
184-
} else if (m_cycles.at(streetId).contains(Direction::LEFTANDSTRAIGHT)) {
185-
direction = Direction::LEFTANDSTRAIGHT;
186-
} else if (m_cycles.at(streetId).contains(Direction::ANY)) {
187-
direction = Direction::ANY;
188-
}
189-
break;
190-
case Direction::UTURN:
191-
if (m_cycles.at(streetId).contains(Direction::LEFT)) {
192-
direction = Direction::LEFT;
193-
} else if (m_cycles.at(streetId).contains(Direction::LEFTANDSTRAIGHT)) {
194-
direction = Direction::LEFTANDSTRAIGHT;
195-
} else if (m_cycles.at(streetId).contains(Direction::ANY)) {
196-
direction = Direction::ANY;
197-
}
198-
break;
199-
default:
200-
if (m_cycles.at(streetId).contains(Direction::ANY)) {
201-
direction = Direction::ANY;
202-
} else {
166+
if (m_cycles.at(streetId).contains(Direction::ANY)) {
167+
direction = Direction::ANY;
168+
} else {
169+
switch (direction) {
170+
case Direction::RIGHT:
171+
if (m_cycles.at(streetId).contains(Direction::RIGHTANDSTRAIGHT)) {
172+
direction = Direction::RIGHTANDSTRAIGHT;
173+
} else if (m_cycles.at(streetId).contains(Direction::ANY)) {
174+
direction = Direction::ANY;
175+
}
176+
break;
177+
case Direction::LEFT:
178+
if (m_cycles.at(streetId).contains(Direction::LEFTANDSTRAIGHT)) {
179+
direction = Direction::LEFTANDSTRAIGHT;
180+
} else if (m_cycles.at(streetId).contains(Direction::ANY)) {
181+
direction = Direction::ANY;
182+
}
183+
break;
184+
case Direction::STRAIGHT:
185+
if (m_cycles.at(streetId).contains(Direction::RIGHTANDSTRAIGHT)) {
186+
direction = Direction::RIGHTANDSTRAIGHT;
187+
} else if (m_cycles.at(streetId).contains(Direction::LEFTANDSTRAIGHT)) {
188+
direction = Direction::LEFTANDSTRAIGHT;
189+
} else if (m_cycles.at(streetId).contains(Direction::ANY)) {
190+
direction = Direction::ANY;
191+
}
192+
break;
193+
case Direction::UTURN:
194+
if (m_cycles.at(streetId).contains(Direction::LEFT)) {
195+
direction = Direction::LEFT;
196+
} else if (m_cycles.at(streetId).contains(Direction::LEFTANDSTRAIGHT)) {
197+
direction = Direction::LEFTANDSTRAIGHT;
198+
} else if (m_cycles.at(streetId).contains(Direction::ANY)) {
199+
direction = Direction::ANY;
200+
}
201+
break;
202+
default:
203203
Logger::warning(std::format(
204204
"Street {} has ...ANDSTRAIGHT phase but Traffic Light {} doesn't.",
205205
streetId,
206206
m_id));
207-
}
207+
}
208208
}
209209
}
210210
if (!m_cycles.at(streetId).contains(direction)) {

0 commit comments

Comments
 (0)