SUMO - Simulation of Urban MObility
MSSOTLPolicy.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // The class for low-level policy
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2013-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 #include "MSSOTLPolicy.h"
24 #include <math.h>
25 #include <typeinfo>
27 
28 void PushButtonLogic::init(std::string prefix, const Parameterised* parameterised) {
29  m_prefix = prefix;
30  m_pushButtonScaleFactor = TplConvert::_2double(parameterised->getParameter("PUSH_BUTTON_SCALE_FACTOR", "1").c_str());
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 = TplConvert::_2double(parameterised->getParameter("PLATOON_SIGMOID_K_VALUE", "1").c_str());
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) :
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 = TplConvert::_2double(getParameter("THETA_INIT", "0.5").c_str());
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 
virtual double computeDesirability(double vehInMeasure, double vehOutMeasure)=0
Calculates the desirability of the policy.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:60
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:78
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
static double rand()
Returns a random real number in [0, 1)
Definition: RandHelper.h:62
#define DBG(X)
Definition: SwarmDebug.h:30
std::string getName()
Definition: MSSOTLPolicy.h:125
An upper class for objects with additional parameters.
Definition: Parameterised.h:51
void init(std::string prefix, const Parameterised *parameterised)
This class determines the desirability algorithm of a MSSOTLPolicy when used in combination with a hi...
const std::string & getParameter(const std::string &key, const std::string &defaultValue) const
Returns the value for a given key.
virtual ~MSSOTLPolicy()
double m_pushButtonScaleFactor
Definition: MSSOTLPolicy.h:52
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:297
long long int SUMOTime
Definition: TraCIDefs.h:52
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
std::string myName
The name of the policy.
Definition: MSSOTLPolicy.h:82
The definition of a single phase of a tls logic.
std::string m_prefix
Definition: MSSOTLPolicy.h:53
bool isDecisional() const
MSSOTLPolicyDesirability * myDesirabilityAlgorithm
A pointer to the policy desirability object.&#39;s an optional component related to the computeDesirabili...
Definition: MSSOTLPolicy.h:87
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...