LCOV - code coverage report
Current view: top level - src/microsim/traffic_lights - MSSOTLPolicy.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 36 53 67.9 %
Date: 2024-05-19 15:37:39 Functions: 9 12 75.0 %

          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    MSSOTLPolicy.cpp
      15             : /// @author  Alessio Bonfietti
      16             : /// @author  Anna Chiara Bellini
      17             : /// @author  Federico Caselli
      18             : /// @date    Jun 2013
      19             : ///
      20             : // The class for low-level policy
      21             : /****************************************************************************/
      22             : 
      23             : #include <cmath>
      24             : #include <typeinfo>
      25             : #include "utils/common/RandHelper.h"
      26             : #include "MSSOTLPolicy.h"
      27             : //#define SWARM_DEBUG
      28             : 
      29             : 
      30             : // ===========================================================================
      31             : // method definitions
      32             : // ===========================================================================
      33         192 : void PushButtonLogic::init(std::string prefix, const Parameterised* parameterised) {
      34         192 :     m_prefix = prefix;
      35         384 :     m_pushButtonScaleFactor = StringUtils::toDouble(parameterised->getParameter("PUSH_BUTTON_SCALE_FACTOR", "1"));
      36         576 :     WRITE_MESSAGE(m_prefix + "::PushButtonLogic::init use " + parameterised->getParameter("USE_PUSH_BUTTON", "0") + " scale " + parameterised->getParameter("PUSH_BUTTON_SCALE_FACTOR", "1"));
      37         192 : }
      38             : 
      39       88692 : bool PushButtonLogic::pushButtonLogic(SUMOTime elapsed, bool pushButtonPressed, const MSPhaseDefinition* stage) {
      40             :     //pushbutton logic
      41       88692 :     if (pushButtonPressed && (double)elapsed >= (double)stage->duration * m_pushButtonScaleFactor) {
      42             :         //If the stage duration has been passed
      43             : #ifdef SWARM_DEBUG
      44             :         std::ostringstream oss;
      45             :         oss << m_prefix << "::pushButtonLogic pushButtonPressed elapsed " << elapsed << " stage duration " << (stage->duration * m_pushButtonScaleFactor);
      46             :         WRITE_MESSAGE(oss.str());
      47             : #endif
      48           0 :         return true;
      49             :     }
      50             :     return false;
      51             : }
      52             : 
      53         128 : void SigmoidLogic::init(std::string prefix, const Parameterised* parameterised) {
      54         128 :     m_prefix = prefix;
      55         384 :     m_useSigmoid = parameterised->getParameter("PLATOON_USE_SIGMOID", "0") != "0";
      56         256 :     m_k = StringUtils::toDouble(parameterised->getParameter("PLATOON_SIGMOID_K_VALUE", "1"));
      57             : //  DBG(
      58         512 :     WRITE_MESSAGE(m_prefix + "::SigmoidLogic::init use " + parameterised->getParameter("PLATOON_USE_SIGMOID", "0") + " k " + parameterised->getParameter("PLATOON_SIGMOID_K_VALUE", "1"));
      59             : //    for (int elapsed = 10; elapsed < 51; ++elapsed)
      60             : //    {
      61             : //        double sigmoidValue = 1.0 / (1.0 + exp(-m_k * (elapsed - 31)));
      62             : //        std::ostringstream oss;
      63             : //        oss << "elapsed " << elapsed << " value " << sigmoidValue;
      64             : //        WRITE_MESSAGE(oss.str())
      65             : //    }
      66             : //  )
      67         128 : }
      68             : 
      69       29192 : bool SigmoidLogic::sigmoidLogic(SUMOTime elapsed, const MSPhaseDefinition* stage, int vehicleCount) {
      70             :     //use the sigmoid logic
      71       29192 :     if (m_useSigmoid && vehicleCount == 0) {
      72           0 :         double sigmoidValue = 1.0 / (1.0 + exp(-m_k * STEPS2TIME(elapsed - stage->duration)));
      73           0 :         double rnd = RandHelper::rand();
      74             : //    DBG(
      75           0 :         std::ostringstream oss;
      76           0 :         oss << m_prefix << "::sigmoidLogic [k=" << m_k << " elapsed " << elapsed << " stage->duration " << stage->duration << " ] value "
      77             :             << sigmoidValue;
      78           0 :         oss << " rnd " << rnd << " retval " << (rnd < sigmoidValue ? "true" : "false");
      79           0 :         WRITE_MESSAGE(oss.str())
      80             : //    );
      81           0 :         return rnd < sigmoidValue;
      82           0 :     }
      83             :     return false;
      84             : }
      85             : 
      86             : 
      87         128 : MSSOTLPolicy::MSSOTLPolicy(std::string name,
      88         128 :                            const Parameterised::Map& parameters) :
      89         128 :     Parameterised(parameters), myName(name) {
      90         128 :     theta_sensitivity = 0;
      91         128 : }
      92             : 
      93           0 : MSSOTLPolicy::MSSOTLPolicy(std::string name,
      94           0 :                            MSSOTLPolicyDesirability* desirabilityAlgorithm) :
      95           0 :     Parameterised(), myName(name), myDesirabilityAlgorithm(
      96           0 :         desirabilityAlgorithm) {
      97           0 :     theta_sensitivity = 0;
      98           0 : }
      99             : 
     100         128 : MSSOTLPolicy::MSSOTLPolicy(std::string name,
     101             :                            MSSOTLPolicyDesirability* desirabilityAlgorithm,
     102         128 :                            const Parameterised::Map& parameters) :
     103         256 :     Parameterised(parameters), myName(name), myDesirabilityAlgorithm(
     104         128 :         desirabilityAlgorithm) {
     105         128 :     theta_sensitivity = StringUtils::toDouble(getParameter("THETA_INIT", "0.5"));
     106         128 : }
     107             : 
     108         128 : MSSOTLPolicy::~MSSOTLPolicy(void) {
     109         128 : }
     110             : 
     111         160 : double MSSOTLPolicy::computeDesirability(double vehInMeasure, double vehOutMeasure, double vehInDispersionMeasure,      double vehOutDispersionMeasure) {
     112             : #ifdef SWARM_DEBUG
     113             :     std::ostringstream str;
     114             :     str << "\nMSSOTLPolicy::computeStimulus\n" << getName();
     115             :     WRITE_MESSAGE(str.str());
     116             : #endif
     117         160 :     return myDesirabilityAlgorithm->computeDesirability(vehInMeasure, vehOutMeasure, vehInDispersionMeasure, vehOutDispersionMeasure);
     118             : }
     119             : 
     120           0 : double MSSOTLPolicy::computeDesirability(double vehInMeasure, double vehOutMeasure) {
     121             : #ifdef SWARM_DEBUG
     122             :     std::ostringstream str;
     123             :     str << "\nMSSOTLPolicy::computeStimulus\n" << getName();
     124             :     WRITE_MESSAGE(str.str());
     125             : #endif
     126           0 :     return myDesirabilityAlgorithm->computeDesirability(vehInMeasure, vehOutMeasure, 0, 0);
     127             : }
     128             : 
     129      165652 : int MSSOTLPolicy::decideNextPhase(SUMOTime elapsed,
     130             :                                   const MSPhaseDefinition* stage, int currentPhaseIndex,
     131             :                                   int phaseMaxCTS, bool thresholdPassed, bool pushButtonPressed, int vehicleCount) {
     132             : 
     133             :     //If the junction was in a commit step
     134             :     //=> go to the target step that gives green to the set with the current highest CTS
     135             :     //   and return computeReturnTime()
     136      165652 :     if (stage->isCommit()) {
     137             :         // decide which chain to activate. Gotta work on this
     138             :         return phaseMaxCTS;
     139             :     }
     140      159354 :     if (stage->isTransient()) {
     141             :         //If the junction was in a transient step
     142             :         //=> go to the next step and return computeReturnTime()
     143        6298 :         return currentPhaseIndex + 1;
     144             :     }
     145             : 
     146             :     if (stage->isDecisional()) {
     147             : #ifdef SWARM_DEBUG
     148             :         std::ostringstream phero_str;
     149             :         phero_str << "getCurrentPhaseElapsed()=" << time2string(elapsed) << " isThresholdPassed()=" << thresholdPassed << " countVehicles()=" << vehicleCount;
     150             :         WRITE_MESSAGE("MSSOTLPolicy::decideNextPhase: " + phero_str.str());
     151             : #endif
     152      153056 :         if (canRelease(elapsed, thresholdPassed, pushButtonPressed, stage, vehicleCount)) {
     153        6298 :             return currentPhaseIndex + 1;
     154             :         }
     155             :     }
     156             : 
     157             :     return currentPhaseIndex;
     158             : }
     159             : 
     160             : /*
     161             :  bool MSSOTLPolicy::canRelease(SUMOTime elapsed, bool thresholdPassed, const MSPhaseDefinition* stage, int vehicleCount) {
     162             :  if (getName().compare("request") == 0) {
     163             :  return elapsed > 3000 && thresholdPassed;
     164             :  } else if (getName().compare("phase") == 0) {
     165             :  return thresholdPassed && elapsed >= stage->minDuration;
     166             :  } else if (getName().compare("platoon") == 0) {
     167             :  return thresholdPassed && (vehicleCount == 0 || elapsed >= stage->maxDuration);
     168             :  } else if (getName().compare("marching") == 0) {
     169             :  return elapsed >= stage->duration;
     170             :  } else if (getName().compare("congestion") == 0) {
     171             :  return elapsed >= stage->minDuration;
     172             :  }
     173             :  return true; //
     174             : 
     175             :  }
     176             :  */

Generated by: LCOV version 1.14