LCOV - code coverage report
Current view: top level - src/microsim/traffic_lights - MSTrafficLightLogic.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 71.0 % 31 22
Test Date: 2026-03-02 16:00:03 Functions: 60.0 % 10 6

            Line data    Source code
       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              : /****************************************************************************/
      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      6500939 :         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      4956479 :         if (i < (int)myLanes.size()) {
     198      4951622 :             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        13502 :         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      1340260 :         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 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              : 
     278              :     /** @brief Returns the cycle time (in ms)
     279              :      * @return The (maybe changing) cycle time of this tls
     280              :      */
     281              :     SUMOTime getDefaultCycleTime() const {
     282          764 :         return myDefaultCycleTime;
     283              :     }
     284              : 
     285              :     /// @brief return time within the current cycle
     286              :     SUMOTime getTimeInCycle() const;
     287              : 
     288              :     /// @brief map the given time into the current cycle
     289              :     virtual SUMOTime mapTimeInCycle(SUMOTime t) const;
     290              : 
     291              :     /// @brief return the number of controlled link indices
     292              :     int getNumLinks() const {
     293        37198 :         return myNumLinks;
     294              :     }
     295              : 
     296              :     /** @brief Returns the assumed next switch time
     297              :      *
     298              :      * The time may change in case of adaptive/actuated traffic lights.
     299              :      * @return The assumed next switch time (simulation time)
     300              :      */
     301              :     SUMOTime getNextSwitchTime() const;
     302              : 
     303              : 
     304              :     /** @brief Returns the duration spent in the current phase
     305              :      *
     306              :      * @return The time spent in the current phase
     307              :      */
     308              :     SUMOTime getSpentDuration(SUMOTime simStep = -1) const;
     309              :     /// @}
     310              : 
     311              : 
     312              : 
     313              :     /// @name Conversion between time and phase
     314              :     /// @{
     315              : 
     316              :     /** @brief Returns the index of the logic at the given simulation step
     317              :      * @return The (estimated) index of the tls at the given simulation time step
     318              :      */
     319              :     virtual SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const = 0;
     320              : 
     321              : 
     322              :     /** @brief Returns the position (start of a phase during a cycle) from of a given step
     323              :      * @param[in] index The index of the phase to return the begin of
     324              :      * @return The begin time of the phase
     325              :      */
     326              :     virtual SUMOTime getOffsetFromIndex(int index) const = 0;
     327              : 
     328              : 
     329              :     /** @brief Returns the step (the phasenumber) of a given position of the cycle
     330              :      * @param[in] offset The offset (time) for which the according phase shall be returned
     331              :      * @return The according phase
     332              :      */
     333              :     virtual int getIndexFromOffset(SUMOTime offset) const = 0;
     334              :     /// @}
     335              : 
     336              : 
     337              :     SUMOTime getOffset() const {
     338            0 :         return myOffset;
     339              :     }
     340              : 
     341              :     /// @name Changing phases and phase durations
     342              :     /// @{
     343              : 
     344              :     /** @brief Changes the duration of the next phase
     345              :      * @param[in] duration The new duration
     346              :      */
     347              :     void addOverridingDuration(SUMOTime duration);
     348              : 
     349              : 
     350              :     /** @brief Delays current phase by the given delay
     351              :      * @param[in] delay The time by which the current phase shall be delayed
     352              :      */
     353              :     void setCurrentDurationIncrement(SUMOTime delay);
     354              : 
     355              : 
     356              :     /** @brief Changes the current phase and her duration
     357              :      * @param[in] tlcontrol The responsible traffic lights control
     358              :      * @param[in] simStep The current simulation step
     359              :      * @param[in] step Index of the phase to use
     360              :      * @param[in] stepDuration The left duration of the phase
     361              :      */
     362              :     virtual void changeStepAndDuration(MSTLLogicControl& tlcontrol,
     363              :                                        SUMOTime simStep, int step, SUMOTime stepDuration) = 0;
     364              : 
     365              :     /// @}
     366              : 
     367              :     /// @brief whether this logic is selected in the GUI
     368              :     bool isSelected() const;
     369              : 
     370              :     /// @brief whether this logic is the active program
     371              :     bool isActive() const {
     372     21775993 :         return myAmActive;
     373              :     }
     374              : 
     375              :     /// @brief whether the given link index ever turns 'G'
     376              :     virtual bool getsMajorGreen(int linkIndex) const;
     377              : 
     378              : 
     379              :     /// @brief return activation state of all detectors that affect this traffic light
     380          181 :     virtual std::map<std::string, double> getDetectorStates() const {
     381          181 :         return std::map<std::string, double>();
     382              :     }
     383              : 
     384              :     /// @brief return activation state of a specific detector that affect this traffic light
     385            0 :     virtual double getDetectorState(const std::string) const {
     386            0 :         return 0.0;
     387              :     }
     388              : 
     389              :     /// @brief return the estimated queue length at the upcoming traffic light
     390            0 :     virtual double getTLQueueLength(const std::string) const {
     391            0 :         return 0.0;
     392              :     }
     393              : 
     394              :     /// @brief return all named conditions defined for this traffic light
     395          181 :     virtual std::map<std::string, double> getConditions() const {
     396          181 :         return std::map<std::string, double>();
     397              :     }
     398              : 
     399              :     /// @brief return vehicles that block the intersection/rail signal for vehicles that wish to pass the given linkIndex
     400            8 :     virtual VehicleVector getBlockingVehicles(int linkIndex) {
     401              :         UNUSED_PARAMETER(linkIndex);
     402            8 :         return VehicleVector();
     403              :     }
     404              : 
     405              :     /// @brief return vehicles that approach the intersection/rail signal and are in conflict with vehicles that wish to pass the given linkIndex
     406            8 :     virtual VehicleVector getRivalVehicles(int linkIndex) {
     407              :         UNUSED_PARAMETER(linkIndex);
     408            8 :         return VehicleVector();
     409              :     }
     410              : 
     411              :     /// @brief return vehicles that approach the intersection/rail signal and have priority over vehicles that wish to pass the given linkIndex
     412            8 :     virtual VehicleVector getPriorityVehicles(int linkIndex) {
     413              :         UNUSED_PARAMETER(linkIndex);
     414            8 :         return VehicleVector();
     415              :     }
     416              : 
     417              :     /// @brief return vehicles that approach the intersection/rail signal and have priority over vehicles that wish to pass the given linkIndex
     418            0 :     virtual std::vector<const MSDriveWay*> getBlockingDriveWays(int linkIndex) {
     419              :         UNUSED_PARAMETER(linkIndex);
     420            0 :         return std::vector<const MSDriveWay*>();
     421              :     }
     422              : 
     423              :     /// @brief return vehicles that approach the intersection/rail signal and have priority over vehicles that wish to pass the given linkIndex
     424            0 :     virtual std::string getRequestedDriveWay(int linkIndex) {
     425              :         UNUSED_PARAMETER(linkIndex);
     426            0 :         return "";
     427              :     }
     428              : 
     429              :     /** @brief Saves the current tls states into the given stream
     430              :         */
     431           60 :     virtual void saveState(OutputDevice& /*out*/) const {};
     432              : 
     433              : 
     434              :     /** @brief restores the tls state */
     435              :     virtual void loadState(MSTLLogicControl& tlcontrol, SUMOTime t, int step, SUMOTime spentDuration, bool active);
     436              : 
     437              : 
     438              : protected:
     439              :     /**
     440              :      * @class SwitchCommand
     441              :      * @brief Class realising the switch between the traffic light phases
     442              :      */
     443              :     class SwitchCommand : public Command {
     444              :     public:
     445              :         /** @brief Constructor
     446              :          * @param[in] tlcontrol The responsible traffic lights control
     447              :          * @param[in] tlLogic The controlled tls logic
     448              :          * @param[in] duration Duration till next switch
     449              :          */
     450              :         SwitchCommand(MSTLLogicControl& tlcontrol,
     451              :                       MSTrafficLightLogic* tlLogic,
     452              :                       SUMOTime nextSwitch);
     453              : 
     454              :         /// @brief Destructor
     455              :         ~SwitchCommand();
     456              : 
     457              :         /** @brief Executes the regarded junction's "trySwitch"- method
     458              :          * @param[in] currentTime The current simulation time
     459              :          * @return The time after which the command shall be executed again (the time of next switch)
     460              :          */
     461              :         SUMOTime execute(SUMOTime currentTime);
     462              : 
     463              : 
     464              :         /** @brief Marks this swicth as invalid (if the phase duration has changed, f.e.)
     465              :          * @param[in] tlLogic The controlled tls logic
     466              :          */
     467              :         void deschedule(MSTrafficLightLogic* tlLogic);
     468              : 
     469              : 
     470              :         /** @brief Returns the assumed next switch time
     471              :          * @return The assumed next switch time
     472              :          */
     473              :         SUMOTime getNextSwitchTime() const {
     474       118177 :             return myAssumedNextSwitch;
     475              :         }
     476              : 
     477              :         /** @brief Reschedule or deschedule the command when quick-loading state
     478              :          *
     479              :          * The implementations should return -1 if the command shall not be re-scheduled,
     480              :          *  or a value >= 0 that describe the new time at which the command
     481              :          *  shall be executed again.
     482              :          *
     483              :          * @param[in] currentTime The current simulation time
     484              :          * @param[in] execTime The time at which the command would have been executed
     485              :          * @param[in] newTime The simulation time at which the simulation is restarted
     486              :          * @return The time at which the command shall be executed again
     487              :          */
     488              :         SUMOTime shiftTime(SUMOTime currentTime, SUMOTime execTime, SUMOTime newTime);
     489              : 
     490              :     private:
     491              :         /// @brief The responsible traffic lights control
     492              :         MSTLLogicControl& myTLControl;
     493              : 
     494              :         /// @brief The logic to be executed on a switch
     495              :         MSTrafficLightLogic* myTLLogic;
     496              : 
     497              :         /// @brief Assumed switch time (may change in case of adaptive traffic lights)
     498              :         SUMOTime myAssumedNextSwitch;
     499              : 
     500              :         /// @brief Information whether this switch command is still valid
     501              :         bool myAmValid;
     502              : 
     503              :     private:
     504              :         /// @brief Invalidated copy constructor.
     505              :         SwitchCommand(const SwitchCommand&);
     506              : 
     507              :         /// @brief Invalidated assignment operator.
     508              :         SwitchCommand& operator=(const SwitchCommand&);
     509              : 
     510              :     };
     511              : 
     512              :     SUMOTime computeCycleTime(const Phases& phases);
     513              : 
     514              : 
     515              : protected:
     516              :     /// @brief The id of the logic
     517              :     const std::string myProgramID;
     518              : 
     519              :     /// @brief the offset parameter of the current program
     520              :     SUMOTime myOffset;
     521              : 
     522              :     /// @brief The type of the logic
     523              :     const TrafficLightType myLogicType;
     524              : 
     525              :     /// @brief The list of LinkVectors; each vector contains the links that belong to the same link index
     526              :     LinkVectorVector myLinks;
     527              : 
     528              :     /// @brief The list of LaneVectors; each vector contains the incoming lanes that belong to the same link index
     529              :     LaneVectorVector myLanes;
     530              : 
     531              :     /// @brief number of controlled links
     532              :     int myNumLinks;
     533              : 
     534              :     /// @brief A list of duration overrides
     535              :     std::vector<SUMOTime> myOverridingTimes;
     536              : 
     537              :     /// @brief A value for enlarge the current duration
     538              :     SUMOTime myCurrentDurationIncrement;
     539              : 
     540              :     /// @brief The current switch command
     541              :     SwitchCommand* mySwitchCommand;
     542              : 
     543              :     /// @brief The cycle time (without changes)
     544              :     SUMOTime myDefaultCycleTime;
     545              : 
     546              :     /// @brief An empty lane vector
     547              :     static const LaneVector myEmptyLaneVector;
     548              : 
     549              :     /// @brief list of indices that are ignored in mesoscopic simulatino
     550              :     std::set<int> myIgnoredIndices;
     551              : 
     552              :     /// @brief whether the current program is active
     553              :     bool myAmActive;
     554              : 
     555              : private:
     556              :     /// @brief invalidated copy constructor
     557              :     MSTrafficLightLogic(const MSTrafficLightLogic& s);
     558              : 
     559              :     /// @brief invalidated assignment operator
     560              :     MSTrafficLightLogic& operator=(const MSTrafficLightLogic& s);
     561              : 
     562              : };
        

Generated by: LCOV version 2.0-1