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