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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // The class for low-level policy
18 /****************************************************************************/
19 
20 #include "MSSOTLPolicy.h"
21 #include <cmath>
22 #include <typeinfo>
24 
25 void PushButtonLogic::init(std::string prefix, const Parameterised* parameterised) {
26  m_prefix = prefix;
27  m_pushButtonScaleFactor = StringUtils::toDouble(parameterised->getParameter("PUSH_BUTTON_SCALE_FACTOR", "1"));
28  WRITE_MESSAGE(m_prefix + "::PushButtonLogic::init use " + parameterised->getParameter("USE_PUSH_BUTTON", "0") + " scale " + parameterised->getParameter("PUSH_BUTTON_SCALE_FACTOR", "1"));
29 }
30 
31 bool PushButtonLogic::pushButtonLogic(SUMOTime elapsed, bool pushButtonPressed, const MSPhaseDefinition* stage) {
32  //pushbutton logic
33  if (pushButtonPressed && elapsed >= (stage->duration * m_pushButtonScaleFactor)) {
34  //If the stage duration has been passed
35 // DBG(
36  std::ostringstream oss;
37  oss << m_prefix << "::pushButtonLogic pushButtonPressed elapsed " << elapsed << " stage duration " << (stage->duration * m_pushButtonScaleFactor);
38  WRITE_MESSAGE(oss.str());
39 // );
40  return true;
41  }
42  return false;
43 }
44 
45 void SigmoidLogic::init(std::string prefix, const Parameterised* parameterised) {
46  m_prefix = prefix;
47  m_useSigmoid = parameterised->getParameter("PLATOON_USE_SIGMOID", "0") != "0";
48  m_k = StringUtils::toDouble(parameterised->getParameter("PLATOON_SIGMOID_K_VALUE", "1"));
49 // DBG(
50  WRITE_MESSAGE(m_prefix + "::SigmoidLogic::init use " + parameterised->getParameter("PLATOON_USE_SIGMOID", "0") + " k " + parameterised->getParameter("PLATOON_SIGMOID_K_VALUE", "1"));
51 // for (int elapsed = 10; elapsed < 51; ++elapsed)
52 // {
53 // double sigmoidValue = 1.0 / (1.0 + exp(-m_k * (elapsed - 31)));
54 // std::ostringstream oss;
55 // oss << "elapsed " << elapsed << " value " << sigmoidValue;
56 // WRITE_MESSAGE(oss.str())
57 // }
58 // )
59 }
60 
61 bool SigmoidLogic::sigmoidLogic(SUMOTime elapsed, const MSPhaseDefinition* stage, int vehicleCount) {
62  //use the sigmoid logic
63  if (m_useSigmoid && vehicleCount == 0) {
64  double sigmoidValue = 1.0 / (1.0 + exp(-m_k * (elapsed / 1000 - stage->duration / 1000)));
65  double rnd = RandHelper::rand();
66 // DBG(
67  std::ostringstream oss;
68  oss << m_prefix << "::sigmoidLogic [k=" << m_k << " elapsed " << elapsed << " stage->duration " << stage->duration << " ] value "
69  << sigmoidValue;
70  oss << " rnd " << rnd << " retval " << (rnd < sigmoidValue ? "true" : "false");
71  WRITE_MESSAGE(oss.str())
72 // );
73  return rnd < sigmoidValue;
74  }
75  return false;
76 }
77 
78 
79 MSSOTLPolicy::MSSOTLPolicy(std::string name,
80  const std::map<std::string, std::string>& parameters) :
81  Parameterised(parameters), myName(name) {
83 }
84 
85 MSSOTLPolicy::MSSOTLPolicy(std::string name,
86  MSSOTLPolicyDesirability* desirabilityAlgorithm) :
88  desirabilityAlgorithm) {
90 }
91 
92 MSSOTLPolicy::MSSOTLPolicy(std::string name,
93  MSSOTLPolicyDesirability* desirabilityAlgorithm,
94  const std::map<std::string, std::string>& parameters) :
95  Parameterised(parameters), myName(name), myDesirabilityAlgorithm(
96  desirabilityAlgorithm) {
98 }
99 
101 }
102 
103 double MSSOTLPolicy::computeDesirability(double vehInMeasure, double vehOutMeasure, double vehInDispersionMeasure, double vehOutDispersionMeasure) {
104 
105  DBG(
106  std::ostringstream str; str << "\nMSSOTLPolicy::computeStimulus\n" << getName(); WRITE_MESSAGE(str.str());)
107 
108  return myDesirabilityAlgorithm->computeDesirability(vehInMeasure, vehOutMeasure, vehInDispersionMeasure, vehOutDispersionMeasure);
109 
110 }
111 
112 double MSSOTLPolicy::computeDesirability(double vehInMeasure, double vehOutMeasure) {
113 
114  DBG(
115  std::ostringstream str; str << "\nMSSOTLPolicy::computeStimulus\n" << getName(); WRITE_MESSAGE(str.str());)
116 
117  return myDesirabilityAlgorithm->computeDesirability(vehInMeasure, vehOutMeasure, 0, 0);
118 
119 }
120 
122  const MSPhaseDefinition* stage, int currentPhaseIndex,
123  int phaseMaxCTS, bool thresholdPassed, bool pushButtonPressed, int vehicleCount) {
124 
125  //If the junction was in a commit step
126  //=> go to the target step that gives green to the set with the current highest CTS
127  // and return computeReturnTime()
128  if (stage->isCommit()) {
129  // decide which chain to activate. Gotta work on this
130  return phaseMaxCTS;
131  }
132  if (stage->isTransient()) {
133  //If the junction was in a transient step
134  //=> go to the next step and return computeReturnTime()
135  return currentPhaseIndex + 1;
136  }
137 
138  if (stage->isDecisional()) {
139  DBG(
140  std::ostringstream phero_str;
141  phero_str << "getCurrentPhaseElapsed()=" << time2string(elapsed) << " isThresholdPassed()=" << thresholdPassed << " countVehicles()=" << vehicleCount;
142  WRITE_MESSAGE("MSSOTLPolicy::decideNextPhase: " + phero_str.str());
143  )
144  if (canRelease(elapsed, thresholdPassed, pushButtonPressed, stage, vehicleCount)) {
145  return currentPhaseIndex + 1;
146  }
147  }
148 
149  return currentPhaseIndex;
150 }
151 
152 /*
153  bool MSSOTLPolicy::canRelease(SUMOTime elapsed, bool thresholdPassed, const MSPhaseDefinition* stage, int vehicleCount) {
154  if (getName().compare("request") == 0) {
155  return elapsed > 3000 && thresholdPassed;
156  } else if (getName().compare("phase") == 0) {
157  return thresholdPassed && elapsed >= stage->minDuration;
158  } else if (getName().compare("platoon") == 0) {
159  return thresholdPassed && (vehicleCount == 0 || elapsed >= stage->maxDuration);
160  } else if (getName().compare("marching") == 0) {
161  return elapsed >= stage->duration;
162  } else if (getName().compare("congestion") == 0) {
163  return elapsed >= stage->minDuration;
164  }
165  return true; //
166 
167  }
168  */
169 
long long int SUMOTime
Definition: SUMOTime.h:36
virtual double computeDesirability(double vehInMeasure, double vehOutMeasure)=0
Calculates the desirability of the policy.
static double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
Definition: RandHelper.h:61
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:65
void init(std::string prefix, const Parameterised *parameterised)
bool pushButtonLogic(SUMOTime elapsed, bool pushButtonPressed, const MSPhaseDefinition *stage)
virtual int decideNextPhase(SUMOTime elapsed, const MSPhaseDefinition *stage, int currentPhaseIndex, int phaseMaxCTS, bool thresholdPassed, bool pushButtonPressed, int vehicleCount)
double theta_sensitivity
The sensitivity of this policy.
Definition: MSSOTLPolicy.h:71
virtual bool canRelease(SUMOTime elapsed, bool thresholdPassed, bool pushButtonPressed, const MSPhaseDefinition *stage, int vehicleCount)=0
bool sigmoidLogic(SUMOTime elapsed, const MSPhaseDefinition *stage, int vehicleCount)
SUMOTime duration
The duration of the phase.
MSSOTLPolicy(std::string name, const std::map< std::string, std::string > &parameters)
Simple constructor.
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter ...
bool isTransient() const
#define DBG(X)
Definition: SwarmDebug.h:27
std::string getName()
Definition: MSSOTLPolicy.h:118
An upper class for objects with additional parameters.
Definition: Parameterised.h:44
void init(std::string prefix, const Parameterised *parameterised)
This class determines the desirability algorithm of a MSSOTLPolicy when used in combination with a hi...
virtual ~MSSOTLPolicy()
double m_pushButtonScaleFactor
Definition: MSSOTLPolicy.h:45
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:242
std::string myName
The name of the policy.
Definition: MSSOTLPolicy.h:75
The definition of a single phase of a tls logic.
std::string m_prefix
Definition: MSSOTLPolicy.h:46
bool isDecisional() const
MSSOTLPolicyDesirability * myDesirabilityAlgorithm
A pointer to the policy desirability object.&#39;s an optional component related to the computeDesirabili...
Definition: MSSOTLPolicy.h:80
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...