SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MSSwarmTrafficLightLogic.h
Go to the documentation of this file.
1 /****************************************************************************/
8 // The class for Swarm-based logics
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright 2001-2009 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 #ifndef MSSwarmTrafficLightLogic_h
22 #define MSSwarmTrafficLightLogic_h
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 //#define SWARM_DEBUG
36 #include "MSSOTLPhasePolicy.h"
37 #include "MSSOTLPlatoonPolicy.h"
38 #include "MSSOTLMarchingPolicy.h"
39 #include "MSSOTLCongestionPolicy.h"
40 #include "MSSOTLPolicy3DStimulus.h"
42 
43 template<class T>
45 public:
47  m_size(size), m_currentIndex(0), m_firstTime(true) {
48  m_buffer = new T[m_size];
49  }
50 
51  virtual ~CircularBuffer() {
52  delete m_buffer;
53  }
54 
55  bool addValue(const T newValue, T& replacedValue) {
56  bool result = !m_firstTime;
57  if (result) {
58  replacedValue = m_buffer[m_currentIndex];
59  }
60  insert(newValue);
61  return result;
62  }
63 
64  void push_front(const T value) {
65  insert(value);
66  }
67 
68  T at(const int index) const {
69  int idx = (m_currentIndex - 1 - index + m_size) % m_size;
70  return m_buffer[idx];
71  }
72 
73  T front() const {
74  return at(0);
75  }
76 
77  T back() const {
78  return at(size() - 1);
79  }
80 
81  int size() const {
82  if (m_firstTime) {
83  return m_currentIndex;
84  }
85  return m_size;
86  }
87 
88  void clear() {
89  m_currentIndex = 0;
90  m_firstTime = true;
91  }
92 
93 private:
95  int m_size;
98 
99  inline void insert(const T& value) {
100  m_buffer[m_currentIndex++] = value;
101  if (m_currentIndex == m_size) {
102  m_currentIndex = 0;
103  m_firstTime = false;
104  }
105  }
106 };
107 
109 public:
110  //****************************************************
121  MSSwarmTrafficLightLogic(MSTLLogicControl& tlcontrol, const std::string& id,
122  const std::string& subid, const Phases& phases, int step,
123  SUMOTime delay,
124  const std::map<std::string, std::string>& parameters);
125 
127 
134  void init(NLDetectorBuilder& nb) throw(ProcessError);
135 
137  return TplConvert::_2int(getParameter("MAX_CONGESTION_DUR", "120").c_str());
138  }
139 
141  return TplConvert::_2SUMOReal(getParameter("PHERO_MAXVAL", "10").c_str());
142  }
143 
145  return TplConvert::_2SUMOReal(getParameter("BETA_NO", "0.99").c_str());
146  }
147 
149  return TplConvert::_2SUMOReal(getParameter("GAMMA_NO", "1.0").c_str());
150  }
151 
153  return TplConvert::_2SUMOReal(getParameter("BETA_SP", "0.99").c_str());
154  }
155 
157  return TplConvert::_2SUMOReal(getParameter("GAMMA_SP", "1.0").c_str());
158  }
159 
161  return TplConvert::_2SUMOReal(getParameter("CHANGE_PLAN_PROBABILITY", "0.003").c_str());
162  }
163 
165  return TplConvert::_2SUMOReal(getParameter("THETA_MAX", "0.8").c_str());
166  }
167 
169  return TplConvert::_2SUMOReal(getParameter("THETA_MIN", "0.2").c_str());
170  }
171 
173  return TplConvert::_2SUMOReal(getParameter("THETA_INIT", "0.5").c_str());
174  }
175 
177  return TplConvert::_2SUMOReal(getParameter("LEARNING_COX", "0.0005").c_str());
178  }
179 
181  return TplConvert::_2SUMOReal(getParameter("FORGETTING_COX", "0.0005").c_str());
182  }
183 
186  }
187 
190  }
191 
195  const std::string getLogicType() const {
196  return "swarmBasedTrafficLogic";
197  }
199 
200 protected:
208 
216 
223 
224  /*
225  * This member has to contain the switching logic for SOTL policies
226  */
227 
228  int decideNextPhase();
229 
230  bool canRelease();
231 
232  /*
233  * Computes how much time will pass after decideNextPhase will be executed again
234  */
236 
237  return DELTA_T;
238 
239  }
240 
244  void resetPheromone();
245 
246  /*
247  * @return The average pheromone level regarding congestion on input lanes
248  */
250 
251  /*
252  * @return The average pheromone level regarding congestion on output lanes
253  */
255 
256  /*
257  * @return The dispersion level regarding congestion on input lanes
258  */
259  SUMOReal getDispersionForInputLanes(SUMOReal average_phero_in);
260 
261  /*
262  * @return The dispersion level regarding congestion on output lanes
263  */
264  SUMOReal getDispersionForOutputLanes(SUMOReal average_phero_out);
265 
266  /*
267  * @return The difference between the current max phero value and the average phero of the other lanes
268  */
270 
271  /*
272  * @return The difference between the current max phero value and the average phero of the other lanes
273  */
280  void updatePheromoneLevels();
281 
285  void updatePheromoneLevels(MSLaneId_PheromoneMap&, std::string, const SUMOReal, const SUMOReal);
286 
292  void updateSensitivities();
293 
298  void decidePolicy();
299 
306  SUMOReal calculatePhi(int factor);
307 
315 
317 
318  /*
319  * \brief Method to reset the map that stores if a lane is already been checked during the
320  * evaluation of eta.
321  */
322  void resetLaneCheck();
323  void choosePolicy(SUMOReal phero_in, SUMOReal phero_out, SUMOReal dispersion_in, SUMOReal dispersion_out);
324  void choosePolicy(SUMOReal phero_in, SUMOReal phero_out);
325 
326  std::string getPoliciesParam() {
327  return getParameter("POLICIES", "Platoon;Phase;Marching;Congestion");
328  }
329 
330  /*
331  * Reinforcement modes:
332  * 0-> elapsed time
333  * 1-> diff
334  * 2-> ratio
335  */
337  return TplConvert::_2int(getParameter("REIMODE", "0").c_str());
338  }
339 
340  void initScaleFactorDispersionIn(int lanes_in) {
341  std::vector<SUMOReal> phero_values;
342 
343  for (int i = 0; i < lanes_in / 2; i++) {
344  phero_values.push_back(getPheroMaxVal());
345  }
346  for (int i = lanes_in / 2; i < lanes_in; i++) {
347  phero_values.push_back(0.0);
348  }
349 
350  SUMOReal sum_avg_tmp = 0;
351 
352  for (int i = 0; i < (int)phero_values.size(); i++) {
353  sum_avg_tmp += phero_values[i];
354  }
355 
356  SUMOReal mean = sum_avg_tmp / phero_values.size();
357 
358  SUMOReal sum_dev_tmp = 0;
359  for (int i = 0; i < (int)phero_values.size(); i++) {
360  sum_dev_tmp += pow(phero_values[i] - mean, 2);
361  }
362 
363  SUMOReal deviation = sqrt(sum_dev_tmp / phero_values.size());
364 
365  scaleFactorDispersionIn = getPheroMaxVal() / deviation;
366  }
367 
368  void initScaleFactorDispersionOut(int lanes_out) {
369  std::vector<SUMOReal> phero_values;
370 
371  for (int i = 0; i < lanes_out / 2; i++) {
372  phero_values.push_back(getPheroMaxVal());
373  }
374  for (int i = lanes_out / 2; i < lanes_out; i++) {
375  phero_values.push_back(0.0);
376  }
377 
378  SUMOReal sum_avg_tmp = 0;
379  for (int i = 0; i < (int)phero_values.size(); i++) {
380  sum_avg_tmp += phero_values[i];
381  }
382  SUMOReal mean = sum_avg_tmp / phero_values.size();
383 
384  SUMOReal sum_dev_tmp = 0;
385 
386  for (int i = 0; i < (int)phero_values.size(); i++) {
387  sum_dev_tmp += pow(phero_values[i] - mean, 2);
388  }
389 
390  SUMOReal deviation = sqrt(sum_dev_tmp / phero_values.size());
391 
393  }
394 
400  bool allowLine(MSLane*);
401 
402  bool logData;
403  std::ofstream swarmLogFile;
410 
423  bool skipEta;
429 
430  int carsIn;
431  int carsOut;
432  int inTarget;
439 
440 // For every lane its index. Esed to get the current lane state for the lane
441  std::map<std::string, std::vector<int> > m_laneIndexMap;
442  std::string getLaneLightState(const std::string& laneId);
443 // store the last message logged. if equal do not log it again
444  std::map<std::string, std::string> m_pheroLevelLog;
445 
446  //derivative
447  std::map<std::string, CircularBuffer<SUMOReal>* > m_meanSpeedHistory;
448  std::map<std::string, CircularBuffer<SUMOReal>* > m_derivativeHistory;
450  int m_losCounter;//los: loss of signal
453 
454 // SUMOReal pheroBegin;
455 };
456 
457 
458 
459 
460 #endif
461 /****************************************************************************/
void initScaleFactorDispersionOut(int lanes_out)
Builds detectors for microsim.
long long int SUMOTime
Definition: SUMOTime.h:43
std::map< std::string, CircularBuffer< SUMOReal > * > m_derivativeHistory
static SUMOReal _2SUMOReal(const E *const data)
converts a char-type array into the SUMOReal value described by it
Definition: TplConvert.h:290
std::map< std::string, CircularBuffer< SUMOReal > * > m_meanSpeedHistory
MSSwarmTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &subid, const Phases &phases, int step, SUMOTime delay, const std::map< std::string, std::string > &parameters)
Constructor without sensors passed.
bool allowLine(MSLane *)
Check if a lane is allowed to be added to the maps pheromoneInputLanes and pheromoneOutputLanes Contr...
SUMOTime DELTA_T
Definition: SUMOTime.cpp:39
void updatePheromoneLevels()
Update pheromone levels Pheromone on input lanes is costantly updated Pheromone follows a discrete-ti...
SUMOReal getDispersionForInputLanes(SUMOReal average_phero_in)
LaneIdVector targetLanes
A copy of the target lanes of this phase.
const std::string & getParameter(const std::string &key, const std::string &defaultValue) const
Returns the value for a given key.
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's used to force the exit from the co...
T at(const int index) const
SUMOReal calculatePhi(int factor)
Method that should calculate the valor of phi a coefficient to amplify/attenuate eta based on a facto...
std::string getLaneLightState(const std::string &laneId)
std::map< std::string, SUMOReal > MSLaneId_PheromoneMap
std::map< std::string, std::string > m_pheroLevelLog
bool skipEta
When true indicates that we can skip the evaluation of eta since we've a congestion policy that is la...
void decidePolicy()
Decide the current policy according to pheromone levels The decision reflects on currentPolicy value...
const std::string getLogicType() const
Returns the type of the logic as a string.
bool gotTargetLane
When true indicates that we've already acquired the target lanes for this particular phase...
SUMOReal getDispersionForOutputLanes(SUMOReal average_phero_out)
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:149
void init(NLDetectorBuilder &nb)
Initialises the tls with sensors on incoming and outgoing lanes Sensors are built in the simulation a...
void insert(const T &value)
void choosePolicy(SUMOReal phero_in, SUMOReal phero_out, SUMOReal dispersion_in, SUMOReal dispersion_out)
MSLaneId_PheromoneMap pheromoneInputLanes
This pheronome is an indicator of congestion on input lanes. Its levels refer to the average speed of...
#define SUMOReal
Definition: config.h:214
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
SUMOReal calculateEtaDiff()
Method that should calculate the valor of eta a coefficient to evaluate the current policy's work...
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
std::map< std::string, std::vector< int > > m_laneIndexMap
void resetPheromone()
Resets pheromone levels.