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.org/sumo
3 // Copyright (C) 2013-2020 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 "MSSOTLPolicy.h"
24 #include <cmath>
25 #include <typeinfo>
27 
28 void PushButtonLogic::init(std::string prefix, const Parameterised* parameterised) {
29  m_prefix = prefix;
30  m_pushButtonScaleFactor = StringUtils::toDouble(parameterised->getParameter("PUSH_BUTTON_SCALE_FACTOR", "1"));
31  WRITE_MESSAGE(m_prefix + "::PushButtonLogic::init use " + parameterised->getParameter("USE_PUSH_BUTTON", "0") + " scale " + parameterised->getParameter("PUSH_BUTTON_SCALE_FACTOR", "1"));
32 }
33 
34 bool PushButtonLogic::pushButtonLogic(SUMOTime elapsed, bool pushButtonPressed, const MSPhaseDefinition* stage) {
35  //pushbutton logic
36  if (pushButtonPressed && elapsed >= (stage->duration * m_pushButtonScaleFactor)) {
37  //If the stage duration has been passed
38 // DBG(
39  std::ostringstream oss;
40  oss << m_prefix << "::pushButtonLogic pushButtonPressed elapsed " << elapsed << " stage duration " << (stage->duration * m_pushButtonScaleFactor);
41  WRITE_MESSAGE(oss.str());
42 // );
43  return true;
44  }
45  return false;
46 }
47 
48 void SigmoidLogic::init(std::string prefix, const Parameterised* parameterised) {
49  m_prefix = prefix;
50  m_useSigmoid = parameterised->getParameter("PLATOON_USE_SIGMOID", "0") != "0";
51  m_k = StringUtils::toDouble(parameterised->getParameter("PLATOON_SIGMOID_K_VALUE", "1"));
52 // DBG(
53  WRITE_MESSAGE(m_prefix + "::SigmoidLogic::init use " + parameterised->getParameter("PLATOON_USE_SIGMOID", "0") + " k " + parameterised->getParameter("PLATOON_SIGMOID_K_VALUE", "1"));
54 // for (int elapsed = 10; elapsed < 51; ++elapsed)
55 // {
56 // double sigmoidValue = 1.0 / (1.0 + exp(-m_k * (elapsed - 31)));
57 // std::ostringstream oss;
58 // oss << "elapsed " << elapsed << " value " << sigmoidValue;
59 // WRITE_MESSAGE(oss.str())
60 // }
61 // )
62 }
63 
64 bool SigmoidLogic::sigmoidLogic(SUMOTime elapsed, const MSPhaseDefinition* stage, int vehicleCount) {
65  //use the sigmoid logic
66  if (m_useSigmoid && vehicleCount == 0) {
67  double sigmoidValue = 1.0 / (1.0 + exp(-m_k * (elapsed / 1000 - stage->duration / 1000)));
68  double rnd = RandHelper::rand();
69 // DBG(
70  std::ostringstream oss;
71  oss << m_prefix << "::sigmoidLogic [k=" << m_k << " elapsed " << elapsed << " stage->duration " << stage->duration << " ] value "
72  << sigmoidValue;
73  oss << " rnd " << rnd << " retval " << (rnd < sigmoidValue ? "true" : "false");
74  WRITE_MESSAGE(oss.str())
75 // );
76  return rnd < sigmoidValue;
77  }
78  return false;
79 }
80 
81 
82 MSSOTLPolicy::MSSOTLPolicy(std::string name,
83  const std::map<std::string, std::string>& parameters) :
84  Parameterised(parameters), myName(name) {
86 }
87 
88 MSSOTLPolicy::MSSOTLPolicy(std::string name,
89  MSSOTLPolicyDesirability* desirabilityAlgorithm) :
90  Parameterised(), myName(name), myDesirabilityAlgorithm(
91  desirabilityAlgorithm) {
93 }
94 
95 MSSOTLPolicy::MSSOTLPolicy(std::string name,
96  MSSOTLPolicyDesirability* desirabilityAlgorithm,
97  const std::map<std::string, std::string>& parameters) :
98  Parameterised(parameters), myName(name), myDesirabilityAlgorithm(
99  desirabilityAlgorithm) {
100  theta_sensitivity = StringUtils::toDouble(getParameter("THETA_INIT", "0.5"));
101 }
102 
104 }
105 
106 double MSSOTLPolicy::computeDesirability(double vehInMeasure, double vehOutMeasure, double vehInDispersionMeasure, double vehOutDispersionMeasure) {
107 
108  DBG(
109  std::ostringstream str; str << "\nMSSOTLPolicy::computeStimulus\n" << getName(); WRITE_MESSAGE(str.str());)
110 
111  return myDesirabilityAlgorithm->computeDesirability(vehInMeasure, vehOutMeasure, vehInDispersionMeasure, vehOutDispersionMeasure);
112 
113 }
114 
115 double MSSOTLPolicy::computeDesirability(double vehInMeasure, double vehOutMeasure) {
116 
117  DBG(
118  std::ostringstream str; str << "\nMSSOTLPolicy::computeStimulus\n" << getName(); WRITE_MESSAGE(str.str());)
119 
120  return myDesirabilityAlgorithm->computeDesirability(vehInMeasure, vehOutMeasure, 0, 0);
121 
122 }
123 
125  const MSPhaseDefinition* stage, int currentPhaseIndex,
126  int phaseMaxCTS, bool thresholdPassed, bool pushButtonPressed, int vehicleCount) {
127 
128  //If the junction was in a commit step
129  //=> go to the target step that gives green to the set with the current highest CTS
130  // and return computeReturnTime()
131  if (stage->isCommit()) {
132  // decide which chain to activate. Gotta work on this
133  return phaseMaxCTS;
134  }
135  if (stage->isTransient()) {
136  //If the junction was in a transient step
137  //=> go to the next step and return computeReturnTime()
138  return currentPhaseIndex + 1;
139  }
140 
141  if (stage->isDecisional()) {
142  DBG(
143  std::ostringstream phero_str;
144  phero_str << "getCurrentPhaseElapsed()=" << time2string(elapsed) << " isThresholdPassed()=" << thresholdPassed << " countVehicles()=" << vehicleCount;
145  WRITE_MESSAGE("MSSOTLPolicy::decideNextPhase: " + phero_str.str());
146  )
147  if (canRelease(elapsed, thresholdPassed, pushButtonPressed, stage, vehicleCount)) {
148  return currentPhaseIndex + 1;
149  }
150  }
151 
152  return currentPhaseIndex;
153 }
154 
155 /*
156  bool MSSOTLPolicy::canRelease(SUMOTime elapsed, bool thresholdPassed, const MSPhaseDefinition* stage, int vehicleCount) {
157  if (getName().compare("request") == 0) {
158  return elapsed > 3000 && thresholdPassed;
159  } else if (getName().compare("phase") == 0) {
160  return thresholdPassed && elapsed >= stage->minDuration;
161  } else if (getName().compare("platoon") == 0) {
162  return thresholdPassed && (vehicleCount == 0 || elapsed >= stage->maxDuration);
163  } else if (getName().compare("marching") == 0) {
164  return elapsed >= stage->duration;
165  } else if (getName().compare("congestion") == 0) {
166  return elapsed >= stage->minDuration;
167  }
168  return true; //
169 
170  }
171  */
172 
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:278
std::string time2string(SUMOTime t)
convert SUMOTime to string
Definition: SUMOTime.cpp:68
long long int SUMOTime
Definition: SUMOTime.h:31
#define DBG(X)
Definition: SwarmDebug.h:30
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:78
MSSOTLPolicy(std::string name, const std::map< std::string, std::string > &parameters)
Simple constructor.
virtual ~MSSOTLPolicy()
double theta_sensitivity
The sensitivity of this policy.
Definition: MSSOTLPolicy.h:69
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:116
An upper class for objects with additional parameters.
Definition: Parameterised.h:39
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:44
double m_pushButtonScaleFactor
Definition: MSSOTLPolicy.h:43
bool pushButtonLogic(SUMOTime elapsed, bool pushButtonPressed, const MSPhaseDefinition *stage)
void init(std::string prefix, const Parameterised *parameterised)
static double rand(std::mt19937 *rng=nullptr)
Returns a random real number in [0, 1)
Definition: RandHelper.h:51
bool m_useSigmoid
Definition: MSSOTLPolicy.h:53
void init(std::string prefix, const Parameterised *parameterised)
std::string m_prefix
Definition: MSSOTLPolicy.h:55
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