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-2022 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 std::map<std::string, std::string>& 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 
275  return myDefaultCycleTime;
276  }
277 
279  SUMOTime getTimeInCycle() const;
280 
282  virtual SUMOTime mapTimeInCycle(SUMOTime t) const;
283 
285  int getNumLinks() const {
286  return myNumLinks;
287  }
288 
294  SUMOTime getNextSwitchTime() const;
295 
296 
301  SUMOTime getSpentDuration(SUMOTime simStep = -1) const;
303 
304 
305 
308 
312  virtual SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const = 0;
313 
314 
319  virtual SUMOTime getOffsetFromIndex(int index) const = 0;
320 
321 
326  virtual int getIndexFromOffset(SUMOTime offset) const = 0;
328 
329 
330  SUMOTime getOffset() const {
331  return myOffset;
332  }
333 
336 
340  void addOverridingDuration(SUMOTime duration);
341 
342 
347 
348 
355  virtual void changeStepAndDuration(MSTLLogicControl& tlcontrol,
356  SUMOTime simStep, int step, SUMOTime stepDuration) = 0;
357 
359 
361  bool isSelected() const;
362 
364  bool isActive() const {
365  return myAmActive;
366  }
367 
369  virtual bool getsMajorGreen(int linkIndex) const;
370 
371 
373  virtual std::map<std::string, double> getDetectorStates() const {
374  return std::map<std::string, double>();
375  }
376 
378  virtual std::map<std::string, double> getConditions() const {
379  return std::map<std::string, double>();
380  }
381 
383  virtual VehicleVector getBlockingVehicles(int linkIndex) {
384  UNUSED_PARAMETER(linkIndex);
385  return VehicleVector();
386  }
387 
389  virtual VehicleVector getRivalVehicles(int linkIndex) {
390  UNUSED_PARAMETER(linkIndex);
391  return VehicleVector();
392  }
393 
395  virtual VehicleVector getPriorityVehicles(int linkIndex) {
396  UNUSED_PARAMETER(linkIndex);
397  return VehicleVector();
398  }
399 
402  virtual void saveState(OutputDevice& /*out*/) const {};
403 
404 
405 protected:
410  class SwitchCommand : public Command {
411  public:
417  SwitchCommand(MSTLLogicControl& tlcontrol,
418  MSTrafficLightLogic* tlLogic,
419  SUMOTime nextSwitch);
420 
422  ~SwitchCommand();
423 
428  SUMOTime execute(SUMOTime currentTime);
429 
430 
434  void deschedule(MSTrafficLightLogic* tlLogic);
435 
436 
441  return myAssumedNextSwitch;
442  }
443 
455  SUMOTime shiftTime(SUMOTime currentTime, SUMOTime execTime, SUMOTime newTime);
456 
457  private:
460 
463 
466 
468  bool myAmValid;
469 
470  private:
473 
476 
477  };
478 
479 
480 protected:
482  const std::string myProgramID;
483 
486 
489 
492 
495 
498 
500  std::vector<SUMOTime> myOverridingTimes;
501 
504 
507 
510 
513 
515  std::set<int> myIgnoredIndices;
516 
519 
520 private:
523 
526 
527 };
long long int SUMOTime
Definition: SUMOTime.h:32
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:82
The simulated network and simulation perfomer.
Definition: MSNet.h:88
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
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.
MSTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const SUMOTime offset, const TrafficLightType logicType, const SUMOTime delay, const std::map< std::string, std::string > &parameters)
Constructor.
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.
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.
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 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 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