Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSDeterministicHiLevelTrafficLightLogic.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2010-2024 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/****************************************************************************/
18// The class for deterministic high level traffic light logic
19/****************************************************************************/
20
22//#define SWARM_DEBUG
23
24
25// ===========================================================================
26// method definitions
27// ===========================================================================
29 MSTLLogicControl& tlcontrol, const std::string& id,
30 const std::string& programID, const Phases& phases, int step,
31 SUMOTime delay, const Parameterised::Map& parameters) :
32 MSSOTLHiLevelTrafficLightLogic(tlcontrol, id, programID, TrafficLightType::HILVL_DETERMINISTIC, phases, step,
33 delay, parameters) {
34
35 addPolicy(new MSSOTLPlatoonPolicy(new MSSOTLPolicy3DStimulus("PLATOON", parameters), parameters));
36 addPolicy(new MSSOTLPhasePolicy(new MSSOTLPolicy3DStimulus("PHASE", parameters), parameters));
37 addPolicy(new MSSOTLMarchingPolicy(new MSSOTLPolicy3DStimulus("MARCHING", parameters), parameters));
38 addPolicy(new MSSOTLCongestionPolicy(new MSSOTLPolicy3DStimulus("CONGESTION", parameters), parameters));
39
40}
41
45
48 //Setting the startup policy
49 choosePolicy(0, 0);
51 "*** Intersection " + getID()
52 + " will run using MSDeterministicHiLevelTrafficLightLogic ***");
53
54 MSLane* currentLane = nullptr;
55 for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector =
56 myLanes.begin(); laneVector != myLanes.end(); laneVector++) {
57 for (MSTrafficLightLogic::LaneVector::const_iterator lane =
58 laneVector->begin(); lane != laneVector->end(); lane++) {
59 currentLane = (*lane);
60 if (inputLanes.find(currentLane->getID()) == inputLanes.end()) {
61 inputLanes.insert(currentLane->getID());
62#ifdef SWARM_DEBUG
63 WRITE_MESSAGE("*** Intersection " + getID() + " inputLanes adding " + currentLane->getID());
64#endif
65 }
66 }
67 }
68
69 for (const LinkVector& oneLink : getLinks()) {
70 for (int j = 0; j < (int)oneLink.size(); j++) {
71 currentLane = oneLink[j]->getLane();
72 if (outputLanes.find(currentLane->getID()) == outputLanes.end()) {
73 outputLanes.insert(currentLane->getID());
74#ifdef SWARM_DEBUG
75 WRITE_MESSAGE("*** Intersection " + getID() + " outputLanes adding " + currentLane->getID());
76#endif
77 }
78 }
79 }
80
81}
82
84#ifdef SWARM_DEBUG
85 MsgHandler::getMessageInstance()->inform("\n" + time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSDeterministicHiLevelTrafficLightLogic decideNextPhase()");
86 std::ostringstream dnp;
87 dnp << (MSNet::getInstance()->getCurrentTimeStep()) << " MSDeterministicHiLevelTrafficLightLogic::decideNextPhase:: " << "tlsid=" << getID() << " getCurrentPhaseDef().getState()=" << getCurrentPhaseDef().getState() << " is commit?" << getCurrentPhaseDef().isCommit();
89#endif
90
91 //Decide the current policy according to pheromone levels. this should be done only at the end of a chain, before selecting the new one
92 if (getCurrentPhaseDef().isCommit()) {
94 }
95
96#ifdef SWARM_DEBUG
97 std::ostringstream str;
98 str << "tlsID=" << getID() << " currentPolicyname=" + getCurrentPolicy()->getName();
99 WRITE_MESSAGE(str.str());
100#endif
101
102 //Execute current policy. congestion "policy" must maintain the commit phase, and that must be an all-red one
107}
108
110 if (inputLanes.size() == 0) {
111 return 0;
112 }
113 double vSpeedInTot = 0;
114 for (MSLaneID_set::iterator laneIterator = inputLanes.begin();
115 laneIterator != inputLanes.end(); laneIterator++) {
116 std::string laneId = *laneIterator;
117 double maxSpeed = getSensors()->meanVehiclesSpeed(laneId);
118 if (maxSpeed > -1) {
119 vSpeedInTot += (13.89 - maxSpeed) * 10. / 13.89;
120 }
121#ifdef SWARM_DEBUG
122 std::ostringstream i_str;
123 i_str << " meanVehiclesSpeed " << maxSpeed << " inputLane " << laneId << " ID " << getID() << " .";
124 WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSDeterministicHiLevelTrafficLightLogic::getMeanSpeedForInputLanes:: in" + i_str.str());
125#endif
126 }
127 return vSpeedInTot / (double)inputLanes.size();
128}
129
131 if (outputLanes.size() == 0) {
132 return 0;
133 }
134 double vSpeedOutTot = 0;
135 for (MSLaneID_set::iterator laneIterator = outputLanes.begin();
136 laneIterator != outputLanes.end(); laneIterator++) {
137 std::string laneId = *laneIterator;
138 double maxSpeed = getSensors()->meanVehiclesSpeed(laneId);
139 if (maxSpeed > -1) {
140 vSpeedOutTot += (13.89 - maxSpeed) * 10. / 13.89;
141 }
142#ifdef SWARM_DEBUG
143 std::ostringstream i_str;
144 i_str << " meanVehiclesSpeed " << maxSpeed << " outputLane " << laneId << " ID " << getID() << " .";
145 WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSDeterministicHiLevelTrafficLightLogic::getMeanSpeedForOutputLanes:: out" + i_str.str());
146#endif
147 }
148 return vSpeedOutTot / (double)outputLanes.size();
149}
150
152 // Decide if it is the case to check for another plan
153 double mean_vSpeed_in = getMeanSpeedForInputLanes();
154 double mean_vSpeed_out = getMeanSpeedForOutputLanes();
155#ifdef SWARM_DEBUG
156 MSSOTLPolicy* oldPolicy = getCurrentPolicy();
157#endif
158 choosePolicy(mean_vSpeed_in, mean_vSpeed_out);
159
160#ifdef SWARM_DEBUG
161 MSSOTLPolicy* newPolicy = getCurrentPolicy();
162 if (newPolicy != oldPolicy) {
164 std::ostringstream phero_str;
165 phero_str << " (mean_vSpeed_in= " << mean_vSpeed_in << " ,mean_vSpeed_out= " << mean_vSpeed_out << " )";
166 WRITE_MESSAGE("TL " + getID() + " time=" + time2string(step) + " Policy: " + newPolicy->getName() + phero_str.str() + " OldPolicy: " + oldPolicy->getName() + " id " + getID() + " .");
167 } else { //debug purpose only
168 std::ostringstream phero_str;
169 phero_str << " (mean_vSpeed_in= " << mean_vSpeed_in << " ,mean_vSpeed_out= " << mean_vSpeed_out << " )";
171 WRITE_MESSAGE("TL " + getID() + " time=" + time2string(step) + " Policy: Nochanges" + phero_str.str() + " OldPolicy: " + oldPolicy->getName() + " id " + getID() + " .");
172 }
173#endif
174}
175
177 double mean_vSpeed_in, double mean_vSpeed_out) {
178
179 int index_maxStimulus = 0;
180 double maxStimulus = -1;
181 // Compute simulus for each policy
182 for (int i = 0; i < (int)myPolicies.size(); i++) {
183 double stimulus = myPolicies[i]->computeDesirability(mean_vSpeed_in,
184 mean_vSpeed_out);
185 if (stimulus > maxStimulus) {
186 maxStimulus = stimulus;
187 index_maxStimulus = i;
188 }
189#ifdef SWARM_DEBUG
190 std::ostringstream so_str;
191 so_str << " policy " << getPolicies()[i]->getName() << " stimulus " << stimulus;
192 WRITE_MESSAGE("MSDeterministicHiLevelTrafficLightLogic::choosePolicy::" + so_str.str());
193#endif
194 }
195 activate(myPolicies[index_maxStimulus]);
196
197}
198
200#ifdef SWARM_DEBUG
201 std::ostringstream phero_str;
202 phero_str << "getCurrentPhaseElapsed()=" << time2string(getCurrentPhaseElapsed()) << " isThresholdPassed()=" << isThresholdPassed() << " currentPhase=" << (&getCurrentPhaseDef())->getState() << " countVehicles()=" << countVehicles(getCurrentPhaseDef());
203 WRITE_MESSAGE("\nMSDeterministicHiLevelTrafficLightLogic::canRelease(): " + phero_str.str());
204#endif
208}
long long int SUMOTime
Definition GUI.h:36
#define WRITE_MESSAGE(msg)
Definition MsgHandler.h:297
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:69
void choosePolicy(double mean_vSpeed_in, double mean_vSpeed_out)
void init(NLDetectorBuilder &nb)
Initialises the tls with sensors on incoming and outgoing lanes Sensors are built in the simulation a...
MSLaneID_set inputLanes
This pheronome is an indicator of congestion on input lanes. Its levels refer to the average speed of...
void decidePolicy()
Decide the current policy according to pheromone levels The decision reflects on currentPolicy value.
MSLaneID_set outputLanes
This pheromone is an indicator of congestion on output lanes. Its levels refer to the average speed o...
MSDeterministicHiLevelTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const Phases &phases, int step, SUMOTime delay, const Parameterised::Map &parameters)
Constructor without sensors passed.
Representation of a lane in the micro simulation.
Definition MSLane.h:84
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:185
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition MSNet.h:320
const std::string & getState() const
Returns the state within this phase.
Class for low-level congestion policy.
A self-organizing high-level traffic light logic.
void init(NLDetectorBuilder &nb)
Initialises the tls.
Class for low-level marching policy.
Class for low-level phase policy.
Class for low-level platoon policy.
Class for a low-level policy.
virtual bool canRelease(SUMOTime elapsed, bool thresholdPassed, bool pushButtonPressed, const MSPhaseDefinition *stage, int vehicleCount)=0
virtual int decideNextPhase(SUMOTime elapsed, const MSPhaseDefinition *stage, int currentPhaseIndex, int phaseMaxCTS, bool thresholdPassed, bool pushButtonPressed, int vehicleCount)
std::string getName()
virtual double meanVehiclesSpeed(MSLane *lane)=0
int countVehicles(MSPhaseDefinition phase)
int getCurrentPhaseIndex() const override
Returns the current index within the program.
const MSPhaseDefinition & getCurrentPhaseDef() const override
Returns the definition of the current phase.
A class that stores and controls tls and switching of their programs.
const LinkVectorVector & getLinks() const
Returns the list of lists of all affected links.
LaneVectorVector myLanes
The list of LaneVectors; each vector contains the incoming lanes that belong to the same link index.
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
std::vector< MSLink * > LinkVector
Definition of the list of links that are subjected to this tls.
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Builds detectors for microsim.
const std::string & getID() const
Returns the id.
Definition Named.h:74
std::map< std::string, std::string > Map
parameters map