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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
22 // The parent class for traffic light logics
23 /****************************************************************************/
24 #pragma once
25 #include <config.h>
26 
27 #include <map>
28 #include <string>
29 #include <bitset>
30 #include <utils/common/Command.h>
33 #include <microsim/MSLink.h>
34 #include "MSPhaseDefinition.h"
35 
36 
37 // ===========================================================================
38 // class declarations
39 // ===========================================================================
40 class MSNet;
41 class MSLink;
42 class MSTLLogicControl;
43 class NLDetectorBuilder;
44 
45 
46 // ===========================================================================
47 // class definitions
48 // ===========================================================================
53 class MSTrafficLightLogic : public Named, public Parameterised {
54 public:
57 
59  typedef std::vector<MSPhaseDefinition*> Phases;
60 
62  typedef std::vector<MSLink*> LinkVector;
63 
65  typedef std::vector<LinkVector> LinkVectorVector;
66 
68  typedef std::vector<MSLane*> LaneVector;
69 
71  typedef std::vector<LaneVector> LaneVectorVector;
72 
74  typedef std::vector<const SUMOVehicle*> VehicleVector;
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 
99  virtual void init(NLDetectorBuilder& nb);
100 
102  void initMesoTLSPenalties();
103 
105  virtual ~MSTrafficLightLogic();
106 
107 
108 
111 
117  virtual void addLink(MSLink* link, MSLane* lane, int pos);
118 
120  void ignoreLinkIndex(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 
156  virtual void activateProgram();
157  virtual void deactivateProgram();
158 
165  bool setTrafficLightSignals(SUMOTime t) const;
167 
168 
169 
172 
176  const std::string& getProgramID() const {
177  return myProgramID;
178  }
179 
180 
185  return myLanes;
186  }
187 
188 
193  const LaneVector& getLanesAt(int i) const {
194  if (i < (int)myLanes.size()) {
195  return myLanes[i];
196  } else {
197  return myEmptyLaneVector;
198  }
199  }
200 
201 
205  const LinkVectorVector& getLinks() const {
206  return myLinks;
207  }
208 
209 
214  const LinkVector& getLinksAt(int i) const {
215  return myLinks[i];
216  }
217 
218 
223  int getLinkIndex(const MSLink* const link) const;
224 
225 
229  virtual int getPhaseNumber() const = 0;
230 
231 
235  virtual const Phases& getPhases() const = 0;
236 
237 
242  virtual const MSPhaseDefinition& getPhase(int givenstep) const = 0;
243 
248  return myLogicType;
249  }
251 
252 
253 
256 
260  virtual int getCurrentPhaseIndex() const = 0;
261 
262 
266  virtual const MSPhaseDefinition& getCurrentPhaseDef() const = 0;
267 
268 
273  return myDefaultCycleTime;
274  }
275 
277  int getNumLinks() {
278  return myNumLinks;
279  }
280 
286  SUMOTime getNextSwitchTime() const;
287 
288 
293  SUMOTime getSpentDuration(SUMOTime simStep = -1) const;
295 
296 
297 
300 
304  virtual SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const = 0;
305 
306 
311  virtual SUMOTime getOffsetFromIndex(int index) const = 0;
312 
313 
318  virtual int getIndexFromOffset(SUMOTime offset) const = 0;
320 
321 
322 
325 
329  void addOverridingDuration(SUMOTime duration);
330 
331 
336 
337 
344  virtual void changeStepAndDuration(MSTLLogicControl& tlcontrol,
345  SUMOTime simStep, int step, SUMOTime stepDuration) = 0;
346 
348 
350  bool isSelected() const;
351 
353  bool isActive() const {
354  return myAmActive;
355  }
356 
358  virtual VehicleVector getBlockingVehicles(int linkIndex) {
359  UNUSED_PARAMETER(linkIndex);
360  return VehicleVector();
361  }
362 
364  virtual VehicleVector getRivalVehicles(int linkIndex) {
365  UNUSED_PARAMETER(linkIndex);
366  return VehicleVector();
367  }
368 
370  virtual VehicleVector getPriorityVehicles(int linkIndex) {
371  UNUSED_PARAMETER(linkIndex);
372  return VehicleVector();
373  }
374 
375 protected:
380  class SwitchCommand : public Command {
381  public:
387  SwitchCommand(MSTLLogicControl& tlcontrol,
388  MSTrafficLightLogic* tlLogic,
389  SUMOTime nextSwitch);
390 
392  ~SwitchCommand();
393 
398  SUMOTime execute(SUMOTime currentTime);
399 
400 
404  void deschedule(MSTrafficLightLogic* tlLogic);
405 
406 
411  return myAssumedNextSwitch;
412  }
413 
425  SUMOTime shiftTime(SUMOTime currentTime, SUMOTime execTime, SUMOTime newTime);
426 
427  private:
430 
433 
436 
438  bool myAmValid;
439 
440  private:
443 
446 
447  };
448 
449 
450 protected:
452  const std::string myProgramID;
453 
456 
459 
462 
465 
467  std::vector<SUMOTime> myOverridingTimes;
468 
471 
474 
477 
480 
482  std::set<int> myIgnoredIndices;
483 
486 
487 private:
490 
493 
494 };
long long int SUMOTime
Definition: SUMOTime.h:31
TrafficLightType
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:29
Base (microsim) event class.
Definition: Command.h:49
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
The simulated network and simulation perfomer.
Definition: MSNet.h:89
The definition of a single phase of a tls logic.
A class that stores and controls tls and switching of their programs.
Class realising the switch between the traffic light phases.
void deschedule(MSTrafficLightLogic *tlLogic)
Marks this swicth as invalid (if the phase duration has changed, f.e.)
SUMOTime shiftTime(SUMOTime currentTime, SUMOTime execTime, SUMOTime newTime)
Reschedule or deschedule the command when quick-loading state.
SwitchCommand(MSTLLogicControl &tlcontrol, MSTrafficLightLogic *tlLogic, SUMOTime nextSwitch)
Constructor.
MSTLLogicControl & myTLControl
The responsible traffic lights control.
SUMOTime getNextSwitchTime() const
Returns the assumed next switch time.
MSTrafficLightLogic * myTLLogic
The logic to be executed on a switch.
bool myAmValid
Information whether this switch command is still valid.
SUMOTime execute(SUMOTime currentTime)
Executes the regarded junction's "trySwitch"- method.
SwitchCommand(const SwitchCommand &)
Invalidated copy constructor.
SwitchCommand & operator=(const SwitchCommand &)
Invalidated assignment operator.
SUMOTime myAssumedNextSwitch
Assumed switch time (may change in case of adaptive traffic lights)
The parent class for traffic light logics.
int getNumLinks()
return the number of controlled link indices
const LinkVector & getLinksAt(int i) const
Returns the list of links that are controlled by the signals at the given position.
virtual void adaptLinkInformationFrom(const MSTrafficLightLogic &logic)
Applies information about controlled links and lanes from the given logic.
virtual const MSPhaseDefinition & getCurrentPhaseDef() const =0
Returns the definition of the current phase.
MSTrafficLightLogic & operator=(const MSTrafficLightLogic &s)
invalidated assignment operator
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 int getPhaseNumber() const =0
Returns the number of phases.
int getLinkIndex(const MSLink *const link) const
Returns the index of the given link.
void setCurrentDurationIncrement(SUMOTime delay)
Delays current phase by the given delay.
virtual int getCurrentPhaseIndex() const =0
Returns the current index within the program.
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.
std::map< MSLink *, LinkState > collectLinkStates() const
Returns the (uncontrolled) states of the controlled links.
std::vector< MSLane * > LaneVector
Definition of the list of arrival lanes subjected to this tls.
virtual ~MSTrafficLightLogic()
Destructor.
virtual const MSPhaseDefinition & getPhase(int givenstep) const =0
Returns the definition of the phase from the given position within the plan.
virtual void deactivateProgram()
virtual SUMOTime getOffsetFromIndex(int index) const =0
Returns the position (start of a phase during a cycle) from of a given step.
const LaneVectorVector & getLaneVectors() const
Returns the list of lists of all lanes controlled by this tls.
std::vector< const SUMOVehicle * > VehicleVector
list of vehicles
std::vector< SUMOTime > myOverridingTimes
A list of duration overrides.
virtual VehicleVector getPriorityVehicles(int linkIndex)
return vehicles that approach the intersection/rail signal and have priority over vehicles that wish ...
void ignoreLinkIndex(int pos)
ignore pedestrian crossing index in mesosim
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.
MSTrafficLightLogic(const MSTrafficLightLogic &s)
invalidated copy constructor
const LinkVectorVector & getLinks() const
Returns the list of lists of all affected links.
const TrafficLightType myLogicType
The type of the logic.
virtual VehicleVector getBlockingVehicles(int linkIndex)
return vehicles that block the intersection/rail signal for vehicles that wish to pass the given link...
bool isSelected() const
whether this logic is selected in the GUI
SUMOTime myDefaultCycleTime
The cycle time (without changes)
virtual SUMOTime trySwitch()=0
Switches to the next phase.
virtual VehicleVector getRivalVehicles(int linkIndex)
return vehicles that approach the intersection/rail signal and are in conflict with vehicles that wis...
const std::string myProgramID
The id of the logic.
TrafficLightType getLogicType() const
Returns the type of the logic.
LaneVectorVector myLanes
The list of LaneVectors; each vector contains the incoming lanes that belong to the same link index.
SwitchCommand * mySwitchCommand
The current switch command.
int myNumLinks
number of controlled links
SUMOTime myCurrentDurationIncrement
A value for enlarge the current duration.
virtual int getIndexFromOffset(SUMOTime offset) const =0
Returns the step (the phasenumber) of a given position of the cycle.
SUMOTime getDefaultCycleTime() const
Returns the cycle time (in ms)
void resetLinkStates(const std::map< MSLink *, LinkState > &vals) const
Resets the states of controlled links.
virtual void activateProgram()
called when switching programs
bool isActive() const
whether this logic is the active program
void addOverridingDuration(SUMOTime duration)
Changes the duration of the next phase.
bool myAmActive
whether the current program is active
SUMOTime getNextSwitchTime() const
Returns the assumed next switch time.
std::set< int > myIgnoredIndices
list of indices that are ignored in mesoscopic simulatino
SUMOTime getSpentDuration(SUMOTime simStep=-1) const
Returns the duration spent in the current phase.
const LaneVector & getLanesAt(int i) const
Returns the list of lanes that are controlled by the signals at the given position.
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
virtual void addLink(MSLink *link, MSLane *lane, int pos)
Adds a link on building.
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
std::vector< LinkVector > LinkVectorVector
Definition of a list that holds lists of links that do have the same attribute.
std::vector< MSLink * > LinkVector
Definition of the list of links that are subjected to this tls.
LinkVectorVector myLinks
The list of LinkVectors; each vector contains the links that belong to the same link index.
void initMesoTLSPenalties()
initialize optional meso penalties
virtual const Phases & getPhases() const =0
Returns the phases of this tls program.
virtual void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
const std::string & getProgramID() const
Returns this tl-logic's id.
Builds detectors for microsim.
Base class for objects which have an id.
Definition: Named.h:53
An upper class for objects with additional parameters.
Definition: Parameterised.h:39