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