Eclipse 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-2019 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:
88  const std::string& id,
89  const std::string& programID,
90  const TrafficLightType logicType,
91  const SUMOTime delay,
92  const std::map<std::string, std::string>& parameters);
93 
94 
100  virtual void init(NLDetectorBuilder& nb);
101 
102 
104  virtual ~MSTrafficLightLogic();
105 
106 
107 
110 
116  virtual void addLink(MSLink* link, MSLane* lane, int pos);
117 
119  void ignoreLinkIndex(int pos);
120 
121 
129  virtual void adaptLinkInformationFrom(const MSTrafficLightLogic& logic);
130 
131 
135  std::map<MSLink*, LinkState> collectLinkStates() const;
136 
137 
141  void resetLinkStates(const std::map<MSLink*, LinkState>& vals) const;
143 
144 
145 
148 
152  virtual SUMOTime trySwitch() = 0;
153 
154 
161  bool setTrafficLightSignals(SUMOTime t) const;
163 
164 
165 
168 
172  const std::string& getProgramID() const {
173  return myProgramID;
174  }
175 
176 
181  return myLanes;
182  }
183 
184 
189  const LaneVector& getLanesAt(int i) const {
190  if (i < (int)myLanes.size()) {
191  return myLanes[i];
192  } else {
193  return myEmptyLaneVector;
194  }
195  }
196 
197 
201  const LinkVectorVector& getLinks() const {
202  return myLinks;
203  }
204 
205 
210  const LinkVector& getLinksAt(int i) const {
211  return myLinks[i];
212  }
213 
214 
219  int getLinkIndex(const MSLink* const link) const;
220 
221 
225  virtual int getPhaseNumber() const = 0;
226 
227 
231  virtual const Phases& getPhases() const = 0;
232 
233 
238  virtual const MSPhaseDefinition& getPhase(int givenstep) const = 0;
239 
244  return myLogicType;
245  }
247 
248 
249 
252 
256  virtual int getCurrentPhaseIndex() const = 0;
257 
258 
262  virtual const MSPhaseDefinition& getCurrentPhaseDef() const = 0;
263 
264 
269  return myDefaultCycleTime;
270  }
271 
272 
278  SUMOTime getNextSwitchTime() const;
279 
280 
285  SUMOTime getSpentDuration() const;
287 
288 
289 
292 
296  virtual SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const = 0;
297 
298 
303  virtual SUMOTime getOffsetFromIndex(int index) const = 0;
304 
305 
310  virtual int getIndexFromOffset(SUMOTime offset) const = 0;
312 
313 
314 
317 
321  void addOverridingDuration(SUMOTime duration);
322 
323 
328 
329 
336  virtual void changeStepAndDuration(MSTLLogicControl& tlcontrol,
337  SUMOTime simStep, int step, SUMOTime stepDuration) = 0;
338 
340 
342  bool isSelected() const;
343 
344 protected:
349  class SwitchCommand : public Command {
350  public:
356  SwitchCommand(MSTLLogicControl& tlcontrol,
357  MSTrafficLightLogic* tlLogic,
358  SUMOTime nextSwitch);
359 
361  ~SwitchCommand();
362 
367  SUMOTime execute(SUMOTime currentTime);
368 
369 
373  void deschedule(MSTrafficLightLogic* tlLogic);
374 
375 
380  return myAssumedNextSwitch;
381  }
382 
383 
384  private:
387 
390 
393 
395  bool myAmValid;
396 
397  private:
400 
403 
404  };
405 
406 
407 protected:
409  const std::string myProgramID;
410 
413 
416 
419 
421  std::vector<SUMOTime> myOverridingTimes;
422 
425 
428 
431 
434 
436  std::set<int> myIgnoredIndices;
437 
438 private:
440  void initMesoTLSPenalties();
441 
442 
443 private:
446 
449 
450 };
451 
452 
453 #endif
454 
455 /****************************************************************************/
456 
MSTrafficLightLogic::getPhases
virtual const Phases & getPhases() const =0
Returns the phases of this tls program.
MSTrafficLightLogic::setTrafficLightSignals
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
Definition: MSTrafficLightLogic.cpp:235
MSTrafficLightLogic::SwitchCommand::myAmValid
bool myAmValid
Information whether this switch command is still valid.
Definition: MSTrafficLightLogic.h:395
MSTrafficLightLogic::getLinksAt
const LinkVector & getLinksAt(int i) const
Returns the list of links that are controlled by the signals at the given position.
Definition: MSTrafficLightLogic.h:210
MSTrafficLightLogic::myLinks
LinkVectorVector myLinks
The list of LinkVectors; each vector contains the links that belong to the same link index.
Definition: MSTrafficLightLogic.h:415
MSTrafficLightLogic::myLanes
LaneVectorVector myLanes
The list of LaneVectors; each vector contains the incoming lanes that belong to the same link index.
Definition: MSTrafficLightLogic.h:418
MSTrafficLightLogic::getPhaseIndexAtTime
virtual SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const =0
Returns the index of the logic at the given simulation step.
MSTrafficLightLogic::myOverridingTimes
std::vector< SUMOTime > myOverridingTimes
A list of duration overrides.
Definition: MSTrafficLightLogic.h:421
MSTrafficLightLogic::SwitchCommand::myAssumedNextSwitch
SUMOTime myAssumedNextSwitch
Assumed switch time (may change in case of adaptive traffic lights)
Definition: MSTrafficLightLogic.h:392
Parameterised
An upper class for objects with additional parameters.
Definition: Parameterised.h:43
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
Named
Base class for objects which have an id.
Definition: Named.h:57
MSTrafficLightLogic::getProgramID
const std::string & getProgramID() const
Returns this tl-logic's id.
Definition: MSTrafficLightLogic.h:172
MSTrafficLightLogic::ignoreLinkIndex
void ignoreLinkIndex(int pos)
ignore pedestrian crossing index in mesosim
Definition: MSTrafficLightLogic.cpp:366
MSTrafficLightLogic::getLogicType
TrafficLightType getLogicType() const
Returns the type of the logic.
Definition: MSTrafficLightLogic.h:243
MSTrafficLightLogic::getOffsetFromIndex
virtual SUMOTime getOffsetFromIndex(int index) const =0
Returns the position (start of a phase during a cycle) from of a given step.
MSTrafficLightLogic::SwitchCommand
Class realising the switch between the traffic light phases.
Definition: MSTrafficLightLogic.h:349
MSTrafficLightLogic::Phases
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
Definition: MSTrafficLightLogic.h:62
MSNet
The simulated network and simulation perfomer.
Definition: MSNet.h:92
MSTrafficLightLogic::SwitchCommand::deschedule
void deschedule(MSTrafficLightLogic *tlLogic)
Marks this swicth as invalid (if the phase duration has changed, f.e.)
Definition: MSTrafficLightLogic.cpp:90
MSTrafficLightLogic::getSpentDuration
SUMOTime getSpentDuration() const
Returns the duration spent in the current phase.
Definition: MSTrafficLightLogic.cpp:287
TrafficLightType
TrafficLightType
Definition: SUMOXMLDefinitions.h:1192
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:35
MSTrafficLightLogic::addOverridingDuration
void addOverridingDuration(SUMOTime duration)
Changes the duration of the next phase.
Definition: MSTrafficLightLogic.cpp:300
MSTrafficLightLogic::getCurrentPhaseIndex
virtual int getCurrentPhaseIndex() const =0
Returns the current index within the program.
MSTrafficLightLogic::adaptLinkInformationFrom
virtual void adaptLinkInformationFrom(const MSTrafficLightLogic &logic)
Applies information about controlled links and lanes from the given logic.
Definition: MSTrafficLightLogic.cpp:214
MSTrafficLightLogic::addLink
virtual void addLink(MSLink *link, MSLane *lane, int pos)
Adds a link on building.
Definition: MSTrafficLightLogic.cpp:196
MSTrafficLightLogic::mySwitchCommand
SwitchCommand * mySwitchCommand
The current switch command.
Definition: MSTrafficLightLogic.h:427
MSTrafficLightLogic::SwitchCommand::SwitchCommand
SwitchCommand(MSTLLogicControl &tlcontrol, MSTrafficLightLogic *tlLogic, SUMOTime nextSwitch)
Constructor.
Definition: MSTrafficLightLogic.cpp:53
MSTrafficLightLogic::SwitchCommand::~SwitchCommand
~SwitchCommand()
Destructor.
Definition: MSTrafficLightLogic.cpp:59
MSTrafficLightLogic::myCurrentDurationIncrement
SUMOTime myCurrentDurationIncrement
A value for enlarge the current duration.
Definition: MSTrafficLightLogic.h:424
MSTrafficLightLogic::getIndexFromOffset
virtual int getIndexFromOffset(SUMOTime offset) const =0
Returns the step (the phasenumber) of a given position of the cycle.
MSTrafficLightLogic::LaneVector
std::vector< MSLane * > LaneVector
Definition of the list of arrival lanes subjected to this tls.
Definition: MSTrafficLightLogic.h:71
MSTrafficLightLogic::SwitchCommand::myTLControl
MSTLLogicControl & myTLControl
The responsible traffic lights control.
Definition: MSTrafficLightLogic.h:386
MSTrafficLightLogic::changeStepAndDuration
virtual void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)=0
Changes the current phase and her duration.
MSTrafficLightLogic::MSTrafficLightLogic
MSTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const TrafficLightType logicType, const SUMOTime delay, const std::map< std::string, std::string > &parameters)
Constructor.
Definition: MSTrafficLightLogic.cpp:101
MSTrafficLightLogic::setCurrentDurationIncrement
void setCurrentDurationIncrement(SUMOTime delay)
Delays current phase by the given delay.
Definition: MSTrafficLightLogic.cpp:306
MSTrafficLightLogic::SwitchCommand::execute
SUMOTime execute(SUMOTime currentTime)
Executes the regarded junction's "trySwitch"- method.
Definition: MSTrafficLightLogic.cpp:64
Parameterised.h
MSTrafficLightLogic::getDefaultCycleTime
SUMOTime getDefaultCycleTime() const
Returns the cycle time (in ms)
Definition: MSTrafficLightLogic.h:268
MSTrafficLightLogic::getLinkIndex
int getLinkIndex(const MSLink *const link) const
Returns the index of the given link.
Definition: MSTrafficLightLogic.cpp:264
MSTrafficLightLogic
The parent class for traffic light logics.
Definition: MSTrafficLightLogic.h:56
MSTrafficLightLogic::SwitchCommand::getNextSwitchTime
SUMOTime getNextSwitchTime() const
Returns the assumed next switch time.
Definition: MSTrafficLightLogic.h:379
MSTrafficLightLogic::collectLinkStates
std::map< MSLink *, LinkState > collectLinkStates() const
Returns the (uncontrolled) states of the controlled links.
Definition: MSTrafficLightLogic.cpp:222
MSTrafficLightLogic::getPhaseNumber
virtual int getPhaseNumber() const =0
Returns the number of phases.
MSTrafficLightLogic::myProgramID
const std::string myProgramID
The id of the logic.
Definition: MSTrafficLightLogic.h:409
MSTrafficLightLogic::myLogicType
const TrafficLightType myLogicType
The type of the logic.
Definition: MSTrafficLightLogic.h:412
MSTrafficLightLogic::getPhase
virtual const MSPhaseDefinition & getPhase(int givenstep) const =0
Returns the definition of the phase from the given position within the plan.
MSTrafficLightLogic::getLaneVectors
const LaneVectorVector & getLaneVectors() const
Returns the list of lists of all lanes controlled by this tls.
Definition: MSTrafficLightLogic.h:180
MSTrafficLightLogic::getLinks
const LinkVectorVector & getLinks() const
Returns the list of lists of all affected links.
Definition: MSTrafficLightLogic.h:201
MSTrafficLightLogic::myIgnoredIndices
std::set< int > myIgnoredIndices
list of indices that are ignored in mesoscopic simulatino
Definition: MSTrafficLightLogic.h:436
MSTrafficLightLogic::initMesoTLSPenalties
void initMesoTLSPenalties()
initialize optional meso penalties
Definition: MSTrafficLightLogic.cpp:311
MSTrafficLightLogic::myEmptyLaneVector
static const LaneVector myEmptyLaneVector
An empty lane vector.
Definition: MSTrafficLightLogic.h:433
Command
Base (microsim) event class.
Definition: Command.h:53
MSPhaseDefinition.h
MSTrafficLightLogic::operator=
MSTrafficLightLogic & operator=(const MSTrafficLightLogic &s)
invalidated assignment operator
MSTrafficLightLogic::LaneVectorVector
std::vector< LaneVector > LaneVectorVector
Definition of a list that holds lists of lanes that do have the same attribute.
Definition: MSTrafficLightLogic.h:74
config.h
MSTrafficLightLogic::myDefaultCycleTime
SUMOTime myDefaultCycleTime
The cycle time (without changes)
Definition: MSTrafficLightLogic.h:430
MSTrafficLightLogic::~MSTrafficLightLogic
virtual ~MSTrafficLightLogic()
Destructor.
Definition: MSTrafficLightLogic.cpp:188
MSLogicJunction.h
MSTrafficLightLogic::isSelected
bool isSelected() const
whether this logic is selected in the GUI
Definition: MSTrafficLightLogic.cpp:372
MSTLLogicControl
A class that stores and controls tls and switching of their programs.
Definition: MSTLLogicControl.h:60
MSTrafficLightLogic::resetLinkStates
void resetLinkStates(const std::map< MSLink *, LinkState > &vals) const
Resets the states of controlled links.
Definition: MSTrafficLightLogic.cpp:251
Command.h
MSTrafficLightLogic::getNextSwitchTime
SUMOTime getNextSwitchTime() const
Returns the assumed next switch time.
Definition: MSTrafficLightLogic.cpp:281
MSPhaseDefinition
The definition of a single phase of a tls logic.
Definition: MSPhaseDefinition.h:52
MSTrafficLightLogic::init
virtual void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
Definition: MSTrafficLightLogic.cpp:115
MSTrafficLightLogic::LinkVectorVector
std::vector< LinkVector > LinkVectorVector
Definition of a list that holds lists of links that do have the same attribute.
Definition: MSTrafficLightLogic.h:68
MSTrafficLightLogic::trySwitch
virtual SUMOTime trySwitch()=0
Switches to the next phase.
MSTrafficLightLogic::SwitchCommand::operator=
SwitchCommand & operator=(const SwitchCommand &)
Invalidated assignment operator.
MSTrafficLightLogic::LinkVector
std::vector< MSLink * > LinkVector
Definition of the list of links that are subjected to this tls.
Definition: MSTrafficLightLogic.h:65
NLDetectorBuilder
Builds detectors for microsim.
Definition: NLDetectorBuilder.h:56
MSTrafficLightLogic::SwitchCommand::myTLLogic
MSTrafficLightLogic * myTLLogic
The logic to be executed on a switch.
Definition: MSTrafficLightLogic.h:389
MSTrafficLightLogic::getCurrentPhaseDef
virtual const MSPhaseDefinition & getCurrentPhaseDef() const =0
Returns the definition of the current phase.
MSTrafficLightLogic::getLanesAt
const LaneVector & getLanesAt(int i) const
Returns the list of lanes that are controlled by the signals at the given position.
Definition: MSTrafficLightLogic.h:189