Line data Source code
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 : /****************************************************************************/
14 : /// @file MSTrafficLightLogic.h
15 : /// @author Daniel Krajzewicz
16 : /// @author Eric Nicolay
17 : /// @author Jakob Erdmann
18 : /// @author Michael Behrisch
19 : /// @author Friedemann Wesner
20 : /// @date Sept 2002
21 : ///
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>
31 : #include <utils/common/Parameterised.h>
32 : #include <microsim/MSLogicJunction.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;
43 : class MSDetectorFileOutput;
44 : class NLDetectorBuilder;
45 : class MSDriveWay;
46 :
47 :
48 : // ===========================================================================
49 : // class definitions
50 : // ===========================================================================
51 : /**
52 : * @class MSTrafficLightLogic
53 : * @brief The parent class for traffic light logics
54 : */
55 : class MSTrafficLightLogic : public Named, public Parameterised {
56 : public:
57 : /// @name Structure definitions
58 : /// @{
59 :
60 : /// @brief Definition of a list of phases, being the junction logic
61 : typedef std::vector<MSPhaseDefinition*> Phases;
62 :
63 : /// @brief Definition of the list of links that are subjected to this tls
64 : typedef std::vector<MSLink*> LinkVector;
65 :
66 : /// @brief Definition of a list that holds lists of links that do have the same attribute
67 : typedef std::vector<LinkVector> LinkVectorVector;
68 :
69 : /// @brief Definition of the list of arrival lanes subjected to this tls
70 : typedef std::vector<MSLane*> LaneVector;
71 :
72 : /// @brief Definition of a list that holds lists of lanes that do have the same attribute
73 : typedef std::vector<LaneVector> LaneVectorVector;
74 :
75 : /// @brief list of vehicles
76 : typedef std::vector<const SUMOVehicle*> VehicleVector;
77 : /// @}
78 :
79 :
80 : public:
81 : /** @brief Constructor
82 : * @param[in] tlcontrol The tls control responsible for this tls
83 : * @param[in] id This tls' id
84 : * @param[in] programID This tls' sub-id (program id)
85 : * @param[in] offset the time offset of the program
86 : * @param[in] logicType This tls' type (static, actuated etc.)
87 : * @param[in] delay The time to wait before the first switch
88 : * @param[in] parameters Additional parameters (especially for actuated logics)
89 : */
90 : MSTrafficLightLogic(MSTLLogicControl& tlcontrol,
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 :
99 : /** @brief Initialises the tls with information about incoming lanes
100 : * @param[in] nb The detector builder
101 : * @exception ProcessError If something fails on initialisation
102 : */
103 : virtual void init(NLDetectorBuilder& nb);
104 :
105 : /// @brief initialize optional meso penalties
106 : void initMesoTLSPenalties();
107 :
108 : /// @brief Destructor
109 : virtual ~MSTrafficLightLogic();
110 :
111 :
112 :
113 : /// @name Handling of controlled links
114 : /// @{
115 :
116 : /** @brief Adds a link on building
117 : * @param[in] link The controlled link
118 : * @param[in] lane The lane this link starts at
119 : * @param[in] pos The link's index (signal group) within this program
120 : */
121 : virtual void addLink(MSLink* link, MSLane* lane, int pos);
122 :
123 : /// @brief ignore pedestrian crossing index in mesosim
124 : void ignoreLinkIndex(int pos);
125 :
126 :
127 : /** @brief Applies information about controlled links and lanes from the given logic
128 : *
129 : * If we load a logic after the network has been loaded, we do not get the information
130 : * about controlled links afterwards. We have to copy them from a previously loaded logic.
131 : *
132 : * @param[in] logic The logic to use the information about controlled links/lanes from
133 : */
134 : virtual void adaptLinkInformationFrom(const MSTrafficLightLogic& logic);
135 :
136 :
137 : /** @brief Returns the (uncontrolled) states of the controlled links
138 : * @return The controlled link's states
139 : */
140 : std::map<MSLink*, LinkState> collectLinkStates() const;
141 :
142 :
143 : /** @brief Resets the states of controlled links
144 : * @param[in] vals The state of controlled links to use
145 : */
146 : void resetLinkStates(const std::map<MSLink*, LinkState>& vals) const;
147 : /// @}
148 :
149 :
150 :
151 : /// @name Switching and setting current rows
152 : /// @{
153 :
154 : /** @brief Switches to the next phase
155 : * @return The time of the next switch
156 : */
157 : virtual SUMOTime trySwitch() = 0;
158 :
159 : /// @brief called when switching programs
160 : virtual void activateProgram();
161 : virtual void deactivateProgram();
162 :
163 : /** @brief Applies the current signal states to controlled links
164 : * @param[in] t The current time
165 : * @return Always true
166 : * @see LinkState
167 : * @see MSLink::setTLState
168 : */
169 : bool setTrafficLightSignals(SUMOTime t) const;
170 : /// @}
171 :
172 :
173 : /// @name Static Information Retrieval
174 : /// @{
175 :
176 : /** @brief Returns this tl-logic's id
177 : * @return This program's id
178 : */
179 : const std::string& getProgramID() const {
180 3449042 : return myProgramID;
181 : }
182 :
183 :
184 : /** @brief Returns the list of lists of all lanes controlled by this tls
185 : * @return All lanes controlled by this tls, sorted by the signal index
186 : */
187 : const LaneVectorVector& getLaneVectors() const {
188 96 : return myLanes;
189 : }
190 :
191 :
192 : /** @brief Returns the list of lanes that are controlled by the signals at the given position
193 : * @param[in] i The index of the signal
194 : * @return The lanes controlled by the signal at the given index
195 : */
196 : const LaneVector& getLanesAt(int i) const {
197 1946738 : if (i < (int)myLanes.size()) {
198 1946298 : return myLanes[i];
199 : } else {
200 : return myEmptyLaneVector;
201 : }
202 : }
203 :
204 :
205 : /** @brief Returns the list of lists of all affected links
206 : * @return All links controlled by this tls, sorted by the signal index
207 : */
208 : const LinkVectorVector& getLinks() const {
209 112 : return myLinks;
210 : }
211 :
212 :
213 : /** @brief Returns the list of links that are controlled by the signals at the given position
214 : * @param[in] i The index of the signal
215 : * @return The links controlled by the signal at the given index
216 : */
217 : const LinkVector& getLinksAt(int i) const {
218 10738 : return myLinks[i];
219 : }
220 :
221 :
222 : /** @brief Returns the index of the given link
223 : * @param[in] link The link to retrieve the index for
224 : * @return The index of the given link (-1 if it is not controlled by this tls)
225 : */
226 : int getLinkIndex(const MSLink* const link) const;
227 :
228 :
229 : /** @brief Returns the number of phases
230 : * @return The number of this tls program's phases
231 : */
232 : virtual int getPhaseNumber() const = 0;
233 :
234 :
235 : /** @brief Returns the phases of this tls program
236 : * @return The phases of this tls program
237 : */
238 : virtual const Phases& getPhases() const = 0;
239 :
240 :
241 : /** @brief Returns the definition of the phase from the given position within the plan
242 : * @param[in] givenstep The index of the phase within the plan
243 : * @return The definition of the phase at the given position
244 : */
245 : virtual const MSPhaseDefinition& getPhase(int givenstep) const = 0;
246 :
247 : /** @brief Returns the type of the logic
248 : * @return The type of the logic
249 : */
250 : TrafficLightType getLogicType() const {
251 1032049 : return myLogicType;
252 : }
253 : /// @}
254 :
255 :
256 :
257 : /// @name Dynamic Information Retrieval
258 : /// @{
259 :
260 : /** @brief Returns the current index within the program
261 : * @return The index of the current phase within the tls
262 : */
263 : virtual int getCurrentPhaseIndex() const = 0;
264 :
265 :
266 : /** @brief Returns the definition of the current phase
267 : * @return The current phase
268 : */
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 :
276 : /** @brief Returns the cycle time (in ms)
277 : * @return The (maybe changing) cycle time of this tls
278 : */
279 : SUMOTime getDefaultCycleTime() const {
280 418 : return myDefaultCycleTime;
281 : }
282 :
283 : /// @brief return time within the current cycle
284 : SUMOTime getTimeInCycle() const;
285 :
286 : /// @brief map the given time into the current cycle
287 : virtual SUMOTime mapTimeInCycle(SUMOTime t) const;
288 :
289 : /// @brief return the number of controlled link indices
290 : int getNumLinks() const {
291 42962 : return myNumLinks;
292 : }
293 :
294 : /** @brief Returns the assumed next switch time
295 : *
296 : * The time may change in case of adaptive/actuated traffic lights.
297 : * @return The assumed next switch time (simulation time)
298 : */
299 : SUMOTime getNextSwitchTime() const;
300 :
301 :
302 : /** @brief Returns the duration spent in the current phase
303 : *
304 : * @return The time spent in the current phase
305 : */
306 : SUMOTime getSpentDuration(SUMOTime simStep = -1) const;
307 : /// @}
308 :
309 :
310 :
311 : /// @name Conversion between time and phase
312 : /// @{
313 :
314 : /** @brief Returns the index of the logic at the given simulation step
315 : * @return The (estimated) index of the tls at the given simulation time step
316 : */
317 : virtual SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const = 0;
318 :
319 :
320 : /** @brief Returns the position (start of a phase during a cycle) from of a given step
321 : * @param[in] index The index of the phase to return the begin of
322 : * @return The begin time of the phase
323 : */
324 : virtual SUMOTime getOffsetFromIndex(int index) const = 0;
325 :
326 :
327 : /** @brief Returns the step (the phasenumber) of a given position of the cycle
328 : * @param[in] offset The offset (time) for which the according phase shall be returned
329 : * @return The according phase
330 : */
331 : virtual int getIndexFromOffset(SUMOTime offset) const = 0;
332 : /// @}
333 :
334 :
335 : SUMOTime getOffset() const {
336 0 : return myOffset;
337 : }
338 :
339 : /// @name Changing phases and phase durations
340 : /// @{
341 :
342 : /** @brief Changes the duration of the next phase
343 : * @param[in] duration The new duration
344 : */
345 : void addOverridingDuration(SUMOTime duration);
346 :
347 :
348 : /** @brief Delays current phase by the given delay
349 : * @param[in] delay The time by which the current phase shall be delayed
350 : */
351 : void setCurrentDurationIncrement(SUMOTime delay);
352 :
353 :
354 : /** @brief Changes the current phase and her duration
355 : * @param[in] tlcontrol The responsible traffic lights control
356 : * @param[in] simStep The current simulation step
357 : * @param[in] step Index of the phase to use
358 : * @param[in] stepDuration The left duration of the phase
359 : */
360 : virtual void changeStepAndDuration(MSTLLogicControl& tlcontrol,
361 : SUMOTime simStep, int step, SUMOTime stepDuration) = 0;
362 :
363 : /// @}
364 :
365 : /// @brief whether this logic is selected in the GUI
366 : bool isSelected() const;
367 :
368 : /// @brief whether this logic is the active program
369 : bool isActive() const {
370 24158367 : return myAmActive;
371 : }
372 :
373 : /// @brief whether the given link index ever turns 'G'
374 : virtual bool getsMajorGreen(int linkIndex) const;
375 :
376 :
377 : /// @brief return activation state of all detectors that affect this traffic light
378 181 : virtual std::map<std::string, double> getDetectorStates() const {
379 181 : return std::map<std::string, double>();
380 : }
381 :
382 : /// @brief return activation state of a specific detector that affect this traffic light
383 0 : virtual double getDetectorState(const std::string) const {
384 0 : return 0.0;
385 : }
386 :
387 : /// @brief return the estimated queue length at the upcoming traffic light
388 0 : virtual double getTLQueueLength(const std::string) const {
389 0 : return 0.0;
390 : }
391 :
392 : /// @brief return all named conditions defined for this traffic light
393 181 : virtual std::map<std::string, double> getConditions() const {
394 181 : return std::map<std::string, double>();
395 : }
396 :
397 : /// @brief return vehicles that block the intersection/rail signal for vehicles that wish to pass the given linkIndex
398 0 : virtual VehicleVector getBlockingVehicles(int linkIndex) {
399 : UNUSED_PARAMETER(linkIndex);
400 0 : return VehicleVector();
401 : }
402 :
403 : /// @brief return vehicles that approach the intersection/rail signal and are in conflict with vehicles that wish to pass the given linkIndex
404 0 : virtual VehicleVector getRivalVehicles(int linkIndex) {
405 : UNUSED_PARAMETER(linkIndex);
406 0 : return VehicleVector();
407 : }
408 :
409 : /// @brief return vehicles that approach the intersection/rail signal and have priority over vehicles that wish to pass the given linkIndex
410 0 : virtual VehicleVector getPriorityVehicles(int linkIndex) {
411 : UNUSED_PARAMETER(linkIndex);
412 0 : return VehicleVector();
413 : }
414 :
415 : /// @brief return vehicles that approach the intersection/rail signal and have priority over vehicles that wish to pass the given linkIndex
416 0 : virtual std::vector<const MSDriveWay*> getBlockingDriveWays(int linkIndex) {
417 : UNUSED_PARAMETER(linkIndex);
418 0 : return std::vector<const MSDriveWay*>();
419 : }
420 :
421 : /// @brief return vehicles that approach the intersection/rail signal and have priority over vehicles that wish to pass the given linkIndex
422 0 : virtual std::string getRequestedDriveWay(int linkIndex) {
423 : UNUSED_PARAMETER(linkIndex);
424 0 : return "";
425 : }
426 :
427 : /** @brief Saves the current tls states into the given stream
428 : */
429 40 : virtual void saveState(OutputDevice& /*out*/) const {};
430 :
431 :
432 : /** @brief restores the tls state */
433 : virtual void loadState(MSTLLogicControl& tlcontrol, SUMOTime t, int step, SUMOTime spentDuration);
434 :
435 :
436 : protected:
437 : /**
438 : * @class SwitchCommand
439 : * @brief Class realising the switch between the traffic light phases
440 : */
441 : class SwitchCommand : public Command {
442 : public:
443 : /** @brief Constructor
444 : * @param[in] tlcontrol The responsible traffic lights control
445 : * @param[in] tlLogic The controlled tls logic
446 : * @param[in] duration Duration till next switch
447 : */
448 : SwitchCommand(MSTLLogicControl& tlcontrol,
449 : MSTrafficLightLogic* tlLogic,
450 : SUMOTime nextSwitch);
451 :
452 : /// @brief Destructor
453 : ~SwitchCommand();
454 :
455 : /** @brief Executes the regarded junction's "trySwitch"- method
456 : * @param[in] currentTime The current simulation time
457 : * @return The time after which the command shall be executed again (the time of next switch)
458 : */
459 : SUMOTime execute(SUMOTime currentTime);
460 :
461 :
462 : /** @brief Marks this swicth as invalid (if the phase duration has changed, f.e.)
463 : * @param[in] tlLogic The controlled tls logic
464 : */
465 : void deschedule(MSTrafficLightLogic* tlLogic);
466 :
467 :
468 : /** @brief Returns the assumed next switch time
469 : * @return The assumed next switch time
470 : */
471 : SUMOTime getNextSwitchTime() const {
472 111496 : return myAssumedNextSwitch;
473 : }
474 :
475 : /** @brief Reschedule or deschedule the command when quick-loading state
476 : *
477 : * The implementations should return -1 if the command shall not be re-scheduled,
478 : * or a value >= 0 that describe the new time at which the command
479 : * shall be executed again.
480 : *
481 : * @param[in] currentTime The current simulation time
482 : * @param[in] execTime The time at which the command would have been executed
483 : * @param[in] newTime The simulation time at which the simulation is restarted
484 : * @return The time at which the command shall be executed again
485 : */
486 : SUMOTime shiftTime(SUMOTime currentTime, SUMOTime execTime, SUMOTime newTime);
487 :
488 : private:
489 : /// @brief The responsible traffic lights control
490 : MSTLLogicControl& myTLControl;
491 :
492 : /// @brief The logic to be executed on a switch
493 : MSTrafficLightLogic* myTLLogic;
494 :
495 : /// @brief Assumed switch time (may change in case of adaptive traffic lights)
496 : SUMOTime myAssumedNextSwitch;
497 :
498 : /// @brief Information whether this switch command is still valid
499 : bool myAmValid;
500 :
501 : private:
502 : /// @brief Invalidated copy constructor.
503 : SwitchCommand(const SwitchCommand&);
504 :
505 : /// @brief Invalidated assignment operator.
506 : SwitchCommand& operator=(const SwitchCommand&);
507 :
508 : };
509 :
510 : SUMOTime computeCycleTime(const Phases& phases);
511 :
512 :
513 : protected:
514 : /// @brief The id of the logic
515 : const std::string myProgramID;
516 :
517 : /// @brief the offset parameter of the current program
518 : SUMOTime myOffset;
519 :
520 : /// @brief The type of the logic
521 : const TrafficLightType myLogicType;
522 :
523 : /// @brief The list of LinkVectors; each vector contains the links that belong to the same link index
524 : LinkVectorVector myLinks;
525 :
526 : /// @brief The list of LaneVectors; each vector contains the incoming lanes that belong to the same link index
527 : LaneVectorVector myLanes;
528 :
529 : /// @brief number of controlled links
530 : int myNumLinks;
531 :
532 : /// @brief A list of duration overrides
533 : std::vector<SUMOTime> myOverridingTimes;
534 :
535 : /// @brief A value for enlarge the current duration
536 : SUMOTime myCurrentDurationIncrement;
537 :
538 : /// @brief The current switch command
539 : SwitchCommand* mySwitchCommand;
540 :
541 : /// @brief The cycle time (without changes)
542 : SUMOTime myDefaultCycleTime;
543 :
544 : /// @brief An empty lane vector
545 : static const LaneVector myEmptyLaneVector;
546 :
547 : /// @brief list of indices that are ignored in mesoscopic simulatino
548 : std::set<int> myIgnoredIndices;
549 :
550 : /// @brief whether the current program is active
551 : bool myAmActive;
552 :
553 : private:
554 : /// @brief invalidated copy constructor
555 : MSTrafficLightLogic(const MSTrafficLightLogic& s);
556 :
557 : /// @brief invalidated assignment operator
558 : MSTrafficLightLogic& operator=(const MSTrafficLightLogic& s);
559 :
560 : };
|