58 std::vector<std::string>
59 TrafficLight::getIDList() {
65 TrafficLight::getIDCount() {
66 return (
int)getIDList().size();
71 TrafficLight::getRedYellowGreenState(
const std::string& tlsID) {
76 std::vector<TraCILogic>
77 TrafficLight::getAllProgramLogics(
const std::string& tlsID) {
78 std::vector<TraCILogic> result;
81 TraCILogic l(logic->getProgramID(), (
int)logic->getLogicType(), logic->getCurrentPhaseIndex());
82 l.subParameter = logic->getParametersMap();
84 l.phases.emplace_back(
new TraCIPhase(
STEPS2TIME(phase->duration), phase->getState(),
86 phase->getNextPhases(), phase->getName()));
88 result.emplace_back(l);
94 std::vector<std::string>
95 TrafficLight::getControlledJunctions(
const std::string& tlsID) {
96 std::set<std::string> junctionIDs;
99 for (
const MSLink* l : llinks) {
100 junctionIDs.insert(l->getJunction()->getID());
103 return std::vector<std::string>(junctionIDs.begin(), junctionIDs.end());
107 std::vector<std::string>
108 TrafficLight::getControlledLanes(
const std::string& tlsID) {
109 std::vector<std::string> laneIDs;
112 for (
const MSLane* l : llanes) {
113 laneIDs.push_back(l->getID());
120 std::vector<std::vector<TraCILink> >
121 TrafficLight::getControlledLinks(
const std::string& tlsID) {
122 std::vector<std::vector<TraCILink> > result;
125 for (
int i = 0; i < (int)lanes.size(); ++i) {
126 std::vector<TraCILink> subList;
130 for (
int j = 0; j < (int)llanes.size(); ++j) {
136 subList.emplace_back(TraCILink(llanes[j]->getID(), via, to));
138 result.emplace_back(subList);
145 TrafficLight::getProgram(
const std::string& tlsID) {
151 TrafficLight::getPhase(
const std::string& tlsID) {
157 TrafficLight::getPhaseName(
const std::string& tlsID) {
163 TrafficLight::getPhaseDuration(
const std::string& tlsID) {
169 TrafficLight::getNextSwitch(
const std::string& tlsID) {
174 TrafficLight::getServedPersonCount(
const std::string& tlsID,
int index) {
176 if (index < 0 || active->getPhaseNumber() <= index) {
177 throw TraCIException(
"The phase index " +
toString(index) +
" is not in the allowed range [0,"
183 const std::string& state = active->
getPhases()[index]->getState();
184 for (
int i = 0; i < (int)state.size(); i++) {
186 if (link->getLane()->getEdge().isCrossing()) {
200 }
else if (link->getLaneBefore()->getEdge().isCrossing()) {
203 if (
static_cast<MSPerson*
>(person)->
getNextEdge() == link->getLaneBefore()->getEdge().getID()) {
213 std::vector<std::string>
214 TrafficLight::getBlockingVehicles(
const std::string& tlsID,
int linkIndex) {
215 std::vector<std::string> result;
218 if (linkIndex < 0 || linkIndex >= active->
getNumLinks()) {
219 throw TraCIException(
"The link index " +
toString(linkIndex) +
" is not in the allowed range [0,"
223 result.push_back(veh->getID());
228 std::vector<std::string>
229 TrafficLight::getRivalVehicles(
const std::string& tlsID,
int linkIndex) {
230 std::vector<std::string> result;
232 if (linkIndex < 0 || linkIndex >= active->
getNumLinks()) {
233 throw TraCIException(
"The link index " +
toString(linkIndex) +
" is not in the allowed range [0,"
237 result.push_back(veh->getID());
242 std::vector<std::string>
243 TrafficLight::getPriorityVehicles(
const std::string& tlsID,
int linkIndex) {
244 std::vector<std::string> result;
246 if (linkIndex < 0 || linkIndex >= active->
getNumLinks()) {
247 throw TraCIException(
"The link index " +
toString(linkIndex) +
" is not in the allowed range [0,"
251 result.push_back(veh->getID());
256 std::vector<TraCISignalConstraint>
257 TrafficLight::getConstraints(
const std::string& tlsID,
const std::string& tripId) {
258 std::vector<TraCISignalConstraint> result;
262 throw TraCIException(
"'" + tlsID +
"' is not a rail signal");
265 if (tripId !=
"" && tripId != item.first) {
269 result.push_back(buildConstraint(tlsID, item.first, c,
false));
273 if (tripId !=
"" && tripId != item.first) {
277 result.push_back(buildConstraint(tlsID, item.first, c,
true));
283 std::vector<TraCISignalConstraint>
284 TrafficLight::getConstraintsByFoe(
const std::string& foeSignal,
const std::string& foeId) {
287 std::vector<TraCISignalConstraint> result;
288 for (
const std::string& tlsID : getIDList()) {
296 && (foeId ==
"" || pc->
myTripId == foeId)) {
297 result.push_back(buildConstraint(s->
getID(), item.first, pc,
false));
305 && (foeId ==
"" || pc->
myTripId == foeId)) {
306 result.push_back(buildConstraint(s->
getID(), item.first, pc,
true));
315 std::vector<TraCISignalConstraint>
316 TrafficLight::swapConstraints(
const std::string& tlsID,
const std::string& tripId,
const std::string& foeSignal,
const std::string& foeId) {
317 #ifdef DEBUG_CONSTRAINT_DEADLOCK
318 std::cout <<
"swapConstraints tlsId=" << tlsID <<
" tripId=" << tripId <<
" foeSignal=" << foeSignal <<
" foeId=" << foeId <<
"\n";
325 throw TraCIException(
"'" + tlsID +
"' is not a rail signal");
328 throw TraCIException(
"'" + foeSignal +
"' is not a rail signal");
332 if (tripId == item.first) {
347 return findConstraintsDeadLocks(foeId, tripId, foeSignal, tlsID);
349 throw TraCIException(
"Rail signal '" + tlsID +
"' does not have a contraint for tripId '" + tripId +
"' with foeSignal '" + foeSignal +
"' and foeId '" + foeId +
"'");
354 TrafficLight::removeConstraints(
const std::string& tlsID,
const std::string& tripId,
const std::string& foeSignal,
const std::string& foeId) {
357 for (
const std::string& tlsCand : getIDList()) {
358 if (tlsID ==
"" || tlsCand == tlsID) {
363 for (
auto item : cands) {
364 if (tripId ==
"" || item.first == tripId) {
368 && (foeId ==
"" || pc->
myTripId == foeId)
381 std::vector<TraCISignalConstraint>
382 TrafficLight::findConstraintsDeadLocks(
const std::string& foeId,
const std::string& tripId,
const std::string& foeSignal,
const std::string& tlsID) {
383 std::vector<TraCISignalConstraint> result;
389 std::map<std::string, TraCISignalConstraint> constraintsOnTripId;
390 std::map<std::string, TraCISignalConstraint> constrainedByFoeId;
391 std::set<std::string> foeId2Cands1;
392 std::set<std::string> foeId2Cands2;
394 for (
auto item : s->getConstraints()) {
397 if (pc !=
nullptr && !pc->
cleared()) {
398 if (item.first == tripId) {
401 constraintsOnTripId[pc->
myTripId] = buildConstraint(s->getID(), item.first, pc,
false);
405 constrainedByFoeId[item.first] = buildConstraint(s->getID(), item.first, pc,
false);
406 foeId2Cands2.insert(item.first);
412 if (foeId2Cands1.size() > 0) {
420 if (foe !=
nullptr) {
423 for (
const std::string& foeId2 : foeId2Cands1) {
426 if (foe2 !=
nullptr) {
428 const TraCISignalConstraint& c = constraintsOnTripId[foeId2];
429 bool foeAhead =
false;
431 const MSEdge* e = foe2Route[i];
440 #ifdef DEBUG_CONSTRAINT_DEADLOCK
441 std::cout <<
"findConstraintsDeadLocks foeId=" << foeId <<
" tripId=" << tripId <<
" foeSignal=" << foeSignal <<
"\n";
445 <<
" " << constraintsOnTripId[foeId2].getString() <<
"\n";
456 TraCISignalConstraint nc;
459 nc.signalId = c.foeSignal;
460 nc.foeSignal = c.signalId;
464 result.push_back(nc);
466 std::vector<TraCISignalConstraint> result2 = swapConstraints(c.signalId, c.tripId, c.foeSignal, c.foeId);
467 result.insert(result.end(), result2.begin(), result2.end());
470 const std::vector<TraCISignalConstraint>& result4 = findConstraintsDeadLocks(foeId, tripId, foeSignal, c.signalId);
471 result.insert(result.end(), result4.begin(), result4.end());
479 if (foeId2Cands2.size() > 0) {
486 std::set<const MSEdge*> egoToSignal;
490 const MSEdge* e = egoRoute[i];
491 egoToSignal.insert(e);
497 for (
const std::string& foeId2 : foeId2Cands2) {
501 if (foe2 !=
nullptr) {
502 if (egoToSignal.count(foe2->
getEdge()) != 0
504 const TraCISignalConstraint& c = constrainedByFoeId[foeId2];
505 #ifdef DEBUG_CONSTRAINT_DEADLOCK
506 std::cout <<
"findConstraintsDeadLocks foeId=" << foeId <<
" tripId=" << tripId <<
" foeSignal=" << foeSignal <<
"\n";
508 <<
" " << c.getString() <<
"\n";
511 TraCISignalConstraint nc;
514 nc.signalId = c.foeSignal;
515 nc.foeSignal = c.signalId;
519 result.push_back(nc);
521 std::vector<TraCISignalConstraint> result2 = swapConstraints(c.signalId, c.tripId, c.foeSignal, c.foeId);
522 result.insert(result.end(), result2.begin(), result2.end());
525 const std::vector<TraCISignalConstraint>& result4 = findConstraintsDeadLocks(c.foeId, c.tripId, c.foeSignal, c.signalId);
526 result.insert(result.end(), result4.begin(), result4.end());
531 }
else if (ego !=
nullptr) {
532 WRITE_WARNING(
"Cannot check for all deadlocks on swapConstraints because the route for vehicle '" + ego->
getID() +
"' is not computed yet");
537 std::vector<std::string> foeIds2;
538 std::set_intersection(
539 foeId2Cands1.begin(), foeId2Cands1.end(),
540 foeId2Cands2.begin(), foeId2Cands2.end(),
541 std::back_inserter(foeIds2));
542 #ifdef DEBUG_CONSTRAINT_DEADLOCK
543 std::cout <<
"findConstraintsDeadLocks foeId=" << foeId <<
" tripId=" << tripId <<
" foeSignal=" << foeSignal <<
"\n";
544 for (
const std::string& foeId2 : foeIds2) {
545 std::cout <<
" deadlockId=" << foeId2 <<
" " << constraintsOnTripId[foeId2].getString() <<
" " << constrainedByFoeId[foeId2].getString() <<
"\n";
548 if (foeIds2.size() > 0) {
549 const TraCISignalConstraint& c = constrainedByFoeId[foeIds2.front()];
550 TraCISignalConstraint nc;
553 nc.signalId = c.foeSignal;
554 nc.foeSignal = c.signalId;
558 result.push_back(nc);
560 const std::vector<TraCISignalConstraint>& result2 = swapConstraints(c.signalId, c.tripId, c.foeSignal, c.foeId);
561 result.insert(result.end(), result2.begin(), result2.end());
562 if (foeIds2.size() > 1) {
564 const std::vector<TraCISignalConstraint>& result3 = findConstraintsDeadLocks(foeId, tripId, foeSignal, c.signalId);
565 result.insert(result.end(), result3.begin(), result3.end());
573 TrafficLight::getVehicleByTripId(
const std::string tripOrVehID) {
586 TrafficLight::getParameter(
const std::string& tlsID,
const std::string& paramName) {
589 throw TraCIException(
"'" + tlsID +
"' is not a NEMA controller");
599 TrafficLight::setRedYellowGreenState(
const std::string& tlsID,
const std::string& state) {
605 TrafficLight::setPhase(
const std::string& tlsID,
const int index) {
607 if (index < 0 || active->getPhaseNumber() <= index) {
608 throw TraCIException(
"The phase index " +
toString(index) +
" is not in the allowed range [0,"
617 TrafficLight::setPhaseName(
const std::string& tlsID,
const std::string& name) {
624 TrafficLight::setProgram(
const std::string& tlsID,
const std::string& programID) {
628 throw TraCIException(e.what());
634 TrafficLight::setPhaseDuration(
const std::string& tlsID,
const double phaseDuration) {
642 TrafficLight::setProgramLogic(
const std::string& tlsID,
const TraCILogic& logic) {
645 if (logic.currentPhaseIndex >= (
int)logic.phases.size()) {
646 throw TraCIException(
"set program: parameter index must be less than parameter phase number.");
648 std::vector<MSPhaseDefinition*> phases;
649 for (
const std::shared_ptr<libsumo::TraCIPhase>& phase : logic.phases) {
654 phases.push_back(sumoPhase);
656 if (vars.
getLogic(logic.programID) ==
nullptr) {
658 int step = logic.currentPhaseIndex;
659 const std::string basePath =
"";
665 tlsID, logic.programID, 0,
666 phases, step, nextSwitch,
667 logic.subParameter, basePath);
671 tlsID, logic.programID, 0,
672 phases, step, nextSwitch,
673 logic.subParameter, basePath);
677 tlsID, logic.programID, 0,
678 phases, step, nextSwitch,
679 logic.subParameter, basePath);
684 phases, step, nextSwitch,
688 throw TraCIException(
"Unsupported traffic light type '" +
toString(logic.type) +
"'");
690 vars.
addLogic(logic.programID, tlLogic,
true,
true);
702 TrafficLight::setParameter(
const std::string& tlsID,
const std::string& paramName,
const std::string& value) {
705 throw TraCIException(
"'" + tlsID +
"' is not a NEMA controller");
713 TrafficLight::setNemaSplits(
const std::string& tlsID,
const std::vector<double>& splits) {
714 setParameter(tlsID,
"NEMA.splits",
toString(splits));
718 TrafficLight::setNemaMaxGreens(
const std::string& tlsID,
const std::vector<double>& maxGreens) {
719 setParameter(tlsID,
"NEMA.maxGreens",
toString(maxGreens));
723 TrafficLight::setNemaCycleLength(
const std::string& tlsID,
double cycleLength) {
724 setParameter(tlsID,
"NEMA.cycleLength",
toString(cycleLength));
728 TrafficLight::setNemaOffset(
const std::string& tlsID,
double offset) {
729 setParameter(tlsID,
"NEMA.offset",
toString(offset));
734 TrafficLight::buildConstraint(
const std::string& tlsID,
const std::string& tripId,
MSRailSignalConstraint* constraint,
bool insertionConstraint) {
735 TraCISignalConstraint c;
746 c.type = insertionConstraint ? 1 : 0;
753 std::shared_ptr<VariableWrapper>
754 TrafficLight::makeWrapper() {
755 return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
760 TrafficLight::handleVariable(
const std::string& objID,
const int variable, VariableWrapper* wrapper,
tcpip::Storage* paramData) {
763 return wrapper->wrapStringList(objID, variable, getIDList());
765 return wrapper->wrapInt(objID, variable, getIDCount());
767 return wrapper->wrapString(objID, variable, getRedYellowGreenState(objID));
769 return wrapper->wrapStringList(objID, variable, getControlledLanes(objID));
771 return wrapper->wrapInt(objID, variable, getPhase(objID));
773 return wrapper->wrapString(objID, variable, getPhaseName(objID));
775 return wrapper->wrapString(objID, variable, getProgram(objID));
777 return wrapper->wrapDouble(objID, variable, getPhaseDuration(objID));
779 return wrapper->wrapDouble(objID, variable, getNextSwitch(objID));
781 return wrapper->wrapStringList(objID, variable, getControlledJunctions(objID));
784 return wrapper->wrapString(objID, variable, getParameter(objID, paramData->
readString()));
787 return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->
readString()));
std::vector< const MSEdge * > ConstMSEdgeVector
#define WRITE_WARNING(msg)
const int VEHPARS_FORCE_REROUTE
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
An actuated (adaptive) traffic light logic.
An actuated traffic light logic based on time delay of approaching vehicles.
A road/street connecting two junctions.
const std::set< MSTransportable * > & getPersons() const
Returns this edge's persons set.
const MSJunction * getToJunction() const
Representation of a lane in the micro simulation.
const std::vector< MSLink * > & getLinkCont() const
returns the container with all links !!!
MSEdge & getEdge() const
Returns the lane's edge.
MSLane * getViaLane() const
Returns the following inner lane.
MSLane * getLane() const
Returns the connected lane.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
MSTLLogicControl & getTLSControl()
Returns the tls logics control.
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
virtual void createTLWrapper(MSTrafficLightLogic *)
creates a wrapper for the given logic (see GUINet)
const std::string & getNextEdge() const
return the list of internal edges if this person is walking and the pedestrian model allows it
The definition of a single phase of a tls logic.
const std::string & getState() const
Returns the state within this phase.
SUMOTime maxDuration
The maximum duration of the phase.
SUMOTime minDuration
The minimum duration of the phase.
SUMOTime duration
The duration of the phase.
const std::string & getName() const
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 occured
A base class for constraints.
static MSRailSignalControl & getInstance()
void addConstraint(const std::string &tripId, MSRailSignalConstraint *constraint)
register contraint for signal switching
const std::map< std::string, std::vector< MSRailSignalConstraint * > > & getInsertionConstraints() const
bool removeConstraint(const std::string &tripId, MSRailSignalConstraint *constraint)
remove contraint for signal switching
const std::map< std::string, std::vector< MSRailSignalConstraint * > > & getConstraints() const
const ConstMSEdgeVector & getEdges() const
A fixed traffic light logic.
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)
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 LinkVector & getLinksAt(int i) const
Returns the list of links that are controlled by the signals at the given position.
virtual const MSPhaseDefinition & getCurrentPhaseDef() const =0
Returns the definition of the current phase.
std::vector< LaneVector > LaneVectorVector
Definition of a list that holds lists of lanes that do have the same attribute.
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 const MSPhaseDefinition & getPhase(int givenstep) const =0
Returns the definition of the phase from the given position within the plan.
const LaneVectorVector & getLaneVectors() const
Returns the list of lists of all lanes controlled by 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.
const LinkVectorVector & getLinks() const
Returns the list of lists of all affected links.
virtual VehicleVector getBlockingVehicles(int linkIndex)
return vehicles that block the intersection/rail signal for vehicles that wish to pass the given link...
virtual VehicleVector getRivalVehicles(int linkIndex)
return vehicles that approach the intersection/rail signal and are in conflict with vehicles that wis...
TrafficLightType getLogicType() const
Returns the type of the logic.
int getNumLinks() const
return the number of controlled link indices
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 const Phases & getPhases() const =0
Returns the phases of this tls program.
virtual void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
const std::string & getProgramID() const
Returns this tl-logic's id.
const MSEdge * getEdge() const
Returns the current edge.
const MSLane * getLane() const
Returns the current lane (may be nullptr)
The class responsible for building and deletion of vehicles.
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.
virtual const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
virtual void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle's parameter (including departure definition)
virtual const MSEdge * getEdge() const =0
Returns the edge the object is currently at.
virtual double getPositionOnLane() const =0
Get the object's position along the lane.
Representation of a vehicle.
virtual const MSRoute & getRoute() const =0
Returns the current route.
virtual bool hasDeparted() const =0
Returns whether this vehicle has departed.
virtual NumericalID getNumericalID() const =0
return the numerical ID which is only for internal usage
virtual int getRoutePosition() const =0
return index of edge within route
bool wasSet(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 MSTLLogicControl::TLSLogicVariants & getTLS(const std::string &id)
virtual std::string readString()
virtual int readUnsignedByte()
TRACI_CONST int TRACI_ID_LIST
std::map< std::string, libsumo::SubscriptionResults > ContextSubscriptionResults
TRACI_CONST int TL_CONTROLLED_LANES
TRACI_CONST int TL_CONTROLLED_JUNCTIONS
std::map< std::string, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
TRACI_CONST int VAR_PARAMETER
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
std::string tripId
the tripId or vehicle id of the train that is constrained