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 /****************************************************************************/
15 // The class for deterministic high level traffic light logic
16 /****************************************************************************/
17 
19 
21  MSTLLogicControl& tlcontrol, const std::string& id,
22  const std::string& programID, const Phases& phases, int step,
23  SUMOTime delay, const std::map<std::string, std::string>& parameters) :
24  MSSOTLHiLevelTrafficLightLogic(tlcontrol, id, programID, TLTYPE_HILVL_DETERMINISTIC, phases, step,
25  delay, parameters) {
26 
27  addPolicy(new MSSOTLPlatoonPolicy(new MSSOTLPolicy3DStimulus("PLATOON", parameters), parameters));
28  addPolicy(new MSSOTLPhasePolicy(new MSSOTLPolicy3DStimulus("PHASE", parameters), parameters));
29  addPolicy(new MSSOTLMarchingPolicy(new MSSOTLPolicy3DStimulus("MARCHING", parameters), parameters));
30  addPolicy(new MSSOTLCongestionPolicy(new MSSOTLPolicy3DStimulus("CONGESTION", parameters), parameters));
31 
32 }
33 
35 
36 }
37 
40  //Setting the startup policy
41  choosePolicy(0, 0);
43  "*** Intersection " + getID()
44  + " will run using MSDeterministicHiLevelTrafficLightLogic ***");
45 
46  MSLane* currentLane = nullptr;
47  for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector =
48  myLanes.begin(); laneVector != myLanes.end(); laneVector++) {
49  for (MSTrafficLightLogic::LaneVector::const_iterator lane =
50  laneVector->begin(); lane != laneVector->end(); lane++) {
51  currentLane = (*lane);
52  if (inputLanes.find(currentLane->getID()) == inputLanes.end()) {
53  inputLanes.insert(currentLane->getID());
54  DBG(
55  WRITE_MESSAGE("*** Intersection " + getID() + " inputLanes adding " + currentLane->getID());)
56  }
57  }
58  }
59 
61 
62  for (int i = 0; i < (int)myLinks.size(); i++) {
63  LinkVector oneLink = getLinksAt(i);
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 }
MSSOTLPolicy::decideNextPhase
virtual int decideNextPhase(SUMOTime elapsed, const MSPhaseDefinition *stage, int currentPhaseIndex, int phaseMaxCTS, bool thresholdPassed, bool pushButtonPressed, int vehicleCount)
Definition: MSSOTLPolicy.cpp:121
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:210
MSTrafficLightLogic::myLinks
LinkVectorVector myLinks
The list of LinkVectors; each vector contains the links that belong to the same link index.
Definition: MSTrafficLightLogic.h:415
MSTrafficLightLogic::myLanes
LaneVectorVector myLanes
The list of LaneVectors; each vector contains the incoming lanes that belong to the same link index.
Definition: MSTrafficLightLogic.h:418
MSDeterministicHiLevelTrafficLightLogic::~MSDeterministicHiLevelTrafficLightLogic
~MSDeterministicHiLevelTrafficLightLogic()
Definition: MSDeterministicHiLevelTrafficLightLogic.cpp:34
MSSOTLPolicy
Class for a low-level policy.
Definition: MSSOTLPolicy.h:65
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
MSSOTLMarchingPolicy
Class for low-level marching policy.
Definition: MSSOTLMarchingPolicy.h:34
MSSOTLTrafficLightLogic::isThresholdPassed
bool isThresholdPassed()
Definition: MSSOTLTrafficLightLogic.cpp:306
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:38
DBG
#define DBG(X)
Definition: SwarmDebug.h:27
MSDeterministicHiLevelTrafficLightLogic::decidePolicy
void decidePolicy()
Decide the current policy according to pheromone levels The decision reflects on currentPolicy value.
Definition: MSDeterministicHiLevelTrafficLightLogic.cpp:132
MSPhasedTrafficLightLogic::getCurrentPhaseDef
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
Definition: MSPhasedTrafficLightLogic.cpp:132
MSTrafficLightLogic::Phases
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
Definition: MSTrafficLightLogic.h:62
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:35
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:20
MSSOTLHiLevelTrafficLightLogic
A self-organizing high-level traffic light logic.
Definition: MSSOTLHiLevelTrafficLightLogic.h:45
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:81
MSSOTLTrafficLightLogic::getSensors
MSSOTLSensors * getSensors()
Definition: MSSOTLTrafficLightLogic.h:176
MSSOTLTrafficLightLogic::isPushButtonPressed
bool isPushButtonPressed()
Definition: MSSOTLTrafficLightLogic.cpp:507
MSSOTLSensors::meanVehiclesSpeed
virtual double meanVehiclesSpeed(MSLane *lane)=0
MSSOTLCongestionPolicy
Class for low-level congestion policy.
Definition: MSSOTLCongestionPolicy.h:34
MSDeterministicHiLevelTrafficLightLogic::decideNextPhase
int decideNextPhase()
Definition: MSDeterministicHiLevelTrafficLightLogic.cpp:76
MSDeterministicHiLevelTrafficLightLogic::getMeanSpeedForInputLanes
double getMeanSpeedForInputLanes()
Definition: MSDeterministicHiLevelTrafficLightLogic.cpp:96
MSSOTLPolicy::getName
std::string getName()
Definition: MSSOTLPolicy.h:118
MSSOTLPolicy3DStimulus
Definition: MSSOTLPolicy3DStimulus.h:40
MSPhaseDefinition::getState
const std::string & getState() const
Returns the state within this phase.
Definition: MSPhaseDefinition.h:200
MSNet::getCurrentTimeStep
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:284
MSPhaseDefinition::isCommit
bool isCommit() const
Definition: MSPhaseDefinition.h:293
MSDeterministicHiLevelTrafficLightLogic.h
time2string
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:65
MSSOTLTrafficLightLogic::getPhaseIndexWithMaxCTS
int getPhaseIndexWithMaxCTS()
Definition: MSSOTLTrafficLightLogic.cpp:370
MSSOTLHiLevelTrafficLightLogic::getCurrentPolicy
MSSOTLPolicy * getCurrentPolicy()
Returns the low-level policy currently selected by this high-level tll.
Definition: MSSOTLHiLevelTrafficLightLogic.h:91
MSPhasedTrafficLightLogic::getCurrentPhaseIndex
int getCurrentPhaseIndex() const
Returns the current index within the program.
Definition: MSPhasedTrafficLightLogic.cpp:126
MSDeterministicHiLevelTrafficLightLogic::canRelease
bool canRelease()
Definition: MSDeterministicHiLevelTrafficLightLogic.cpp:172
MSTrafficLightLogic::getLinks
const LinkVectorVector & getLinks() const
Returns the list of lists of all affected links.
Definition: MSTrafficLightLogic.h:201
MSSOTLPhasePolicy
Class for low-level phase policy.
Definition: MSSOTLPhasePolicy.h:34
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:168
MSSOTLTrafficLightLogic::countVehicles
int countVehicles(MSPhaseDefinition phase)
Definition: MSSOTLTrafficLightLogic.cpp:265
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:89
MSSOTLHiLevelTrafficLightLogic::getPolicies
std::vector< MSSOTLPolicy * > & getPolicies()
Returns the vector of the low-level policies used by this high-level tll.
Definition: MSSOTLHiLevelTrafficLightLogic.h:85
MSSOTLHiLevelTrafficLightLogic::addPolicy
void addPolicy(MSSOTLPolicy *policy)
Definition: MSSOTLHiLevelTrafficLightLogic.cpp:45
MSDeterministicHiLevelTrafficLightLogic::getMeanSpeedForOutputLanes
double getMeanSpeedForOutputLanes()
Definition: MSDeterministicHiLevelTrafficLightLogic.cpp:114
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:151
MSTLLogicControl
A class that stores and controls tls and switching of their programs.
Definition: MSTLLogicControl.h:60
MSSOTLPlatoonPolicy
Class for low-level platoon policy.
Definition: MSSOTLPlatoonPolicy.h:36
TLTYPE_HILVL_DETERMINISTIC
Definition: SUMOXMLDefinitions.h:1204
MSSOTLHiLevelTrafficLightLogic::init
void init(NLDetectorBuilder &nb)
Initialises the tls.
Definition: MSSOTLHiLevelTrafficLightLogic.cpp:49
MSTrafficLightLogic::LinkVectorVector
std::vector< LinkVector > LinkVectorVector
Definition of a list that holds lists of links that do have the same attribute.
Definition: MSTrafficLightLogic.h:68
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:77
MSTrafficLightLogic::LinkVector
std::vector< MSLink * > LinkVector
Definition of the list of links that are subjected to this tls.
Definition: MSTrafficLightLogic.h:65
WRITE_MESSAGE
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:240
NLDetectorBuilder
Builds detectors for microsim.
Definition: NLDetectorBuilder.h:56
MSSOTLTrafficLightLogic::getCurrentPhaseElapsed
SUMOTime getCurrentPhaseElapsed()
Definition: MSSOTLTrafficLightLogic.cpp:359
MSSOTLHiLevelTrafficLightLogic::activate
void activate(MSSOTLPolicy *policy)
Definition: MSSOTLHiLevelTrafficLightLogic.cpp:53
MsgHandler::getMessageInstance
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:59