SUMO - Simulation of Urban MObility
MSTrafficLightLogic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // The parent class for traffic light logics
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2015 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <cassert>
34 #include <string>
35 #include <iostream>
36 #include <map>
37 #include <microsim/MSLink.h>
38 #include <microsim/MSLane.h>
41 #include <microsim/MSNet.h>
42 #include "MSTLLogicControl.h"
43 #include "MSTrafficLightLogic.h"
44 
45 #ifdef CHECK_MEMORY_LEAKS
46 #include <foreign/nvwa/debug_new.h>
47 #endif // CHECK_MEMORY_LEAKS
48 
49 
50 // ===========================================================================
51 // static value definitions
52 // ===========================================================================
54 
55 
56 // ===========================================================================
57 // member method definitions
58 // ===========================================================================
59 /* -------------------------------------------------------------------------
60  * member method definitions
61  * ----------------------------------------------------------------------- */
63  MSTrafficLightLogic* tlLogic, SUMOTime nextSwitch)
64  : myTLControl(tlcontrol), myTLLogic(tlLogic),
65  myAssumedNextSwitch(nextSwitch), myAmValid(true) {}
66 
67 
69 
70 
71 
74  // check whether this command has been descheduled
75  if (!myAmValid) {
76  return 0;
77  }
78  //
79  const bool isActive = myTLControl.isActive(myTLLogic);
80  size_t step1 = myTLLogic->getCurrentPhaseIndex();
81  SUMOTime next = myTLLogic->trySwitch();
82  size_t step2 = myTLLogic->getCurrentPhaseIndex();
83  if (step1 != step2) {
84  if (isActive) {
85  // execute any action connected to this tls
87  // set link priorities
89  // execute switch actions
91  }
92  }
93  myAssumedNextSwitch += next;
94  return next;
95 }
96 
97 
98 void
100  if (tlLogic == myTLLogic) {
101  myAmValid = false;
102  myAssumedNextSwitch = -1;
103  }
104 }
105 
106 
107 /* -------------------------------------------------------------------------
108  * member method definitions
109  * ----------------------------------------------------------------------- */
111  const std::string& programID, SUMOTime delay, const std::map<std::string, std::string>& parameters) :
112  Named(id), Parameterised(parameters),
113  myProgramID(programID),
115  myDefaultCycleTime(0) {
116  mySwitchCommand = new SwitchCommand(tlcontrol, this, delay);
119 }
120 
121 
122 void
124  const Phases& phases = getPhases();
125  if (phases.size() > 1) {
126  bool haveWarnedAboutUnusedStates = false;
127  std::vector<bool> foundGreen(phases.front()->getState().size(), false);
128  for (int i = 0; i < (int)phases.size(); ++i) {
129  // warn about unused stats
130  const int iNext = (i + 1) % phases.size();
131  const std::string& state1 = phases[i]->getState();
132  const std::string& state2 = phases[iNext]->getState();
133  assert(state1.size() == state2.size());
134  if (!haveWarnedAboutUnusedStates && state1.size() > myLanes.size()) {
135  WRITE_WARNING("Unused states in tlLogic '" + getID()
136  + "', program '" + getProgramID() + "' in phase " + toString(i)
137  + " after tl-index " + toString((int)myLanes.size() - 1));
138  haveWarnedAboutUnusedStates = true;
139  }
140  // warn about transitions from green to red without intermediate yellow
141  for (int j = 0; j < (int)MIN3(state1.size(), state2.size(), myLanes.size()); ++j) {
142  if ((LinkState)state2[j] == LINKSTATE_TL_RED
143  && ((LinkState)state1[j] == LINKSTATE_TL_GREEN_MAJOR
144  || (LinkState)state1[j] == LINKSTATE_TL_GREEN_MINOR)) {
145  for (LaneVector::const_iterator it = myLanes[j].begin(); it != myLanes[j].end(); ++it) {
146  if ((*it)->getPermissions() != SVC_PEDESTRIAN) {
147  WRITE_WARNING("Missing yellow phase in tlLogic '" + getID()
148  + "', program '" + getProgramID() + "' for tl-index " + toString(j)
149  + " when switching to phase " + toString(iNext));
150  return; // one warning per program is enough
151  }
152  }
153  }
154  }
155  // warn about links that never get the green light
156  for (int j = 0; j < (int)state1.size(); ++j) {
157  LinkState ls = (LinkState)state1[j];
159  foundGreen[j] = true;
160  }
161  }
162  }
163  for (int j = 0; j < (int)foundGreen.size(); ++j) {
164  if (!foundGreen[j]) {
165  WRITE_WARNING("Missing green phase in tlLogic '" + getID()
166  + "', program '" + getProgramID() + "' for tl-index " + toString(j));
167  break;
168  }
169  }
170 
171  }
172 }
173 
174 
176  // no need to do something about mySwitchCommand here,
177  // it is handled by the event control
178 }
179 
180 
181 // ----------- Handling of controlled links
182 void
183 MSTrafficLightLogic::addLink(MSLink* link, MSLane* lane, unsigned int pos) {
184  // !!! should be done within the loader (checking necessary)
185  myLinks.reserve(pos + 1);
186  while (myLinks.size() <= pos) {
187  myLinks.push_back(LinkVector());
188  }
189  myLinks[pos].push_back(link);
190  //
191  myLanes.reserve(pos + 1);
192  while (myLanes.size() <= pos) {
193  myLanes.push_back(LaneVector());
194  }
195  myLanes[pos].push_back(lane);
196  link->setTLState((LinkState) getCurrentPhaseDef().getState()[pos], MSNet::getInstance()->getCurrentTimeStep());
197 }
198 
199 
200 void
202  myLinks = logic.myLinks;
203  myLanes = logic.myLanes;
204 }
205 
206 
207 std::map<MSLink*, LinkState>
209  std::map<MSLink*, LinkState> ret;
210  for (LinkVectorVector::const_iterator i1 = myLinks.begin(); i1 != myLinks.end(); ++i1) {
211  const LinkVector& l = (*i1);
212  for (LinkVector::const_iterator i2 = l.begin(); i2 != l.end(); ++i2) {
213  ret[*i2] = (*i2)->getState();
214  }
215  }
216  return ret;
217 }
218 
219 
220 bool
222  // get the current traffic light signal combination
223  const std::string& state = getCurrentPhaseDef().getState();
224  // go through the links
225  for (size_t i = 0; i < myLinks.size(); i++) {
226  const LinkVector& currGroup = myLinks[i];
227  LinkState ls = (LinkState) state[i];
228  for (LinkVector::const_iterator j = currGroup.begin(); j != currGroup.end(); j++) {
229  (*j)->setTLState(ls, t);
230  }
231  }
232  return true;
233 }
234 
235 
236 void
237 MSTrafficLightLogic::resetLinkStates(const std::map<MSLink*, LinkState>& vals) const {
238  for (LinkVectorVector::const_iterator i1 = myLinks.begin(); i1 != myLinks.end(); ++i1) {
239  const LinkVector& l = (*i1);
240  for (LinkVector::const_iterator i2 = l.begin(); i2 != l.end(); ++i2) {
241  assert(vals.find(*i2) != vals.end());
242  (*i2)->setTLState(vals.find(*i2)->second, MSNet::getInstance()->getCurrentTimeStep());
243  }
244  }
245 }
246 
247 
248 // ----------- Static Information Retrieval
249 int
250 MSTrafficLightLogic::getLinkIndex(const MSLink* const link) const {
251  int index = 0;
252  for (LinkVectorVector::const_iterator i1 = myLinks.begin(); i1 != myLinks.end(); ++i1, ++index) {
253  const LinkVector& l = (*i1);
254  for (LinkVector::const_iterator i2 = l.begin(); i2 != l.end(); ++i2) {
255  if ((*i2) == link) {
256  return index;
257  }
258  }
259  }
260  return -1;
261 }
262 
263 
264 
265 // ----------- Dynamic Information Retrieval
266 SUMOTime
268  return mySwitchCommand != 0 ? mySwitchCommand->getNextSwitchTime() : -1;
269 }
270 
271 
272 // ----------- Changing phases and phase durations
273 void
275  myOverridingTimes.push_back(duration);
276 }
277 
278 
279 void
282 }
283 
284 
285 /****************************************************************************/
286 
SUMOTime myCurrentDurationIncrement
A value for enlarge the current duration.
The link has green light, may pass.
bool isActive(const MSTrafficLightLogic *tl) const
Returns whether the given tls program is the currently active for his tls.
void resetLinkStates(const std::map< MSLink *, LinkState > &vals) const
Resets the states of controlled links.
virtual const MSPhaseDefinition & getCurrentPhaseDef() const =0
Returns the definition of the current phase.
Builds detectors for microsim.
long long int SUMOTime
Definition: SUMOTime.h:43
const std::string & getState() const
Returns the state within this phase.
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
virtual unsigned int getCurrentPhaseIndex() const =0
Returns the current index within the program.
is a pedestrian
Storage for all programs of a single tls.
MSTLLogicControl & myTLControl
The responsible traffic lights control.
std::vector< SUMOTime > myOverridingTimes
A list of duration overrides.
int getLinkIndex(const MSLink *const link) const
Returns the index of the given link.
SwitchCommand(MSTLLogicControl &tlcontrol, MSTrafficLightLogic *tlLogic, SUMOTime nextSwitch)
Constructor.
std::string myProgramID
The id of the logic.
SUMOTime myAssumedNextSwitch
Assumed switch time (may change in case of adaptive traffic lights)
The link has green light, has to brake.
static const LaneVector myEmptyLaneVector
An empty lane vector.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:162
MSTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, SUMOTime delay, const std::map< std::string, std::string > &parameters)
Constructor.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:255
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
LaneVectorVector myLanes
The list of LaneVectors; each vector contains the incoming lanes that belong to the same link index...
virtual ~MSTrafficLightLogic()
Destructor.
SUMOTime getNextSwitchTime() const
Returns the assumed next switch time.
A class that stores and controls tls and switching of their programs.
const std::string & getID() const
Returns the id.
Definition: Named.h:65
void deschedule(MSTrafficLightLogic *tlLogic)
Marks this swicth as invalid (if the phase duration has changed, f.e.)
Class realising the switch between the traffic light phases.
SUMOTime myDefaultCycleTime
The cycle time (without changes)
MSTrafficLightLogic * myTLLogic
The logic to be executed on a switch.
virtual void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
MSEventControl * getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
Definition: MSNet.h:389
virtual void adaptLinkInformationFrom(const MSTrafficLightLogic &logic)
Applies information about controlled links and lanes from the given logic.
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic, in MSLink and GNEInternalLane.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
An upper class for objects with additional parameters.
Definition: Parameterised.h:47
virtual SUMOTime addEvent(Command *operation, SUMOTime execTimeStep, AdaptType type)
Adds an Event.
void setCurrentDurationIncrement(SUMOTime delay)
Delays current phase by the given delay.
Base class for objects which have an id.
Definition: Named.h:45
std::vector< MSLink * > LinkVector
Definition of the list of links that participate in this tl-light.
SUMOTime getNextSwitchTime() const
Returns the assumed next switch time.
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
virtual const Phases & getPhases() const =0
Returns the phases of this tls program.
void addOverridingDuration(SUMOTime duration)
Changes the duration of the next phase.
LinkVectorVector myLinks
The list of LinkVectors; each vector contains the links that belong to the same link index...
std::vector< MSLane * > LaneVector
Definition of the list of links that participate in this tl-light.
The link has red light (must brake)
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
SwitchCommand * mySwitchCommand
The current switch command.
The parent class for traffic light logics.
const std::string & getProgramID() const
Returns this tl-logic&#39;s id.
void addLink(MSLink *link, MSLane *lane, unsigned int pos)
Adds a link on building.
T MIN3(T a, T b, T c)
Definition: StdDefs.h:86
bool myAmValid
Information whether this switch command is still valid.
virtual SUMOTime trySwitch()=0
Switches to the next phase.
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
std::map< MSLink *, LinkState > collectLinkStates() const
Returns the (uncontrolled) states of the controlled links.
SUMOTime execute(SUMOTime currentTime)
Executes the regarded junction&#39;s "trySwitch"- method.