Eclipse SUMO - Simulation of Urban MObility
MSSOTLPolicy.cpp
Go to the documentation of this file.
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 /****************************************************************************/
20 // The class for low-level policy
21 /****************************************************************************/
22 
23 #include <cmath>
24 #include <typeinfo>
26 #include "MSSOTLPolicy.h"
27 //#define SWARM_DEBUG
28 
29 
30 // ===========================================================================
31 // method definitions
32 // ===========================================================================
33 void PushButtonLogic::init(std::string prefix, const Parameterised* parameterised) {
34  m_prefix = prefix;
35  m_pushButtonScaleFactor = StringUtils::toDouble(parameterised->getParameter("PUSH_BUTTON_SCALE_FACTOR", "1"));
36  WRITE_MESSAGE(m_prefix + "::PushButtonLogic::init use " + parameterised->getParameter("USE_PUSH_BUTTON", "0") + " scale " + parameterised->getParameter("PUSH_BUTTON_SCALE_FACTOR", "1"));
37 }
38 
39 bool PushButtonLogic::pushButtonLogic(SUMOTime elapsed, bool pushButtonPressed, const MSPhaseDefinition* stage) {
40  //pushbutton logic
41  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  return true;
49  }
50  return false;
51 }
52 
53 void SigmoidLogic::init(std::string prefix, const Parameterised* parameterised) {
54  m_prefix = prefix;
55  m_useSigmoid = parameterised->getParameter("PLATOON_USE_SIGMOID", "0") != "0";
56  m_k = StringUtils::toDouble(parameterised->getParameter("PLATOON_SIGMOID_K_VALUE", "1"));
57 // DBG(
58  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 }
68 
69 bool SigmoidLogic::sigmoidLogic(SUMOTime elapsed, const MSPhaseDefinition* stage, int vehicleCount) {
70  //use the sigmoid logic
71  if (m_useSigmoid && vehicleCount == 0) {
72  double sigmoidValue = 1.0 / (1.0 + exp(-m_k * STEPS2TIME(elapsed - stage->duration)));
73  double rnd = RandHelper::rand();
74 // DBG(
75  std::ostringstream oss;
76  oss << m_prefix << "::sigmoidLogic [k=" << m_k << " elapsed " << elapsed << " stage->duration " << stage->duration << " ] value "
77  << sigmoidValue;
78  oss << " rnd " << rnd << " retval " << (rnd < sigmoidValue ? "true" : "false");
79  WRITE_MESSAGE(oss.str())
80 // );
81  return rnd < sigmoidValue;
82  }
83  return false;
84 }
85 
86 
87 MSSOTLPolicy::MSSOTLPolicy(std::string name,
88  const Parameterised::Map& parameters) :
89  Parameterised(parameters), myName(name) {
91 }
92 
93 MSSOTLPolicy::MSSOTLPolicy(std::string name,
94  MSSOTLPolicyDesirability* desirabilityAlgorithm) :
95  Parameterised(), myName(name), myDesirabilityAlgorithm(
96  desirabilityAlgorithm) {
98 }
99 
100 MSSOTLPolicy::MSSOTLPolicy(std::string name,
101  MSSOTLPolicyDesirability* desirabilityAlgorithm,
102  const Parameterised::Map& parameters) :
103  Parameterised(parameters), myName(name), myDesirabilityAlgorithm(
104  desirabilityAlgorithm) {
105  theta_sensitivity = StringUtils::toDouble(getParameter("THETA_INIT", "0.5"));
106 }
107 
109 }
110 
111 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  return myDesirabilityAlgorithm->computeDesirability(vehInMeasure, vehOutMeasure, vehInDispersionMeasure, vehOutDispersionMeasure);
118 }
119 
120 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  return myDesirabilityAlgorithm->computeDesirability(vehInMeasure, vehOutMeasure, 0, 0);
127 }
128 
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  if (stage->isCommit()) {
137  // decide which chain to activate. Gotta work on this
138  return phaseMaxCTS;
139  }
140  if (stage->isTransient()) {
141  //If the junction was in a transient step
142  //=> go to the next step and return computeReturnTime()
143  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  if (canRelease(elapsed, thresholdPassed, pushButtonPressed, stage, vehicleCount)) {
153  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  */
long long int SUMOTime
Definition: GUI.h:35
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:297
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition: SUMOTime.cpp:69
#define STEPS2TIME(x)
Definition: SUMOTime.h:55
The definition of a single phase of a tls logic.
bool isTransient() const
SUMOTime duration
The duration of the phase.
bool isDecisional() const
This class determines the desirability algorithm of a MSSOTLPolicy when used in combination with a hi...
virtual double computeDesirability(double vehInMeasure, double vehOutMeasure)=0
Calculates the desirability of the policy.
MSSOTLPolicyDesirability * myDesirabilityAlgorithm
A pointer to the policy desirability object.\nIt's an optional component related to the computeDesira...
Definition: MSSOTLPolicy.h:76
virtual ~MSSOTLPolicy()
MSSOTLPolicy(std::string name, const Parameterised::Map &parameters)
Simple constructor.
double theta_sensitivity
The sensitivity of this policy.
Definition: MSSOTLPolicy.h:67
virtual bool canRelease(SUMOTime elapsed, bool thresholdPassed, bool pushButtonPressed, const MSPhaseDefinition *stage, int vehicleCount)=0
double computeDesirability(double vehInMeasure, double vehOutMeasure, double vehInDispersionMeasure, double vehOutDispersionMeasure)
Computes the desirability of this policy, necessary when used in combination with an high level polic...
virtual int decideNextPhase(SUMOTime elapsed, const MSPhaseDefinition *stage, int currentPhaseIndex, int phaseMaxCTS, bool thresholdPassed, bool pushButtonPressed, int vehicleCount)
std::string getName()
Definition: MSSOTLPolicy.h:114
An upper class for objects with additional parameters.
Definition: Parameterised.h:41
std::map< std::string, std::string > Map
parameters map
Definition: Parameterised.h:45
virtual const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
std::string m_prefix
Definition: MSSOTLPolicy.h:42
double m_pushButtonScaleFactor
Definition: MSSOTLPolicy.h:41
bool pushButtonLogic(SUMOTime elapsed, bool pushButtonPressed, const MSPhaseDefinition *stage)
void init(std::string prefix, const Parameterised *parameterised)
static double rand(SumoRNG *rng=nullptr)
Returns a random real number in [0, 1)
Definition: RandHelper.cpp:94
bool m_useSigmoid
Definition: MSSOTLPolicy.h:51
void init(std::string prefix, const Parameterised *parameterised)
std::string m_prefix
Definition: MSSOTLPolicy.h:53
bool sigmoidLogic(SUMOTime elapsed, const MSPhaseDefinition *stage, int vehicleCount)
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter