LCOV - code coverage report
Current view: top level - src/microsim/traffic_lights - MSSOTLTrafficLightLogic.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 85.7 % 21 18
Test Date: 2024-11-22 15:46:21 Functions: 87.5 % 8 7

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2013-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    MSSOTLTrafficLightLogic.h
      15              : /// @author  Gianfilippo Slager
      16              : /// @author      Anna Chiara Bellini
      17              : /// @author  Federico Caselli
      18              : /// @date    Apr 2013
      19              : ///
      20              : // The base abstract class for SOTL logics
      21              : /****************************************************************************/
      22              : #pragma once
      23              : #include <config.h>
      24              : 
      25              : #include <utils/common/RandHelper.h>
      26              : #include <utils/common/StringUtils.h>
      27              : #include <utils/common/SUMOTime.h>
      28              : #include "MSSimpleTrafficLightLogic.h"
      29              : #include "MSSOTLDefinitions.h"
      30              : #include "MSSOTLE2Sensors.h"
      31              : 
      32              : 
      33              : // ===========================================================================
      34              : // class declarations
      35              : // ===========================================================================
      36              : class MSPushButton;
      37              : 
      38              : 
      39              : // ===========================================================================
      40              : // class definitions
      41              : // ===========================================================================
      42              : 
      43              : /**
      44              :  * @class MSSOTLTrafficLightLogic
      45              :  * @brief A self-organizing traffic light logic
      46              :  *
      47              :  * The base class for SOTL traffic light which switches between
      48              :  * its phases and sets the lights to red in between.
      49              :  * Some functions are called with an information about the current step. This
      50              :  * is needed as a single logic may be used by many junctions and so the current
      51              :  * step is stored within them, not within methods.
      52              :  * Basically a SOTL logic overrides the natural flow of steps in order and length
      53              :  * according to sensors on input (and output) lanes
      54              :  */
      55              : class MSSOTLTrafficLightLogic: public MSSimpleTrafficLightLogic {
      56              : public:
      57              :     // typedef int CTS;
      58              : 
      59              :     /**
      60              :      * @brief Constructor without sensors passed
      61              :      * @param[in] tlcontrol The tls control responsible for this tls
      62              :      * @param[in] id This traffic light id
      63              :      * @param[in] programID This tls' sub-id (program id)
      64              :      * @param[in] logicType This tls' type (static, actuated etc.)
      65              :      * @param[in] phases Definitions of the phases
      66              :      * @param[in] step The initial phase index
      67              :      * @param[in] delay The time to wait before the first switch
      68              :      * @param[in] parameters Parameters defined for the tll
      69              :      */
      70              :     MSSOTLTrafficLightLogic(MSTLLogicControl& tlcontrol, const std::string& id,
      71              :                             const std::string& programID, const TrafficLightType logicType, const Phases& phases, int step,
      72              :                             SUMOTime delay,
      73              :                             const Parameterised::Map& parameters);
      74              : 
      75              :     /**
      76              :      * @brief Constructor with sensors passed
      77              :      * @param[in] tlcontrol The tls control responsible for this tls
      78              :      * @param[in] id This tls' id
      79              :      * @param[in] programID This tls' sub-id (program id)
      80              :      * @param[in] logicType This tls' type (static, actuated etc.)
      81              :      * @param[in] phases Definitions of the phases
      82              :      * @param[in] step The initial phase index
      83              :      * @param[in] delay The time to wait before the first switch
      84              :      * @param[in] parameters Parameters defined for the tll
      85              :      * @param[in] sensors The already defined sensor logic
      86              :      */
      87              :     MSSOTLTrafficLightLogic(MSTLLogicControl& tlcontrol, const std::string& id,
      88              :                             const std::string& programID, const TrafficLightType logicType, const Phases& phases, int step,
      89              :                             SUMOTime delay,
      90              :                             const Parameterised::Map& parameters,
      91              :                             MSSOTLSensors* sensors);
      92              : 
      93              :     /// @brief Destructor
      94              :     ~MSSOTLTrafficLightLogic();
      95              : 
      96              :     /**
      97              :      * @brief Initialises the tls with sensors on incoming and outgoing lanes
      98              :      * Sensors are built in the simulation according to the type of sensor specified in the simulation parameter
      99              :      * @param[in] nb The detector builder
     100              :      * @exception ProcessError If something fails on initialisation
     101              :      */
     102              :     void init(NLDetectorBuilder& nb);
     103              : 
     104              :     /*
     105              :      * This member implements the base operations for all SOTL logics.
     106              :      * SOTL politics are implementated through decidePhase() member
     107              :      * @see MSTrafficLightLogic::trySwitch
     108              :      */
     109              :     SUMOTime trySwitch();
     110              : 
     111              : protected:
     112              : 
     113              :     typedef std::map<const std::string, std::vector<MSPushButton*> > PhasePushButtons;
     114              :     PhasePushButtons m_pushButtons;
     115              : 
     116              :     void logStatus();
     117              :     /*
     118              :      * This member has to contain the switching logic for SOTL policies
     119              :      */
     120              : 
     121              :     virtual int decideNextPhase();
     122              : 
     123              :     virtual bool canRelease() = 0;
     124              : 
     125              :     SUMOTime getCurrentPhaseElapsed();
     126              : 
     127              :     /*
     128              :      * Count the number of vehicles approaching the target lanes for the given phase.
     129              :      * If the phase in not a target phase the function member will return 0.
     130              :      * @param[in] The target phase index
     131              :      */
     132              :     int countVehicles(MSPhaseDefinition phase);
     133              : 
     134              :     /*
     135              :      * Every target step except the one from the current chain is checked.
     136              :      * This is because the current chain is not eligible to be directly
     137              :      * targeted again, it would be unfair.
     138              :      * @return True if at least a target phase has passed the threshold for input cars-timesteps
     139              :      */
     140              :     bool isThresholdPassed();
     141              : 
     142              :     /**
     143              :      * @breef Checks the if the pushbutton was activated for the current stage
     144              :      * @return True if at least a push button was pressed
     145              :      */
     146              :     bool isPushButtonPressed();
     147              : 
     148        82826 :     int getThreshold() {
     149       165652 :         return StringUtils::toInt(getParameter("THRESHOLD", "10"));
     150              :     }
     151              : 
     152           96 :     double getSpeedThreshold() {
     153          192 :         return StringUtils::toDouble(getParameter("THRESHOLDSPEED", "2"));
     154              :     }
     155              : 
     156           96 :     double getInputSensorsLength() {
     157          192 :         return StringUtils::toDouble(getParameter("INSENSORSLENGTH", "100"));
     158              :     }
     159              : 
     160           96 :     double getOutputSensorsLength() {
     161          192 :         return StringUtils::toDouble(getParameter("OUTSENSORSLENGTH", "80"));
     162              :     }
     163              : 
     164              :     /*
     165              :      * Every target step except the one from the current chain is checked.
     166              :      * This is because the current chain is not eligible to be directly
     167              :      * targeted again, it would be unfair.
     168              :      * @return The index of the phase with the maximum value of cars-timesteps
     169              :      */
     170              :     int getPhaseIndexWithMaxCTS();
     171              : 
     172              :     MSSOTLSensors* getSensors() {
     173       259244 :         return mySensors;
     174              :     }
     175              : 
     176              :     /**
     177              :      *\brief Return the sensors that count the passage of vehicles in and out of the tl.
     178              :      */
     179              :     MSSOTLE2Sensors* getCountSensors() {
     180            0 :         return myCountSensors;
     181              :     }
     182              :     /*
     183              :      * Computes how much time will pass after decideNextPhase will be executed again
     184              :      */
     185        83172 :     virtual SUMOTime computeReturnTime() {
     186        83172 :         if (getCurrentPhaseDef().isTransient()) {
     187         6726 :             return getCurrentPhaseDef().duration;
     188              : 
     189              :         }
     190        76446 :         return DELTA_T;
     191              : 
     192              :     }
     193              : 
     194              :     /// @brief Forces a specific step
     195              :     void setStep(int step);
     196              : 
     197              : private:
     198              :     /*
     199              :      * Pointer to the sensor logic regarding the junction controlled by this SOTLTrafficLightLogic
     200              :      */
     201              :     MSSOTLSensors* mySensors;
     202              : 
     203              :     /*
     204              :      * Pointer to the sensor logic regarding the count of the vehicles that pass into a tl.
     205              :      */
     206              :     MSSOTLE2Sensors* myCountSensors;
     207              : 
     208              :     /*
     209              :      * When true means the class has responsibilities to intantiate and delete the SOTLSensors instance,
     210              :      * otherwise MSSOTLTrafficLightLogic::init and MSSOTLTrafficLightLogic::~MSSOTLTrafficLightLogic have not to affect SOTLSensors instance lifecycle
     211              :      */
     212              :     bool sensorsSelfBuilt;
     213              : 
     214              :     // The map to store the cars*timesteps for each target phase
     215              :     std::map<int, SUMOTime> targetPhasesCTS;
     216              : 
     217              :     //The map to store the time each target phase have been checked last
     218              :     //This helps to compute the timesteps to get the cars*timesteps value
     219              :     std::map<int, SUMOTime> lastCheckForTargetPhase;
     220              : 
     221              :     //Map to store how many selection rounds have been done from the last selection of the phase
     222              :     std::map<int, int> targetPhasesLastSelection;
     223              : 
     224              :     int getTargetPhaseMaxLastSelection() {
     225              :         //return 2 * targetPhasesCTS.size() - 1;
     226        86703 :         return (int)targetPhasesCTS.size() - 1;
     227              :     }
     228              : 
     229              :     /*
     230              :      * This member keeps track which is the current steps chain, i.e.
     231              :      * which is the last target phase executed (even if it is currently executed)
     232              :      * (a steps chain starts always with a target phase)
     233              :      */
     234              :     int lastChain;
     235              : 
     236              :     double decayThreshold;
     237              :     /*
     238              :      * @brief Check for phases compliancy
     239              :      */
     240              :     void checkPhases();
     241              : 
     242              :     /*
     243              :      * Find the first target phase and set the current step on it
     244              :      */
     245              :     void setToATargetPhase();
     246              : 
     247              :     /*
     248              :      * This function member helps to set up the map keeping track of target phases and associated timesteps
     249              :      */
     250              :     void setupCTS();
     251              : 
     252              :     /*
     253              :      * Updates the cars-timesteps counters for each target phase except the one belonging to the current steps chain
     254              :      */
     255              :     void updateCTS();
     256              : 
     257              :     /*
     258              :      * To reset the cars-timesteps counter when a target phase is newly selected
     259              :      * If phaseStep is not a target phase nothing happens
     260              :      */
     261              :     void resetCTS(int phaseStep);
     262              :     /*
     263              :      * TEST
     264              :      */
     265              :     void updateDecayThreshold();
     266              : 
     267              :     /*
     268              :      * Traffic threshold calculation mode:
     269              :      * 0-> cars times seconds
     270              :      * 1-> estimated cars times seconds
     271              :      * 2-> queue length
     272              :      */
     273       450500 :     int getMode() {
     274       901000 :         return StringUtils::toInt(getParameter("MODE", "0"));
     275              :     }
     276              :     /*
     277              :      * Decay threshold that should be used in case of penetration rate != 100%
     278              :      * 0-> not active
     279              :      * 1-> active
     280              :      */
     281       295276 :     bool isDecayThresholdActivated() {
     282       590552 :         return StringUtils::toBool(getParameter("DECAY_THRESHOLD", "0"));
     283              :     }
     284              : 
     285            0 :     double getDecayConstant() {
     286            0 :         return StringUtils::toDouble(getParameter("DECAY_CONSTANT", "-0.001"));
     287              :     }
     288              : 
     289              : };
        

Generated by: LCOV version 2.0-1