SUMO - Simulation of Urban MObility
MSDeterministicHiLevelTrafficLightLogic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // The class for deterministic high level traffic light logic
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
10 // Copyright 2001-2009 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
22 
24  MSTLLogicControl& tlcontrol, const std::string& id,
25  const std::string& subid, const Phases& phases, int step,
26  SUMOTime delay, const std::map<std::string, std::string>& parameters) :
27  MSSOTLHiLevelTrafficLightLogic(tlcontrol, id, subid, 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 
42 throw(ProcessError) {
44  //Setting the startup policy
45  choosePolicy(0, 0);
47  "*** Intersection " + getID()
48  + " will run using MSDeterministicHiLevelTrafficLightLogic ***");
49 
50  MSLane* currentLane = NULL;
51  for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector =
52  myLanes.begin(); laneVector != myLanes.end(); laneVector++) {
53  for (MSTrafficLightLogic::LaneVector::const_iterator lane =
54  laneVector->begin(); lane != laneVector->end(); lane++) {
55  currentLane = (*lane);
56  if (inputLanes.find(currentLane->getID()) == inputLanes.end()) {
57  inputLanes.insert(currentLane->getID());
58  DBG(
59  WRITE_MESSAGE("*** Intersection " + getID() + " inputLanes adding " + currentLane->getID());)
60  }
61  }
62  }
63 
65 
66  for (int i = 0; i < (int)myLinks.size(); i++) {
67  LinkVector oneLink = getLinksAt(i);
68  for (int j = 0; j < (int)oneLink.size(); j++) {
69  currentLane = oneLink[j]->getLane();
70  if (outputLanes.find(currentLane->getID()) == outputLanes.end()) {
71  outputLanes.insert(currentLane->getID());
72  DBG(
73  WRITE_MESSAGE("*** Intersection " + getID() + " outputLanes adding " + currentLane->getID());)
74  }
75  }
76  }
77 
78 }
79 
81 
82  DBG(
83  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());)
84 
85  //Decide the current policy according to pheromone levels. this should be done only at the end of a chain, before selecting the new one
86  if (getCurrentPhaseDef().isCommit()) {
87  decidePolicy();
88  }
89 
90  DBG(
91  std::ostringstream str; str << "tlsID=" << getID() << " currentPolicyname=" + getCurrentPolicy()->getName(); WRITE_MESSAGE(str.str());)
92 
93  //Execute current policy. congestion "policy" must maintain the commit phase, and that must be an all-red one
98 }
99 
101  if (inputLanes.size() == 0) {
102  return 0;
103  }
104  SUMOReal vSpeedInTot = 0;
105  for (MSLaneID_set::iterator laneIterator = inputLanes.begin();
106  laneIterator != inputLanes.end(); laneIterator++) {
107  std::string laneId = *laneIterator;
108  SUMOReal maxSpeed = getSensors()->meanVehiclesSpeed(laneId);
109  if (maxSpeed > -1) {
110  vSpeedInTot += (13.89 - maxSpeed) * 10. / 13.89;
111  }
112  DBG(
113  std::ostringstream i_str; i_str << " meanVehiclesSpeed " << maxSpeed << " inputLane " << laneId << " ID " << getID() << " ."; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSDeterministicHiLevelTrafficLightLogic::getMeanSpeedForInputLanes:: in" + i_str.str());)
114  }
115  return vSpeedInTot / inputLanes.size();
116 }
117 
119  if (outputLanes.size() == 0) {
120  return 0;
121  }
122  SUMOReal vSpeedOutTot = 0;
123  for (MSLaneID_set::iterator laneIterator = outputLanes.begin();
124  laneIterator != outputLanes.end(); laneIterator++) {
125  std::string laneId = *laneIterator;
126  SUMOReal maxSpeed = getSensors()->meanVehiclesSpeed(laneId);
127  if (maxSpeed > -1) {
128  vSpeedOutTot += (13.89 - maxSpeed) * 10. / 13.89;
129  }
130  DBG(
131  std::ostringstream i_str; i_str << " meanVehiclesSpeed " << maxSpeed << " outputLane " << laneId << " ID " << getID() << " ."; WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSDeterministicHiLevelTrafficLightLogic::getMeanSpeedForOutputLanes:: out" + i_str.str());)
132  }
133  return vSpeedOutTot / outputLanes.size();
134 }
135 
137  // Decide if it is the case to check for another plan
138  SUMOReal mean_vSpeed_in = getMeanSpeedForInputLanes();
139  SUMOReal mean_vSpeed_out = getMeanSpeedForOutputLanes();
140  MSSOTLPolicy* oldPolicy = getCurrentPolicy();
141  choosePolicy(mean_vSpeed_in, mean_vSpeed_out);
142  MSSOTLPolicy* newPolicy = getCurrentPolicy();
143 
144  if (newPolicy != oldPolicy) {
146  DBG(
147  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() + " .");)
148  } else { //debug purpose only
149  DBG(
150  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() + " .");)
151  }
152 
153 }
154 
156  SUMOReal mean_vSpeed_in, SUMOReal mean_vSpeed_out) {
157 
158  int index_maxStimulus = 0;
159  SUMOReal maxStimulus = -1;
160  // Compute simulus for each policy
161  for (int i = 0; i < (int)getPolicies().size(); i++) {
162  SUMOReal stimulus = getPolicies()[i]->computeDesirability(mean_vSpeed_in,
163  mean_vSpeed_out);
164  if (stimulus > maxStimulus) {
165  maxStimulus = stimulus;
166  index_maxStimulus = i;
167  }
168  DBG(
169  std::ostringstream so_str; so_str << " policy " << getPolicies()[i]->getName() << " stimulus " << stimulus; WRITE_MESSAGE("MSDeterministicHiLevelTrafficLightLogic::choosePolicy::" + so_str.str());)
170 
171  }
172  activate(getPolicies()[index_maxStimulus]);
173 
174 }
175 
177  DBG(
178  std::ostringstream phero_str; phero_str << "getCurrentPhaseElapsed()=" << time2string(getCurrentPhaseElapsed()) << " isThresholdPassed()=" << isThresholdPassed() << " currentPhase=" << (&getCurrentPhaseDef())->getState() << " countVehicles()=" << countVehicles(getCurrentPhaseDef()); WRITE_MESSAGE("\nMSDeterministicHiLevelTrafficLightLogic::canRelease(): " + phero_str.str());)
182 }
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.
long long int SUMOTime
Definition: SUMOTime.h:43
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:159
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:66
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:254
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:62
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:30
std::string getName()
Definition: MSSOTLPolicy.h:125
virtual SUMOReal meanVehiclesSpeed(MSLane *lane)=0
std::vector< MSLink * > LinkVector
Definition of the list of links that participate in this tl-light.
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.
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:89
Class for a low-level policy.
Definition: MSSOTLPolicy.h:72
Class for low-level congestion policy.
#define SUMOReal
Definition: config.h:213
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
void choosePolicy(SUMOReal mean_vSpeed_in, SUMOReal mean_vSpeed_out)
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
int countVehicles(MSPhaseDefinition phase)