SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-sim.org/
12 // Copyright (C) 2001-2014 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>
39 #include "MSTrafficLightLogic.h"
41 #include "MSTLLogicControl.h"
43 
44 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
47 
48 
49 // ===========================================================================
50 // member method definitions
51 // ===========================================================================
52 /* -------------------------------------------------------------------------
53  * member method definitions
54  * ----------------------------------------------------------------------- */
56  MSTrafficLightLogic* tlLogic, SUMOTime nextSwitch)
57  : myTLControl(tlcontrol), myTLLogic(tlLogic),
58  myAssumedNextSwitch(nextSwitch), myAmValid(true) {}
59 
60 
62 
63 
64 
67  // check whether this command has been descheduled
68  if (!myAmValid) {
69  return 0;
70  }
71  //
72  bool isActive = myTLControl.isActive(myTLLogic);
73  size_t step1 = myTLLogic->getCurrentPhaseIndex();
74  SUMOTime next = myTLLogic->trySwitch(isActive);
75  size_t step2 = myTLLogic->getCurrentPhaseIndex();
76  if (step1 != step2) {
77  if (isActive) {
78  // execute any action connected to this tls
79  const MSTLLogicControl::TLSLogicVariants& vars = myTLControl.get(myTLLogic->getID());
80  // set link priorities
81  myTLLogic->setTrafficLightSignals(t);
82  // execute switch actions
84  }
85  }
86  myAssumedNextSwitch += next;
87  return next;
88 }
89 
90 
91 void
93  if (tlLogic == myTLLogic) {
94  myAmValid = false;
95  myAssumedNextSwitch = -1;
96  }
97 }
98 
99 
100 /* -------------------------------------------------------------------------
101  * member method definitions
102  * ----------------------------------------------------------------------- */
104  const std::string& programID, SUMOTime delay, const std::map<std::string, std::string>& parameters) :
105  Named(id), Parameterised(parameters),
106  myProgramID(programID),
108  myDefaultCycleTime(0) {
109  mySwitchCommand = new SwitchCommand(tlcontrol, this, delay);
112 }
113 
114 
115 void
117  const Phases& phases = getPhases();
118  if (phases.size() > 1) {
119  // warn about transistions from green to red without intermediate yellow
120  for (int i = 0; i < (int)phases.size(); ++i) {
121  const int iNext = (i + 1) % phases.size();
122  const std::string& state1 = phases[i]->getState();
123  const std::string& state2 = phases[iNext]->getState();
124  assert(state1.size() == state2.size());
125  for (int j = 0; j < (int)MIN2(state1.size(), state2.size()); ++j) {
126  if ((LinkState)state2[j] == LINKSTATE_TL_RED
127  && ((LinkState)state1[j] == LINKSTATE_TL_GREEN_MAJOR
128  || (LinkState)state1[j] == LINKSTATE_TL_GREEN_MINOR)) {
129  for (LaneVector::const_iterator it = myLanes[j].begin(); it != myLanes[j].end(); ++it) {
130  if ((*it)->getPermissions() != SVC_PEDESTRIAN) {
131  WRITE_WARNING("Missing yellow phase in tlLogic '" + getID()
132  + "', program '" + getProgramID() + "' for tl-index " + toString(j)
133  + " when switching to phase " + toString(iNext));
134  return; // one warning per program is enough
135  }
136  }
137  }
138  }
139  }
140  }
141 }
142 
143 
145  // no need to do something about mySwitchCommand here,
146  // it is handled by the event control
147 }
148 
149 
150 // ----------- Handling of controlled links
151 void
152 MSTrafficLightLogic::addLink(MSLink* link, MSLane* lane, unsigned int pos) {
153  // !!! should be done within the loader (checking necessary)
154  myLinks.reserve(pos + 1);
155  while (myLinks.size() <= pos) {
156  myLinks.push_back(LinkVector());
157  }
158  myLinks[pos].push_back(link);
159  //
160  myLanes.reserve(pos + 1);
161  while (myLanes.size() <= pos) {
162  myLanes.push_back(LaneVector());
163  }
164  myLanes[pos].push_back(lane);
165  link->setTLState((LinkState) getCurrentPhaseDef().getState()[pos], MSNet::getInstance()->getCurrentTimeStep());
166 }
167 
168 
169 void
171  myLinks = logic.myLinks;
172  myLanes = logic.myLanes;
173 }
174 
175 
176 std::map<MSLink*, LinkState>
178  std::map<MSLink*, LinkState> ret;
179  for (LinkVectorVector::const_iterator i1 = myLinks.begin(); i1 != myLinks.end(); ++i1) {
180  const LinkVector& l = (*i1);
181  for (LinkVector::const_iterator i2 = l.begin(); i2 != l.end(); ++i2) {
182  ret[*i2] = (*i2)->getState();
183  }
184  }
185  return ret;
186 }
187 
188 
189 bool
191  // get the current traffic light signal combination
192  const std::string& state = getCurrentPhaseDef().getState();
193  // go through the links
194  for (size_t i = 0; i < myLinks.size(); i++) {
195  const LinkVector& currGroup = myLinks[i];
196  LinkState ls = (LinkState) state[i];
197  for (LinkVector::const_iterator j = currGroup.begin(); j != currGroup.end(); j++) {
198  (*j)->setTLState(ls, t);
199  }
200  }
201  return true;
202 }
203 
204 
205 void
206 MSTrafficLightLogic::resetLinkStates(const std::map<MSLink*, LinkState>& vals) const {
207  for (LinkVectorVector::const_iterator i1 = myLinks.begin(); i1 != myLinks.end(); ++i1) {
208  const LinkVector& l = (*i1);
209  for (LinkVector::const_iterator i2 = l.begin(); i2 != l.end(); ++i2) {
210  assert(vals.find(*i2) != vals.end());
211  (*i2)->setTLState(vals.find(*i2)->second, MSNet::getInstance()->getCurrentTimeStep());
212  }
213  }
214 }
215 
216 
217 // ----------- Static Information Retrieval
218 int
219 MSTrafficLightLogic::getLinkIndex(const MSLink* const link) const {
220  int index = 0;
221  for (LinkVectorVector::const_iterator i1 = myLinks.begin(); i1 != myLinks.end(); ++i1, ++index) {
222  const LinkVector& l = (*i1);
223  for (LinkVector::const_iterator i2 = l.begin(); i2 != l.end(); ++i2) {
224  if ((*i2) == link) {
225  return index;
226  }
227  }
228  }
229  return -1;
230 }
231 
232 
233 
234 // ----------- Dynamic Information Retrieval
235 SUMOTime
237  return mySwitchCommand != 0 ? mySwitchCommand->getNextSwitchTime() : -1;
238 }
239 
240 
241 // ----------- Changing phases and phase durations
242 void
244  myOverridingTimes.push_back(duration);
245 }
246 
247 
248 void
251 }
252 
253 
254 /****************************************************************************/
255 
SUMOTime myCurrentDurationIncrement
A value for enlarge the current duration.
The link has green light, may pass.
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.
const std::string & getState() const
Returns the state within this phase.
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
is a pedestrian
Storage for all programs of a single tls.
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.
The link has green light, has to brake.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
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:216
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
LaneVectorVector myLanes
The list of links which do participate in this traffic light.
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:60
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)
virtual void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
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.
T MIN2(T a, T b)
Definition: StdDefs.h:66
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 links which do participate in this traffic light.
std::vector< MSLane * > LaneVector
Definition of the list of links that participate in this tl-light.
The link has red light (must brake)
int SUMOTime
Definition: SUMOTime.h:43
SwitchCommand * mySwitchCommand
The current switch command.
The parent class for traffic light logics.
const std::string & getProgramID() const
Returns this tl-logic's id.
void addLink(MSLink *link, MSLane *lane, unsigned int pos)
Adds a link on building.
MSEventControl & getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
Definition: MSNet.h:340
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's "trySwitch"- method.