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.sourceforge.net/
14 // Copyright (C) 2001-2013 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 // member method definitions
53 // ===========================================================================
55  MSTLLogicControl& tlcontrol,
56  const std::string& id, const std::string& programID,
57  const Phases& phases, unsigned int step, SUMOTime delay,
58  const ParameterMap& parameter) :
59  MSSimpleTrafficLightLogic(tlcontrol, id, programID, phases, step, delay, parameter),
60  tSinceLastDecision(0), stepOfLastDecision(0) {
61 
62  tDecide = 1;
63  if (parameter.find("decision-horizon") != parameter.end()) {
64  tDecide = (unsigned int) TplConvert::_2int(parameter.find("decision-horizon")->second.c_str());
65  }
66  numberOfValues = 3;
67  if (parameter.find("learn-horizon") != parameter.end()) {
68  numberOfValues = (unsigned int) TplConvert::_2int(parameter.find("learn-horizon")->second.c_str());
69  }
70  tCycle = 90;
71  if (parameter.find("tcycle") != parameter.end()) {
72  tCycle = (unsigned int) TplConvert::_2SUMOReal(parameter.find("tcycle")->second.c_str());
73  }
74  deltaLimit = 1;
75  if (parameter.find("min-diff") != parameter.end()) {
76  deltaLimit = TplConvert::_2int(parameter.find("min-diff")->second.c_str());
77  }
78 }
79 
80 
81 void
83  SUMOReal det_offset = TplConvert::_2SUMOReal(myParameter.find("detector_offset")->second.c_str());
84  LaneVectorVector::const_iterator i2;
85  LaneVector::const_iterator i;
86  // build the detectors
87  for (i2 = myLanes.begin(); i2 != myLanes.end(); ++i2) {
88  const LaneVector& lanes = *i2;
89  for (i = lanes.begin(); i != lanes.end(); i++) {
90  MSLane* lane = (*i);
91  // Build the lane state detetcor and set it into the container
92  std::string id = "TL_" + myID + "_" + myProgramID + "_E2OverLanesDetectorStartingAt_" + lane->getID();
93 
94  if (myE2Detectors.find(lane) == myE2Detectors.end()) {
96  nb.buildMultiLaneE2Det(id,
97  DU_TL_CONTROL, lane, 0, det_offset,
98  /*haltingTimeThreshold!!!*/ 1,
99  /*haltingSpeedThreshold!!!*/(SUMOReal)(5.0 / 3.6),
100  /*jamDistThreshold!!!*/ 10);
101  myE2Detectors[lane] = static_cast<MS_E2_ZS_CollectorOverLanes*>(det);
102  }
103  }
104  }
105 
106 
107  // initialise the duration
108  unsigned int tCycleIst = 0; // the actual cycletime
109  unsigned int tCycleMin = 0; // the minimum cycle time
110  unsigned int tDeltaGreen = 0; // the difference between the actual cycle time and the required cycle time
111 
113  for (unsigned int actStep = 0; actStep != myPhases.size(); actStep++) {
114  unsigned int dur = (unsigned int) myPhases[actStep]->duration;
115  tCycleIst = tCycleIst + dur;
116  if (myPhases[actStep]->isGreenPhase()) {
117  unsigned int mindur = (unsigned int) myPhases[actStep]->minDuration;
118  tCycleMin = tCycleMin + mindur;
119  } else {
120  tCycleMin = tCycleMin + dur;
121  }
122  }
123  if (tCycle < tCycleMin) {
124  tCycle = tCycleMin;
125  }
126  if (tCycleIst < tCycle) {
127  tDeltaGreen = tCycle - tCycleIst;
128  lengthenCycleTime(tDeltaGreen);
129  }
130  if (tCycleIst > tCycle) {
131  tDeltaGreen = tCycleIst - tCycle;
132  cutCycleTime(tDeltaGreen);
133  }
134 }
135 
136 
138 
139 
140 // ------------ Switching and setting current rows
141 SUMOTime
143  assert(getCurrentPhaseDef().minDuration >= 0);
144  assert(getCurrentPhaseDef().minDuration <= getCurrentPhaseDef().duration);
145  if (myPhases[myStep]->isGreenPhase()) {
146  // collects the data for the signal control
147  collectData();
148  // decides wheter greentime shall distributed between phases
149  if (tDecide <= tSinceLastDecision) {
151  }
152  }
153  // increment the index to the current phase
154  nextStep();
155  // set the next event
156  while (getCurrentPhaseDef().duration == 0) {
157  nextStep();
158  }
159  assert(myPhases.size() > myStep);
160  return getCurrentPhaseDef().duration;
161 }
162 
163 
164 // ------------ "agentbased" algorithm methods
165 unsigned int
167  // increment the index to the current phase
168  myStep++;
169  assert(myStep <= myPhases.size());
170  if (myStep == myPhases.size()) {
171  myStep = 0;
172  }
173  // increment the number of cycles since last decision
174  if (myStep == stepOfLastDecision) {
176  }
177  return myStep;
178 }
179 
180 
181 void
183  const std::string& state = getCurrentPhaseDef().getState();
184  // finds the maximum QUEUE_LENGTH_AHEAD_OF_TRAFFIC_LIGHTS_IN_VEHICLES of one phase
185  SUMOReal maxPerPhase = 0;
186  for (unsigned int i = 0; i < (unsigned int) state.size(); i++) {
187  // finds the maximum QUEUE_LENGTH_AHEAD_OF_TRAFFIC_LIGHTS_IN_VEHICLES of all lanes that have green
188  if (state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR) {
189  const std::vector<MSLane*>& lanes = getLanesAt(i);
190  if (lanes.empty()) {
191  break;
192  }
193  SUMOReal maxPerBit = 0;
194  for (LaneVector::const_iterator j = lanes.begin(); j != lanes.end(); j++) {
195  if ((*j)->getEdge().getPurpose() == MSEdge::EDGEFUNCTION_INTERNAL) {
196  continue;
197  }
206  }
207  if (maxPerPhase < maxPerBit) {
208  maxPerPhase = maxPerBit;
209  }
210  }
211  }
212  // if still no entry for the phase exists a new entry with an empty value is created
213  if (myRawDetectorData.find(myStep) == myRawDetectorData.end()) {
214  ValueType firstData;
215  myRawDetectorData[myStep] = firstData;
216  }
217  /* checks whether the number of values that are already in the dataqueue is
218  the same number of values taht shall be consideres in the traffic control
219  if both numbers are the same, the oldest value is deleted */
220  if (myRawDetectorData[myStep].size() == numberOfValues) {
221  myRawDetectorData[myStep].pop_back();
222  }
223  // adds the detectorvalue of the considered phase
224  myRawDetectorData[myStep].push_front(maxPerPhase);
225 }
226 
227 
228 void
230  for (PhaseValueMap::const_iterator i = myRawDetectorData.begin(); i != myRawDetectorData.end(); i++) {
231  SUMOReal sum = 0;
232  for (ValueType:: const_iterator it = myRawDetectorData[(*i).first].begin(); it != myRawDetectorData[(*i).first].end(); it ++) {
233  sum = sum + *it;
234  }
235  SUMOReal meanvalue = sum / myRawDetectorData[(*i).first].size();
236  myMeanDetectorData[(*i).first] = meanvalue;
237  }
238 }
239 
240 
241 void
244  unsigned int stepOfMaxValue = findStepOfMaxValue();
245  if (stepOfMaxValue == myPhases.size()) {
246  return;
247  }
248  unsigned int stepOfMinValue = findStepOfMinValue();
249  if (stepOfMinValue == myPhases.size()) {
250  return;
251  }
252  if (stepOfMinValue == stepOfMaxValue) {
253  return;
254  }
255  SUMOReal deltaIst = (myMeanDetectorData[stepOfMaxValue] - myMeanDetectorData[stepOfMinValue])
256  / myMeanDetectorData[stepOfMaxValue];
257  if (deltaIst > deltaLimit) {
258  myPhases[stepOfMaxValue]->duration = myPhases[stepOfMaxValue]->duration + 1;
259  myPhases[stepOfMinValue]->duration = myPhases[stepOfMinValue]->duration - 1;
260  tSinceLastDecision = 0;
262  }
263 }
264 
265 
266 void
268  typedef std::pair <unsigned int, unsigned int> contentType;
269  typedef std::vector< std::pair <unsigned int, unsigned int> > GreenPhasesVector;
270  GreenPhasesVector tmp_phases(myPhases.size());
271  tmp_phases.clear();
272  unsigned int maxLengthen = 0; // the sum of all times, that is possible to lengthen
273 
274  /* fills the vector tmp_phases with the difference between
275  duration and maxduration and the myStep of the phases.
276  only phases with duration < maxDuration are written in the vector.
277  sorts the vector after the difference. */
278  for (unsigned int i_Step = 0; i_Step != myPhases.size(); i_Step++) {
279  if (myPhases[i_Step]->isGreenPhase()) {
280  unsigned int dur = (unsigned int) myPhases[i_Step]->duration;
281  unsigned int maxdur = (unsigned int) myPhases[i_Step]->maxDuration;
282  if (dur < maxdur) {
283  contentType tmp;
284  tmp.second = i_Step;
285  tmp.first = maxdur - dur;
286  tmp_phases.push_back(tmp);
287  maxLengthen = maxLengthen + tmp.first;
288  }
289  }
290  }
291  sort(tmp_phases.begin(), tmp_phases.end());
292  //lengthens the phases acording to the difference between duration and maxDuration
293  for (GreenPhasesVector::iterator i = tmp_phases.begin(); i != tmp_phases.end(); i++) {
294  SUMOTime toLengthenPerPhase = 0;
295  SUMOReal tmpdb = ((*i).first * toLengthen / SUMOReal(maxLengthen)) + (SUMOReal) 0.5;
296  toLengthenPerPhase = static_cast<SUMOTime>(tmpdb);
297  toLengthen = toLengthen - (unsigned int) toLengthenPerPhase;
298  maxLengthen = maxLengthen - (*i).first;
299  SUMOTime newDur = myPhases[(*i).second]->duration + toLengthenPerPhase;
300  myPhases[(*i).second]->duration = newDur;
301  }
302 }
303 
304 
305 void
307  typedef std::pair <unsigned int, unsigned int> contentType;
308  typedef std::vector< std::pair <unsigned int, unsigned int> > GreenPhasesVector;
309  GreenPhasesVector tmp_phases(myPhases.size());
310  tmp_phases.clear();
311  unsigned maxCut = 0; // the sum of all times, that is possible to cut
312 
313  /* fills the vector tmp_phases with the difference between
314  duration and minduration and the myStep of the phases.
315  only phases with duration > minDuration are written in the vector.
316  sorts the vector after the difference. */
317  for (unsigned i_Step = 0; i_Step != myPhases.size(); i_Step++) {
318  if (myPhases[i_Step]->isGreenPhase()) {
319  unsigned int dur = (unsigned int) myPhases[i_Step]->duration;
320  unsigned int mindur = (unsigned int) myPhases[i_Step]->minDuration;
321  if (dur > mindur) {
322  contentType tmp;
323  tmp.second = i_Step;
324  tmp.first = dur - mindur;
325  tmp_phases.push_back(tmp);
326  maxCut = maxCut + tmp.first;
327  }
328  }
329  }
330  std::sort(tmp_phases.begin(), tmp_phases.end());
331  //cuts the phases acording to the difference between duration and minDuration
332  for (GreenPhasesVector::iterator i = tmp_phases.begin(); i != tmp_phases.end(); i++) {
333  SUMOTime toCutPerPhase = 0;
334  SUMOReal tmpdb = ((*i).first * toCut / SUMOReal(maxCut)) + (SUMOReal) 0.5;
335  toCutPerPhase = static_cast<SUMOTime>(tmpdb);
336  toCut = toCut - (unsigned int) toCutPerPhase;
337  maxCut = maxCut - (*i).first;
338  SUMOTime newDur = myPhases[(*i).second]->duration - toCutPerPhase;
339  myPhases[(*i).second]->duration = newDur;
340  }
341 }
342 
343 
344 unsigned int
346  unsigned int StepOfMaxValue = (unsigned int) myPhases.size();
347  SUMOReal MaxValue = -1;
348  for (MeanDataMap::const_iterator it = myMeanDetectorData.begin(); it != myMeanDetectorData.end(); it++) {
349  // checks whether the actual duruation is shorter than maxduration
350  // otherwise the phase can't be lenghten
351  unsigned int maxDur = (unsigned int) myPhases[(*it).first]->maxDuration;
352  unsigned int actDur = (unsigned int) myPhases[(*it).first]->duration;
353  if (actDur >= maxDur) {
354  continue;
355  }
356  if ((*it).second > MaxValue) {
357  MaxValue = (*it).second;
358  StepOfMaxValue = (*it).first;
359  }
360  }
361  return StepOfMaxValue;
362 }
363 
364 
365 unsigned int
367  unsigned int StepOfMinValue = (unsigned int) myPhases.size();
368  SUMOReal MinValue = 9999;
369  for (MeanDataMap::const_iterator it = myMeanDetectorData.begin(); it != myMeanDetectorData.end(); it++) {
370  // checks whether the actual duruation is longer than minduration
371  // otherwise the phase can't be cut
372  unsigned int minDur = (unsigned int) myPhases[(*it).first]->minDuration;
373  unsigned int actDur = (unsigned int) myPhases[(*it).first]->duration;
374  if (actDur <= minDur) {
375  continue;
376  }
377  if ((*it).second < MinValue) {
378  MinValue = (*it).second;
379  StepOfMinValue = (*it).first;
380  }
381  }
382  return StepOfMinValue;
383 }
384 
385 
386 /*
387 SUMOReal
388 MSAgentbasedTrafficLightLogic::currentForLane(E2::DetType what,
389  MSLane *lane) const
390 {
391 
392  E2DetectorMap::const_iterator i=myE2Detectors.find(lane);
393  return (*i).second->getCurrent(what);
394 }
395 */
396 
397 
398 /****************************************************************************/
399