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-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 /****************************************************************************/
18 // The class for Swarm-based logics
19 /****************************************************************************/
20 #ifndef MSSwarmTrafficLightLogic_h
21 #define MSSwarmTrafficLightLogic_h
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 //#define SWARM_DEBUG
35 #include "MSSOTLPhasePolicy.h"
36 #include "MSSOTLPlatoonPolicy.h"
37 #include "MSSOTLMarchingPolicy.h"
38 #include "MSSOTLCongestionPolicy.h"
39 #include "MSSOTLPolicy3DStimulus.h"
41 
42 template<class T>
44 public:
46  m_size(size), m_currentIndex(0), m_firstTime(true) {
47  m_buffer = new T[m_size];
48  }
49 
50  virtual ~CircularBuffer() {
51  delete m_buffer;
52  }
53 
54  bool addValue(const T newValue, T& replacedValue) {
55  bool result = !m_firstTime;
56  if (result) {
57  replacedValue = m_buffer[m_currentIndex];
58  }
59  insert(newValue);
60  return result;
61  }
62 
63  void push_front(const T value) {
64  insert(value);
65  }
66 
67  T at(const int index) const {
68  int idx = (m_currentIndex - 1 - index + m_size) % m_size;
69  return m_buffer[idx];
70  }
71 
72  T front() const {
73  return at(0);
74  }
75 
76  T back() const {
77  return at(size() - 1);
78  }
79 
80  int size() const {
81  if (m_firstTime) {
82  return m_currentIndex;
83  }
84  return m_size;
85  }
86 
87  void clear() {
88  m_currentIndex = 0;
89  m_firstTime = true;
90  }
91 
92 private:
94  int m_size;
97 
98  inline void insert(const T& value) {
99  m_buffer[m_currentIndex++] = value;
100  if (m_currentIndex == m_size) {
101  m_currentIndex = 0;
102  m_firstTime = false;
103  }
104  }
105 };
106 
108 public:
109  //****************************************************
120  MSSwarmTrafficLightLogic(MSTLLogicControl& tlcontrol, const std::string& id,
121  const std::string& subid, const Phases& phases, int step,
122  SUMOTime delay,
123  const std::map<std::string, std::string>& parameters);
124 
126 
133  void init(NLDetectorBuilder& nb) throw(ProcessError);
134 
136  return TplConvert::_2int(getParameter("MAX_CONGESTION_DUR", "120").c_str());
137  }
138 
139  double getPheroMaxVal() {
140  return TplConvert::_2double(getParameter("PHERO_MAXVAL", "10").c_str());
141  }
142 
143  double getBetaNo() {
144  return TplConvert::_2double(getParameter("BETA_NO", "0.99").c_str());
145  }
146 
147  double getGammaNo() {
148  return TplConvert::_2double(getParameter("GAMMA_NO", "1.0").c_str());
149  }
150 
151  double getBetaSp() {
152  return TplConvert::_2double(getParameter("BETA_SP", "0.99").c_str());
153  }
154 
155  double getGammaSp() {
156  return TplConvert::_2double(getParameter("GAMMA_SP", "1.0").c_str());
157  }
158 
160  return TplConvert::_2double(getParameter("CHANGE_PLAN_PROBABILITY", "0.003").c_str());
161  }
162 
163  double getThetaMax() {
164  return TplConvert::_2double(getParameter("THETA_MAX", "0.8").c_str());
165  }
166 
167  double getThetaMin() {
168  return TplConvert::_2double(getParameter("THETA_MIN", "0.2").c_str());
169  }
170 
171  double getThetaInit() {
172  return TplConvert::_2double(getParameter("THETA_INIT", "0.5").c_str());
173  }
174 
175  double getLearningCox() {
176  return TplConvert::_2double(getParameter("LEARNING_COX", "0.0005").c_str());
177  }
178 
179  double getForgettingCox() {
180  return TplConvert::_2double(getParameter("FORGETTING_COX", "0.0005").c_str());
181  }
182 
184  return scaleFactorDispersionIn;
185  }
186 
188  return scaleFactorDispersionOut;
189  }
190 
194  const std::string getLogicType() const {
195  return "swarmBasedTrafficLogic";
196  }
198 
199 protected:
207 
215 
222 
223  /*
224  * This member has to contain the switching logic for SOTL policies
225  */
226 
227  int decideNextPhase();
228 
229  bool canRelease();
230 
231  /*
232  * Computes how much time will pass after decideNextPhase will be executed again
233  */
235 
236  return DELTA_T;
237 
238  }
239 
243  void resetPheromone();
244 
245  /*
246  * @return The average pheromone level regarding congestion on input lanes
247  */
248  double getPheromoneForInputLanes();
249 
250  /*
251  * @return The average pheromone level regarding congestion on output lanes
252  */
253  double getPheromoneForOutputLanes();
254 
255  /*
256  * @return The dispersion level regarding congestion on input lanes
257  */
258  double getDispersionForInputLanes(double average_phero_in);
259 
260  /*
261  * @return The dispersion level regarding congestion on output lanes
262  */
263  double getDispersionForOutputLanes(double average_phero_out);
264 
265  /*
266  * @return The difference between the current max phero value and the average phero of the other lanes
267  */
268  double getDistanceOfMaxPheroForInputLanes();
269 
270  /*
271  * @return The difference between the current max phero value and the average phero of the other lanes
272  */
273  double getDistanceOfMaxPheroForOutputLanes();
279  void updatePheromoneLevels();
280 
284  void updatePheromoneLevels(MSLaneId_PheromoneMap&, std::string, const double, const double);
285 
291  void updateSensitivities();
292 
297  void decidePolicy();
298 
305  double calculatePhi(int factor);
306 
313  double calculateEtaDiff();
314 
315  double calculateEtaRatio();
316 
317  /*
318  * \brief Method to reset the map that stores if a lane is already been checked during the
319  * evaluation of eta.
320  */
321  void resetLaneCheck();
322  void choosePolicy(double phero_in, double phero_out, double dispersion_in, double dispersion_out);
323  void choosePolicy(double phero_in, double phero_out);
324 
325  std::string getPoliciesParam() {
326  return getParameter("POLICIES", "Platoon;Phase;Marching;Congestion");
327  }
328 
329  /*
330  * Reinforcement modes:
331  * 0-> elapsed time
332  * 1-> diff
333  * 2-> ratio
334  */
336  return TplConvert::_2int(getParameter("REIMODE", "0").c_str());
337  }
338 
339  void initScaleFactorDispersionIn(int lanes_in) {
340  std::vector<double> phero_values;
341 
342  for (int i = 0; i < lanes_in / 2; i++) {
343  phero_values.push_back(getPheroMaxVal());
344  }
345  for (int i = lanes_in / 2; i < lanes_in; i++) {
346  phero_values.push_back(0.0);
347  }
348 
349  double sum_avg_tmp = 0;
350 
351  for (int i = 0; i < (int)phero_values.size(); i++) {
352  sum_avg_tmp += phero_values[i];
353  }
354 
355  double mean = sum_avg_tmp / phero_values.size();
356 
357  double sum_dev_tmp = 0;
358  for (int i = 0; i < (int)phero_values.size(); i++) {
359  sum_dev_tmp += pow(phero_values[i] - mean, 2);
360  }
361 
362  double deviation = sqrt(sum_dev_tmp / phero_values.size());
363 
364  scaleFactorDispersionIn = getPheroMaxVal() / deviation;
365  }
366 
367  void initScaleFactorDispersionOut(int lanes_out) {
368  std::vector<double> phero_values;
369 
370  for (int i = 0; i < lanes_out / 2; i++) {
371  phero_values.push_back(getPheroMaxVal());
372  }
373  for (int i = lanes_out / 2; i < lanes_out; i++) {
374  phero_values.push_back(0.0);
375  }
376 
377  double sum_avg_tmp = 0;
378  for (int i = 0; i < (int)phero_values.size(); i++) {
379  sum_avg_tmp += phero_values[i];
380  }
381  double mean = sum_avg_tmp / phero_values.size();
382 
383  double sum_dev_tmp = 0;
384 
385  for (int i = 0; i < (int)phero_values.size(); i++) {
386  sum_dev_tmp += pow(phero_values[i] - mean, 2);
387  }
388 
389  double deviation = sqrt(sum_dev_tmp / phero_values.size());
390 
391  scaleFactorDispersionOut = getPheroMaxVal() / deviation;
392  }
393 
399  bool allowLine(MSLane*);
400 
401  bool logData;
402  std::ofstream swarmLogFile;
409 
422  bool skipEta;
428 
429  int carsIn;
430  int carsOut;
431  int inTarget;
438 
439 // For every lane its index. Esed to get the current lane state for the lane
440  std::map<std::string, std::vector<int> > m_laneIndexMap;
441  std::string getLaneLightState(const std::string& laneId);
442 // store the last message logged. if equal do not log it again
443  std::map<std::string, std::string> m_pheroLevelLog;
444 
445  //derivative
446  std::map<std::string, CircularBuffer<double>* > m_meanSpeedHistory;
447  std::map<std::string, CircularBuffer<double>* > m_derivativeHistory;
449  int m_losCounter;//los: loss of signal
452 
453 // double pheroBegin;
454 };
455 
456 
457 
458 
459 #endif
460 /****************************************************************************/
std::map< std::string, double > MSLaneId_PheromoneMap
void initScaleFactorDispersionOut(int lanes_out)
Builds detectors for microsim.
SUMOTime DELTA_T
Definition: SUMOTime.cpp:39
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...
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
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.
static int _2int(const E *const data)
converts a char-type array into the integer value described by it
Definition: TplConvert.h:155
void insert(const T &value)
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:311
MSLaneId_PheromoneMap pheromoneInputLanes
This pheronome is an indicator of congestion on input lanes. Its levels refer to the average speed of...
long long int SUMOTime
Definition: TraCIDefs.h:51
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:77
std::map< std::string, std::vector< int > > m_laneIndexMap
std::map< std::string, CircularBuffer< double > *> m_derivativeHistory