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.dev/sumo
3 // Copyright (C) 2001-2024 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;
44 class NLDetectorBuilder;
45 
46 
47 // ===========================================================================
48 // class definitions
49 // ===========================================================================
54 class MSTrafficLightLogic : public Named, public Parameterised {
55 public:
58 
60  typedef std::vector<MSPhaseDefinition*> Phases;
61 
63  typedef std::vector<MSLink*> LinkVector;
64 
66  typedef std::vector<LinkVector> LinkVectorVector;
67 
69  typedef std::vector<MSLane*> LaneVector;
70 
72  typedef std::vector<LaneVector> LaneVectorVector;
73 
75  typedef std::vector<const SUMOVehicle*> VehicleVector;
77 
78 
79 public:
90  const std::string& id,
91  const std::string& programID,
92  const SUMOTime offset,
93  const TrafficLightType logicType,
94  const SUMOTime delay,
95  const Parameterised::Map& parameters);
96 
97 
102  virtual void init(NLDetectorBuilder& nb);
103 
105  void initMesoTLSPenalties();
106 
108  virtual ~MSTrafficLightLogic();
109 
110 
111 
114 
120  virtual void addLink(MSLink* link, MSLane* lane, int pos);
121 
123  void ignoreLinkIndex(int pos);
124 
125 
133  virtual void adaptLinkInformationFrom(const MSTrafficLightLogic& logic);
134 
135 
139  std::map<MSLink*, LinkState> collectLinkStates() const;
140 
141 
145  void resetLinkStates(const std::map<MSLink*, LinkState>& vals) const;
147 
148 
149 
152 
156  virtual SUMOTime trySwitch() = 0;
157 
159  virtual void activateProgram();
160  virtual void deactivateProgram();
161 
168  bool setTrafficLightSignals(SUMOTime t) const;
170 
171 
174 
178  const std::string& getProgramID() const {
179  return myProgramID;
180  }
181 
182 
187  return myLanes;
188  }
189 
190 
195  const LaneVector& getLanesAt(int i) const {
196  if (i < (int)myLanes.size()) {
197  return myLanes[i];
198  } else {
199  return myEmptyLaneVector;
200  }
201  }
202 
203 
207  const LinkVectorVector& getLinks() const {
208  return myLinks;
209  }
210 
211 
216  const LinkVector& getLinksAt(int i) const {
217  return myLinks[i];
218  }
219 
220 
225  int getLinkIndex(const MSLink* const link) const;
226 
227 
231  virtual int getPhaseNumber() const = 0;
232 
233 
237  virtual const Phases& getPhases() const = 0;
238 
239 
244  virtual const MSPhaseDefinition& getPhase(int givenstep) const = 0;
245 
250  return myLogicType;
251  }
253 
254 
255 
258 
262  virtual int getCurrentPhaseIndex() const = 0;
263 
264 
268  virtual const MSPhaseDefinition& getCurrentPhaseDef() const = 0;
269 
270  virtual SUMOTime getMinDur(int step = -1) const;
271  virtual SUMOTime getMaxDur(int step = -1) const;
272  virtual SUMOTime getEarliestEnd(int step = -1) const;
273  virtual SUMOTime getLatestEnd(int step = -1) const;
274 
279  return myDefaultCycleTime;
280  }
281 
283  SUMOTime getTimeInCycle() const;
284 
286  virtual SUMOTime mapTimeInCycle(SUMOTime t) const;
287 
289  int getNumLinks() const {
290  return myNumLinks;
291  }
292 
298  SUMOTime getNextSwitchTime() const;
299 
300 
305  SUMOTime getSpentDuration(SUMOTime simStep = -1) const;
307 
308 
309 
312 
316  virtual SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const = 0;
317 
318 
323  virtual SUMOTime getOffsetFromIndex(int index) const = 0;
324 
325 
330  virtual int getIndexFromOffset(SUMOTime offset) const = 0;
332 
333 
334  SUMOTime getOffset() const {
335  return myOffset;
336  }
337 
340 
344  void addOverridingDuration(SUMOTime duration);
345 
346 
351 
352 
359  virtual void changeStepAndDuration(MSTLLogicControl& tlcontrol,
360  SUMOTime simStep, int step, SUMOTime stepDuration) = 0;
361 
363 
365  bool isSelected() const;
366 
368  bool isActive() const {
369  return myAmActive;
370  }
371 
373  virtual bool getsMajorGreen(int linkIndex) const;
374 
375 
377  virtual std::map<std::string, double> getDetectorStates() const {
378  return std::map<std::string, double>();
379  }
380 
382  virtual std::map<std::string, double> getConditions() const {
383  return std::map<std::string, double>();
384  }
385 
387  virtual VehicleVector getBlockingVehicles(int linkIndex) {
388  UNUSED_PARAMETER(linkIndex);
389  return VehicleVector();
390  }
391 
393  virtual VehicleVector getRivalVehicles(int linkIndex) {
394  UNUSED_PARAMETER(linkIndex);
395  return VehicleVector();
396  }
397 
399  virtual VehicleVector getPriorityVehicles(int linkIndex) {
400  UNUSED_PARAMETER(linkIndex);
401  return VehicleVector();
402  }
403 
406  virtual void saveState(OutputDevice& /*out*/) const {};
407 
408 
410  virtual void loadState(MSTLLogicControl& tlcontrol, SUMOTime t, int step, SUMOTime spentDuration);
411 
412 
413 protected:
418  class SwitchCommand : public Command {
419  public:
425  SwitchCommand(MSTLLogicControl& tlcontrol,
426  MSTrafficLightLogic* tlLogic,
427  SUMOTime nextSwitch);
428 
430  ~SwitchCommand();
431 
436  SUMOTime execute(SUMOTime currentTime);
437 
438 
442  void deschedule(MSTrafficLightLogic* tlLogic);
443 
444 
449  return myAssumedNextSwitch;
450  }
451 
463  SUMOTime shiftTime(SUMOTime currentTime, SUMOTime execTime, SUMOTime newTime);
464 
465  private:
468 
471 
474 
476  bool myAmValid;
477 
478  private:
481 
484 
485  };
486 
487  SUMOTime computeCycleTime(const Phases& phases);
488 
489 
490 protected:
492  const std::string myProgramID;
493 
496 
499 
502 
505 
508 
510  std::vector<SUMOTime> myOverridingTimes;
511 
514 
517 
520 
523 
525  std::set<int> myIgnoredIndices;
526 
529 
530 private:
533 
536 
537 };
long long int SUMOTime
Definition: GUI.h:35
TrafficLightType
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:30
Base (microsim) event class.
Definition: Command.h:50
Base of value-generating classes (detectors)
Representation of a lane in the micro simulation.
Definition: MSLane.h:84
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.
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.
SUMOTime myOffset
the offset parameter of the current program
virtual int getPhaseNumber() const =0
Returns the number of phases.
int getLinkIndex(const MSLink *const link) const
Returns the index of the given link.
virtual SUMOTime mapTimeInCycle(SUMOTime t) const
map the given time into the current cycle
void setCurrentDurationIncrement(SUMOTime delay)
Delays current phase by the given delay.
virtual int getCurrentPhaseIndex() const =0
Returns the current index within the program.
virtual std::map< std::string, double > getDetectorStates() const
return activation state of all detectors that affect this traffic light
SUMOTime computeCycleTime(const Phases &phases)
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 std::map< std::string, double > getConditions() const
return all named conditions defined for this traffic light
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 SUMOTime getLatestEnd(int step=-1) const
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.
MSTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const SUMOTime offset, const TrafficLightType logicType, const SUMOTime delay, const Parameterised::Map &parameters)
Constructor.
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 ...
virtual SUMOTime getMinDur(int step=-1) const
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.
SUMOTime getOffset() const
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...
SUMOTime getTimeInCycle() const
return time within the current cycle
const std::string myProgramID
The id of the logic.
TrafficLightType getLogicType() const
Returns the type of the logic.
virtual SUMOTime getEarliestEnd(int step=-1) const
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
int getNumLinks() const
return the number of controlled link indices
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.
virtual SUMOTime getMaxDur(int step=-1) const
virtual bool getsMajorGreen(int linkIndex) const
whether the given link index ever turns 'G'
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 saveState(OutputDevice &) const
Saves the current tls states into the given stream.
virtual void loadState(MSTLLogicControl &tlcontrol, SUMOTime t, int step, SUMOTime spentDuration)
restores the tls state
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:54
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
An upper class for objects with additional parameters.
Definition: Parameterised.h:41
std::map< std::string, std::string > Map
parameters map
Definition: Parameterised.h:45