Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
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-2026 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>
33#include <microsim/MSLink.h>
34#include "MSPhaseDefinition.h"
35
36
37// ===========================================================================
38// class declarations
39// ===========================================================================
40class MSNet;
41class MSLink;
45class MSDriveWay;
46
47
48// ===========================================================================
49// class definitions
50// ===========================================================================
55class MSTrafficLightLogic : public Named, public Parameterised {
56public:
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;
74
76 typedef std::vector<const SUMOVehicle*> VehicleVector;
78
79
80public:
91 const std::string& id,
92 const std::string& programID,
93 const SUMOTime offset,
94 const TrafficLightType logicType,
95 const SUMOTime delay,
96 const Parameterised::Map& parameters);
97
98
103 virtual void init(NLDetectorBuilder& nb);
104
107
109 virtual ~MSTrafficLightLogic();
110
111
112
115
121 virtual void addLink(MSLink* link, MSLane* lane, int pos);
122
124 void ignoreLinkIndex(int pos);
125
126
134 virtual void adaptLinkInformationFrom(const MSTrafficLightLogic& logic);
135
136
140 std::map<MSLink*, LinkState> collectLinkStates() const;
141
142
146 void resetLinkStates(const std::map<MSLink*, LinkState>& vals) const;
148
149
150
153
157 virtual SUMOTime trySwitch() = 0;
158
160 virtual void activateProgram();
161 virtual void deactivateProgram();
162
169 bool setTrafficLightSignals(SUMOTime t) const;
171
172
175
179 const std::string& getProgramID() const {
180 return myProgramID;
181 }
182
183
188 return myLanes;
189 }
190
191
196 const LaneVector& getLanesAt(int i) const {
197 if (i < (int)myLanes.size()) {
198 return myLanes[i];
199 } else {
200 return myEmptyLaneVector;
201 }
202 }
203
204
208 const LinkVectorVector& getLinks() const {
209 return myLinks;
210 }
211
212
217 const LinkVector& getLinksAt(int i) const {
218 return myLinks[i];
219 }
220
221
226 int getLinkIndex(const MSLink* const link) const;
227
228
232 virtual int getPhaseNumber() const = 0;
233
234
238 virtual const Phases& getPhases() const = 0;
239
240
245 virtual const MSPhaseDefinition& getPhase(int givenstep) const = 0;
246
251 return myLogicType;
252 }
254
255
256
259
263 virtual int getCurrentPhaseIndex() const = 0;
264
265
269 virtual const MSPhaseDefinition& getCurrentPhaseDef() const = 0;
270
271 virtual void resetLastSwitch(SUMOTime t) = 0;
272
273 virtual SUMOTime getMinDur(int step = -1) const;
274 virtual SUMOTime getMaxDur(int step = -1) const;
275 virtual SUMOTime getEarliestEnd(int step = -1) const;
276 virtual SUMOTime getLatestEnd(int step = -1) const;
277
284
286 SUMOTime getTimeInCycle() const;
287
289 virtual SUMOTime mapTimeInCycle(SUMOTime t) const;
290
292 int getNumLinks() const {
293 return myNumLinks;
294 }
295
302
303
308 SUMOTime getSpentDuration(SUMOTime simStep = -1) const;
310
311
312
315
319 virtual SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const = 0;
320
321
326 virtual SUMOTime getOffsetFromIndex(int index) const = 0;
327
328
333 virtual int getIndexFromOffset(SUMOTime offset) const = 0;
335
336
338 return myOffset;
339 }
340
343
347 void addOverridingDuration(SUMOTime duration);
348
349
354
355
363 SUMOTime simStep, int step, SUMOTime stepDuration) = 0;
364
366
368 bool isSelected() const;
369
371 bool isActive() const {
372 return myAmActive;
373 }
374
376 virtual bool getsMajorGreen(int linkIndex) const;
377
378
380 virtual std::map<std::string, double> getDetectorStates() const {
381 return std::map<std::string, double>();
382 }
383
385 virtual double getDetectorState(const std::string) const {
386 return 0.0;
387 }
388
390 virtual double getTLQueueLength(const std::string) const {
391 return 0.0;
392 }
393
395 virtual std::map<std::string, double> getConditions() const {
396 return std::map<std::string, double>();
397 }
398
400 virtual VehicleVector getBlockingVehicles(int linkIndex) {
401 UNUSED_PARAMETER(linkIndex);
402 return VehicleVector();
403 }
404
406 virtual VehicleVector getRivalVehicles(int linkIndex) {
407 UNUSED_PARAMETER(linkIndex);
408 return VehicleVector();
409 }
410
412 virtual VehicleVector getPriorityVehicles(int linkIndex) {
413 UNUSED_PARAMETER(linkIndex);
414 return VehicleVector();
415 }
416
418 virtual std::vector<const MSDriveWay*> getBlockingDriveWays(int linkIndex) {
419 UNUSED_PARAMETER(linkIndex);
420 return std::vector<const MSDriveWay*>();
421 }
422
424 virtual std::string getRequestedDriveWay(int linkIndex) {
425 UNUSED_PARAMETER(linkIndex);
426 return "";
427 }
428
431 virtual void saveState(OutputDevice& /*out*/) const {};
432
433
435 virtual void loadState(MSTLLogicControl& tlcontrol, SUMOTime t, int step, SUMOTime spentDuration, bool active);
436
437
438protected:
443 class SwitchCommand : public Command {
444 public:
451 MSTrafficLightLogic* tlLogic,
452 SUMOTime nextSwitch);
453
456
461 SUMOTime execute(SUMOTime currentTime);
462
463
467 void deschedule(MSTrafficLightLogic* tlLogic);
468
469
474 return myAssumedNextSwitch;
475 }
476
488 SUMOTime shiftTime(SUMOTime currentTime, SUMOTime execTime, SUMOTime newTime);
489
490 private:
493
496
499
502
503 private:
506
509
510 };
511
512 SUMOTime computeCycleTime(const Phases& phases);
513
514
515protected:
517 const std::string myProgramID;
518
521
524
527
530
533
535 std::vector<SUMOTime> myOverridingTimes;
536
539
542
545
548
550 std::set<int> myIgnoredIndices;
551
554
555private:
558
561
562};
long long int SUMOTime
Definition GUI.h:36
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 & operator=(const SwitchCommand &)
Invalidated assignment operator.
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.
SUMOTime myAssumedNextSwitch
Assumed switch time (may change in case of adaptive traffic lights)
The parent class for traffic light logics.
const LinkVectorVector & getLinks() const
Returns the list of lists of all affected links.
virtual std::string getRequestedDriveWay(int linkIndex)
return vehicles that approach the intersection/rail signal and have priority over vehicles that wish ...
virtual void adaptLinkInformationFrom(const MSTrafficLightLogic &logic)
Applies information about controlled links and lanes from the given logic.
std::vector< LaneVector > LaneVectorVector
Definition of a list that holds lists of lanes that do have the same attribute.
virtual const MSPhaseDefinition & getCurrentPhaseDef() const =0
Returns the definition of the current phase.
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.
SUMOTime computeCycleTime(const Phases &phases)
const LaneVector & getLanesAt(int i) const
Returns the list of lanes that are controlled by the signals at the given position.
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 void resetLastSwitch(SUMOTime t)=0
virtual SUMOTime getLatestEnd(int step=-1) const
virtual SUMOTime getOffsetFromIndex(int index) const =0
Returns the position (start of a phase during a cycle) from of a given step.
virtual double getTLQueueLength(const std::string) const
return the estimated queue length at the upcoming traffic light
MSTrafficLightLogic & operator=(const MSTrafficLightLogic &s)
invalidated assignment operator
virtual void loadState(MSTLLogicControl &tlcontrol, SUMOTime t, int step, SUMOTime spentDuration, bool active)
restores the tls state
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.
virtual std::map< std::string, double > getConditions() const
return all named conditions defined for this traffic light
SUMOTime getOffset() const
MSTrafficLightLogic(const MSTrafficLightLogic &s)
invalidated copy constructor
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
virtual const MSPhaseDefinition & getPhase(int givenstep) const =0
Returns the definition of the phase from the given position within the plan.
SUMOTime myDefaultCycleTime
The cycle time (without changes)
virtual std::vector< const MSDriveWay * > getBlockingDriveWays(int linkIndex)
return vehicles that approach the intersection/rail signal and have priority over vehicles that wish ...
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 & getProgramID() const
Returns this tl-logic's id.
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
virtual std::map< std::string, double > getDetectorStates() const
return activation state of all detectors that affect this traffic light
int getNumLinks() const
return the number of controlled link indices
virtual const Phases & getPhases() const =0
Returns the phases of this tls program.
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 activateProgram()
called when switching programs
const LaneVectorVector & getLaneVectors() const
Returns the list of lists of all lanes controlled by this tls.
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.
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
virtual double getDetectorState(const std::string) const
return activation state of a specific detector that affect this traffic light
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.
const LinkVector & getLinksAt(int i) const
Returns the list of links that are controlled by the signals at the given position.
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 void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
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.
An upper class for objects with additional parameters.
std::map< std::string, std::string > Map
parameters map
#define UNUSED_PARAMETER(x)