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-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>
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 SUMOTime getMinDur(int step = -1) const;
272 virtual SUMOTime getMaxDur(int step = -1) const;
273 virtual SUMOTime getEarliestEnd(int step = -1) const;
274 virtual SUMOTime getLatestEnd(int step = -1) const;
275
282
284 SUMOTime getTimeInCycle() const;
285
287 virtual SUMOTime mapTimeInCycle(SUMOTime t) const;
288
290 int getNumLinks() const {
291 return myNumLinks;
292 }
293
300
301
306 SUMOTime getSpentDuration(SUMOTime simStep = -1) const;
308
309
310
313
317 virtual SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const = 0;
318
319
324 virtual SUMOTime getOffsetFromIndex(int index) const = 0;
325
326
331 virtual int getIndexFromOffset(SUMOTime offset) const = 0;
333
334
336 return myOffset;
337 }
338
341
345 void addOverridingDuration(SUMOTime duration);
346
347
352
353
361 SUMOTime simStep, int step, SUMOTime stepDuration) = 0;
362
364
366 bool isSelected() const;
367
369 bool isActive() const {
370 return myAmActive;
371 }
372
374 virtual bool getsMajorGreen(int linkIndex) const;
375
376
378 virtual std::map<std::string, double> getDetectorStates() const {
379 return std::map<std::string, double>();
380 }
381
383 virtual double getDetectorState(const std::string) const {
384 return 0.0;
385 }
386
388 virtual double getTLQueueLength(const std::string) const {
389 return 0.0;
390 }
391
393 virtual std::map<std::string, double> getConditions() const {
394 return std::map<std::string, double>();
395 }
396
398 virtual VehicleVector getBlockingVehicles(int linkIndex) {
399 UNUSED_PARAMETER(linkIndex);
400 return VehicleVector();
401 }
402
404 virtual VehicleVector getRivalVehicles(int linkIndex) {
405 UNUSED_PARAMETER(linkIndex);
406 return VehicleVector();
407 }
408
410 virtual VehicleVector getPriorityVehicles(int linkIndex) {
411 UNUSED_PARAMETER(linkIndex);
412 return VehicleVector();
413 }
414
416 virtual std::vector<const MSDriveWay*> getBlockingDriveWays(int linkIndex) {
417 UNUSED_PARAMETER(linkIndex);
418 return std::vector<const MSDriveWay*>();
419 }
420
422 virtual std::string getRequestedDriveWay(int linkIndex) {
423 UNUSED_PARAMETER(linkIndex);
424 return "";
425 }
426
429 virtual void saveState(OutputDevice& /*out*/) const {};
430
431
433 virtual void loadState(MSTLLogicControl& tlcontrol, SUMOTime t, int step, SUMOTime spentDuration);
434
435
436protected:
441 class SwitchCommand : public Command {
442 public:
449 MSTrafficLightLogic* tlLogic,
450 SUMOTime nextSwitch);
451
454
459 SUMOTime execute(SUMOTime currentTime);
460
461
465 void deschedule(MSTrafficLightLogic* tlLogic);
466
467
472 return myAssumedNextSwitch;
473 }
474
486 SUMOTime shiftTime(SUMOTime currentTime, SUMOTime execTime, SUMOTime newTime);
487
488 private:
491
494
497
500
501 private:
504
507
508 };
509
510 SUMOTime computeCycleTime(const Phases& phases);
511
512
513protected:
515 const std::string myProgramID;
516
519
522
525
528
531
533 std::vector<SUMOTime> myOverridingTimes;
534
537
540
543
546
548 std::set<int> myIgnoredIndices;
549
552
553private:
556
559
560};
long long int SUMOTime
Definition GUI.h:36
#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 & 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 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
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 loadState(MSTLLogicControl &tlcontrol, SUMOTime t, int step, SUMOTime spentDuration)
restores the tls state
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