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 /****************************************************************************/
18 // The parent class for traffic light logics
19 /****************************************************************************/
20 #ifndef MSTrafficLightLogic_h
21 #define MSTrafficLightLogic_h
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #include <config.h>
28 
29 #include <map>
30 #include <string>
31 #include <bitset>
32 #include <utils/common/Command.h>
35 #include <microsim/MSLink.h>
36 #include "MSPhaseDefinition.h"
37 
38 
39 // ===========================================================================
40 // class declarations
41 // ===========================================================================
42 class MSNet;
43 class MSLink;
44 class MSTLLogicControl;
45 class NLDetectorBuilder;
46 
47 
48 // ===========================================================================
49 // class definitions
50 // ===========================================================================
55 class MSTrafficLightLogic : public Named, public Parameterised {
56 public:
59 
61  typedef std::vector<MSPhaseDefinition*> Phases;
62 
64  typedef std::vector<MSLink*> LinkVector;
65 
67  typedef std::vector<LinkVector> LinkVectorVector;
68 
70  typedef std::vector<MSLane*> LaneVector;
71 
73  typedef std::vector<LaneVector> LaneVectorVector;
75 
76 
77 public:
87  const std::string& id,
88  const std::string& programID,
89  const TrafficLightType logicType,
90  const SUMOTime delay,
91  const std::map<std::string, std::string>& parameters);
92 
93 
99  virtual void init(NLDetectorBuilder& nb);
100 
101 
103  virtual ~MSTrafficLightLogic();
104 
105 
106 
109 
115  virtual void addLink(MSLink* link, MSLane* lane, int pos);
116 
118  void ignoreLinkIndex(int pos);
119 
120 
128  virtual void adaptLinkInformationFrom(const MSTrafficLightLogic& logic);
129 
130 
134  std::map<MSLink*, LinkState> collectLinkStates() const;
135 
136 
140  void resetLinkStates(const std::map<MSLink*, LinkState>& vals) const;
142 
143 
144 
147 
151  virtual SUMOTime trySwitch() = 0;
152 
154  virtual void activateProgram();
155  virtual void deactivateProgram();
156 
163  bool setTrafficLightSignals(SUMOTime t) const;
165 
166 
167 
170 
174  const std::string& getProgramID() const {
175  return myProgramID;
176  }
177 
178 
183  return myLanes;
184  }
185 
186 
191  const LaneVector& getLanesAt(int i) const {
192  if (i < (int)myLanes.size()) {
193  return myLanes[i];
194  } else {
195  return myEmptyLaneVector;
196  }
197  }
198 
199 
203  const LinkVectorVector& getLinks() const {
204  return myLinks;
205  }
206 
207 
212  const LinkVector& getLinksAt(int i) const {
213  return myLinks[i];
214  }
215 
216 
221  int getLinkIndex(const MSLink* const link) const;
222 
223 
227  virtual int getPhaseNumber() const = 0;
228 
229 
233  virtual const Phases& getPhases() const = 0;
234 
235 
240  virtual const MSPhaseDefinition& getPhase(int givenstep) const = 0;
241 
246  return myLogicType;
247  }
249 
250 
251 
254 
258  virtual int getCurrentPhaseIndex() const = 0;
259 
260 
264  virtual const MSPhaseDefinition& getCurrentPhaseDef() const = 0;
265 
266 
271  return myDefaultCycleTime;
272  }
273 
274 
280  SUMOTime getNextSwitchTime() const;
281 
282 
287  SUMOTime getSpentDuration() const;
289 
290 
291 
294 
298  virtual SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const = 0;
299 
300 
305  virtual SUMOTime getOffsetFromIndex(int index) const = 0;
306 
307 
312  virtual int getIndexFromOffset(SUMOTime offset) const = 0;
314 
315 
316 
319 
323  void addOverridingDuration(SUMOTime duration);
324 
325 
330 
331 
338  virtual void changeStepAndDuration(MSTLLogicControl& tlcontrol,
339  SUMOTime simStep, int step, SUMOTime stepDuration) = 0;
340 
342 
344  bool isSelected() const;
345 
346 protected:
351  class SwitchCommand : public Command {
352  public:
358  SwitchCommand(MSTLLogicControl& tlcontrol,
359  MSTrafficLightLogic* tlLogic,
360  SUMOTime nextSwitch);
361 
363  ~SwitchCommand();
364 
369  SUMOTime execute(SUMOTime currentTime);
370 
371 
375  void deschedule(MSTrafficLightLogic* tlLogic);
376 
377 
382  return myAssumedNextSwitch;
383  }
384 
385 
386  private:
389 
392 
395 
397  bool myAmValid;
398 
399  private:
402 
405 
406  };
407 
408 
409 protected:
411  const std::string myProgramID;
412 
415 
418 
421 
423  std::vector<SUMOTime> myOverridingTimes;
424 
427 
430 
433 
436 
438  std::set<int> myIgnoredIndices;
439 
442 
443 private:
445  void initMesoTLSPenalties();
446 
447 
448 private:
451 
454 
455 };
456 
457 
458 #endif
459 
460 /****************************************************************************/
461 
MSTrafficLightLogic::activateProgram
virtual void activateProgram()
called when switching programs
Definition: MSTrafficLightLogic.cpp:378
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:397
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:212
MSTrafficLightLogic::myLinks
LinkVectorVector myLinks
The list of LinkVectors; each vector contains the links that belong to the same link index.
Definition: MSTrafficLightLogic.h:417
MSTrafficLightLogic::myLanes
LaneVectorVector myLanes
The list of LaneVectors; each vector contains the incoming lanes that belong to the same link index.
Definition: MSTrafficLightLogic.h:420
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:423
MSTrafficLightLogic::SwitchCommand::myAssumedNextSwitch
SUMOTime myAssumedNextSwitch
Assumed switch time (may change in case of adaptive traffic lights)
Definition: MSTrafficLightLogic.h:394
Parameterised
An upper class for objects with additional parameters.
Definition: Parameterised.h:42
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
Named
Base class for objects which have an id.
Definition: Named.h:56
MSTrafficLightLogic::getProgramID
const std::string & getProgramID() const
Returns this tl-logic's id.
Definition: MSTrafficLightLogic.h:174
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:245
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:351
MSTrafficLightLogic::Phases
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
Definition: MSTrafficLightLogic.h:61
MSNet
The simulated network and simulation perfomer.
Definition: MSNet.h:91
MSTrafficLightLogic::MSTrafficLightLogic
MSTrafficLightLogic(const MSTrafficLightLogic &s)
invalidated copy constructor
MSTrafficLightLogic::SwitchCommand::deschedule
void deschedule(MSTrafficLightLogic *tlLogic)
Marks this swicth as invalid (if the phase duration has changed, f.e.)
Definition: MSTrafficLightLogic.cpp:89
MSTrafficLightLogic::getSpentDuration
SUMOTime getSpentDuration() const
Returns the duration spent in the current phase.
Definition: MSTrafficLightLogic.cpp:287
TrafficLightType
TrafficLightType
Definition: SUMOXMLDefinitions.h:1197
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
MSTrafficLightLogic::SwitchCommand::SwitchCommand
SwitchCommand(const SwitchCommand &)
Invalidated copy constructor.
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:429
MSTrafficLightLogic::SwitchCommand::SwitchCommand
SwitchCommand(MSTLLogicControl &tlcontrol, MSTrafficLightLogic *tlLogic, SUMOTime nextSwitch)
Constructor.
Definition: MSTrafficLightLogic.cpp:52
MSTrafficLightLogic::SwitchCommand::~SwitchCommand
~SwitchCommand()
Destructor.
Definition: MSTrafficLightLogic.cpp:58
MSTrafficLightLogic::myCurrentDurationIncrement
SUMOTime myCurrentDurationIncrement
A value for enlarge the current duration.
Definition: MSTrafficLightLogic.h:426
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:70
MSTrafficLightLogic::SwitchCommand::myTLControl
MSTLLogicControl & myTLControl
The responsible traffic lights control.
Definition: MSTrafficLightLogic.h:388
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:100
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:63
Parameterised.h
MSTrafficLightLogic::deactivateProgram
virtual void deactivateProgram()
Definition: MSTrafficLightLogic.cpp:384
MSTrafficLightLogic::myAmActive
bool myAmActive
whether the current program is active
Definition: MSTrafficLightLogic.h:441
MSTrafficLightLogic::getDefaultCycleTime
SUMOTime getDefaultCycleTime() const
Returns the cycle time (in ms)
Definition: MSTrafficLightLogic.h:270
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:55
MSTrafficLightLogic::SwitchCommand::getNextSwitchTime
SUMOTime getNextSwitchTime() const
Returns the assumed next switch time.
Definition: MSTrafficLightLogic.h:381
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:411
MSTrafficLightLogic::myLogicType
const TrafficLightType myLogicType
The type of the logic.
Definition: MSTrafficLightLogic.h:414
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:182
MSTrafficLightLogic::getLinks
const LinkVectorVector & getLinks() const
Returns the list of lists of all affected links.
Definition: MSTrafficLightLogic.h:203
MSTrafficLightLogic::myIgnoredIndices
std::set< int > myIgnoredIndices
list of indices that are ignored in mesoscopic simulatino
Definition: MSTrafficLightLogic.h:438
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:435
Command
Base (microsim) event class.
Definition: Command.h:52
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:73
config.h
MSTrafficLightLogic::myDefaultCycleTime
SUMOTime myDefaultCycleTime
The cycle time (without changes)
Definition: MSTrafficLightLogic.h:432
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:59
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:51
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:67
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:64
NLDetectorBuilder
Builds detectors for microsim.
Definition: NLDetectorBuilder.h:55
MSTrafficLightLogic::SwitchCommand::myTLLogic
MSTrafficLightLogic * myTLLogic
The logic to be executed on a switch.
Definition: MSTrafficLightLogic.h:391
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:191