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