Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSTLLogicControl.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// A class that stores and controls tls and switching of their programs
23/****************************************************************************/
24#pragma once
25#include <config.h>
26
27#include <vector>
28#include <map>
32
33
34// ===========================================================================
35// class declarations
36// ===========================================================================
38class MSLink;
39class MSLane;
41class OutputDevice;
42
43
44// ===========================================================================
45// class definitions
46// ===========================================================================
60public:
66 public:
68 virtual ~OnSwitchAction() {};
69
70
73 virtual void execute() = 0;
74
75 };
76
77
78
86 public:
89
92
99 bool checkOriginalTLS() const;
100
108 bool addLogic(const std::string& programID, MSTrafficLightLogic* logic, bool netWasLoaded,
109 bool isNewDefault = true);
110
111 MSTrafficLightLogic* getLogic(const std::string& programID) const;
113 std::vector<MSTrafficLightLogic*> getAllLogics() const;
114 void saveInitialStates();
115 void saveState(OutputDevice& out);
116 bool isActive(const MSTrafficLightLogic* tl) const;
118
121
122 void switchTo(MSTLLogicControl& tlc, const std::string& programID);
123
126
127 /* @brief get logic by programID. For the special case "off"
128 * instantiate an MSOffTrafficLightLogic */
130 const std::string& programID);
131
132 /* @brief sets the state to the given string get for the special program "online"
133 * this program is instantiated only once */
135 const std::string& state);
136
137
138 void executeOnSwitchActions() const;
139 void addLink(MSLink* link, MSLane* lane, int pos);
140 void ignoreLinkIndex(int pos);
141
142 private:
145
148
150 std::map<std::string, MSTrafficLightLogic*> myVariants;
151
153 std::map<MSLink*, LinkState> myOriginalLinkStates;
154
156 std::vector<OnSwitchAction*> mySwitchActions;
157
158 private:
161
164
165
166 };
167
168
171
174
192 bool closeNetworkReading();
193
200 void setTrafficLightSignals(SUMOTime t) const;
201
207 std::vector<MSTrafficLightLogic*> getAllLogics() const;
208
215 TLSLogicVariants& get(const std::string& id) const;
216
223 MSTrafficLightLogic* get(const std::string& id, const std::string& programID) const;
224
230 MSTrafficLightLogic* getActive(const std::string& id) const;
231
237 std::vector<std::string> getAllTLIds() const;
238
260 bool add(const std::string& id, const std::string& programID,
261 MSTrafficLightLogic* logic, bool newDefault = true);
262
267 bool knows(const std::string& id) const;
268
273 bool isActive(const MSTrafficLightLogic* tl) const;
274
283 void switchTo(const std::string& id, const std::string& programID);
284
287
297 void addWAUT(SUMOTime refTime, const std::string& id,
298 const std::string& startProg, SUMOTime period);
299
300
309 void addWAUTSwitch(const std::string& wautid, SUMOTime when,
310 const std::string& to);
311
312
324 void addWAUTJunction(const std::string& wautid, const std::string& tls,
325 const std::string& proc, bool synchron);
326
327
338 void closeWAUT(const std::string& wautid);
340
341
342
347 void check2Switch(SUMOTime step);
348
349
357 std::pair<SUMOTime, MSPhaseDefinition> getPhaseDef(const std::string& tlid) const;
358
360 void switchOffAll();
361
364 void saveState(OutputDevice& out);
365
367 void loadWAUTSwitch(const std::string& wautID, const std::string& junctionID, const std::string& to);
368
370 void clearState(SUMOTime time, bool quickReload = false);
371
372
373
374protected:
382 class SwitchInitCommand : public Command {
383 public:
389 SwitchInitCommand(MSTLLogicControl& p, const std::string& wautid, int index)
390 : myParent(p), myWAUTID(wautid), myIndex(index) { }
391
392
395
396
397
400
416 return myParent.initWautSwitch(*this);
417 }
419
420
421
425 const std::string& getWAUTID() const {
426 return myWAUTID;
427 }
428
429
433 int& getIndex() {
434 return myIndex;
435 }
436
437
438 protected:
441
443 std::string myWAUTID;
444
447
448
449 private:
452
455
456 };
457
458
459
460public:
468
469
470protected:
474 struct WAUTSwitch {
478 std::string to;
479 };
480
481
487 std::string junction;
489 std::string procedure;
492 };
493
494
498 struct WAUT {
500 std::string id;
502 std::string startProg;
508 std::vector<WAUTSwitch> switches;
510 std::vector<WAUTJunction> junctions;
511 };
512
513
518 public:
528 bool synchron)
529 : myFrom(from), myTo(to), mySwitchSynchron(synchron), myWAUT(waut), myControl(control) { }
530
531
534
535
540 virtual bool trySwitch(SUMOTime step);
541
543 const std::string& getWAUTID() const {
544 return myWAUT.id;
545 }
546
547
548 protected:
559 bool isPosAtGSP(SUMOTime currentTime, const MSTrafficLightLogic& logic);
560
567
573 void switchToPos(SUMOTime simStep, MSTrafficLightLogic& logic, SUMOTime toTime);
574
582 SUMOTime getGSPTime(const MSTrafficLightLogic& logic) const;
583
589 virtual void adaptLogic(SUMOTime step) {
590 UNUSED_PARAMETER(step);
591 }
592
593 protected:
596
599
602
605
608
609
610 private:
613
616
617 };
618
619
625 public:
635 bool synchron);
636
639
644 bool trySwitch(SUMOTime step);
645
646 };
647
648
649
655 public:
665 bool synchron);
666
669
670 protected:
673 void adaptLogic(SUMOTime step);
674
675 };
676
677
683 public:
693 bool synchron);
694
697
698 protected:
704 void adaptLogic(SUMOTime step);
705
711 void stretchLogic(SUMOTime step, SUMOTime startPos, SUMOTime allStretchTime);
712
718 void cutLogic(SUMOTime step, SUMOTime startPos, SUMOTime allCutTime);
719
720 protected:
732
733 protected:
735 std::vector<StretchRange> myStretchRanges;
736 };
737
738
753
754
756 void initSwitchProcess(WAUT& waut, const WAUTJunction& junction, const std::string& to);
757
759 std::map<std::string, WAUT*> myWAUTs;
760
762 std::vector<WAUTSwitchProcess> myCurrentlySwitched;
763
765 std::map<std::string, TLSLogicVariants*> myLogics;
766
769
770
771private:
774
777
778};
long long int SUMOTime
Definition GUI.h:36
Base (microsim) event class.
Definition Command.h:50
Representation of a lane in the micro simulation.
Definition MSLane.h:84
The definition of a single phase of a tls logic.
Base class for things to execute if a tls switches to a new phase.
virtual void execute()=0
Executes the action.
virtual ~OnSwitchAction()
Destructor.
This event-class is used to initialise a WAUT switch at a certain time.
MSTLLogicControl & myParent
The control to call.
const std::string & getWAUTID() const
Returns the WAUT-id.
SwitchInitCommand & operator=(const SwitchInitCommand &)
Invalidated assignment operator.
int myIndex
The current index within the WAUT switch table.
SwitchInitCommand(MSTLLogicControl &p, const std::string &wautid, int index)
Constructor.
int & getIndex()
Returns a reference to the index.
SwitchInitCommand(const SwitchInitCommand &)
Invalidated copy constructor.
SUMOTime execute(SUMOTime)
Begins a WAUT switch by executing the command.
std::string myWAUTID
The id of the WAUT that shall switch.
Storage for all programs of a single tls.
void addLink(MSLink *link, MSLane *lane, int pos)
void switchTo(MSTLLogicControl &tlc, const std::string &programID)
void addSwitchCommand(OnSwitchAction *c)
TLSLogicVariants & operator=(const TLSLogicVariants &)
Invalidated assignment operator.
void setStateInstantiatingOnline(MSTLLogicControl &tlc, const std::string &state)
bool checkOriginalTLS() const
Verifies traffic lights loaded from the network.
std::vector< OnSwitchAction * > mySwitchActions
The list of actions/commands to execute on switch.
std::map< MSLink *, LinkState > myOriginalLinkStates
Originally loaded link states.
TLSLogicVariants(const TLSLogicVariants &)
Invalidated copy constructor.
std::vector< MSTrafficLightLogic * > getAllLogics() const
MSTrafficLightLogic * getLogic(const std::string &programID) const
std::map< std::string, MSTrafficLightLogic * > myVariants
A map of subkeys to programs.
MSTrafficLightLogic * myDefaultProgram
The program that would be used in the absence of TraCI.
bool addLogic(const std::string &programID, MSTrafficLightLogic *logic, bool netWasLoaded, bool isNewDefault=true)
Adds a logic (program). In case of an error the logic gets deleted.
void switchToLoaded(MSTrafficLightLogic *tl)
set current program upon loading saved state
MSTrafficLightLogic * myCurrentProgram
The currently used program.
MSTrafficLightLogic * getActive() const
MSTrafficLightLogic * getDefault() const
return the default program (that last used program except TRACI_PROGRAM)
MSTrafficLightLogic * getLogicInstantiatingOff(MSTLLogicControl &tlc, const std::string &programID)
bool isActive(const MSTrafficLightLogic *tl) const
This class switches using the GSP algorithm.
void adaptLogic(SUMOTime step)
Stretches the destination program's phase to which the tls was switched.
This class simply switches to the next program.
bool trySwitch(SUMOTime step)
Determines whether a switch is possible.
This class switches using the Stretch algorithm.
void adaptLogic(SUMOTime step)
Determines the destination program's changes and applies them.
std::vector< StretchRange > myStretchRanges
the given Stretch-areas for the "to" program, this is 0-based indexed, while the input is 1-based
void cutLogic(SUMOTime step, SUMOTime startPos, SUMOTime allCutTime)
Cuts the logic to synchronize.
void stretchLogic(SUMOTime step, SUMOTime startPos, SUMOTime allStretchTime)
Stretches the logic to synchronize.
This is the abstract base class for switching from one tls program to another.
virtual bool trySwitch(SUMOTime step)
Determines whether a switch is possible.
MSTrafficLightLogic * myTo
The program to switch the tls to.
WAUTSwitchProcedure(const WAUTSwitchProcedure &)
Invalidated copy constructor.
bool isPosAtGSP(SUMOTime currentTime, const MSTrafficLightLogic &logic)
Checks, whether the position of a signal program is at the GSP ("Good Switching Point")
bool mySwitchSynchron
Information whether to switch synchron (?)
const std::string & getWAUTID() const
Returns the id of the WAUT responsible for switching.
WAUTSwitchProcedure & operator=(const WAUTSwitchProcedure &)
Invalidated assignment operator.
WAUTSwitchProcedure(MSTLLogicControl &control, WAUT &waut, MSTrafficLightLogic *from, MSTrafficLightLogic *to, bool synchron)
Constructor.
MSTLLogicControl & myControl
The control the logic belongs to.
MSTrafficLightLogic * myFrom
The current program of the tls to switch.
SUMOTime getGSPTime(const MSTrafficLightLogic &logic) const
Returns the GSP-value.
SUMOTime getDiffToStartOfPhase(MSTrafficLightLogic &logic, SUMOTime toTime)
Returns the difference between a given time and the start of the phase.
virtual void adaptLogic(SUMOTime step)
Changes the destination program's phase to which the tls was switched.
void switchToPos(SUMOTime simStep, MSTrafficLightLogic &logic, SUMOTime toTime)
switches the given logic directly to the given position
WAUT & myWAUT
The WAUT responsible for switching.
A class that stores and controls tls and switching of their programs.
void addWAUTJunction(const std::string &wautid, const std::string &tls, const std::string &proc, bool synchron)
Adds a tls to the list of tls to be switched by the named WAUT.
MSTLLogicControl & operator=(const MSTLLogicControl &)
Invalidated assignment operator.
void clearState(SUMOTime time, bool quickReload=false)
Clear all tls states before quick-loading state.
std::vector< MSTrafficLightLogic * > getAllLogics() const
Returns a vector which contains all logics.
std::vector< WAUTSwitchProcess > myCurrentlySwitched
A list of currently running switching procedures.
std::pair< SUMOTime, MSPhaseDefinition > getPhaseDef(const std::string &tlid) const
return the complete phase definition for a named traffic lights logic
std::map< std::string, TLSLogicVariants * > myLogics
A map from ids to the corresponding variants.
void addWAUT(SUMOTime refTime, const std::string &id, const std::string &startProg, SUMOTime period)
Adds a WAUT definition.
std::vector< std::string > getAllTLIds() const
void switchTo(const std::string &id, const std::string &programID)
Switches the named (id) tls to the named (programID) program.
MSTrafficLightLogic * getActive(const std::string &id) const
Returns the active program of a named tls.
bool closeNetworkReading()
Lets MSTLLogicControl know that the network has been loaded.
void setTrafficLightSignals(SUMOTime t) const
Lets all running (current) tls programs apply their current signal states to links they control.
bool knows(const std::string &id) const
Returns the information whether the named tls is stored.
MSTLLogicControl(const MSTLLogicControl &)
Invalidated copy constructor.
void saveState(OutputDevice &out)
Saves the current tls states into the given stream.
bool myNetWasLoaded
Information whether the net was completely loaded.
void switchOffAll()
switch all logic variants to 'off'
void addWAUTSwitch(const std::string &wautid, SUMOTime when, const std::string &to)
Adds a WAUT switch step to a previously built WAUT.
void loadWAUTSwitch(const std::string &wautID, const std::string &junctionID, const std::string &to)
Restores a WAUT switch that waited for its switching point when the state was saved.
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
MSTLLogicControl()
Constructor.
SUMOTime initWautSwitch(SwitchInitCommand &cmd)
Initialises switching a WAUT.
void initSwitchProcess(WAUT &waut, const WAUTJunction &junction, const std::string &to)
Starts switching a junction of the given WAUT to the given program.
~MSTLLogicControl()
Destructor.
void check2Switch(SUMOTime step)
Checks whether any WAUT is trying to switch a tls into another program.
std::map< std::string, WAUT * > myWAUTs
A map of ids to corresponding WAUTs.
bool isActive(const MSTrafficLightLogic *tl) const
Returns whether the given tls program is the currently active for his tls.
bool add(const std::string &id, const std::string &programID, MSTrafficLightLogic *logic, bool newDefault=true)
Adds a tls program to the container.
void closeWAUT(const std::string &wautid)
Closes loading of a WAUT.
The parent class for traffic light logics.
Static storage of an output device and its base (abstract) implementation.
#define UNUSED_PARAMETER(x)
std::string startProg
The name of the start program.
std::vector< WAUTSwitch > switches
The list of switches to be done by the WAUT.
std::vector< WAUTJunction > junctions
The list of switches assigned to the WAUT.
SUMOTime period
The period with which to repeat switches.
std::string id
The id of the WAUT.
SUMOTime refTime
The reference time (offset to the switch times)
Storage for a junction assigned to a WAUT.
std::string procedure
The procedure to switch the junction with.
bool synchron
Information whether this junction shall be switched synchron.
std::string junction
The junction name.
Storage for a WAUTs switch point.
SUMOTime when
The time the WAUT shall switch the TLS.
std::string to
The program name the WAUT shall switch the TLS to.
double fac
The weight factor of a stretch/cut area.
An initialised switch process.
MSTrafficLightLogic * to
The program to switch the tls to.
std::string junction
The id of the junction to switch.
MSTrafficLightLogic * from
The current program of the tls.
WAUTSwitchProcedure * proc
The used procedure.