Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSSwarmTrafficLightLogic.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2010-2024 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
19// The class for Swarm-based logics
20/****************************************************************************/
21#pragma once
22#include <config.h>
23
25#include "MSSOTLPhasePolicy.h"
26#include "MSSOTLPlatoonPolicy.h"
31
32template<class T>
34public:
37 m_buffer = new T[m_size];
38 }
39
40 virtual ~CircularBuffer() {
41 delete[] m_buffer;
42 }
43
44 bool addValue(const T newValue, T& replacedValue) {
45 bool result = !m_firstTime;
46 if (result) {
47 replacedValue = m_buffer[m_currentIndex];
48 }
49 insert(newValue);
50 return result;
51 }
52
53 void push_front(const T value) {
54 insert(value);
55 }
56
57 T at(const int index) const {
58 int idx = (m_currentIndex - 1 - index + m_size) % m_size;
59 return m_buffer[idx];
60 }
61
62 T front() const {
63 return at(0);
64 }
65
66 T back() const {
67 return at(size() - 1);
68 }
69
70 int size() const {
71 if (m_firstTime) {
72 return m_currentIndex;
73 }
74 return m_size;
75 }
76
77 void clear() {
79 m_firstTime = true;
80 }
81
82private:
84 int m_size;
87
88 inline void insert(const T& value) {
89 m_buffer[m_currentIndex++] = value;
90 if (m_currentIndex == m_size) {
92 m_firstTime = false;
93 }
94 }
95};
96
98public:
99 //****************************************************
110 MSSwarmTrafficLightLogic(MSTLLogicControl& tlcontrol, const std::string& id,
111 const std::string& programID, const Phases& phases, int step,
112 SUMOTime delay,
113 const Parameterised::Map& parameters);
114
116
123 void init(NLDetectorBuilder& nb);
124
126 return StringUtils::toInt(getParameter("MAX_CONGESTION_DUR", "120"));
127 }
128
129 double getPheroMaxVal() {
130 return StringUtils::toDouble(getParameter("PHERO_MAXVAL", "10"));
131 }
132
133 double getBetaNo() {
134 return StringUtils::toDouble(getParameter("BETA_NO", "0.99"));
135 }
136
137 double getGammaNo() {
138 return StringUtils::toDouble(getParameter("GAMMA_NO", "1.0"));
139 }
140
141 double getBetaSp() {
142 return StringUtils::toDouble(getParameter("BETA_SP", "0.99"));
143 }
144
145 double getGammaSp() {
146 return StringUtils::toDouble(getParameter("GAMMA_SP", "1.0"));
147 }
148
150 return StringUtils::toDouble(getParameter("CHANGE_PLAN_PROBABILITY", "0.003"));
151 }
152
153 double getThetaMax() {
154 return StringUtils::toDouble(getParameter("THETA_MAX", "0.8"));
155 }
156
157 double getThetaMin() {
158 return StringUtils::toDouble(getParameter("THETA_MIN", "0.2"));
159 }
160
161 double getThetaInit() {
162 return StringUtils::toDouble(getParameter("THETA_INIT", "0.5"));
163 }
164
165 double getLearningCox() {
166 return StringUtils::toDouble(getParameter("LEARNING_COX", "0.0005"));
167 }
168
170 return StringUtils::toDouble(getParameter("FORGETTING_COX", "0.0005"));
171 }
172
176
180
184 const std::string getLogicType() const {
185 return "swarmBasedTrafficLogic";
186 }
188
189protected:
197
205
212
213 /*
214 * This member has to contain the switching logic for SOTL policies
215 */
216
217 int decideNextPhase();
218
219 bool canRelease();
220
221 /*
222 * Computes how much time will pass after decideNextPhase will be executed again
223 */
225
226 return DELTA_T;
227
228 }
229
233 void resetPheromone();
234
235 /*
236 * @return The average pheromone level regarding congestion on input lanes
237 */
239
240 /*
241 * @return The average pheromone level regarding congestion on output lanes
242 */
244
245 /*
246 * @return The dispersion level regarding congestion on input lanes
247 */
248 double getDispersionForInputLanes(double average_phero_in);
249
250 /*
251 * @return The dispersion level regarding congestion on output lanes
252 */
253 double getDispersionForOutputLanes(double average_phero_out);
254
255 /*
256 * @return The difference between the current max phero value and the average phero of the other lanes
257 */
259
260 /*
261 * @return The difference between the current max phero value and the average phero of the other lanes
262 */
270
274 void updatePheromoneLevels(MSLaneId_PheromoneMap&, std::string, const double, const double);
275
281 void updateSensitivities();
282
287 void decidePolicy();
288
295 double calculatePhi(int factor);
296
303 double calculateEtaDiff();
304
305 double calculateEtaRatio();
306
307 /*
308 * \brief Method to reset the map that stores if a lane is already been checked during the
309 * evaluation of eta.
310 */
311 void resetLaneCheck();
312 void choosePolicy(double phero_in, double phero_out, double dispersion_in, double dispersion_out);
313 void choosePolicy(double phero_in, double phero_out);
314
315 std::string getPoliciesParam() {
316 return getParameter("POLICIES", "Platoon;Phase;Marching;Congestion");
317 }
318
319 /*
320 * Reinforcement modes:
321 * 0-> elapsed time
322 * 1-> diff
323 * 2-> ratio
324 */
326 return StringUtils::toInt(getParameter("REIMODE", "0"));
327 }
328
329 void initScaleFactorDispersionIn(int lanes_in) {
330 std::vector<double> phero_values;
331
332 for (int i = 0; i < lanes_in / 2; i++) {
333 phero_values.push_back(getPheroMaxVal());
334 }
335 for (int i = lanes_in / 2; i < lanes_in; i++) {
336 phero_values.push_back(0.0);
337 }
338
339 double sum_avg_tmp = 0;
340
341 for (int i = 0; i < (int)phero_values.size(); i++) {
342 sum_avg_tmp += phero_values[i];
343 }
344
345 const double mean = sum_avg_tmp / (double)phero_values.size();
346
347 double sum_dev_tmp = 0;
348 for (int i = 0; i < (int)phero_values.size(); i++) {
349 sum_dev_tmp += pow(phero_values[i] - mean, 2);
350 }
351
352 const double deviation = sqrt(sum_dev_tmp / (double)phero_values.size());
353
355 }
356
357 void initScaleFactorDispersionOut(int lanes_out) {
358 std::vector<double> phero_values;
359
360 for (int i = 0; i < lanes_out / 2; i++) {
361 phero_values.push_back(getPheroMaxVal());
362 }
363 for (int i = lanes_out / 2; i < lanes_out; i++) {
364 phero_values.push_back(0.0);
365 }
366
367 double sum_avg_tmp = 0;
368 for (int i = 0; i < (int)phero_values.size(); i++) {
369 sum_avg_tmp += phero_values[i];
370 }
371 const double mean = sum_avg_tmp / (double)phero_values.size();
372
373 double sum_dev_tmp = 0;
374
375 for (int i = 0; i < (int)phero_values.size(); i++) {
376 sum_dev_tmp += pow(phero_values[i] - mean, 2);
377 }
378
379 const double deviation = sqrt(sum_dev_tmp / (double)phero_values.size());
380
382 }
383
389 bool allowLine(MSLane*);
390
392 std::ofstream swarmLogFile;
399
418
428
429// For every lane its index. Esed to get the current lane state for the lane
430 std::map<std::string, std::vector<int> > m_laneIndexMap;
431 std::string getLaneLightState(const std::string& laneId);
432// store the last message logged. if equal do not log it again
434
435 //derivative
436 std::map<std::string, CircularBuffer<double>* > m_meanSpeedHistory;
437 std::map<std::string, CircularBuffer<double>* > m_derivativeHistory;
439 int m_losCounter;//los: loss of signal
442
443// double pheroBegin;
444};
long long int SUMOTime
Definition GUI.h:36
std::map< std::string, double > MSLaneId_PheromoneMap
std::map< MSLane *, bool > LaneCheckMap
std::vector< std::string > LaneIdVector
SUMOTime DELTA_T
Definition SUMOTime.cpp:38
bool addValue(const T newValue, T &replacedValue)
T at(const int index) const
void push_front(const T value)
void insert(const T &value)
Representation of a lane in the micro simulation.
Definition MSLane.h:84
A self-organizing high-level traffic light logic.
virtual const std::string getParameter(const std::string &key, const std::string defaultValue="") const override
gets a parameter
MSLaneId_PheromoneMap pheromoneOutputLanes
This pheromone is an indicator of congestion on output lanes. Its levels refer to the average speed o...
bool mustChange
When true, indicates that the current policy MUST be changed. It's used to force the exit from the co...
bool allowLine(MSLane *)
Check if a lane is allowed to be added to the maps pheromoneInputLanes and pheromoneOutputLanes Contr...
bool gotTargetLane
When true indicates that we've already acquired the target lanes for this particular phase.
void updatePheromoneLevels()
Update pheromone levels Pheromone on input lanes is costantly updated Pheromone follows a discrete-ti...
void choosePolicy(double phero_in, double phero_out, double dispersion_in, double dispersion_out)
void initScaleFactorDispersionIn(int lanes_in)
LaneIdVector targetLanes
A copy of the target lanes of this phase.
double getDispersionForOutputLanes(double average_phero_out)
bool skipEta
When true indicates that we can skip the evaluation of eta since we've a congestion policy that is la...
double calculateEtaDiff()
Method that should calculate the valor of eta a coefficient to evaluate the current policy's work....
std::map< std::string, CircularBuffer< double > * > m_meanSpeedHistory
double getDispersionForInputLanes(double average_phero_in)
std::map< std::string, CircularBuffer< double > * > m_derivativeHistory
void resetPheromone()
Resets pheromone levels.
MSLaneId_PheromoneMap pheromoneInputLanes
This pheronome is an indicator of congestion on input lanes. Its levels refer to the average speed of...
std::string getLaneLightState(const std::string &laneId)
void initScaleFactorDispersionOut(int lanes_out)
std::map< std::string, std::vector< int > > m_laneIndexMap
void decidePolicy()
Decide the current policy according to pheromone levels The decision reflects on currentPolicy value.
LaneCheckMap laneCheck
Map to check if a lane was already controlled during the elaboration of eta.
double calculatePhi(int factor)
Method that should calculate the valor of phi a coefficient to amplify/attenuate eta based on a facto...
void init(NLDetectorBuilder &nb)
Initialises the tls with sensors on incoming and outgoing lanes Sensors are built in the simulation a...
const std::string getLogicType() const
Returns the type of the logic as a string.
A class that stores and controls tls and switching of their programs.
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
Builds detectors for microsim.
std::map< std::string, std::string > Map
parameters map
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter,...