LCOV - code coverage report
Current view: top level - src/microsim/traffic_lights - MSDeterministicHiLevelTrafficLightLogic.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 0 63 0.0 %
Date: 2024-05-19 15:37:39 Functions: 0 10 0.0 %

          Line data    Source code
       1             : /****************************************************************************/
       2             : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3             : // Copyright (C) 2010-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    MSDeterministicHiLevelTrafficLightLogic.cpp
      15             : /// @author  Riccardo Belletti
      16             : /// @date    Mar 2014
      17             : ///
      18             : // The class for deterministic high level traffic light logic
      19             : /****************************************************************************/
      20             : 
      21             : #include "MSDeterministicHiLevelTrafficLightLogic.h"
      22             : //#define SWARM_DEBUG
      23             : 
      24             : 
      25             : // ===========================================================================
      26             : // method definitions
      27             : // ===========================================================================
      28           0 : MSDeterministicHiLevelTrafficLightLogic::MSDeterministicHiLevelTrafficLightLogic(
      29             :     MSTLLogicControl& tlcontrol, const std::string& id,
      30             :     const std::string& programID, const Phases& phases, int step,
      31           0 :     SUMOTime delay, const Parameterised::Map& parameters) :
      32             :     MSSOTLHiLevelTrafficLightLogic(tlcontrol, id, programID, TrafficLightType::HILVL_DETERMINISTIC, phases, step,
      33           0 :                                    delay, parameters) {
      34             : 
      35           0 :     addPolicy(new MSSOTLPlatoonPolicy(new MSSOTLPolicy3DStimulus("PLATOON", parameters), parameters));
      36           0 :     addPolicy(new MSSOTLPhasePolicy(new MSSOTLPolicy3DStimulus("PHASE", parameters), parameters));
      37           0 :     addPolicy(new MSSOTLMarchingPolicy(new MSSOTLPolicy3DStimulus("MARCHING", parameters), parameters));
      38           0 :     addPolicy(new MSSOTLCongestionPolicy(new MSSOTLPolicy3DStimulus("CONGESTION", parameters), parameters));
      39             : 
      40           0 : }
      41             : 
      42           0 : MSDeterministicHiLevelTrafficLightLogic::~MSDeterministicHiLevelTrafficLightLogic() {
      43             : 
      44           0 : }
      45             : 
      46           0 : void MSDeterministicHiLevelTrafficLightLogic::init(NLDetectorBuilder& nb) {
      47           0 :     MSSOTLHiLevelTrafficLightLogic::init(nb);
      48             :     //Setting the startup policy
      49           0 :     choosePolicy(0, 0);
      50           0 :     WRITE_MESSAGE(
      51             :         "*** Intersection " + getID()
      52             :         + " will run using MSDeterministicHiLevelTrafficLightLogic ***");
      53             : 
      54             :     MSLane* currentLane = nullptr;
      55           0 :     for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector =
      56           0 :                 myLanes.begin(); laneVector != myLanes.end(); laneVector++) {
      57           0 :         for (MSTrafficLightLogic::LaneVector::const_iterator lane =
      58           0 :                     laneVector->begin(); lane != laneVector->end(); lane++) {
      59           0 :             currentLane = (*lane);
      60           0 :             if (inputLanes.find(currentLane->getID()) == inputLanes.end()) {
      61             :                 inputLanes.insert(currentLane->getID());
      62             : #ifdef SWARM_DEBUG
      63             :                 WRITE_MESSAGE("*** Intersection " + getID() + " inputLanes adding " + currentLane->getID());
      64             : #endif
      65             :             }
      66             :         }
      67             :     }
      68             : 
      69           0 :     for (const LinkVector& oneLink : getLinks()) {
      70           0 :         for (int j = 0; j < (int)oneLink.size(); j++) {
      71           0 :             currentLane = oneLink[j]->getLane();
      72           0 :             if (outputLanes.find(currentLane->getID()) == outputLanes.end()) {
      73             :                 outputLanes.insert(currentLane->getID());
      74             : #ifdef SWARM_DEBUG
      75             :                 WRITE_MESSAGE("*** Intersection " + getID() + " outputLanes adding " + currentLane->getID());
      76             : #endif
      77             :             }
      78             :         }
      79             :     }
      80             : 
      81           0 : }
      82             : 
      83           0 : int MSDeterministicHiLevelTrafficLightLogic::decideNextPhase() {
      84             : #ifdef SWARM_DEBUG
      85             :     MsgHandler::getMessageInstance()->inform("\n" + time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSDeterministicHiLevelTrafficLightLogic decideNextPhase()");
      86             :     std::ostringstream dnp;
      87             :     dnp << (MSNet::getInstance()->getCurrentTimeStep()) << " MSDeterministicHiLevelTrafficLightLogic::decideNextPhase:: " << "tlsid=" << getID() << " getCurrentPhaseDef().getState()=" << getCurrentPhaseDef().getState() << " is commit?" << getCurrentPhaseDef().isCommit();
      88             :     MsgHandler::getMessageInstance()->inform(dnp.str());
      89             : #endif
      90             : 
      91             :     //Decide the current policy according to pheromone levels. this should be done only at the end of a chain, before selecting the new one
      92           0 :     if (getCurrentPhaseDef().isCommit()) {
      93           0 :         decidePolicy();
      94             :     }
      95             : 
      96             : #ifdef SWARM_DEBUG
      97             :     std::ostringstream str;
      98             :     str << "tlsID=" << getID() << " currentPolicyname=" + getCurrentPolicy()->getName();
      99             :     WRITE_MESSAGE(str.str());
     100             : #endif
     101             : 
     102             :     //Execute current policy. congestion "policy" must maintain the commit phase, and that must be an all-red one
     103           0 :     return myCurrentPolicy->decideNextPhase(getCurrentPhaseElapsed(),
     104           0 :                                             &getCurrentPhaseDef(), getCurrentPhaseIndex(),
     105           0 :                                             getPhaseIndexWithMaxCTS(), isThresholdPassed(), isPushButtonPressed(),
     106           0 :                                             countVehicles(getCurrentPhaseDef()));
     107             : }
     108             : 
     109           0 : double MSDeterministicHiLevelTrafficLightLogic::getMeanSpeedForInputLanes() {
     110           0 :     if (inputLanes.size() == 0) {
     111             :         return 0;
     112             :     }
     113             :     double vSpeedInTot = 0;
     114           0 :     for (MSLaneID_set::iterator laneIterator = inputLanes.begin();
     115           0 :             laneIterator != inputLanes.end(); laneIterator++) {
     116             :         std::string laneId = *laneIterator;
     117           0 :         double maxSpeed = getSensors()->meanVehiclesSpeed(laneId);
     118           0 :         if (maxSpeed > -1) {
     119           0 :             vSpeedInTot += (13.89 - maxSpeed) * 10. / 13.89;
     120             :         }
     121             : #ifdef SWARM_DEBUG
     122             :         std::ostringstream i_str;
     123             :         i_str << " meanVehiclesSpeed " << maxSpeed << " inputLane " << laneId << " ID " << getID() << " .";
     124             :         WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSDeterministicHiLevelTrafficLightLogic::getMeanSpeedForInputLanes:: in" + i_str.str());
     125             : #endif
     126             :     }
     127           0 :     return vSpeedInTot / (double)inputLanes.size();
     128             : }
     129             : 
     130           0 : double MSDeterministicHiLevelTrafficLightLogic::getMeanSpeedForOutputLanes() {
     131           0 :     if (outputLanes.size() == 0) {
     132             :         return 0;
     133             :     }
     134             :     double vSpeedOutTot = 0;
     135           0 :     for (MSLaneID_set::iterator laneIterator = outputLanes.begin();
     136           0 :             laneIterator != outputLanes.end(); laneIterator++) {
     137             :         std::string laneId = *laneIterator;
     138           0 :         double maxSpeed = getSensors()->meanVehiclesSpeed(laneId);
     139           0 :         if (maxSpeed > -1) {
     140           0 :             vSpeedOutTot += (13.89 - maxSpeed) * 10. / 13.89;
     141             :         }
     142             : #ifdef SWARM_DEBUG
     143             :         std::ostringstream i_str;
     144             :         i_str << " meanVehiclesSpeed " << maxSpeed << " outputLane " << laneId << " ID " << getID() << " .";
     145             :         WRITE_MESSAGE(time2string(MSNet::getInstance()->getCurrentTimeStep()) + " MSDeterministicHiLevelTrafficLightLogic::getMeanSpeedForOutputLanes:: out" + i_str.str());
     146             : #endif
     147             :     }
     148           0 :     return vSpeedOutTot / (double)outputLanes.size();
     149             : }
     150             : 
     151           0 : void MSDeterministicHiLevelTrafficLightLogic::decidePolicy() {
     152             :     // Decide if it is the case to check for another plan
     153           0 :     double mean_vSpeed_in = getMeanSpeedForInputLanes();
     154           0 :     double mean_vSpeed_out = getMeanSpeedForOutputLanes();
     155             : #ifdef SWARM_DEBUG
     156             :     MSSOTLPolicy* oldPolicy = getCurrentPolicy();
     157             : #endif
     158           0 :     choosePolicy(mean_vSpeed_in, mean_vSpeed_out);
     159             : 
     160             : #ifdef SWARM_DEBUG
     161             :     MSSOTLPolicy* newPolicy = getCurrentPolicy();
     162             :     if (newPolicy != oldPolicy) {
     163             :         SUMOTime step = MSNet::getInstance()->getCurrentTimeStep();
     164             :         std::ostringstream phero_str;
     165             :         phero_str << " (mean_vSpeed_in= " << mean_vSpeed_in << " ,mean_vSpeed_out= " << mean_vSpeed_out << " )";
     166             :         WRITE_MESSAGE("TL " + getID() + " time=" + time2string(step) + " Policy: " + newPolicy->getName() + phero_str.str() + " OldPolicy: " + oldPolicy->getName() + " id " + getID() + " .");
     167             :     } else { //debug purpose only
     168             :         std::ostringstream phero_str;
     169             :         phero_str << " (mean_vSpeed_in= " << mean_vSpeed_in << " ,mean_vSpeed_out= " << mean_vSpeed_out << " )";
     170             :         SUMOTime step = MSNet::getInstance()->getCurrentTimeStep();
     171             :         WRITE_MESSAGE("TL " + getID() + " time=" + time2string(step) + " Policy: Nochanges" + phero_str.str() + " OldPolicy: " + oldPolicy->getName() + " id " + getID() + " .");
     172             :     }
     173             : #endif
     174           0 : }
     175             : 
     176           0 : void MSDeterministicHiLevelTrafficLightLogic::choosePolicy(
     177             :     double mean_vSpeed_in, double mean_vSpeed_out) {
     178             : 
     179             :     int index_maxStimulus = 0;
     180             :     double maxStimulus = -1;
     181             :     // Compute simulus for each policy
     182           0 :     for (int i = 0; i < (int)myPolicies.size(); i++) {
     183           0 :         double stimulus = myPolicies[i]->computeDesirability(mean_vSpeed_in,
     184             :                           mean_vSpeed_out);
     185           0 :         if (stimulus > maxStimulus) {
     186             :             maxStimulus = stimulus;
     187             :             index_maxStimulus = i;
     188             :         }
     189             : #ifdef SWARM_DEBUG
     190             :         std::ostringstream so_str;
     191             :         so_str << " policy " << getPolicies()[i]->getName() << " stimulus " << stimulus;
     192             :         WRITE_MESSAGE("MSDeterministicHiLevelTrafficLightLogic::choosePolicy::" + so_str.str());
     193             : #endif
     194             :     }
     195           0 :     activate(myPolicies[index_maxStimulus]);
     196             : 
     197           0 : }
     198             : 
     199           0 : bool MSDeterministicHiLevelTrafficLightLogic::canRelease() {
     200             : #ifdef SWARM_DEBUG
     201             :     std::ostringstream phero_str;
     202             :     phero_str << "getCurrentPhaseElapsed()=" << time2string(getCurrentPhaseElapsed()) << " isThresholdPassed()=" << isThresholdPassed() << " currentPhase=" << (&getCurrentPhaseDef())->getState() << " countVehicles()=" << countVehicles(getCurrentPhaseDef());
     203             :     WRITE_MESSAGE("\nMSDeterministicHiLevelTrafficLightLogic::canRelease(): " + phero_str.str());
     204             : #endif
     205           0 :     return myCurrentPolicy->canRelease(getCurrentPhaseElapsed(),
     206           0 :                                        isThresholdPassed(), isPushButtonPressed(), &getCurrentPhaseDef(),
     207           0 :                                        countVehicles(getCurrentPhaseDef()));
     208             : }

Generated by: LCOV version 1.14