SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSAgentbasedTrafficLightLogic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // An agentbased traffic light logic
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
14 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <cassert>
36 #include <utility>
37 #include <vector>
38 #include <bitset>
41 #include "MSTrafficLightLogic.h"
43 #include <microsim/MSLane.h>
44 #include <microsim/MSEdge.h>
45 
46 #ifdef CHECK_MEMORY_LEAKS
47 #include <foreign/nvwa/debug_new.h>
48 #endif // CHECK_MEMORY_LEAKS
49 
50 
51 // ===========================================================================
52 // parameter defaults definitions
53 // ===========================================================================
54 #define DEFAULT_T_DECIDE "1"
55 #define DEFAULT_LEARN_HORIZON "3"
56 #define DEFAULT_CYCLE_TIME "90"
57 #define DEFAULT_MIN_DIFF "1"
58 #define DEFAULT_DETECTOR_OFFSET "0"
59 
60 
61 // ===========================================================================
62 // member method definitions
63 // ===========================================================================
65  MSTLLogicControl& tlcontrol,
66  const std::string& id, const std::string& programID,
67  const Phases& phases, unsigned int step, SUMOTime delay,
68  const std::map<std::string, std::string>& parameter) :
69  MSSimpleTrafficLightLogic(tlcontrol, id, programID, phases, step, delay, parameter),
70  tSinceLastDecision(0), stepOfLastDecision(0) {
71 
72  tDecide = TplConvert::_2int(getParameter("decision-horizon", DEFAULT_T_DECIDE).c_str());
76 }
77 
78 
79 void
82  SUMOReal det_offset = TplConvert::_2SUMOReal(getParameter("detector_offset", DEFAULT_DETECTOR_OFFSET).c_str());
83  LaneVectorVector::const_iterator i2;
84  LaneVector::const_iterator i;
85  // build the detectors
86  for (i2 = myLanes.begin(); i2 != myLanes.end(); ++i2) {
87  const LaneVector& lanes = *i2;
88  for (i = lanes.begin(); i != lanes.end(); i++) {
89  MSLane* lane = (*i);
90  // Build the lane state detetcor and set it into the container
91  std::string id = "TL_" + myID + "_" + myProgramID + "_E2OverLanesDetectorStartingAt_" + lane->getID();
92 
93  if (myE2Detectors.find(lane) == myE2Detectors.end()) {
95  nb.buildMultiLaneE2Det(id,
96  DU_TL_CONTROL, lane, 0, det_offset,
97  /*haltingTimeThreshold!!!*/ 1,
98  /*haltingSpeedThreshold!!!*/(SUMOReal)(5.0 / 3.6),
99  /*jamDistThreshold!!!*/ 10);
100  myE2Detectors[lane] = static_cast<MS_E2_ZS_CollectorOverLanes*>(det);
101  }
102  }
103  }
104 
105 
106  // initialise the duration
107  unsigned int tCycleIst = 0; // the actual cycletime
108  unsigned int tCycleMin = 0; // the minimum cycle time
109  unsigned int tDeltaGreen = 0; // the difference between the actual cycle time and the required cycle time
110 
112  for (unsigned int actStep = 0; actStep != myPhases.size(); actStep++) {
113  unsigned int dur = (unsigned int) myPhases[actStep]->duration;
114  tCycleIst = tCycleIst + dur;
115  if (myPhases[actStep]->isGreenPhase()) {
116  unsigned int mindur = (unsigned int) myPhases[actStep]->minDuration;
117  tCycleMin = tCycleMin + mindur;
118  } else {
119  tCycleMin = tCycleMin + dur;
120  }
121  }
122  if (tCycle < tCycleMin) {
123  tCycle = tCycleMin;
124  }
125  if (tCycleIst < tCycle) {
126  tDeltaGreen = tCycle - tCycleIst;
127  lengthenCycleTime(tDeltaGreen);
128  }
129  if (tCycleIst > tCycle) {
130  tDeltaGreen = tCycleIst - tCycle;
131  cutCycleTime(tDeltaGreen);
132  }
133 }
134 
135 
137 
138 
139 // ------------ Switching and setting current rows
140 SUMOTime
142  assert(getCurrentPhaseDef().minDuration >= 0);
143  assert(getCurrentPhaseDef().minDuration <= getCurrentPhaseDef().duration);
144  if (myPhases[myStep]->isGreenPhase()) {
145  // collects the data for the signal control
146  collectData();
147  // decides wheter greentime shall distributed between phases
148  if (tDecide <= tSinceLastDecision) {
150  }
151  }
152  // increment the index to the current phase
153  nextStep();
154  // set the next event
155  while (getCurrentPhaseDef().duration == 0) {
156  nextStep();
157  }
158  assert(myPhases.size() > myStep);
159  return getCurrentPhaseDef().duration;
160 }
161 
162 
163 // ------------ "agentbased" algorithm methods
164 unsigned int
166  // increment the index to the current phase
167  myStep++;
168  assert(myStep <= myPhases.size());
169  if (myStep == myPhases.size()) {
170  myStep = 0;
171  }
172  // increment the number of cycles since last decision
173  if (myStep == stepOfLastDecision) {
175  }
176  return myStep;
177 }
178 
179 
180 void
182  const std::string& state = getCurrentPhaseDef().getState();
183  // finds the maximum QUEUE_LENGTH_AHEAD_OF_TRAFFIC_LIGHTS_IN_VEHICLES of one phase
184  SUMOReal maxPerPhase = 0;
185  for (unsigned int i = 0; i < (unsigned int) state.size(); i++) {
186  // finds the maximum QUEUE_LENGTH_AHEAD_OF_TRAFFIC_LIGHTS_IN_VEHICLES of all lanes that have green
187  if (state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR) {
188  const std::vector<MSLane*>& lanes = getLanesAt(i);
189  if (lanes.empty()) {
190  break;
191  }
192  SUMOReal maxPerBit = 0;
193  for (LaneVector::const_iterator j = lanes.begin(); j != lanes.end(); j++) {
194  if ((*j)->getEdge().getPurpose() == MSEdge::EDGEFUNCTION_INTERNAL) {
195  continue;
196  }
205  }
206  if (maxPerPhase < maxPerBit) {
207  maxPerPhase = maxPerBit;
208  }
209  }
210  }
211  // if still no entry for the phase exists a new entry with an empty value is created
212  if (myRawDetectorData.find(myStep) == myRawDetectorData.end()) {
213  ValueType firstData;
214  myRawDetectorData[myStep] = firstData;
215  }
216  /* checks whether the number of values that are already in the dataqueue is
217  the same number of values taht shall be consideres in the traffic control
218  if both numbers are the same, the oldest value is deleted */
219  if (myRawDetectorData[myStep].size() == numberOfValues) {
220  myRawDetectorData[myStep].pop_back();
221  }
222  // adds the detectorvalue of the considered phase
223  myRawDetectorData[myStep].push_front(maxPerPhase);
224 }
225 
226 
227 void
229  for (PhaseValueMap::const_iterator i = myRawDetectorData.begin(); i != myRawDetectorData.end(); i++) {
230  SUMOReal sum = 0;
231  for (ValueType:: const_iterator it = myRawDetectorData[(*i).first].begin(); it != myRawDetectorData[(*i).first].end(); it ++) {
232  sum = sum + *it;
233  }
234  SUMOReal meanvalue = sum / myRawDetectorData[(*i).first].size();
235  myMeanDetectorData[(*i).first] = meanvalue;
236  }
237 }
238 
239 
240 void
243  unsigned int stepOfMaxValue = findStepOfMaxValue();
244  if (stepOfMaxValue == myPhases.size()) {
245  return;
246  }
247  unsigned int stepOfMinValue = findStepOfMinValue();
248  if (stepOfMinValue == myPhases.size()) {
249  return;
250  }
251  if (stepOfMinValue == stepOfMaxValue) {
252  return;
253  }
254  SUMOReal deltaIst = (myMeanDetectorData[stepOfMaxValue] - myMeanDetectorData[stepOfMinValue])
255  / myMeanDetectorData[stepOfMaxValue];
256  if (deltaIst > deltaLimit) {
257  myPhases[stepOfMaxValue]->duration = myPhases[stepOfMaxValue]->duration + 1;
258  myPhases[stepOfMinValue]->duration = myPhases[stepOfMinValue]->duration - 1;
259  tSinceLastDecision = 0;
261  }
262 }
263 
264 
265 void
267  typedef std::pair <unsigned int, unsigned int> contentType;
268  typedef std::vector< std::pair <unsigned int, unsigned int> > GreenPhasesVector;
269  GreenPhasesVector tmp_phases(myPhases.size());
270  tmp_phases.clear();
271  unsigned int maxLengthen = 0; // the sum of all times, that is possible to lengthen
272 
273  /* fills the vector tmp_phases with the difference between
274  duration and maxduration and the myStep of the phases.
275  only phases with duration < maxDuration are written in the vector.
276  sorts the vector after the difference. */
277  for (unsigned int i_Step = 0; i_Step != myPhases.size(); i_Step++) {
278  if (myPhases[i_Step]->isGreenPhase()) {
279  unsigned int dur = (unsigned int) myPhases[i_Step]->duration;
280  unsigned int maxdur = (unsigned int) myPhases[i_Step]->maxDuration;
281  if (dur < maxdur) {
282  contentType tmp;
283  tmp.second = i_Step;
284  tmp.first = maxdur - dur;
285  tmp_phases.push_back(tmp);
286  maxLengthen = maxLengthen + tmp.first;
287  }
288  }
289  }
290  sort(tmp_phases.begin(), tmp_phases.end());
291  //lengthens the phases acording to the difference between duration and maxDuration
292  for (GreenPhasesVector::iterator i = tmp_phases.begin(); i != tmp_phases.end(); i++) {
293  SUMOTime toLengthenPerPhase = 0;
294  SUMOReal tmpdb = ((*i).first * toLengthen / SUMOReal(maxLengthen)) + (SUMOReal) 0.5;
295  toLengthenPerPhase = static_cast<SUMOTime>(tmpdb);
296  toLengthen = toLengthen - (unsigned int) toLengthenPerPhase;
297  maxLengthen = maxLengthen - (*i).first;
298  SUMOTime newDur = myPhases[(*i).second]->duration + toLengthenPerPhase;
299  myPhases[(*i).second]->duration = newDur;
300  }
301 }
302 
303 
304 void
306  typedef std::pair <unsigned int, unsigned int> contentType;
307  typedef std::vector< std::pair <unsigned int, unsigned int> > GreenPhasesVector;
308  GreenPhasesVector tmp_phases(myPhases.size());
309  tmp_phases.clear();
310  unsigned maxCut = 0; // the sum of all times, that is possible to cut
311 
312  /* fills the vector tmp_phases with the difference between
313  duration and minduration and the myStep of the phases.
314  only phases with duration > minDuration are written in the vector.
315  sorts the vector after the difference. */
316  for (unsigned i_Step = 0; i_Step != myPhases.size(); i_Step++) {
317  if (myPhases[i_Step]->isGreenPhase()) {
318  unsigned int dur = (unsigned int) myPhases[i_Step]->duration;
319  unsigned int mindur = (unsigned int) myPhases[i_Step]->minDuration;
320  if (dur > mindur) {
321  contentType tmp;
322  tmp.second = i_Step;
323  tmp.first = dur - mindur;
324  tmp_phases.push_back(tmp);
325  maxCut = maxCut + tmp.first;
326  }
327  }
328  }
329  std::sort(tmp_phases.begin(), tmp_phases.end());
330  //cuts the phases acording to the difference between duration and minDuration
331  for (GreenPhasesVector::iterator i = tmp_phases.begin(); i != tmp_phases.end(); i++) {
332  SUMOTime toCutPerPhase = 0;
333  SUMOReal tmpdb = ((*i).first * toCut / SUMOReal(maxCut)) + (SUMOReal) 0.5;
334  toCutPerPhase = static_cast<SUMOTime>(tmpdb);
335  toCut = toCut - (unsigned int) toCutPerPhase;
336  maxCut = maxCut - (*i).first;
337  SUMOTime newDur = myPhases[(*i).second]->duration - toCutPerPhase;
338  myPhases[(*i).second]->duration = newDur;
339  }
340 }
341 
342 
343 unsigned int
345  unsigned int StepOfMaxValue = (unsigned int) myPhases.size();
346  SUMOReal MaxValue = -1;
347  for (MeanDataMap::const_iterator it = myMeanDetectorData.begin(); it != myMeanDetectorData.end(); it++) {
348  // checks whether the actual duruation is shorter than maxduration
349  // otherwise the phase can't be lenghten
350  unsigned int maxDur = (unsigned int) myPhases[(*it).first]->maxDuration;
351  unsigned int actDur = (unsigned int) myPhases[(*it).first]->duration;
352  if (actDur >= maxDur) {
353  continue;
354  }
355  if ((*it).second > MaxValue) {
356  MaxValue = (*it).second;
357  StepOfMaxValue = (*it).first;
358  }
359  }
360  return StepOfMaxValue;
361 }
362 
363 
364 unsigned int
366  unsigned int StepOfMinValue = (unsigned int) myPhases.size();
367  SUMOReal MinValue = 9999;
368  for (MeanDataMap::const_iterator it = myMeanDetectorData.begin(); it != myMeanDetectorData.end(); it++) {
369  // checks whether the actual duruation is longer than minduration
370  // otherwise the phase can't be cut
371  unsigned int minDur = (unsigned int) myPhases[(*it).first]->minDuration;
372  unsigned int actDur = (unsigned int) myPhases[(*it).first]->duration;
373  if (actDur <= minDur) {
374  continue;
375  }
376  if ((*it).second < MinValue) {
377  MinValue = (*it).second;
378  StepOfMinValue = (*it).first;
379  }
380  }
381  return StepOfMinValue;
382 }
383 
384 
385 /*
386 SUMOReal
387 MSAgentbasedTrafficLightLogic::currentForLane(E2::DetType what,
388  MSLane *lane) const
389 {
390 
391  E2DetectorMap::const_iterator i=myE2Detectors.find(lane);
392  return (*i).second->getCurrent(what);
393 }
394 */
395 
396 
397 /****************************************************************************/
398