LCOV - code coverage report
Current view: top level - src/microsim/traffic_lights - MSTrafficLightLogic.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 16 23 69.6 %
Date: 2024-05-19 15:37:39 Functions: 3 6 50.0 %

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

Generated by: LCOV version 1.14