SUMO - Simulation of Urban MObility
MSTrafficLightLogic.h
Go to the documentation of this file.
1 /****************************************************************************/
11 // The parent class for traffic light logics
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 #ifndef MSTrafficLightLogic_h
25 #define MSTrafficLightLogic_h
26 
27 
28 // ===========================================================================
29 // included modules
30 // ===========================================================================
31 #ifdef _MSC_VER
32 #include <windows_config.h>
33 #else
34 #include <config.h>
35 #endif
36 
37 #include <map>
38 #include <string>
39 #include <bitset>
40 #include <utils/common/Command.h>
43 #include <microsim/MSLink.h>
44 #include "MSPhaseDefinition.h"
45 
46 
47 // ===========================================================================
48 // class declarations
49 // ===========================================================================
50 class MSNet;
51 class MSLink;
52 class MSTLLogicControl;
53 class NLDetectorBuilder;
54 
55 
56 // ===========================================================================
57 // class definitions
58 // ===========================================================================
63 class MSTrafficLightLogic : public Named, public Parameterised {
64 public:
67 
69  typedef std::vector<MSPhaseDefinition*> Phases;
70 
72  typedef std::vector<MSLink*> LinkVector;
73 
75  typedef std::vector<LinkVector> LinkVectorVector;
76 
78  typedef std::vector<MSLane*> LaneVector;
79 
81  typedef std::vector<LaneVector> LaneVectorVector;
83 
84 
85 public:
93  const std::string& id,
94  const std::string& programID,
95  SUMOTime delay,
96  const std::map<std::string, std::string>& parameters);
97 
98 
104  virtual void init(NLDetectorBuilder& nb);
105 
106 
108  virtual ~MSTrafficLightLogic();
109 
110 
111 
114 
120  virtual void addLink(MSLink* link, MSLane* lane, int pos);
121 
122 
130  virtual void adaptLinkInformationFrom(const MSTrafficLightLogic& logic);
131 
132 
136  std::map<MSLink*, LinkState> collectLinkStates() const;
137 
138 
142  void resetLinkStates(const std::map<MSLink*, LinkState>& vals) const;
144 
145 
146 
149 
153  virtual SUMOTime trySwitch() = 0;
154 
155 
162  bool setTrafficLightSignals(SUMOTime t) const;
164 
165 
166 
169 
173  const std::string& getProgramID() const {
174  return myProgramID;
175  }
176 
177 
181  const LaneVectorVector& getLaneVectors() const {
182  return myLanes;
183  }
184 
185 
190  const LaneVector& getLanesAt(int i) const {
191  if (i < (int)myLanes.size()) {
192  return myLanes[i];
193  } else {
194  return myEmptyLaneVector;
195  }
196  }
197 
198 
202  const LinkVectorVector& getLinks() const {
203  return myLinks;
204  }
205 
206 
211  const LinkVector& getLinksAt(int i) const {
212  return myLinks[i];
213  }
214 
215 
220  int getLinkIndex(const MSLink* const link) const;
221 
222 
226  virtual int getPhaseNumber() const = 0;
227 
228 
232  virtual const Phases& getPhases() const = 0;
233 
234 
239  virtual const MSPhaseDefinition& getPhase(int givenstep) const = 0;
240 
244  virtual const std::string getLogicType() const = 0;
246 
247 
248 
251 
255  virtual int getCurrentPhaseIndex() const = 0;
256 
257 
261  virtual const MSPhaseDefinition& getCurrentPhaseDef() const = 0;
262 
263 
268  return myDefaultCycleTime;
269  }
270 
271 
277  SUMOTime getNextSwitchTime() const;
278 
279 
284  SUMOTime getSpentDuration() const;
286 
287 
288 
291 
295  virtual SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const = 0;
296 
297 
302  virtual SUMOTime getOffsetFromIndex(int index) const = 0;
303 
304 
309  virtual int getIndexFromOffset(SUMOTime offset) const = 0;
311 
312 
313 
316 
320  void addOverridingDuration(SUMOTime duration);
321 
322 
327 
328 
335  virtual void changeStepAndDuration(MSTLLogicControl& tlcontrol,
336  SUMOTime simStep, int step, SUMOTime stepDuration) = 0;
337 
339 
340 
341 protected:
346  class SwitchCommand : public Command {
347  public:
353  SwitchCommand(MSTLLogicControl& tlcontrol,
354  MSTrafficLightLogic* tlLogic,
355  SUMOTime nextSwitch);
356 
358  ~SwitchCommand();
359 
364  SUMOTime execute(SUMOTime currentTime);
365 
366 
370  void deschedule(MSTrafficLightLogic* tlLogic);
371 
372 
377  return myAssumedNextSwitch;
378  }
379 
380 
381  private:
384 
387 
390 
392  bool myAmValid;
393 
394  private:
397 
400 
401  };
402 
403 
404 protected:
406  std::string myProgramID;
407 
409  LinkVectorVector myLinks;
410 
412  LaneVectorVector myLanes;
413 
415  std::vector<SUMOTime> myOverridingTimes;
416 
419 
422 
425 
427  static const LaneVector myEmptyLaneVector;
428 
429 private:
431  void initMesoTLSPenalties();
432 
433 
434 private:
437 
440 
441 };
442 
443 
444 #endif
445 
446 /****************************************************************************/
447 
SUMOTime myCurrentDurationIncrement
A value for enlarge the current duration.
virtual int getCurrentPhaseIndex() const =0
Returns the current index within the program.
void resetLinkStates(const std::map< MSLink *, LinkState > &vals) const
Resets the states of controlled links.
Builds detectors for microsim.
void initMesoTLSPenalties()
initialize optional meso penalties
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
const LaneVector & getLanesAt(int i) const
Returns the list of lanes that are controlled by the signals at the given position.
virtual int getIndexFromOffset(SUMOTime offset) const =0
Returns the step (the phasenumber) of a given position of the cycle.
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)
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 const std::string getLogicType() const =0
Returns the type of the logic as a string.
virtual SUMOTime getOffsetFromIndex(int index) const =0
Returns the position (start of a phase during a cycle) from of a given step.
MSTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, SUMOTime delay, const std::map< std::string, std::string > &parameters)
Constructor.
Base (microsim) event class.
Definition: Command.h:61
The simulated network and simulation perfomer.
Definition: MSNet.h:89
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.
virtual const MSPhaseDefinition & getPhase(int givenstep) const =0
Returns the definition of the phase from the given position within the plan.
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.
const LinkVector & getLinksAt(int i) const
Returns the list of links that are controlled by the signals at the given position.
SUMOTime myDefaultCycleTime
The cycle time (without changes)
MSTrafficLightLogic * myTLLogic
The logic to be executed on a switch.
virtual const MSPhaseDefinition & getCurrentPhaseDef() const =0
Returns the definition of the current phase.
virtual void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
const LinkVectorVector & getLinks() const
Returns the list of lists of all affected links.
virtual void adaptLinkInformationFrom(const MSTrafficLightLogic &logic)
Applies information about controlled links and lanes from the given logic.
An upper class for objects with additional parameters.
Definition: Parameterised.h:51
void setCurrentDurationIncrement(SUMOTime delay)
Delays current phase by the given delay.
Base class for objects which have an id.
Definition: Named.h:46
const LaneVectorVector & getLaneVectors() const
Returns the list of lists of all lanes controlled by this tls.
std::vector< MSLink * > LinkVector
Definition of the list of links that are subjected to this tls.
SwitchCommand & operator=(const SwitchCommand &)
Invalidated assignment operator.
SUMOTime getNextSwitchTime() const
Returns the assumed next switch time.
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
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.
virtual SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const =0
Returns the index of the logic at the given simulation step.
virtual const Phases & getPhases() const =0
Returns the phases of this tls program.
virtual void addLink(MSLink *link, MSLane *lane, int pos)
Adds a link on building.
virtual int getPhaseNumber() const =0
Returns the number of phases.
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.
long long int SUMOTime
Definition: TraCIDefs.h:52
bool myAmValid
Information whether this switch command is still valid.
virtual SUMOTime trySwitch()=0
Switches to the next phase.
SUMOTime getSpentDuration() const
Returns the duration spent in the current phase.
The definition of a single phase of a tls logic.
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
std::map< MSLink *, LinkState > collectLinkStates() const
Returns the (uncontrolled) states of the controlled links.
SUMOTime getDefaultCycleTime() const
Returns the cycle time (in ms)
SUMOTime execute(SUMOTime currentTime)
Executes the regarded junction&#39;s "trySwitch"- method.