SUMO - Simulation of Urban MObility
MSTrafficLightLogic.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2018 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 /****************************************************************************/
19 // The parent class for traffic light logics
20 /****************************************************************************/
21 #ifndef MSTrafficLightLogic_h
22 #define MSTrafficLightLogic_h
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #include <config.h>
29 
30 #include <map>
31 #include <string>
32 #include <bitset>
33 #include <utils/common/Command.h>
36 #include <microsim/MSLink.h>
37 #include "MSPhaseDefinition.h"
38 
39 
40 // ===========================================================================
41 // class declarations
42 // ===========================================================================
43 class MSNet;
44 class MSLink;
45 class MSTLLogicControl;
46 class NLDetectorBuilder;
47 
48 
49 // ===========================================================================
50 // class definitions
51 // ===========================================================================
56 class MSTrafficLightLogic : public Named, public Parameterised {
57 public:
60 
62  typedef std::vector<MSPhaseDefinition*> Phases;
63 
65  typedef std::vector<MSLink*> LinkVector;
66 
68  typedef std::vector<LinkVector> LinkVectorVector;
69 
71  typedef std::vector<MSLane*> LaneVector;
72 
74  typedef std::vector<LaneVector> LaneVectorVector;
76 
77 
78 public:
86  const std::string& id,
87  const std::string& programID,
88  SUMOTime delay,
89  const std::map<std::string, std::string>& parameters);
90 
91 
97  virtual void init(NLDetectorBuilder& nb);
98 
99 
101  virtual ~MSTrafficLightLogic();
102 
103 
104 
107 
113  virtual void addLink(MSLink* link, MSLane* lane, int pos);
114 
115 
123  virtual void adaptLinkInformationFrom(const MSTrafficLightLogic& logic);
124 
125 
129  std::map<MSLink*, LinkState> collectLinkStates() const;
130 
131 
135  void resetLinkStates(const std::map<MSLink*, LinkState>& vals) const;
137 
138 
139 
142 
146  virtual SUMOTime trySwitch() = 0;
147 
148 
155  bool setTrafficLightSignals(SUMOTime t) const;
157 
158 
159 
162 
166  const std::string& getProgramID() const {
167  return myProgramID;
168  }
169 
170 
174  const LaneVectorVector& getLaneVectors() const {
175  return myLanes;
176  }
177 
178 
183  const LaneVector& getLanesAt(int i) const {
184  if (i < (int)myLanes.size()) {
185  return myLanes[i];
186  } else {
187  return myEmptyLaneVector;
188  }
189  }
190 
191 
195  const LinkVectorVector& getLinks() const {
196  return myLinks;
197  }
198 
199 
204  const LinkVector& getLinksAt(int i) const {
205  return myLinks[i];
206  }
207 
208 
213  int getLinkIndex(const MSLink* const link) const;
214 
215 
219  virtual int getPhaseNumber() const = 0;
220 
221 
225  virtual const Phases& getPhases() const = 0;
226 
227 
232  virtual const MSPhaseDefinition& getPhase(int givenstep) const = 0;
233 
237  virtual const std::string getLogicType() const = 0;
239 
240 
241 
244 
248  virtual int getCurrentPhaseIndex() const = 0;
249 
250 
254  virtual const MSPhaseDefinition& getCurrentPhaseDef() const = 0;
255 
256 
261  return myDefaultCycleTime;
262  }
263 
264 
270  SUMOTime getNextSwitchTime() const;
271 
272 
277  SUMOTime getSpentDuration() const;
279 
280 
281 
284 
288  virtual SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const = 0;
289 
290 
295  virtual SUMOTime getOffsetFromIndex(int index) const = 0;
296 
297 
302  virtual int getIndexFromOffset(SUMOTime offset) const = 0;
304 
305 
306 
309 
313  void addOverridingDuration(SUMOTime duration);
314 
315 
320 
321 
328  virtual void changeStepAndDuration(MSTLLogicControl& tlcontrol,
329  SUMOTime simStep, int step, SUMOTime stepDuration) = 0;
330 
332 
333 
334 protected:
339  class SwitchCommand : public Command {
340  public:
346  SwitchCommand(MSTLLogicControl& tlcontrol,
347  MSTrafficLightLogic* tlLogic,
348  SUMOTime nextSwitch);
349 
351  ~SwitchCommand();
352 
357  SUMOTime execute(SUMOTime currentTime);
358 
359 
363  void deschedule(MSTrafficLightLogic* tlLogic);
364 
365 
370  return myAssumedNextSwitch;
371  }
372 
373 
374  private:
377 
380 
383 
385  bool myAmValid;
386 
387  private:
390 
393 
394  };
395 
396 
397 protected:
399  std::string myProgramID;
400 
402  LinkVectorVector myLinks;
403 
405  LaneVectorVector myLanes;
406 
408  std::vector<SUMOTime> myOverridingTimes;
409 
412 
415 
418 
420  static const LaneVector myEmptyLaneVector;
421 
422 private:
424  void initMesoTLSPenalties();
425 
426 
427 private:
430 
433 
434 };
435 
436 
437 #endif
438 
439 /****************************************************************************/
440 
SUMOTime myCurrentDurationIncrement
A value for enlarge the current duration.
virtual const MSPhaseDefinition & getCurrentPhaseDef() const =0
Returns the definition of the current phase.
Builds detectors for microsim.
void initMesoTLSPenalties()
initialize optional meso penalties
long long int SUMOTime
Definition: SUMOTime.h:36
MSTLLogicControl & myTLControl
The responsible traffic lights control.
virtual SUMOTime getOffsetFromIndex(int index) const =0
Returns the position (start of a phase during a cycle) from of a given step.
std::vector< SUMOTime > myOverridingTimes
A list of duration overrides.
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)
virtual void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)=0
Changes the current phase and her duration.
static const LaneVector myEmptyLaneVector
An empty lane vector.
virtual SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const =0
Returns the index of the logic at the given simulation step.
const LaneVectorVector & getLaneVectors() const
Returns the list of lists of all lanes controlled by this tls.
virtual const MSPhaseDefinition & getPhase(int givenstep) const =0
Returns the definition of the phase from the given position within the plan.
virtual int getCurrentPhaseIndex() const =0
Returns the current index within the program.
MSTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, SUMOTime delay, const std::map< std::string, std::string > &parameters)
Constructor.
SUMOTime getNextSwitchTime() const
Returns the assumed next switch time.
Base (microsim) event class.
Definition: Command.h:54
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
void resetLinkStates(const std::map< MSLink *, LinkState > &vals) const
Resets the states of controlled links.
const LinkVectorVector & getLinks() const
Returns the list of lists of all affected links.
int getLinkIndex(const MSLink *const link) const
Returns the index of the given link.
const LinkVector & getLinksAt(int i) const
Returns the list of links that are controlled by the signals at the given position.
The simulated network and simulation perfomer.
Definition: MSNet.h:85
LaneVectorVector myLanes
The list of LaneVectors; each vector contains the incoming lanes that belong to the same link index...
const LaneVector & getLanesAt(int i) const
Returns the list of lanes that are controlled by the signals at the given position.
virtual ~MSTrafficLightLogic()
Destructor.
A class that stores and controls tls and switching of their programs.
SUMOTime getDefaultCycleTime() const
Returns the cycle time (in ms)
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.
std::vector< LinkVector > LinkVectorVector
Definition of a list that holds lists of links that do have the same attribute.
virtual int getPhaseNumber() const =0
Returns the number of phases.
SUMOTime myDefaultCycleTime
The cycle time (without changes)
const std::string & getProgramID() const
Returns this tl-logic&#39;s id.
MSTrafficLightLogic * myTLLogic
The logic to be executed on a switch.
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.
std::map< MSLink *, LinkState > collectLinkStates() const
Returns the (uncontrolled) states of the controlled links.
virtual const std::string getLogicType() const =0
Returns the type of the logic as a string.
An upper class for objects with additional parameters.
Definition: Parameterised.h:44
void setCurrentDurationIncrement(SUMOTime delay)
Delays current phase by the given delay.
Base class for objects which have an id.
Definition: Named.h:49
std::vector< MSLink * > LinkVector
Definition of the list of links that are subjected to this tls.
SwitchCommand & operator=(const SwitchCommand &)
Invalidated assignment operator.
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 arrival lanes subjected to this tls.
std::vector< LaneVector > LaneVectorVector
Definition of a list that holds lists of lanes that do have the same attribute.
SUMOTime getNextSwitchTime() const
Returns the assumed next switch time.
virtual void addLink(MSLink *link, MSLane *lane, int pos)
Adds a link on building.
SwitchCommand * mySwitchCommand
The current switch command.
The parent class for traffic light logics.
SUMOTime getSpentDuration() const
Returns the duration spent in the current phase.
bool myAmValid
Information whether this switch command is still valid.
virtual SUMOTime trySwitch()=0
Switches to the next phase.
The definition of a single phase of a tls logic.
Representation of a lane in the micro simulation.
Definition: MSLane.h:71
SUMOTime execute(SUMOTime currentTime)
Executes the regarded junction&#39;s "trySwitch"- method.
virtual int getIndexFromOffset(SUMOTime offset) const =0
Returns the step (the phasenumber) of a given position of the cycle.