Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
libsumo/TrafficLight.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2017-2026 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
21// C++ TraCI client API implementation
22/****************************************************************************/
23#include <config.h>
24
26#include <microsim/MSLane.h>
27#include <microsim/MSEdge.h>
28#include <microsim/MSNet.h>
30#include <microsim/MSStop.h>
44#include "Helper.h"
45#include "TrafficLight.h"
46
47//#define DEBUG_CONSTRAINT_DEADLOCK
48
49namespace libsumo {
50// ===========================================================================
51// static member initializations
52// ===========================================================================
53SubscriptionResults TrafficLight::mySubscriptionResults;
54ContextSubscriptionResults TrafficLight::myContextSubscriptionResults;
55
56// ===========================================================================
57// static member definitions
58// ===========================================================================
59std::vector<std::string>
60TrafficLight::getIDList() {
62}
63
64
65int
66TrafficLight::getIDCount() {
67 return (int)getIDList().size();
68}
69
70
71std::string
72TrafficLight::getRedYellowGreenState(const std::string& tlsID) {
74}
75
76
77std::vector<TraCILogic>
78TrafficLight::getAllProgramLogics(const std::string& tlsID) {
79 std::vector<TraCILogic> result;
80 const std::vector<MSTrafficLightLogic*> logics = Helper::getTLS(tlsID).getAllLogics();
81 for (MSTrafficLightLogic* logic : logics) {
82 TraCILogic l(logic->getProgramID(), (int)logic->getLogicType(), logic->getCurrentPhaseIndex());
83 l.offset = STEPS2TIME(logic->getOffset());
84 l.subParameter = logic->getParametersMap();
85 for (const MSPhaseDefinition* const phase : logic->getPhases()) {
86 l.phases.emplace_back(new TraCIPhase(STEPS2TIME(phase->duration), phase->getState(),
87 STEPS2TIME(phase->minDuration), STEPS2TIME(phase->maxDuration),
88 phase->getNextPhases(), phase->getName()));
89 }
90 result.emplace_back(l);
91 }
92 return result;
93}
94
95
96std::vector<std::string>
97TrafficLight::getControlledJunctions(const std::string& tlsID) {
98 std::set<std::string> junctionIDs;
100 for (const MSTrafficLightLogic::LinkVector& llinks : links) {
101 for (const MSLink* l : llinks) {
102 junctionIDs.insert(l->getJunction()->getID());
103 }
104 }
105 return std::vector<std::string>(junctionIDs.begin(), junctionIDs.end());
106}
107
108
109std::vector<std::string>
110TrafficLight::getControlledLanes(const std::string& tlsID) {
111 std::vector<std::string> laneIDs;
113 for (const MSTrafficLightLogic::LaneVector& llanes : lanes) {
114 for (const MSLane* l : llanes) {
115 laneIDs.push_back(l->getID());
116 }
117 }
118 return laneIDs;
119}
120
121
122std::vector<std::vector<TraCILink> >
123TrafficLight::getControlledLinks(const std::string& tlsID) {
124 std::vector<std::vector<TraCILink> > result;
127 for (int i = 0; i < (int)lanes.size(); ++i) {
128 std::vector<TraCILink> subList;
129 const MSTrafficLightLogic::LaneVector& llanes = lanes[i];
130 const MSTrafficLightLogic::LinkVector& llinks = links[i];
131 // number of links controlled by this signal (signal i)
132 for (int j = 0; j < (int)llanes.size(); ++j) {
133 MSLink* link = llinks[j];
134 // approached non-internal lane (if any)
135 const std::string to = link->getLane() != nullptr ? link->getLane()->getID() : "";
136 // approached "via", internal lane (if any)
137 const std::string via = link->getViaLane() != nullptr ? link->getViaLane()->getID() : "";
138 subList.emplace_back(TraCILink(llanes[j]->getID(), via, to));
139 }
140 result.emplace_back(subList);
141 }
142 return result;
143}
144
145
146std::string
147TrafficLight::getProgram(const std::string& tlsID) {
148 return Helper::getTLS(tlsID).getActive()->getProgramID();
149}
150
151
152int
153TrafficLight::getPhase(const std::string& tlsID) {
155}
156
157
158std::string
159TrafficLight::getPhaseName(const std::string& tlsID) {
161}
162
163
164double
165TrafficLight::getPhaseDuration(const std::string& tlsID) {
166 return STEPS2TIME(Helper::getTLS(tlsID).getActive()->getCurrentPhaseDef().duration);
167}
168
169
170double
171TrafficLight::getNextSwitch(const std::string& tlsID) {
172 return STEPS2TIME(Helper::getTLS(tlsID).getActive()->getNextSwitchTime());
173}
174
175
176double
177TrafficLight::getSpentDuration(const std::string& tlsID) {
178 return STEPS2TIME(Helper::getTLS(tlsID).getActive()->getSpentDuration());
179}
180
181int
182TrafficLight::getServedPersonCount(const std::string& tlsID, int index) {
183 MSTrafficLightLogic* const active = Helper::getTLS(tlsID).getActive();
184 if (index < 0 || active->getPhaseNumber() <= index) {
185 throw TraCIException("The phase index " + toString(index) + " is not in the allowed range [0,"
186 + toString(active->getPhaseNumber() - 1) + "].");
187 }
188 // find all crossings which have a green light in that phas
189 int result = 0;
190
191 const std::string& state = active->getPhases()[index]->getState();
192 for (int i = 0; i < (int)state.size(); i++) {
193 if (state[i] == LINKSTATE_TL_GREEN_MAJOR) {
194 for (MSLink* link : active->getLinksAt(i)) {
195 if (link->getLane()->isCrossing()) {
196 // walking forwards across
197 for (MSTransportable* person : link->getLaneBefore()->getEdge().getPersons()) {
198 if (static_cast<MSPerson*>(person)->getNextEdge() == link->getLane()->getEdge().getID()) {
199 result += 1;
200 }
201 }
202 // walking backwards across
203 MSLane* walkingAreaAcross = link->getLane()->getLinkCont().front()->getLane();
204 for (MSTransportable* person : walkingAreaAcross->getEdge().getPersons()) {
205 if (static_cast<MSPerson*>(person)->getNextEdge() == link->getLane()->getEdge().getID()) {
206 result += 1;
207 }
208 }
209 } else if (link->getLaneBefore()->isCrossing()) {
210 // walking backwards across (in case both sides are separately controlled)
211 for (MSTransportable* person : link->getLane()->getEdge().getPersons()) {
212 if (static_cast<MSPerson*>(person)->getNextEdge() == link->getLaneBefore()->getEdge().getID()) {
213 result += 1;
214 }
215 }
216 }
217 }
218 }
219 }
220 return result;
221}
222
223std::vector<std::string>
224TrafficLight::getBlockingVehicles(const std::string& tlsID, int linkIndex) {
225 std::vector<std::string> result;
226 // for railsignals we cannot use the "online" program
227 MSTrafficLightLogic* const active = Helper::getTLS(tlsID).getDefault();
228 if (linkIndex < 0 || linkIndex >= active->getNumLinks()) {
229 throw TraCIException("The link index " + toString(linkIndex) + " is not in the allowed range [0,"
230 + toString(active->getNumLinks() - 1) + "].");
231 }
232 for (const SUMOVehicle* veh : active->getBlockingVehicles(linkIndex)) {
233 result.push_back(veh->getID());
234 }
235 return result;
236}
237
238std::vector<std::string>
239TrafficLight::getRivalVehicles(const std::string& tlsID, int linkIndex) {
240 std::vector<std::string> result;
241 MSTrafficLightLogic* const active = Helper::getTLS(tlsID).getDefault();
242 if (linkIndex < 0 || linkIndex >= active->getNumLinks()) {
243 throw TraCIException("The link index " + toString(linkIndex) + " is not in the allowed range [0,"
244 + toString(active->getNumLinks() - 1) + "].");
245 }
246 for (const SUMOVehicle* veh : active->getRivalVehicles(linkIndex)) {
247 result.push_back(veh->getID());
248 }
249 return result;
250}
251
252std::vector<std::string>
253TrafficLight::getPriorityVehicles(const std::string& tlsID, int linkIndex) {
254 std::vector<std::string> result;
255 MSTrafficLightLogic* const active = Helper::getTLS(tlsID).getDefault();
256 if (linkIndex < 0 || linkIndex >= active->getNumLinks()) {
257 throw TraCIException("The link index " + toString(linkIndex) + " is not in the allowed range [0,"
258 + toString(active->getNumLinks() - 1) + "].");
259 }
260 for (const SUMOVehicle* veh : active->getPriorityVehicles(linkIndex)) {
261 result.push_back(veh->getID());
262 }
263 return result;
264}
265
266std::vector<TraCISignalConstraint>
267TrafficLight::getConstraints(const std::string& tlsID, const std::string& tripId) {
268 std::vector<TraCISignalConstraint> result;
269 MSTrafficLightLogic* const active = Helper::getTLS(tlsID).getDefault();
270 MSRailSignal* s = dynamic_cast<MSRailSignal*>(active);
271 if (s == nullptr) {
272 throw TraCIException("'" + tlsID + "' is not a rail signal");
273 }
274 for (auto item : s->getConstraints()) {
275 if (tripId != "" && tripId != item.first) {
276 continue;
277 }
278 for (MSRailSignalConstraint* c : item.second) {
279 result.push_back(buildConstraint(tlsID, item.first, c));
280 }
281 }
282 return result;
283}
284
285std::vector<TraCISignalConstraint>
286TrafficLight::getConstraintsByFoe(const std::string& foeSignal, const std::string& foeId) {
287 // retrieve all constraints that have the given foeSignal (optionally filtered by foeId)
288 // @note could improve efficiency by storing a map of rail signals in MSRailSignalControl
289 std::vector<TraCISignalConstraint> result;
290 for (const std::string& tlsID : getIDList()) {
291 MSTrafficLightLogic* const active = Helper::getTLS(tlsID).getDefault();
292 MSRailSignal* s = dynamic_cast<MSRailSignal*>(active);
293 if (s != nullptr) {
294 for (auto item : s->getConstraints()) {
295 for (MSRailSignalConstraint* cand : item.second) {
297 if (pc != nullptr && pc->myFoeSignal->getID() == foeSignal
298 && (foeId == "" || pc->myTripId == foeId)) {
299 result.push_back(buildConstraint(s->getID(), item.first, pc));
300 }
301 }
302 }
303 }
304 }
305 return result;
306}
307
308
309void
310TrafficLight::addConstraint(const std::string& tlsID, const std::string& tripId, const std::string& foeSignal, const std::string& foeId, const int type, const int limit) {
311 MSTrafficLightLogic* const active = Helper::getTLS(tlsID).getDefault();
312 MSTrafficLightLogic* const active2 = Helper::getTLS(foeSignal).getDefault();
313 MSRailSignal* s = dynamic_cast<MSRailSignal*>(active);
314 MSRailSignal* s2 = dynamic_cast<MSRailSignal*>(active2);
315 if (s == nullptr) {
316 throw TraCIException("'" + tlsID + "' is not a rail signal");
317 }
318 if (s2 == nullptr) {
319 throw TraCIException("'" + foeSignal + "' is not a rail signal");
320 }
322 s->addConstraint(tripId, c);
323}
324
325
326std::vector<TraCISignalConstraint>
327TrafficLight::swapConstraints(const std::string& tlsID, const std::string& tripId, const std::string& foeSignal, const std::string& foeId) {
328#ifdef DEBUG_CONSTRAINT_DEADLOCK
329 std::cout << "swapConstraints tlsId=" << tlsID << " tripId=" << tripId << " foeSignal=" << foeSignal << " foeId=" << foeId << "\n";
330#endif
331 MSTrafficLightLogic* const active = Helper::getTLS(tlsID).getDefault();
332 MSTrafficLightLogic* const active2 = Helper::getTLS(foeSignal).getDefault();
333 MSRailSignal* s = dynamic_cast<MSRailSignal*>(active);
334 MSRailSignal* s2 = dynamic_cast<MSRailSignal*>(active2);
335 if (s == nullptr) {
336 throw TraCIException("'" + tlsID + "' is not a rail signal");
337 }
338 if (s2 == nullptr) {
339 throw TraCIException("'" + foeSignal + "' is not a rail signal");
340 }
342 for (auto item : s->getConstraints()) {
343 if (tripId == item.first) {
344 for (MSRailSignalConstraint* cand : item.second) {
346 if (pc != nullptr && pc->myFoeSignal->getID() == foeSignal && pc->myTripId == foeId) {
347 c = pc;
348 break;
349 }
350 }
351 break;
352 }
353 }
354 if (c != nullptr) {
355 const int limit = c->myLimit;
356 // the two constraints are complementary so we actually remove rather than deactivate to avoid redundant conflict information
358 MSRailSignalConstraint* swapped = new MSRailSignalConstraint_Predecessor(type, s, tripId, limit, true);
359 swapped->updateParameters(c->getParametersMap());
360 swapParameters(swapped);
361 s->removeConstraint(tripId, c);
362 s2->addConstraint(foeId, swapped);
363 return findConstraintsDeadLocks(foeId, tripId, foeSignal, tlsID);
364 } else {
365 throw TraCIException("Rail signal '" + tlsID + "' does not have a constraint for tripId '" + tripId + "' with foeSignal '" + foeSignal + "' and foeId '" + foeId + "'");
366 }
367}
368
369
370std::vector<std::pair<std::string, std::string> >
371TrafficLight::getSwapParams(int constraintType) {
372 std::vector<std::pair<std::string, std::string> > result({
373 {"vehID", "foeID"},
374 {"line", "foeLine"},
375 {"arrival", "foeArrival"}});
376
378 std::vector<std::pair<std::string, std::string> > special({
379 {"busStop", "busStop2"},
380 {"priorStop", "priorStop2"},
381 {"stopArrival", "foeStopArrival"}});
382 result.insert(result.end(), special.begin(), special.end());
383 }
384 return result;
385}
386
387
388void
389TrafficLight::swapParameters(MSRailSignalConstraint* c) {
390 // swap parameters that were assigned by generateRailSignalConstraints.py
391 for (auto keys : getSwapParams(c->getType())) {
392 swapParameters(c, keys.first, keys.second);
393 }
394}
395
396void
397TrafficLight::swapParameters(MSRailSignalConstraint* c, const std::string& key1, const std::string& key2) {
398 const std::string value1 = c->getParameter(key1);
399 const std::string value2 = c->getParameter(key2);
400 if (value1 != "") {
401 c->setParameter(key2, value1);
402 } else {
403 c->unsetParameter(key2);
404 }
405 if (value2 != "") {
406 c->setParameter(key1, value2);
407 } else {
408 c->unsetParameter(key1);
409 }
410}
411
412void
413TrafficLight::swapParameters(TraCISignalConstraint& c) {
414 // swap parameters that were assigned by generateRailSignalConstraints.py
415 for (auto keys : getSwapParams(c.type)) {
416 swapParameters(c, keys.first, keys.second);
417 }
418}
419
420void
421TrafficLight::swapParameters(TraCISignalConstraint& c, const std::string& key1, const std::string& key2) {
422 auto it1 = c.param.find(key1);
423 auto it2 = c.param.find(key2);
424 const std::string value1 = it1 != c.param.end() ? it1->second : "";
425 const std::string value2 = it2 != c.param.end() ? it2->second : "";
426 if (value1 != "") {
427 c.param[key2] = value1;
428 } else {
429 c.param.erase(key2);
430 }
431 if (value2 != "") {
432 c.param[key1] = value2;
433 } else {
434 c.param.erase(key1);
435 }
436}
437
438
439void
440TrafficLight::removeConstraints(const std::string& tlsID, const std::string& tripId, const std::string& foeSignal, const std::string& foeId) {
441 // remove all constraints that have the given foeId
442 // @note could improve efficiency by storing a map of rail signals in MSRailSignalControl
443 for (const std::string& tlsCand : getIDList()) {
444 if (tlsID == "" || tlsCand == tlsID) {
445 MSTrafficLightLogic* const active = Helper::getTLS(tlsCand).getDefault();
446 MSRailSignal* s = dynamic_cast<MSRailSignal*>(active);
447 if (s != nullptr) {
448 for (auto item : s->getConstraints()) {
449 if (tripId == "" || item.first == tripId) {
450 for (MSRailSignalConstraint* cand : item.second) {
452 if (pc != nullptr
453 && (foeId == "" || pc->myTripId == foeId)
454 && (foeSignal == "" || pc->myFoeSignal->getID() == foeSignal)) {
455 cand->setActive(false);
456 }
457 }
458 }
459 }
460 }
461 }
462 }
463}
464
465
466void
467TrafficLight::updateConstraints(const std::string& vehID, std::string tripId) {
468 // Removes all constraints that can no longer be met because the route of
469 // vehID does not pass the signal involved in the constraint with the given tripId.
470 // This includes constraints on tripId as well as constraints where tripId is the foeId.
471
472 MSBaseVehicle* veh = Helper::getVehicle(vehID);
473 std::string curTripId = veh->getParameter().getParameter("tripId", veh->getID());
474 tripId = tripId == "" ? curTripId : tripId;
475
476 // find signals and tripId along the route of veh
477 std::map<const MSRailSignal*, std::set<std::string> > onRoute;
478 const ConstMSEdgeVector& route = veh->getRoute().getEdges();
479 auto routeIt = veh->getCurrentRouteEdge();
480 for (const MSStop& stop : veh->getStops()) {
481 for (auto it = routeIt; it < stop.edge; it++) {
482 const MSEdge* edge = *it;
484 if (it + 1 != route.end()) {
485 const MSEdge* next = *(it + 1);
486 const MSLink* link = edge->getLanes()[0]->getLinkTo(next->getLanes()[0]);
487 if (link != nullptr && link->getTLLogic() != nullptr) {
488 const MSRailSignal* s = dynamic_cast<const MSRailSignal*>(link->getTLLogic());
489 onRoute[s].insert(curTripId);
490 }
491 }
492 }
493 }
494 if (stop.pars.tripId != "") {
495 curTripId = stop.pars.tripId;
496 }
497 routeIt = stop.edge;
498 }
499 for (auto it = routeIt; it < route.end(); it++) {
500 const MSEdge* edge = *it;
502 if (it + 1 != route.end()) {
503 const MSEdge* next = *(it + 1);
504 const MSLink* link = edge->getLanes()[0]->getLinkTo(next->getLanes()[0]);
505 if (link != nullptr && link->getTLLogic() != nullptr) {
506 const MSRailSignal* s = dynamic_cast<const MSRailSignal*>(link->getTLLogic());
507 onRoute[s].insert(curTripId);
508 }
509 }
510 }
511 }
512 //for (auto item : onRoute) {
513 // std::cout << " s=" << item.first->getID() << " @" << item.first << " ids=" << toString(item.second) << "\n";
514 //}
515
516 // check relevance for all active contraints
518
519 // record outdated constraints on and by the vehicle
520 std::vector<MSRailSignalConstraint*> onVeh;
521 std::vector<std::pair<std::string, MSRailSignalConstraint*> > byVeh;
522
523 for (auto item : s->getConstraints()) {
524 for (MSRailSignalConstraint* cand : item.second) {
526 if (pc != nullptr && !pc->cleared() && pc->isActive()) {
527 if (item.first == tripId) {
528 if (onRoute[s].count(tripId) == 0) {
529 // constraint on our veh no longer relevant
530 onVeh.push_back(cand);
531 }
532 } else if (pc->myTripId == tripId) {
533 if (onRoute[pc->myFoeSignal].count(tripId) == 0) {
534 // constraint by our veh no longer relevant
535 byVeh.push_back(std::make_pair(item.first, cand));
536 }
537 }
538 }
539 }
540 }
541 for (MSRailSignalConstraint* c : onVeh) {
542 s->removeConstraint(tripId, c);
543 }
544 for (auto item : byVeh) {
545 s->removeConstraint(item.first, item.second);
546 }
547 }
548}
549
550
551std::vector<TraCISignalConstraint>
552TrafficLight::findConstraintsDeadLocks(const std::string& foeId, const std::string& tripId, const std::string& foeSignal, const std::string& tlsID) {
553 std::vector<TraCISignalConstraint> result;
554 // find circular constraints (deadlock)
555 // foeId is now constrainted by tripId and assumed to follow tripId on the
556 // same track without possibility of overtaking
557 // we look for a third vehicle foeId2 where
558 // tripId waits for foeId2 and foeId2 waits on foeId
559 std::map<std::string, TraCISignalConstraint> constraintsOnTripId;
560 std::map<std::string, TraCISignalConstraint> constrainedByFoeId;
561 std::set<std::string> foeId2Cands1;
562 std::set<std::string> foeId2Cands2;
564 for (auto item : s->getConstraints()) {
565 for (MSRailSignalConstraint* cand : item.second) {
567 if (pc != nullptr && !pc->cleared() && pc->isActive()) {
568 if (item.first == tripId) {
569 // tripId waits for foe2
570 // @could there by more than one constraint on tripId by this foe2?
571 libsumo::TraCISignalConstraint tsc = buildConstraint(s->getID(), item.first, pc);
572 constraintsOnTripId[pc->myTripId] = tsc;
573 foeId2Cands1.insert(pc->myTripId);
574 for (std::string& futureFoe2Id : getFutureTripIds(pc->myTripId)) {
575 foeId2Cands1.insert(futureFoe2Id);
576 //tsc.foeId = futureFoe2Id; // if we do this, the constraint to swap will not be found
577 constraintsOnTripId[futureFoe2Id] = tsc;
578 }
579 } else if (pc->myTripId == foeId) {
580 // foeId2 waits for foe
581 libsumo::TraCISignalConstraint tsc = buildConstraint(s->getID(), item.first, pc);
582 constrainedByFoeId[item.first] = tsc;
583 foeId2Cands2.insert(item.first);
584 for (std::string& futureTripId : getFutureTripIds(item.first)) {
585 foeId2Cands2.insert(futureTripId);
586 //tsc.tripId = futureTripId; // if we do this, the constraint to swap will not be found
587 constrainedByFoeId[futureTripId] = tsc;
588 }
589 }
590 }
591 }
592 }
593 }
594#ifdef DEBUG_CONSTRAINT_DEADLOCK
595 std::cout << "findConstraintsDeadLocks foeId=" << foeId << " tripId=" << tripId << " foeSignal=" << foeSignal << " tlsID=" << tlsID << "\n";
596 std::cout << " foeId2Cands1=" << toString(foeId2Cands1) << "\n";
597 std::cout << " foeId2Cands2=" << toString(foeId2Cands2) << "\n";
598#endif
599 if (foeId2Cands1.size() > 0) {
600 // foe2 might be constrained implicitly by foe due to following on the same track
601 // in this case foe must be on the route of foe2 between its current position and foeSignal
602
603 // we have to check this first because it also affects foeInsertion
604 // constraints if the foe is already inserted but hasn't yet passed the
605 // signal (cleared == false).
606 SUMOVehicle* foe = getVehicleByTripId(foeId);
607 if (foe != nullptr) {
608 const MSEdge* foeEdge = foe->getEdge();
609 const double foePos = foe->getPositionOnLane();
610 for (const std::string& foeId2 : foeId2Cands1) {
611 // tripId waits for foeId2
612 SUMOVehicle* foe2 = getVehicleByTripId(foeId2);
613 if (foe2 != nullptr) {
614 const ConstMSEdgeVector& foe2Route = foe2->getRoute().getEdges();
615 const TraCISignalConstraint& c = constraintsOnTripId[foeId2];
616 bool foeAhead = false;
617 for (int i = foe2->getRoutePosition(); i < (int)foe2Route.size(); i++) {
618 const MSEdge* e = foe2Route[i];
619 if (e == foeEdge &&
620 ((e != foe2->getEdge() || foe2->getPositionOnLane() < foePos)
621 || (foe->hasDeparted() && !foe2->hasDeparted())
622 || (!foe->hasDeparted() && !foe2->hasDeparted() &&
623 (foe->getParameter().depart < foe2->getParameter().depart
624 || (foe->getParameter().depart == foe2->getParameter().depart && foe->getNumericalID() < foe2->getNumericalID())))
625 )) {
626 foeAhead = true;
627#ifdef DEBUG_CONSTRAINT_DEADLOCK
628 std::cout << "findConstraintsDeadLocks foeId=" << foeId << " tripId=" << tripId << " foeSignal=" << foeSignal << "\n";
629 std::cout << " foeLeaderDeadlock foeEdge=" << foeEdge->getID() << " foe2=" << foe2->getParameter().getParameter("tripId", foe2->getID())
630 << " routePos=" << foe2->getRoutePosition() << " futureRPos=" << i << " e=" << e->getID()
631 //<< " foePos=" << foePos << " foe2Pos=" << foe2->getPositionOnLane()
632 << " " << constraintsOnTripId[foeId2].getString() << "\n";
633#endif
634 break;
635 }
636 if (e->getToJunction()->getID() == foeSignal
637 || e->getToJunction()->getID() == c.foeSignal) {
638 break;
639 }
640 }
641 if (foeAhead) {
642 // foe cannot wait for foe2 (since it's behind). Instead foe2 must wait for tripId
643 TraCISignalConstraint nc; // constraint after swap
644 nc.tripId = c.foeId;
645 nc.foeId = c.tripId;
646 nc.signalId = c.foeSignal;
647 nc.foeSignal = c.signalId;
648 nc.limit = c.limit;
649 nc.type = c.type;
650 nc.mustWait = true; // ???
651 nc.active = true;
652 nc.param = c.param;
653 swapParameters(nc);
654 result.push_back(nc);
655 // let foe wait for foe2
656 std::vector<TraCISignalConstraint> result2 = swapConstraints(c.signalId, c.tripId, c.foeSignal, c.foeId);
657 result.insert(result.end(), result2.begin(), result2.end());
658
659 // Other deadlocks might not be valid anymore so we need a fresh recheck for remaining implicit or explicit deadlocks
660 const std::vector<TraCISignalConstraint>& result4 = findConstraintsDeadLocks(foeId, tripId, foeSignal, tlsID);
661 result.insert(result.end(), result4.begin(), result4.end());
662 return result;
663 }
664 }
665 }
666 }
667 }
668
669 if (foeId2Cands2.size() > 0) {
670 // tripId might be constrained implicitly by foe2 due to following on the same track
671 // in this case foe2 must be on the route of tripId between its current position and tlsID
672 // if foe2 then waits for foe, deadlock occurs
673
674 SUMOVehicle* ego = getVehicleByTripId(tripId);
675 if (ego != nullptr && (ego->hasDeparted() || !ego->getParameter().wasSet(VEHPARS_FORCE_REROUTE))) {
676 std::set<const MSEdge*> egoToSignal;
677 const double egoPos = ego->getPositionOnLane();
678 const ConstMSEdgeVector& egoRoute = ego->getRoute().getEdges();
679 for (int i = ego->getRoutePosition(); i < (int)egoRoute.size(); i++) {
680 const MSEdge* e = egoRoute[i];
681 egoToSignal.insert(e);
682 if (e->getToJunction()->getID() == tlsID) {
683 break;
684 }
685 }
686
687 for (const std::string& foeId2 : foeId2Cands2) {
688 // foeId2 waits for foe
689 SUMOVehicle* foe2 = getVehicleByTripId(foeId2);
690 //std::cout << " foe2=" << foe2->getID() << " edge=" << foe2->getEdge()->getID() << " egoToSignal=" << toString(egoToSignal) << "\n";
691 if (foe2 != nullptr) {
692 if (egoToSignal.count(foe2->getEdge()) != 0
693 && (foe2->getEdge() != ego->getEdge() || foe2->getPositionOnLane() > egoPos)) {
694 const TraCISignalConstraint& c = constrainedByFoeId[foeId2];
695#ifdef DEBUG_CONSTRAINT_DEADLOCK
696 std::cout << "findConstraintsDeadLocks foeId=" << foeId << " tripId=" << tripId << " foeSignal=" << foeSignal << "\n";
697 std::cout << " egoLeaderDeadlock foe2Edge=" << foe2->getEdge()->getID() << " foe2=" << foe2->getParameter().getParameter("tripId", foe2->getID())
698 << " " << c.getString() << "\n";
699#endif
700 // foe is already waiting for tripId (ego) and should also wait for foeId2
701 TraCISignalConstraint nc; // constraint after swap
702 nc.tripId = c.foeId;
703 nc.foeId = c.tripId;
704 nc.signalId = c.foeSignal;
705 nc.foeSignal = c.signalId;
706 nc.limit = c.limit;
707 nc.type = c.type;
708 nc.mustWait = true; // ???
709 nc.active = true;
710 nc.param = c.param;
711 swapParameters(nc);
712 result.push_back(nc);
713 // let foe wait for foe2
714 std::vector<TraCISignalConstraint> result2 = swapConstraints(c.signalId, c.tripId, c.foeSignal, c.foeId);
715 result.insert(result.end(), result2.begin(), result2.end());
716
717 // Other deadlocks might not be valid anymore so we need a fresh recheck for remaining implicit or explicit deadlocks
718 const std::vector<TraCISignalConstraint>& result4 = findConstraintsDeadLocks(foeId, tripId, foeSignal, tlsID);
719 result.insert(result.end(), result4.begin(), result4.end());
720 return result;
721 }
722 }
723 }
724 } else if (ego != nullptr) {
725 WRITE_WARNINGF(TL("Cannot check for all deadlocks on swapConstraints because the route for vehicle '%' is not computed yet"), ego->getID());
726 }
727 }
728
729 // find deadlock in explicit constraints
730 std::vector<std::string> foeIds2;
731 std::set_intersection(
732 foeId2Cands1.begin(), foeId2Cands1.end(),
733 foeId2Cands2.begin(), foeId2Cands2.end(),
734 std::back_inserter(foeIds2));
735#ifdef DEBUG_CONSTRAINT_DEADLOCK
736 std::cout << "findConstraintsDeadLocks foeId=" << foeId << " tripId=" << tripId << " foeSignal=" << foeSignal << "\n";
737 for (const std::string& foeId2 : foeIds2) {
738 std::cout << " deadlockId=" << foeId2 << " " << constraintsOnTripId[foeId2].getString() << " " << constrainedByFoeId[foeId2].getString() << "\n";
739 }
740#endif
741 if (foeIds2.size() > 0) {
742 TraCISignalConstraint c = constrainedByFoeId[foeIds2.front()];
744 // avoid swapping insertion constraint
745 c = constraintsOnTripId[foeIds2.front()];
746 }
747 TraCISignalConstraint nc; // constraint after swap
748 nc.tripId = c.foeId;
749 nc.foeId = c.tripId;
750 nc.signalId = c.foeSignal;
751 nc.foeSignal = c.signalId;
752 nc.limit = c.limit;
753 nc.type = c.type;
754 nc.mustWait = true; // ???
755 nc.active = true;
756 nc.param = c.param;
757 swapParameters(nc);
758 result.push_back(nc);
759 // let foe wait for foe2
760 const std::vector<TraCISignalConstraint>& result2 = swapConstraints(c.signalId, c.tripId, c.foeSignal, c.foeId);
761 result.insert(result.end(), result2.begin(), result2.end());
762 if (foeIds2.size() > 1) {
763 // calling swapConstraints once may result in further swaps so we have to recheck for remaining deadlocks anew
764 const std::vector<TraCISignalConstraint>& result3 = findConstraintsDeadLocks(foeId, tripId, foeSignal, tlsID);
765 result.insert(result.end(), result3.begin(), result3.end());
766 }
767 }
768 return result;
769}
770
771
773TrafficLight::getVehicleByTripId(const std::string tripOrVehID) {
775 for (MSVehicleControl::constVehIt i = c.loadedVehBegin(); i != c.loadedVehEnd(); ++i) {
776 SUMOVehicle* veh = i->second;
777 if (veh->getParameter().getParameter("tripId", veh->getID()) == tripOrVehID) {
778 return veh;
779 }
780 }
781 return nullptr;
782}
783
784
785std::vector<std::string>
786TrafficLight::getFutureTripIds(const std::string vehID) {
787 std::vector<std::string> result;
789 if (veh) {
790 std::string tripId = veh->getParameter().getParameter("tripId");
791 if (tripId != "") {
792 result.push_back(tripId);
793 }
794 for (const MSStop& stop : veh->getStops()) {
795 if (stop.pars.tripId != "") {
796 result.push_back(stop.pars.tripId);
797 }
798 }
799 }
800 return result;
801}
802
803
804std::string
805TrafficLight::getParameter(const std::string& tlsID, const std::string& paramName) {
807 if (StringUtils::startsWith(paramName, "NEMA.") && tll->getLogicType() != TrafficLightType::NEMA) {
808 throw TraCIException("'" + tlsID + "' is not a NEMA controller");
809 }
810 return tll->getParameter(paramName, "");
811}
812
813
815
816
817void
818TrafficLight::setRedYellowGreenState(const std::string& tlsID, const std::string& state) {
819 Helper::getTLS(tlsID).setStateInstantiatingOnline(MSNet::getInstance()->getTLSControl(), state);
820}
821
822
823void
824TrafficLight::setPhase(const std::string& tlsID, const int index) {
825 MSTrafficLightLogic* const active = Helper::getTLS(tlsID).getActive();
826 if (index < 0 || active->getPhaseNumber() <= index) {
827 throw TraCIException("The phase index " + toString(index) + " is not in the allowed range [0,"
828 + toString(active->getPhaseNumber() - 1) + "].");
829 }
831 const SUMOTime duration = active->getPhase(index).duration;
832 active->changeStepAndDuration(MSNet::getInstance()->getTLSControl(), cTime, index, duration);
833}
834
835void
836TrafficLight::setPhaseName(const std::string& tlsID, const std::string& name) {
837 MSTrafficLightLogic* const active = Helper::getTLS(tlsID).getActive();
838 const_cast<MSPhaseDefinition&>(active->getCurrentPhaseDef()).setName(name);
839}
840
841
842void
843TrafficLight::setProgram(const std::string& tlsID, const std::string& programID) {
844 try {
845 Helper::getTLS(tlsID).switchTo(MSNet::getInstance()->getTLSControl(), programID);
846 } catch (ProcessError& e) {
847 throw TraCIException(e.what());
848 }
849}
850
851
852void
853TrafficLight::setPhaseDuration(const std::string& tlsID, const double phaseDuration) {
854 MSTrafficLightLogic* const active = Helper::getTLS(tlsID).getActive();
856 active->changeStepAndDuration(MSNet::getInstance()->getTLSControl(), cTime, -1, TIME2STEPS(phaseDuration));
857}
858
859
860void
861TrafficLight::setProgramLogic(const std::string& tlsID, const TraCILogic& logic) {
863 // make sure index and phaseNo are consistent
864 if (logic.currentPhaseIndex >= (int)logic.phases.size()) {
865 throw TraCIException("set program: parameter index must be less than parameter phase number.");
866 }
867 std::vector<MSPhaseDefinition*> phases;
868 for (const std::shared_ptr<libsumo::TraCIPhase>& phase : logic.phases) {
869 MSPhaseDefinition* sumoPhase = new MSPhaseDefinition(TIME2STEPS(phase->duration), phase->state, phase->name);
870 sumoPhase->minDuration = TIME2STEPS(phase->minDur);
871 sumoPhase->maxDuration = TIME2STEPS(phase->maxDur);
872 sumoPhase->nextPhases = phase->next;
873 phases.push_back(sumoPhase);
874 }
875 if (vars.getLogic(logic.programID) == nullptr) {
878 int step = logic.currentPhaseIndex;
879 const std::string basePath = "";
880 MSTrafficLightLogic* tlLogic = nullptr;
881 SUMOTime nextSwitch = now + phases[step]->duration;
882 if (logic.offset != 0. && (TrafficLightType)logic.type == TrafficLightType::STATIC) {
883 // apply the offset in the same way as NLJunctionControlBuilder::closeTrafficLightLogic
884 // when loading a program from a network file: a positive offset delays all phases,
885 // a negative offset advances them. The resulting position within the cycle determines
886 // both the current phase and the time of the next switch. This is only done
887 // for static programs; the semantics of offset are undefined for variable-length phases.
888 SUMOTime cycleTime = 0;
889 for (const MSPhaseDefinition* phase : phases) {
890 cycleTime += phase->duration;
891 }
892 if (cycleTime > 0) {
893 const SUMOTime offsetSteps = TIME2STEPS(logic.offset);
894 SUMOTime positionInCycle = (offsetSteps > 0
895 ? (cycleTime - (offsetSteps % cycleTime)) % cycleTime
896 : ((-offsetSteps) % cycleTime));
897 step = 0;
898 while (positionInCycle >= phases[step]->duration) {
899 positionInCycle -= phases[step]->duration;
900 step++;
901 }
902 nextSwitch = now + phases[step]->duration - positionInCycle;
903 }
904 }
905 switch ((TrafficLightType)logic.type) {
907 tlLogic = new MSActuatedTrafficLightLogic(tlc,
908 tlsID, logic.programID, TIME2STEPS(logic.offset),
909 phases, step, nextSwitch,
910 logic.subParameter, basePath);
911 break;
913 tlLogic = new NEMALogic(tlc,
914 tlsID, logic.programID, TIME2STEPS(logic.offset),
915 phases, step, nextSwitch,
916 logic.subParameter, basePath);
917 break;
919 tlLogic = new MSDelayBasedTrafficLightLogic(tlc,
920 tlsID, logic.programID, TIME2STEPS(logic.offset),
921 phases, step, nextSwitch,
922 logic.subParameter, basePath);
923 break;
925 tlLogic = new MSSimpleTrafficLightLogic(tlc,
926 tlsID, logic.programID, TIME2STEPS(logic.offset), TrafficLightType::STATIC,
927 phases, step, nextSwitch,
928 logic.subParameter);
929 break;
930 default:
931 throw TraCIException("Unsupported traffic light type '" + toString(logic.type) + "'");
932 }
933 try {
934 if (!vars.addLogic(logic.programID, tlLogic, true, true)) {
935 throw TraCIException("Could not add traffic light logic '" + logic.programID + "'");
936 }
937 } catch (const ProcessError& e) {
938 throw TraCIException(e.what());
939 }
940 // XXX pass GUIDetectorBuilder when running with gui
942 tlLogic->init(db);
944 } else {
945 MSSimpleTrafficLightLogic* tlLogic = static_cast<MSSimpleTrafficLightLogic*>(vars.getLogic(logic.programID));
946 tlLogic->setPhases(phases, logic.currentPhaseIndex);
949 }
950}
951
952
953void
954TrafficLight::setParameter(const std::string& tlsID, const std::string& paramName, const std::string& value) {
956 if (StringUtils::startsWith(paramName, "NEMA.") && tll->getLogicType() != TrafficLightType::NEMA) {
957 throw TraCIException("'" + tlsID + "' is not a NEMA controller");
958 }
959 tll->setParameter(paramName, value);
960}
961
963
964void
965TrafficLight::setNemaSplits(const std::string& tlsID, const std::vector<double>& splits) {
966 setParameter(tlsID, "NEMA.splits", toString(splits));
967}
968
969void
970TrafficLight::setNemaMaxGreens(const std::string& tlsID, const std::vector<double>& maxGreens) {
971 setParameter(tlsID, "NEMA.maxGreens", toString(maxGreens));
972}
973
974void
975TrafficLight::setNemaCycleLength(const std::string& tlsID, double cycleLength) {
976 setParameter(tlsID, "NEMA.cycleLength", toString(cycleLength));
977}
978
979void
980TrafficLight::setNemaOffset(const std::string& tlsID, double offset) {
981 setParameter(tlsID, "NEMA.offset", toString(offset));
982}
983
984
986TrafficLight::buildConstraint(const std::string& tlsID, const std::string& tripId, MSRailSignalConstraint* constraint) {
987 TraCISignalConstraint c;
988 c.tripId = tripId;
990 if (pc == nullptr) {
991 // unsupported constraint
992 c.type = -1;
993 } else {
994 c.signalId = tlsID;
995 c.foeId = pc->myTripId;
996 c.foeSignal = pc->myFoeSignal->getID();
997 c.limit = pc->myLimit;
998 c.type = pc->getType();
999 c.mustWait = !pc->cleared() && pc->isActive();
1000 c.active = pc->isActive();
1001 c.param = constraint->getParametersMap();
1002 }
1003 return c;
1004}
1005
1006
1007std::shared_ptr<VariableWrapper>
1008TrafficLight::makeWrapper() {
1009 return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
1010}
1011
1012
1013bool
1014TrafficLight::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
1015 switch (variable) {
1016 case TRACI_ID_LIST:
1017 return wrapper->wrapStringList(objID, variable, getIDList());
1018 case ID_COUNT:
1019 return wrapper->wrapInt(objID, variable, getIDCount());
1021 return wrapper->wrapString(objID, variable, getRedYellowGreenState(objID));
1023 return wrapper->wrapLogicVector(objID, variable, getAllProgramLogics(objID));
1025 return wrapper->wrapStringList(objID, variable, getControlledLanes(objID));
1027 return wrapper->wrapLinkVectorVector(objID, variable, getControlledLinks(objID));
1028 case TL_CURRENT_PHASE:
1029 return wrapper->wrapInt(objID, variable, getPhase(objID));
1030 case VAR_NAME:
1031 return wrapper->wrapString(objID, variable, getPhaseName(objID));
1032 case TL_CURRENT_PROGRAM:
1033 return wrapper->wrapString(objID, variable, getProgram(objID));
1034 case TL_PHASE_DURATION:
1035 return wrapper->wrapDouble(objID, variable, getPhaseDuration(objID));
1036 case TL_NEXT_SWITCH:
1037 return wrapper->wrapDouble(objID, variable, getNextSwitch(objID));
1038 case TL_SPENT_DURATION:
1039 return wrapper->wrapDouble(objID, variable, getSpentDuration(objID));
1040 case VAR_PERSON_NUMBER:
1041 return wrapper->wrapInt(objID, variable, getServedPersonCount(objID, StoHelp::readTypedInt(*paramData)));
1043 return wrapper->wrapStringList(objID, variable, getBlockingVehicles(objID, StoHelp::readTypedInt(*paramData)));
1044 case TL_RIVAL_VEHICLES:
1045 return wrapper->wrapStringList(objID, variable, getRivalVehicles(objID, StoHelp::readTypedInt(*paramData)));
1047 return wrapper->wrapStringList(objID, variable, getPriorityVehicles(objID, StoHelp::readTypedInt(*paramData)));
1048 case TL_CONSTRAINT:
1049 return wrapper->wrapSignalConstraintVector(objID, variable, getConstraints(objID, StoHelp::readTypedString(*paramData)));
1051 return wrapper->wrapSignalConstraintVector(objID, variable, getConstraintsByFoe(objID, StoHelp::readTypedString(*paramData)));
1053 return wrapper->wrapStringList(objID, variable, getControlledJunctions(objID));
1054 case VAR_PARAMETER:
1055 return wrapper->wrapString(objID, variable, getParameter(objID, StoHelp::readTypedString(*paramData)));
1057 return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, StoHelp::readTypedString(*paramData)));
1058 default:
1059 return false;
1060 }
1061}
1062}
1063
1064
1065/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
std::vector< const MSEdge * > ConstMSEdgeVector
Definition MSEdge.h:75
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:299
#define TL(string)
Definition MsgHandler.h:316
#define STEPS2TIME(x)
Definition SUMOTime.h:58
constexpr SUMOTime TIME2STEPS(double x)
Definition SUMOTime.h:60
const long long int VEHPARS_FORCE_REROUTE
@ LINKSTATE_TL_GREEN_MAJOR
The link has green light, may pass.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:57
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOM)
Definition TraCIDefs.h:77
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition TraCIDefs.h:124
An actuated (adaptive) traffic light logic.
The base class for microscopic and mesoscopic vehicles.
const SUMOVehicleParameter & getParameter() const
Returns the vehicle's parameter (including departure definition)
const MSRouteIterator & getCurrentRouteEdge() const
Returns an iterator pointing to the current edge in this vehicles route.
const std::list< MSStop > & getStops() const
const MSRoute & getRoute() const
Returns the current route.
An actuated traffic light logic based on time delay of approaching vehicles.
A road/street connecting two junctions.
Definition MSEdge.h:78
const std::set< MSTransportable *, ComparatorNumericalIdLess > & getPersons() const
Returns this edge's persons set.
Definition MSEdge.h:205
const std::vector< MSLane * > & getLanes() const
Returns this edge's lanes.
Definition MSEdge.h:169
const MSJunction * getToJunction() const
Definition MSEdge.h:428
SumoXMLNodeType getType() const
return the type of this Junction
Definition MSJunction.h:133
Representation of a lane in the micro simulation.
Definition MSLane.h:84
MSEdge & getEdge() const
Returns the lane's edge.
Definition MSLane.h:790
const std::vector< MSLink * > & getLinkCont() const
returns the container with all links !!!
Definition MSLane.h:750
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:201
MSTLLogicControl & getTLSControl()
Returns the tls logics control.
Definition MSNet.h:476
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition MSNet.h:335
virtual void createTLWrapper(MSTrafficLightLogic *)
creates a wrapper for the given logic (see GUINet)
Definition MSNet.h:612
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition MSNet.h:403
const std::string & getNextEdge() const
return the list of internal edges if this person is walking and the pedestrian model allows it
Definition MSPerson.cpp:262
The definition of a single phase of a tls logic.
SUMOTime maxDuration
The maximum duration of the phase.
SUMOTime minDuration
The minimum duration of the phase.
const std::string & getName() const
const std::string & getState() const
Returns the state within this phase.
SUMOTime duration
The duration of the phase.
std::vector< int > nextPhases
The index of the phase that suceeds this one (or -1)
const MSRailSignal * myFoeSignal
store the foe signal (for TraCI access)
bool cleared() const
whether the constraint has been met
const std::string myTripId
id of the predecessor that must already have passed
const int myLimit
the number of passed vehicles within which tripId must have occurred
A base class for constraints.
ConstraintType getType() const
ConstraintType getSwappedType() const
static MSRailSignalControl & getInstance()
const std::vector< MSRailSignal * > & getSignals() const
A signal for rails.
void addConstraint(const std::string &tripId, MSRailSignalConstraint *constraint)
register constraint for signal switching
const std::map< std::string, std::vector< MSRailSignalConstraint * > > & getConstraints() const
bool removeConstraint(const std::string &tripId, MSRailSignalConstraint *constraint)
remove constraint for signal switching
const ConstMSEdgeVector & getEdges() const
Definition MSRoute.h:128
A fixed traffic light logic.
void setPhases(const Phases &phases, int index)
Replaces the phases and set the phase index.
Storage for all programs of a single tls.
void switchTo(MSTLLogicControl &tlc, const std::string &programID)
void setStateInstantiatingOnline(MSTLLogicControl &tlc, const std::string &state)
std::vector< MSTrafficLightLogic * > getAllLogics() const
MSTrafficLightLogic * getLogic(const std::string &programID) const
bool addLogic(const std::string &programID, MSTrafficLightLogic *logic, bool netWasLoaded, bool isNewDefault=true)
Adds a logic (program). In case of an error the logic gets deleted.
MSTrafficLightLogic * getActive() const
MSTrafficLightLogic * getDefault() const
return the default program (that last used program except TRACI_PROGRAM)
A class that stores and controls tls and switching of their programs.
std::vector< std::string > getAllTLIds() const
The parent class for traffic light logics.
const LinkVectorVector & getLinks() const
Returns the list of lists of all affected links.
std::vector< LaneVector > LaneVectorVector
Definition of a list that holds lists of lanes that do have the same attribute.
virtual const MSPhaseDefinition & getCurrentPhaseDef() const =0
Returns the definition of the current phase.
virtual int getPhaseNumber() const =0
Returns the number of phases.
virtual int getCurrentPhaseIndex() const =0
Returns the current index within the program.
std::vector< MSLane * > LaneVector
Definition of the list of arrival lanes subjected to this tls.
virtual VehicleVector getPriorityVehicles(int linkIndex)
return vehicles that approach the intersection/rail signal and have priority over vehicles that wish ...
virtual void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)=0
Changes the current phase and her duration.
virtual VehicleVector getBlockingVehicles(int linkIndex)
return vehicles that block the intersection/rail signal for vehicles that wish to pass the given link...
virtual const MSPhaseDefinition & getPhase(int givenstep) const =0
Returns the definition of the phase from the given position within the plan.
virtual VehicleVector getRivalVehicles(int linkIndex)
return vehicles that approach the intersection/rail signal and are in conflict with vehicles that wis...
const std::string & getProgramID() const
Returns this tl-logic's id.
TrafficLightType getLogicType() const
Returns the type of the logic.
int getNumLinks() const
return the number of controlled link indices
virtual const Phases & getPhases() const =0
Returns the phases of this tls program.
const LaneVectorVector & getLaneVectors() const
Returns the list of lists of all lanes controlled by this tls.
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
const LinkVector & getLinksAt(int i) const
Returns the list of links that are controlled by the signals at the given position.
std::vector< LinkVector > LinkVectorVector
Definition of a list that holds lists of links that do have the same attribute.
std::vector< MSLink * > LinkVector
Definition of the list of links that are subjected to this tls.
virtual void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
The class responsible for building and deletion of vehicles.
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
std::map< std::string, SUMOVehicle * >::const_iterator constVehIt
Definition of the internal vehicles map iterator.
constVehIt loadedVehBegin() const
Returns the begin of the internal vehicle map.
constVehIt loadedVehEnd() const
Returns the end of the internal vehicle map.
A NEMA (adaptive) traffic light logic based on E2Detector.
Builds detectors for microsim.
const std::string & getID() const
Returns the id.
Definition Named.h:73
void unsetParameter(const std::string &key)
Removes a parameter.
virtual const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
const Parameterised::Map & getParametersMap() const
Returns the inner key/value map.
virtual void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
void updateParameters(const Parameterised::Map &mapArg)
Adds or updates all given parameters from the map.
virtual NumericalID getNumericalID() const =0
return the numerical ID which is only for internal usage
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle's parameter (including departure definition)
virtual const MSEdge * getEdge() const =0
Returns the (normal) route edge the object is currently at.
virtual double getPositionOnLane() const =0
Get the object's position along the lane.
Representation of a vehicle.
Definition SUMOVehicle.h:63
virtual bool hasDeparted() const =0
Returns whether this vehicle has departed.
virtual int getRoutePosition() const =0
return index of edge within route
virtual const MSRoute & getRoute() const =0
Returns the current route.
bool wasSet(long long int what) const
Returns whether the given parameter was set.
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.
C++ TraCI client API implementation.
static MSBaseVehicle * getVehicle(const std::string &id)
Definition Helper.cpp:516
static MSTLLogicControl::TLSLogicVariants & getTLS(const std::string &id)
Definition Helper.cpp:557
static int readTypedInt(tcpip::Storage &ret, const std::string &error="")
static std::string readTypedString(tcpip::Storage &ret, const std::string &error="")
TRACI_CONST int VAR_NAME
TRACI_CONST int TRACI_ID_LIST
TRACI_CONST int TL_BLOCKING_VEHICLES
std::map< std::string, libsumo::SubscriptionResults > ContextSubscriptionResults
Definition TraCIDefs.h:384
TRACI_CONST int TL_PRIORITY_VEHICLES
TRACI_CONST int TL_CONTROLLED_LANES
TRACI_CONST int TL_SPENT_DURATION
TRACI_CONST int TL_COMPLETE_DEFINITION_RYG
TRACI_CONST int VAR_PERSON_NUMBER
TRACI_CONST int TL_CONTROLLED_JUNCTIONS
TRACI_CONST int TL_CONTROLLED_LINKS
std::map< std::string, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition TraCIDefs.h:383
TRACI_CONST int ID_COUNT
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int TL_CONSTRAINT_BYFOE
TRACI_CONST int TL_CONSTRAINT
TRACI_CONST int TL_NEXT_SWITCH
TRACI_CONST int VAR_PARAMETER_WITH_KEY
TRACI_CONST int TL_PHASE_DURATION
TRACI_CONST int TL_CURRENT_PHASE
TRACI_CONST int TL_RED_YELLOW_GREEN_STATE
TRACI_CONST int TL_CURRENT_PROGRAM
TRACI_CONST int TL_RIVAL_VEHICLES
std::string tripId
the tripId or vehicle id of the train that is constrained
Definition TraCIDefs.h:874