Eclipse SUMO - Simulation of Urban MObility
MSSwarmTrafficLightLogic.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2010-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 Swarm-based logics
17 /****************************************************************************/
18 #ifndef MSSwarmTrafficLightLogic_h
19 #define MSSwarmTrafficLightLogic_h
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 //#define SWARM_DEBUG
29 #include "MSSOTLPhasePolicy.h"
30 #include "MSSOTLPlatoonPolicy.h"
31 #include "MSSOTLMarchingPolicy.h"
32 #include "MSSOTLCongestionPolicy.h"
33 #include "MSSOTLPolicy3DStimulus.h"
35 
36 template<class T>
38 public:
40  m_size(size), m_currentIndex(0), m_firstTime(true) {
41  m_buffer = new T[m_size];
42  }
43 
44  virtual ~CircularBuffer() {
45  delete[] m_buffer;
46  }
47 
48  bool addValue(const T newValue, T& replacedValue) {
49  bool result = !m_firstTime;
50  if (result) {
51  replacedValue = m_buffer[m_currentIndex];
52  }
53  insert(newValue);
54  return result;
55  }
56 
57  void push_front(const T value) {
58  insert(value);
59  }
60 
61  T at(const int index) const {
62  int idx = (m_currentIndex - 1 - index + m_size) % m_size;
63  return m_buffer[idx];
64  }
65 
66  T front() const {
67  return at(0);
68  }
69 
70  T back() const {
71  return at(size() - 1);
72  }
73 
74  int size() const {
75  if (m_firstTime) {
76  return m_currentIndex;
77  }
78  return m_size;
79  }
80 
81  void clear() {
82  m_currentIndex = 0;
83  m_firstTime = true;
84  }
85 
86 private:
88  int m_size;
91 
92  inline void insert(const T& value) {
93  m_buffer[m_currentIndex++] = value;
94  if (m_currentIndex == m_size) {
95  m_currentIndex = 0;
96  m_firstTime = false;
97  }
98  }
99 };
100 
102 public:
103  //****************************************************
114  MSSwarmTrafficLightLogic(MSTLLogicControl& tlcontrol, const std::string& id,
115  const std::string& programID, const Phases& phases, int step,
116  SUMOTime delay,
117  const std::map<std::string, std::string>& parameters);
118 
120 
127  void init(NLDetectorBuilder& nb);
128 
130  return StringUtils::toInt(getParameter("MAX_CONGESTION_DUR", "120"));
131  }
132 
133  double getPheroMaxVal() {
134  return StringUtils::toDouble(getParameter("PHERO_MAXVAL", "10"));
135  }
136 
137  double getBetaNo() {
138  return StringUtils::toDouble(getParameter("BETA_NO", "0.99"));
139  }
140 
141  double getGammaNo() {
142  return StringUtils::toDouble(getParameter("GAMMA_NO", "1.0"));
143  }
144 
145  double getBetaSp() {
146  return StringUtils::toDouble(getParameter("BETA_SP", "0.99"));
147  }
148 
149  double getGammaSp() {
150  return StringUtils::toDouble(getParameter("GAMMA_SP", "1.0"));
151  }
152 
154  return StringUtils::toDouble(getParameter("CHANGE_PLAN_PROBABILITY", "0.003"));
155  }
156 
157  double getThetaMax() {
158  return StringUtils::toDouble(getParameter("THETA_MAX", "0.8"));
159  }
160 
161  double getThetaMin() {
162  return StringUtils::toDouble(getParameter("THETA_MIN", "0.2"));
163  }
164 
165  double getThetaInit() {
166  return StringUtils::toDouble(getParameter("THETA_INIT", "0.5"));
167  }
168 
169  double getLearningCox() {
170  return StringUtils::toDouble(getParameter("LEARNING_COX", "0.0005"));
171  }
172 
173  double getForgettingCox() {
174  return StringUtils::toDouble(getParameter("FORGETTING_COX", "0.0005"));
175  }
176 
178  return scaleFactorDispersionIn;
179  }
180 
182  return scaleFactorDispersionOut;
183  }
184 
188  const std::string getLogicType() const {
189  return "swarmBasedTrafficLogic";
190  }
192 
193 protected:
201 
209 
216 
217  /*
218  * This member has to contain the switching logic for SOTL policies
219  */
220 
221  int decideNextPhase();
222 
223  bool canRelease();
224 
225  /*
226  * Computes how much time will pass after decideNextPhase will be executed again
227  */
229 
230  return DELTA_T;
231 
232  }
233 
237  void resetPheromone();
238 
239  /*
240  * @return The average pheromone level regarding congestion on input lanes
241  */
242  double getPheromoneForInputLanes();
243 
244  /*
245  * @return The average pheromone level regarding congestion on output lanes
246  */
247  double getPheromoneForOutputLanes();
248 
249  /*
250  * @return The dispersion level regarding congestion on input lanes
251  */
252  double getDispersionForInputLanes(double average_phero_in);
253 
254  /*
255  * @return The dispersion level regarding congestion on output lanes
256  */
257  double getDispersionForOutputLanes(double average_phero_out);
258 
259  /*
260  * @return The difference between the current max phero value and the average phero of the other lanes
261  */
262  double getDistanceOfMaxPheroForInputLanes();
263 
264  /*
265  * @return The difference between the current max phero value and the average phero of the other lanes
266  */
267  double getDistanceOfMaxPheroForOutputLanes();
273  void updatePheromoneLevels();
274 
278  void updatePheromoneLevels(MSLaneId_PheromoneMap&, std::string, const double, const double);
279 
285  void updateSensitivities();
286 
291  void decidePolicy();
292 
299  double calculatePhi(int factor);
300 
307  double calculateEtaDiff();
308 
309  double calculateEtaRatio();
310 
311  /*
312  * \brief Method to reset the map that stores if a lane is already been checked during the
313  * evaluation of eta.
314  */
315  void resetLaneCheck();
316  void choosePolicy(double phero_in, double phero_out, double dispersion_in, double dispersion_out);
317  void choosePolicy(double phero_in, double phero_out);
318 
319  std::string getPoliciesParam() {
320  return getParameter("POLICIES", "Platoon;Phase;Marching;Congestion");
321  }
322 
323  /*
324  * Reinforcement modes:
325  * 0-> elapsed time
326  * 1-> diff
327  * 2-> ratio
328  */
330  return StringUtils::toInt(getParameter("REIMODE", "0"));
331  }
332 
333  void initScaleFactorDispersionIn(int lanes_in) {
334  std::vector<double> phero_values;
335 
336  for (int i = 0; i < lanes_in / 2; i++) {
337  phero_values.push_back(getPheroMaxVal());
338  }
339  for (int i = lanes_in / 2; i < lanes_in; i++) {
340  phero_values.push_back(0.0);
341  }
342 
343  double sum_avg_tmp = 0;
344 
345  for (int i = 0; i < (int)phero_values.size(); i++) {
346  sum_avg_tmp += phero_values[i];
347  }
348 
349  double mean = sum_avg_tmp / phero_values.size();
350 
351  double sum_dev_tmp = 0;
352  for (int i = 0; i < (int)phero_values.size(); i++) {
353  sum_dev_tmp += pow(phero_values[i] - mean, 2);
354  }
355 
356  double deviation = sqrt(sum_dev_tmp / phero_values.size());
357 
358  scaleFactorDispersionIn = getPheroMaxVal() / deviation;
359  }
360 
361  void initScaleFactorDispersionOut(int lanes_out) {
362  std::vector<double> phero_values;
363 
364  for (int i = 0; i < lanes_out / 2; i++) {
365  phero_values.push_back(getPheroMaxVal());
366  }
367  for (int i = lanes_out / 2; i < lanes_out; i++) {
368  phero_values.push_back(0.0);
369  }
370 
371  double sum_avg_tmp = 0;
372  for (int i = 0; i < (int)phero_values.size(); i++) {
373  sum_avg_tmp += phero_values[i];
374  }
375  double mean = sum_avg_tmp / phero_values.size();
376 
377  double sum_dev_tmp = 0;
378 
379  for (int i = 0; i < (int)phero_values.size(); i++) {
380  sum_dev_tmp += pow(phero_values[i] - mean, 2);
381  }
382 
383  double deviation = sqrt(sum_dev_tmp / phero_values.size());
384 
385  scaleFactorDispersionOut = getPheroMaxVal() / deviation;
386  }
387 
393  bool allowLine(MSLane*);
394 
395  bool logData;
396  std::ofstream swarmLogFile;
403 
416  bool skipEta;
422 
423  int carsIn;
424  int carsOut;
425  int inTarget;
432 
433 // For every lane its index. Esed to get the current lane state for the lane
434  std::map<std::string, std::vector<int> > m_laneIndexMap;
435  std::string getLaneLightState(const std::string& laneId);
436 // store the last message logged. if equal do not log it again
437  std::map<std::string, std::string> m_pheroLevelLog;
438 
439  //derivative
440  std::map<std::string, CircularBuffer<double>* > m_meanSpeedHistory;
441  std::map<std::string, CircularBuffer<double>* > m_derivativeHistory;
443  int m_losCounter;//los: loss of signal
446 
447 // double pheroBegin;
448 };
449 
450 
451 
452 
453 #endif
454 /****************************************************************************/
std::map< std::string, double > MSLaneId_PheromoneMap
void initScaleFactorDispersionOut(int lanes_out)
Builds detectors for microsim.
long long int SUMOTime
Definition: SUMOTime.h:35
SUMOTime DELTA_T
Definition: SUMOTime.cpp:35
LaneIdVector targetLanes
A copy of the target lanes of this phase.
const std::string getLogicType() const
Returns the type of the logic as a string.
T at(const int index) const
std::map< MSLane *, bool > LaneCheckMap
A self-organizing high-level traffic light logic.
bool addValue(const T newValue, T &replacedValue)
A class that stores and controls tls and switching of their programs.
bool mustChange
When true, indicates that the current policy MUST be changed. It&#39;s used to force the exit from the co...
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter ...
std::map< std::string, std::string > m_pheroLevelLog
bool skipEta
When true indicates that we can skip the evaluation of eta since we&#39;ve a congestion policy that is la...
std::map< std::string, CircularBuffer< double > *> m_meanSpeedHistory
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter...
bool gotTargetLane
When true indicates that we&#39;ve already acquired the target lanes for this particular phase...
void push_front(const T value)
MSLaneId_PheromoneMap pheromoneOutputLanes
This pheromone is an indicator of congestion on output lanes. Its levels refer to the average speed o...
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
void insert(const T &value)
MSLaneId_PheromoneMap pheromoneInputLanes
This pheronome is an indicator of congestion on input lanes. Its levels refer to the average speed of...
void initScaleFactorDispersionIn(int lanes_in)
LaneCheckMap laneCheck
Map to check if a lane was already controlled during the elaboration of eta.
std::vector< std::string > LaneIdVector
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
std::map< std::string, std::vector< int > > m_laneIndexMap
std::map< std::string, CircularBuffer< double > *> m_derivativeHistory