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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
14 // The class for deterministic high level traffic light logic
15 /****************************************************************************/
16 
18 
20  MSTLLogicControl& tlcontrol, const std::string& id,
21  const std::string& programID, const Phases& phases, int step,
22  SUMOTime delay, const std::map<std::string, std::string>& parameters) :
23  MSSOTLHiLevelTrafficLightLogic(tlcontrol, id, programID, TLTYPE_HILVL_DETERMINISTIC, phases, step,
24  delay, parameters) {
25 
26  addPolicy(new MSSOTLPlatoonPolicy(new MSSOTLPolicy3DStimulus("PLATOON", parameters), parameters));
27  addPolicy(new MSSOTLPhasePolicy(new MSSOTLPolicy3DStimulus("PHASE", parameters), parameters));
28  addPolicy(new MSSOTLMarchingPolicy(new MSSOTLPolicy3DStimulus("MARCHING", parameters), parameters));
29  addPolicy(new MSSOTLCongestionPolicy(new MSSOTLPolicy3DStimulus("CONGESTION", parameters), parameters));
30 
31 }
32 
34 
35 }
36 
39  //Setting the startup policy
40  choosePolicy(0, 0);
42  "*** Intersection " + getID()
43  + " will run using MSDeterministicHiLevelTrafficLightLogic ***");
44 
45  MSLane* currentLane = nullptr;
46  for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector =
47  myLanes.begin(); laneVector != myLanes.end(); laneVector++) {
48  for (MSTrafficLightLogic::LaneVector::const_iterator lane =
49  laneVector->begin(); lane != laneVector->end(); lane++) {
50  currentLane = (*lane);
51  if (inputLanes.find(currentLane->getID()) == inputLanes.end()) {
52  inputLanes.insert(currentLane->getID());
53  DBG(
54  WRITE_MESSAGE("*** Intersection " + getID() + " inputLanes adding " + currentLane->getID());)
55  }
56  }
57  }
58 
60 
61  for (int i = 0; i < (int)myLinks.size(); i++) {
62  LinkVector oneLink = getLinksAt(i);
63  for (int j = 0; j < (int)oneLink.size(); j++) {
64  currentLane = oneLink[j]->getLane();
65  if (outputLanes.find(currentLane->getID()) == outputLanes.end()) {
66  outputLanes.insert(currentLane->getID());
67  DBG(
68  WRITE_MESSAGE("*** Intersection " + getID() + " outputLanes adding " + currentLane->getID());)
69  }
70  }
71  }
72 
73 }
74 
76 
77  DBG(
78  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());)
79 
80  //Decide the current policy according to pheromone levels. this should be done only at the end of a chain, before selecting the new one
81  if (getCurrentPhaseDef().isCommit()) {
82  decidePolicy();
83  }
84 
85  DBG(
86  std::ostringstream str; str << "tlsID=" << getID() << " currentPolicyname=" + getCurrentPolicy()->getName(); WRITE_MESSAGE(str.str());)
87 
88  //Execute current policy. congestion "policy" must maintain the commit phase, and that must be an all-red one
93 }
94 
96  if (inputLanes.size() == 0) {
97  return 0;
98  }
99  double vSpeedInTot = 0;
100  for (MSLaneID_set::iterator laneIterator = inputLanes.begin();
101  laneIterator != inputLanes.end(); laneIterator++) {
102  std::string laneId = *laneIterator;
103  double maxSpeed = getSensors()->meanVehiclesSpeed(laneId);
104  if (maxSpeed > -1) {
105  vSpeedInTot += (13.89 - maxSpeed) * 10. / 13.89;
106  }
107  DBG(
108  std::ostringstream i_str; i_str << " meanVehiclesSpeed " << maxSpeed << " inputLane " << laneId << " ID " << getID() << " ."; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSDeterministicHiLevelTrafficLightLogic::getMeanSpeedForInputLanes:: in" + i_str.str());)
109  }
110  return vSpeedInTot / inputLanes.size();
111 }
112 
114  if (outputLanes.size() == 0) {
115  return 0;
116  }
117  double vSpeedOutTot = 0;
118  for (MSLaneID_set::iterator laneIterator = outputLanes.begin();
119  laneIterator != outputLanes.end(); laneIterator++) {
120  std::string laneId = *laneIterator;
121  double maxSpeed = getSensors()->meanVehiclesSpeed(laneId);
122  if (maxSpeed > -1) {
123  vSpeedOutTot += (13.89 - maxSpeed) * 10. / 13.89;
124  }
125  DBG(
126  std::ostringstream i_str; i_str << " meanVehiclesSpeed " << maxSpeed << " outputLane " << laneId << " ID " << getID() << " ."; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSDeterministicHiLevelTrafficLightLogic::getMeanSpeedForOutputLanes:: out" + i_str.str());)
127  }
128  return vSpeedOutTot / outputLanes.size();
129 }
130 
132  // Decide if it is the case to check for another plan
133  double mean_vSpeed_in = getMeanSpeedForInputLanes();
134  double mean_vSpeed_out = getMeanSpeedForOutputLanes();
135  MSSOTLPolicy* oldPolicy = getCurrentPolicy();
136  choosePolicy(mean_vSpeed_in, mean_vSpeed_out);
137  MSSOTLPolicy* newPolicy = getCurrentPolicy();
138 
139  if (newPolicy != oldPolicy) {
141  DBG(
142  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() + " .");)
143  } else { //debug purpose only
144  DBG(
145  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() + " .");)
146  }
147 
148 }
149 
151  double mean_vSpeed_in, double mean_vSpeed_out) {
152 
153  int index_maxStimulus = 0;
154  double maxStimulus = -1;
155  // Compute simulus for each policy
156  for (int i = 0; i < (int)getPolicies().size(); i++) {
157  double stimulus = getPolicies()[i]->computeDesirability(mean_vSpeed_in,
158  mean_vSpeed_out);
159  if (stimulus > maxStimulus) {
160  maxStimulus = stimulus;
161  index_maxStimulus = i;
162  }
163  DBG(
164  std::ostringstream so_str; so_str << " policy " << getPolicies()[i]->getName() << " stimulus " << stimulus; WRITE_MESSAGE("MSDeterministicHiLevelTrafficLightLogic::choosePolicy::" + so_str.str());)
165 
166  }
167  activate(getPolicies()[index_maxStimulus]);
168 
169 }
170 
172  DBG(
173  std::ostringstream phero_str; phero_str << "getCurrentPhaseElapsed()=" << time2string(getCurrentPhaseElapsed()) << " isThresholdPassed()=" << isThresholdPassed() << " currentPhase=" << (&getCurrentPhaseDef())->getState() << " countVehicles()=" << countVehicles(getCurrentPhaseDef()); WRITE_MESSAGE("\nMSDeterministicHiLevelTrafficLightLogic::canRelease(): " + phero_str.str());)
177 }
MSSOTLPolicy::decideNextPhase
virtual int decideNextPhase(SUMOTime elapsed, const MSPhaseDefinition *stage, int currentPhaseIndex, int phaseMaxCTS, bool thresholdPassed, bool pushButtonPressed, int vehicleCount)
Definition: MSSOTLPolicy.cpp:120
MSTrafficLightLogic::getLinksAt
const LinkVector & getLinksAt(int i) const
Returns the list of links that are controlled by the signals at the given position.
Definition: MSTrafficLightLogic.h:212
MSTrafficLightLogic::myLinks
LinkVectorVector myLinks
The list of LinkVectors; each vector contains the links that belong to the same link index.
Definition: MSTrafficLightLogic.h:417
MSTrafficLightLogic::myLanes
LaneVectorVector myLanes
The list of LaneVectors; each vector contains the incoming lanes that belong to the same link index.
Definition: MSTrafficLightLogic.h:420
MSDeterministicHiLevelTrafficLightLogic::~MSDeterministicHiLevelTrafficLightLogic
~MSDeterministicHiLevelTrafficLightLogic()
Definition: MSDeterministicHiLevelTrafficLightLogic.cpp:33
MSSOTLPolicy
Class for a low-level policy.
Definition: MSSOTLPolicy.h:64
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
MSSOTLMarchingPolicy
Class for low-level marching policy.
Definition: MSSOTLMarchingPolicy.h:33
MSSOTLTrafficLightLogic::isThresholdPassed
bool isThresholdPassed()
Definition: MSSOTLTrafficLightLogic.cpp:305
MSDeterministicHiLevelTrafficLightLogic::init
void init(NLDetectorBuilder &nb)
Initialises the tls with sensors on incoming and outgoing lanes Sensors are built in the simulation a...
Definition: MSDeterministicHiLevelTrafficLightLogic.cpp:37
DBG
#define DBG(X)
Definition: SwarmDebug.h:26
MSDeterministicHiLevelTrafficLightLogic::decidePolicy
void decidePolicy()
Decide the current policy according to pheromone levels The decision reflects on currentPolicy value.
Definition: MSDeterministicHiLevelTrafficLightLogic.cpp:131
MSPhasedTrafficLightLogic::getCurrentPhaseDef
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
Definition: MSPhasedTrafficLightLogic.cpp:131
MSTrafficLightLogic::Phases
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
Definition: MSTrafficLightLogic.h:61
MsgHandler::inform
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:118
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
MSDeterministicHiLevelTrafficLightLogic::MSDeterministicHiLevelTrafficLightLogic
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.
Definition: MSDeterministicHiLevelTrafficLightLogic.cpp:19
MSSOTLHiLevelTrafficLightLogic
A self-organizing high-level traffic light logic.
Definition: MSSOTLHiLevelTrafficLightLogic.h:44
MSDeterministicHiLevelTrafficLightLogic::inputLanes
MSLaneID_set inputLanes
This pheronome is an indicator of congestion on input lanes. Its levels refer to the average speed of...
Definition: MSDeterministicHiLevelTrafficLightLogic.h:80
MSSOTLTrafficLightLogic::getSensors
MSSOTLSensors * getSensors()
Definition: MSSOTLTrafficLightLogic.h:175
MSSOTLTrafficLightLogic::isPushButtonPressed
bool isPushButtonPressed()
Definition: MSSOTLTrafficLightLogic.cpp:506
MSSOTLSensors::meanVehiclesSpeed
virtual double meanVehiclesSpeed(MSLane *lane)=0
MSSOTLCongestionPolicy
Class for low-level congestion policy.
Definition: MSSOTLCongestionPolicy.h:33
MSDeterministicHiLevelTrafficLightLogic::decideNextPhase
int decideNextPhase()
Definition: MSDeterministicHiLevelTrafficLightLogic.cpp:75
MSDeterministicHiLevelTrafficLightLogic::getMeanSpeedForInputLanes
double getMeanSpeedForInputLanes()
Definition: MSDeterministicHiLevelTrafficLightLogic.cpp:95
MSSOTLPolicy::getName
std::string getName()
Definition: MSSOTLPolicy.h:117
MSSOTLPolicy3DStimulus
Definition: MSSOTLPolicy3DStimulus.h:39
MSPhaseDefinition::getState
const std::string & getState() const
Returns the state within this phase.
Definition: MSPhaseDefinition.h:199
MSNet::getCurrentTimeStep
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:283
MSPhaseDefinition::isCommit
bool isCommit() const
Definition: MSPhaseDefinition.h:292
MSDeterministicHiLevelTrafficLightLogic.h
time2string
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:67
MSSOTLTrafficLightLogic::getPhaseIndexWithMaxCTS
int getPhaseIndexWithMaxCTS()
Definition: MSSOTLTrafficLightLogic.cpp:369
MSSOTLHiLevelTrafficLightLogic::getCurrentPolicy
MSSOTLPolicy * getCurrentPolicy()
Returns the low-level policy currently selected by this high-level tll.
Definition: MSSOTLHiLevelTrafficLightLogic.h:90
MSPhasedTrafficLightLogic::getCurrentPhaseIndex
int getCurrentPhaseIndex() const
Returns the current index within the program.
Definition: MSPhasedTrafficLightLogic.cpp:125
MSDeterministicHiLevelTrafficLightLogic::canRelease
bool canRelease()
Definition: MSDeterministicHiLevelTrafficLightLogic.cpp:171
MSTrafficLightLogic::getLinks
const LinkVectorVector & getLinks() const
Returns the list of lists of all affected links.
Definition: MSTrafficLightLogic.h:203
MSSOTLPhasePolicy
Class for low-level phase policy.
Definition: MSSOTLPhasePolicy.h:33
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
MSSOTLTrafficLightLogic::countVehicles
int countVehicles(MSPhaseDefinition phase)
Definition: MSSOTLTrafficLightLogic.cpp:264
MSDeterministicHiLevelTrafficLightLogic::outputLanes
MSLaneID_set outputLanes
This pheromone is an indicator of congestion on output lanes. Its levels refer to the average speed o...
Definition: MSDeterministicHiLevelTrafficLightLogic.h:88
MSSOTLHiLevelTrafficLightLogic::getPolicies
std::vector< MSSOTLPolicy * > & getPolicies()
Returns the vector of the low-level policies used by this high-level tll.
Definition: MSSOTLHiLevelTrafficLightLogic.h:84
MSSOTLHiLevelTrafficLightLogic::addPolicy
void addPolicy(MSSOTLPolicy *policy)
Definition: MSSOTLHiLevelTrafficLightLogic.cpp:44
MSDeterministicHiLevelTrafficLightLogic::getMeanSpeedForOutputLanes
double getMeanSpeedForOutputLanes()
Definition: MSDeterministicHiLevelTrafficLightLogic.cpp:113
MSSOTLPolicy::canRelease
virtual bool canRelease(SUMOTime elapsed, bool thresholdPassed, bool pushButtonPressed, const MSPhaseDefinition *stage, int vehicleCount)=0
MSDeterministicHiLevelTrafficLightLogic::choosePolicy
void choosePolicy(double mean_vSpeed_in, double mean_vSpeed_out)
Definition: MSDeterministicHiLevelTrafficLightLogic.cpp:150
MSTLLogicControl
A class that stores and controls tls and switching of their programs.
Definition: MSTLLogicControl.h:59
MSSOTLPlatoonPolicy
Class for low-level platoon policy.
Definition: MSSOTLPlatoonPolicy.h:35
TLTYPE_HILVL_DETERMINISTIC
@ TLTYPE_HILVL_DETERMINISTIC
Definition: SUMOXMLDefinitions.h:1209
MSSOTLHiLevelTrafficLightLogic::init
void init(NLDetectorBuilder &nb)
Initialises the tls.
Definition: MSSOTLHiLevelTrafficLightLogic.cpp:48
MSTrafficLightLogic::LinkVectorVector
std::vector< LinkVector > LinkVectorVector
Definition of a list that holds lists of links that do have the same attribute.
Definition: MSTrafficLightLogic.h:67
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:76
MSTrafficLightLogic::LinkVector
std::vector< MSLink * > LinkVector
Definition of the list of links that are subjected to this tls.
Definition: MSTrafficLightLogic.h:64
WRITE_MESSAGE
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:277
NLDetectorBuilder
Builds detectors for microsim.
Definition: NLDetectorBuilder.h:55
MSSOTLTrafficLightLogic::getCurrentPhaseElapsed
SUMOTime getCurrentPhaseElapsed()
Definition: MSSOTLTrafficLightLogic.cpp:358
MSSOTLHiLevelTrafficLightLogic::activate
void activate(MSSOTLPolicy *policy)
Definition: MSSOTLHiLevelTrafficLightLogic.cpp:52
MsgHandler::getMessageInstance
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:55