Eclipse SUMO - Simulation of Urban MObility
MSDeterministicHiLevelTrafficLightLogic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2010-2020 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 
24  MSTLLogicControl& tlcontrol, const std::string& id,
25  const std::string& programID, const Phases& phases, int step,
26  SUMOTime delay, const std::map<std::string, std::string>& parameters) :
27  MSSOTLHiLevelTrafficLightLogic(tlcontrol, id, programID, TrafficLightType::HILVL_DETERMINISTIC, phases, step,
28  delay, parameters) {
29 
30  addPolicy(new MSSOTLPlatoonPolicy(new MSSOTLPolicy3DStimulus("PLATOON", parameters), parameters));
31  addPolicy(new MSSOTLPhasePolicy(new MSSOTLPolicy3DStimulus("PHASE", parameters), parameters));
32  addPolicy(new MSSOTLMarchingPolicy(new MSSOTLPolicy3DStimulus("MARCHING", parameters), parameters));
33  addPolicy(new MSSOTLCongestionPolicy(new MSSOTLPolicy3DStimulus("CONGESTION", parameters), parameters));
34 
35 }
36 
38 
39 }
40 
43  //Setting the startup policy
44  choosePolicy(0, 0);
46  "*** Intersection " + getID()
47  + " will run using MSDeterministicHiLevelTrafficLightLogic ***");
48 
49  MSLane* currentLane = nullptr;
50  for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector =
51  myLanes.begin(); laneVector != myLanes.end(); laneVector++) {
52  for (MSTrafficLightLogic::LaneVector::const_iterator lane =
53  laneVector->begin(); lane != laneVector->end(); lane++) {
54  currentLane = (*lane);
55  if (inputLanes.find(currentLane->getID()) == inputLanes.end()) {
56  inputLanes.insert(currentLane->getID());
57  DBG(
58  WRITE_MESSAGE("*** Intersection " + getID() + " inputLanes adding " + currentLane->getID());)
59  }
60  }
61  }
62 
63  for (const LinkVector& oneLink : getLinks()) {
64  for (int j = 0; j < (int)oneLink.size(); j++) {
65  currentLane = oneLink[j]->getLane();
66  if (outputLanes.find(currentLane->getID()) == outputLanes.end()) {
67  outputLanes.insert(currentLane->getID());
68  DBG(
69  WRITE_MESSAGE("*** Intersection " + getID() + " outputLanes adding " + currentLane->getID());)
70  }
71  }
72  }
73 
74 }
75 
77 
78  DBG(
79  MsgHandler::getMessageInstance()->inform("\n" + time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSDeterministicHiLevelTrafficLightLogic decideNextPhase()"); std::ostringstream dnp; dnp << (MSNet::getInstance()->getCurrentTimeStep()) << " MSDeterministicHiLevelTrafficLightLogic::decideNextPhase:: " << "tlsid=" << getID() << " getCurrentPhaseDef().getState()=" << getCurrentPhaseDef().getState() << " is commit?" << getCurrentPhaseDef().isCommit(); MsgHandler::getMessageInstance()->inform(dnp.str());)
80 
81  //Decide the current policy according to pheromone levels. this should be done only at the end of a chain, before selecting the new one
82  if (getCurrentPhaseDef().isCommit()) {
83  decidePolicy();
84  }
85 
86  DBG(
87  std::ostringstream str; str << "tlsID=" << getID() << " currentPolicyname=" + getCurrentPolicy()->getName(); WRITE_MESSAGE(str.str());)
88 
89  //Execute current policy. congestion "policy" must maintain the commit phase, and that must be an all-red one
94 }
95 
97  if (inputLanes.size() == 0) {
98  return 0;
99  }
100  double vSpeedInTot = 0;
101  for (MSLaneID_set::iterator laneIterator = inputLanes.begin();
102  laneIterator != inputLanes.end(); laneIterator++) {
103  std::string laneId = *laneIterator;
104  double maxSpeed = getSensors()->meanVehiclesSpeed(laneId);
105  if (maxSpeed > -1) {
106  vSpeedInTot += (13.89 - maxSpeed) * 10. / 13.89;
107  }
108  DBG(
109  std::ostringstream i_str; i_str << " meanVehiclesSpeed " << maxSpeed << " inputLane " << laneId << " ID " << getID() << " ."; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSDeterministicHiLevelTrafficLightLogic::getMeanSpeedForInputLanes:: in" + i_str.str());)
110  }
111  return vSpeedInTot / inputLanes.size();
112 }
113 
115  if (outputLanes.size() == 0) {
116  return 0;
117  }
118  double vSpeedOutTot = 0;
119  for (MSLaneID_set::iterator laneIterator = outputLanes.begin();
120  laneIterator != outputLanes.end(); laneIterator++) {
121  std::string laneId = *laneIterator;
122  double maxSpeed = getSensors()->meanVehiclesSpeed(laneId);
123  if (maxSpeed > -1) {
124  vSpeedOutTot += (13.89 - maxSpeed) * 10. / 13.89;
125  }
126  DBG(
127  std::ostringstream i_str; i_str << " meanVehiclesSpeed " << maxSpeed << " outputLane " << laneId << " ID " << getID() << " ."; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSDeterministicHiLevelTrafficLightLogic::getMeanSpeedForOutputLanes:: out" + i_str.str());)
128  }
129  return vSpeedOutTot / outputLanes.size();
130 }
131 
133  // Decide if it is the case to check for another plan
134  double mean_vSpeed_in = getMeanSpeedForInputLanes();
135  double mean_vSpeed_out = getMeanSpeedForOutputLanes();
136  MSSOTLPolicy* oldPolicy = getCurrentPolicy();
137  choosePolicy(mean_vSpeed_in, mean_vSpeed_out);
138  MSSOTLPolicy* newPolicy = getCurrentPolicy();
139 
140  if (newPolicy != oldPolicy) {
142  DBG(
143  std::ostringstream phero_str; phero_str << " (mean_vSpeed_in= " << mean_vSpeed_in << " ,mean_vSpeed_out= " << mean_vSpeed_out << " )"; WRITE_MESSAGE("TL " + getID() + " time " + time2string(step) + " Policy: " + newPolicy->getName() + phero_str.str() + " OldPolicy: " + oldPolicy->getName() + " id " + getID() + " .");)
144  } else { //debug purpose only
145  DBG(
146  std::ostringstream phero_str; phero_str << " (mean_vSpeed_in= " << mean_vSpeed_in << " ,mean_vSpeed_out= " << mean_vSpeed_out << " )"; SUMOTime step = MSNet::getInstance()->getCurrentTimeStep(); WRITE_MESSAGE("TL " + getID() + " time " + time2string(step) + " Policy: Nochanges" + phero_str.str() + " OldPolicy: " + oldPolicy->getName() + " id " + getID() + " .");)
147  }
148 
149 }
150 
152  double mean_vSpeed_in, double mean_vSpeed_out) {
153 
154  int index_maxStimulus = 0;
155  double maxStimulus = -1;
156  // Compute simulus for each policy
157  for (int i = 0; i < (int)getPolicies().size(); i++) {
158  double stimulus = getPolicies()[i]->computeDesirability(mean_vSpeed_in,
159  mean_vSpeed_out);
160  if (stimulus > maxStimulus) {
161  maxStimulus = stimulus;
162  index_maxStimulus = i;
163  }
164  DBG(
165  std::ostringstream so_str; so_str << " policy " << getPolicies()[i]->getName() << " stimulus " << stimulus; WRITE_MESSAGE("MSDeterministicHiLevelTrafficLightLogic::choosePolicy::" + so_str.str());)
166 
167  }
168  activate(getPolicies()[index_maxStimulus]);
169 
170 }
171 
173  DBG(
174  std::ostringstream phero_str; phero_str << "getCurrentPhaseElapsed()=" << time2string(getCurrentPhaseElapsed()) << " isThresholdPassed()=" << isThresholdPassed() << " currentPhase=" << (&getCurrentPhaseDef())->getState() << " countVehicles()=" << countVehicles(getCurrentPhaseDef()); WRITE_MESSAGE("\nMSDeterministicHiLevelTrafficLightLogic::canRelease(): " + phero_str.str());)
178 }
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:278
std::string time2string(SUMOTime t)
convert SUMOTime to string
Definition: SUMOTime.cpp:68
long long int SUMOTime
Definition: SUMOTime.h:31
TrafficLightType
#define DBG(X)
Definition: SwarmDebug.h:30
MSDeterministicHiLevelTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const Phases &phases, int step, SUMOTime delay, const std::map< std::string, std::string > &parameters)
Constructor without sensors passed.
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...
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:171
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:313
const std::string & getState() const
Returns the state within this phase.
int getCurrentPhaseIndex() const
Returns the current index within the program.
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
Class for low-level congestion policy.
A self-organizing high-level traffic light logic.
MSSOTLPolicy * getCurrentPolicy()
Returns the low-level policy currently selected by this high-level tll.
std::vector< MSSOTLPolicy * > & getPolicies()
Returns the vector of the low-level policies used by this high-level tll.
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.
Definition: MSSOTLPolicy.h:63
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()
Definition: MSSOTLPolicy.h:116
virtual double meanVehiclesSpeed(MSLane *lane)=0
int countVehicles(MSPhaseDefinition 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
Definition: MsgHandler.cpp:117
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:54
Builds detectors for microsim.
const std::string & getID() const
Returns the id.
Definition: Named.h:73