SUMO - Simulation of Urban MObility

MSAgentbasedTrafficLightLogic.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00011 // An agentbased traffic light logic
00012 /****************************************************************************/
00013 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00014 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00015 /****************************************************************************/
00016 //
00017 //   This file is part of SUMO.
00018 //   SUMO is free software: you can redistribute it and/or modify
00019 //   it under the terms of the GNU General Public License as published by
00020 //   the Free Software Foundation, either version 3 of the License, or
00021 //   (at your option) any later version.
00022 //
00023 /****************************************************************************/
00024 
00025 
00026 // ===========================================================================
00027 // included modules
00028 // ===========================================================================
00029 #ifdef _MSC_VER
00030 #include <windows_config.h>
00031 #else
00032 #include <config.h>
00033 #endif
00034 
00035 #include <utility>
00036 #include <vector>
00037 #include <bitset>
00038 #include "MSTrafficLightLogic.h"
00039 #include "MSAgentbasedTrafficLightLogic.h"
00040 #include <netload/NLDetectorBuilder.h>
00041 #include <utils/common/TplConvert.h>
00042 #include <microsim/MSLane.h>
00043 
00044 #ifdef CHECK_MEMORY_LEAKS
00045 #include <foreign/nvwa/debug_new.h>
00046 #endif // CHECK_MEMORY_LEAKS
00047 
00048 
00049 // ===========================================================================
00050 // member method definitions
00051 // ===========================================================================
00052 MSAgentbasedTrafficLightLogic::MSAgentbasedTrafficLightLogic(
00053     MSTLLogicControl& tlcontrol,
00054     const std::string& id, const std::string& programID,
00055     const Phases& phases, unsigned int step, SUMOTime delay,
00056     const std::map<std::string, std::string> &parameter)
00057     : MSSimpleTrafficLightLogic(tlcontrol, id, programID, phases, step, delay),
00058       tSinceLastDecision(0), stepOfLastDecision(0) {
00059 
00060     tDecide = 1;
00061     if (parameter.find("decision-horizon") != parameter.end()) {
00062         tDecide = (unsigned int) TplConvert<char>::_2int(parameter.find("decision-horizon")->second.c_str());
00063     }
00064     numberOfValues = 3;
00065     if (parameter.find("learn-horizon") != parameter.end()) {
00066         numberOfValues = (unsigned int) TplConvert<char>::_2int(parameter.find("learn-horizon")->second.c_str());
00067     }
00068     tCycle = 90;
00069     if (parameter.find("tcycle") != parameter.end()) {
00070         tCycle = (unsigned int) TplConvert<char>::_2SUMOReal(parameter.find("tcycle")->second.c_str());
00071     }
00072     deltaLimit = 1;
00073     if (parameter.find("min-diff") != parameter.end()) {
00074         deltaLimit = TplConvert<char>::_2int(parameter.find("min-diff")->second.c_str());
00075     }
00076 }
00077 
00078 
00079 void
00080 MSAgentbasedTrafficLightLogic::init(NLDetectorBuilder& nb) {
00081     SUMOReal det_offset = TplConvert<char>::_2SUMOReal(myParameter.find("detector_offset")->second.c_str());
00082     LaneVectorVector::const_iterator i2;
00083     LaneVector::const_iterator i;
00084     // build the detectors
00085     for (i2 = myLanes.begin(); i2 != myLanes.end(); ++i2) {
00086         const LaneVector& lanes = *i2;
00087         for (i = lanes.begin(); i != lanes.end(); i++) {
00088             MSLane* lane = (*i);
00089             // Build the lane state detetcor and set it into the container
00090             std::string id = "TL_" + myID + "_" + myProgramID + "_E2OverLanesDetectorStartingAt_" + lane->getID();
00091 
00092             if (myE2Detectors.find(lane) == myE2Detectors.end()) {
00093                 MSDetectorFileOutput* det =
00094                     nb.buildMultiLaneE2Det(id,
00095                                            DU_TL_CONTROL, lane, 0, det_offset,
00096                                            /*haltingTimeThreshold!!!*/ 1,
00097                                            /*haltingSpeedThreshold!!!*/(SUMOReal)(5.0 / 3.6),
00098                                            /*jamDistThreshold!!!*/ 10);
00099                 myE2Detectors[lane] = static_cast<MS_E2_ZS_CollectorOverLanes*>(det);
00100             }
00101         }
00102     }
00103 
00104 
00105     // initialise the duration
00106     unsigned int tCycleIst = 0;          // the actual cycletime
00107     unsigned int tCycleMin = 0;          // the minimum cycle time
00108     unsigned int tDeltaGreen = 0;         // the difference between the actual cycle time and the required cycle time
00109 
00111     for (unsigned int actStep = 0; actStep != myPhases.size(); actStep++) {
00112         unsigned int dur = (unsigned int) myPhases[actStep]->duration;
00113         tCycleIst = tCycleIst + dur;
00114         if (myPhases[actStep]->isGreenPhase()) {
00115             unsigned int mindur = (unsigned int) myPhases[actStep]->minDuration;
00116             tCycleMin = tCycleMin + mindur;
00117         } else {
00118             tCycleMin = tCycleMin + dur;
00119         }
00120     }
00121     if (tCycle < tCycleMin) {
00122         tCycle = tCycleMin;
00123     }
00124     if (tCycleIst < tCycle) {
00125         tDeltaGreen = tCycle - tCycleIst;
00126         lengthenCycleTime(tDeltaGreen);
00127     }
00128     if (tCycleIst > tCycle) {
00129         tDeltaGreen = tCycleIst - tCycle;
00130         cutCycleTime(tDeltaGreen);
00131     }
00132 }
00133 
00134 
00135 MSAgentbasedTrafficLightLogic::~MSAgentbasedTrafficLightLogic() {}
00136 
00137 
00138 // ------------ Switching and setting current rows
00139 SUMOTime
00140 MSAgentbasedTrafficLightLogic::trySwitch(bool) {
00141     assert(getCurrentPhaseDef().minDuration >= 0);
00142     assert(getCurrentPhaseDef().minDuration <= getCurrentPhaseDef().duration);
00143     if (myPhases[myStep]->isGreenPhase()) {
00144         // collects the data for the signal control
00145         collectData();
00146         // decides wheter greentime shall distributed between phases
00147         if (tDecide <= tSinceLastDecision) {
00148             calculateDuration();
00149         }
00150     }
00151     // increment the index to the current phase
00152     nextStep();
00153     // set the next event
00154     while (getCurrentPhaseDef().duration == 0) {
00155         nextStep();
00156     }
00157     assert(myPhases.size() > myStep);
00158     return getCurrentPhaseDef().duration;
00159 }
00160 
00161 
00162 // ------------ "agentbased" algorithm methods
00163 unsigned int
00164 MSAgentbasedTrafficLightLogic::nextStep() {
00165     // increment the index to the current phase
00166     myStep++;
00167     assert(myStep <= myPhases.size());
00168     if (myStep == myPhases.size()) {
00169         myStep = 0;
00170     }
00171     // increment the number of cycles since last decision
00172     if (myStep == stepOfLastDecision) {
00173         tSinceLastDecision = tSinceLastDecision + 1;
00174     }
00175     return myStep;
00176 }
00177 
00178 
00179 void
00180 MSAgentbasedTrafficLightLogic::collectData() {
00181     const std::string& state = getCurrentPhaseDef().getState();
00182     // finds the maximum QUEUE_LENGTH_AHEAD_OF_TRAFFIC_LIGHTS_IN_VEHICLES of one phase
00183     SUMOReal maxPerPhase = 0;
00184     for (unsigned int i = 0; i < (unsigned int) state.size(); i++)  {
00185         // finds the maximum QUEUE_LENGTH_AHEAD_OF_TRAFFIC_LIGHTS_IN_VEHICLES of all lanes that have green
00186         if (state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR) {
00187             const std::vector<MSLane*> &lanes = getLanesAt(i);
00188             if (lanes.empty())    {
00189                 break;
00190             }
00191             SUMOReal maxPerBit = 0;
00192             for (LaneVector::const_iterator j = lanes.begin(); j != lanes.end(); j++) {
00193                 if ((*j)->getEdge().getPurpose() == MSEdge::EDGEFUNCTION_INTERNAL) {
00194                     continue;
00195                 }
00204             }
00205             if (maxPerPhase < maxPerBit) {
00206                 maxPerPhase = maxPerBit;
00207             }
00208         }
00209     }
00210     // if still no entry for the phase exists a new entry with an empty value is created
00211     if (myRawDetectorData.find(myStep) == myRawDetectorData.end()) {
00212         ValueType firstData;
00213         myRawDetectorData[myStep] = firstData;
00214     }
00215     /* checks whether the number of values that are already in the dataqueue is
00216        the same number of values taht shall be consideres in the traffic control
00217        if both numbers are the same, the oldest value is deleted */
00218     if (myRawDetectorData[myStep].size() == numberOfValues) {
00219         myRawDetectorData[myStep].pop_back();
00220     }
00221     // adds the detectorvalue of the considered phase
00222     myRawDetectorData[myStep].push_front(maxPerPhase);
00223 }
00224 
00225 
00226 void
00227 MSAgentbasedTrafficLightLogic::aggregateRawData() {
00228     for (PhaseValueMap::const_iterator i = myRawDetectorData.begin(); i != myRawDetectorData.end(); i++) {
00229         SUMOReal sum = 0;
00230         for (ValueType:: const_iterator it = myRawDetectorData[(*i).first].begin(); it != myRawDetectorData[(*i).first].end(); it ++) {
00231             sum = sum + *it;
00232         }
00233         SUMOReal meanvalue = sum / myRawDetectorData[(*i).first].size();
00234         myMeanDetectorData[(*i).first] = meanvalue;
00235     }
00236 }
00237 
00238 
00239 void
00240 MSAgentbasedTrafficLightLogic::calculateDuration() {
00241     aggregateRawData();
00242     unsigned int stepOfMaxValue = findStepOfMaxValue();
00243     if (stepOfMaxValue == myPhases.size())    {
00244         return;
00245     }
00246     unsigned int stepOfMinValue = findStepOfMinValue();
00247     if (stepOfMinValue == myPhases.size())    {
00248         return;
00249     }
00250     if (stepOfMinValue == stepOfMaxValue)    {
00251         return;
00252     }
00253     SUMOReal deltaIst = (myMeanDetectorData[stepOfMaxValue] - myMeanDetectorData[stepOfMinValue])
00254                         / myMeanDetectorData[stepOfMaxValue];
00255     if (deltaIst > deltaLimit) {
00256         myPhases[stepOfMaxValue]->duration = myPhases[stepOfMaxValue]->duration + 1;
00257         myPhases[stepOfMinValue]->duration = myPhases[stepOfMinValue]->duration - 1;
00258         tSinceLastDecision = 0;
00259         stepOfLastDecision = myStep;
00260     }
00261 }
00262 
00263 
00264 void
00265 MSAgentbasedTrafficLightLogic::lengthenCycleTime(unsigned int toLengthen) {
00266     typedef std::pair <unsigned int, unsigned int> contentType;
00267     typedef std::vector< std::pair <unsigned int, unsigned int> > GreenPhasesVector;
00268     GreenPhasesVector tmp_phases(myPhases.size());
00269     tmp_phases.clear();
00270     unsigned int maxLengthen = 0;  // the sum of all times, that is possible to lengthen
00271 
00272     /* fills the vector tmp_phases with the difference between
00273        duration and maxduration and the myStep of the phases.
00274        only phases with duration < maxDuration are written in the vector.
00275        sorts the vector after the difference. */
00276     for (unsigned int i_Step = 0; i_Step != myPhases.size(); i_Step++) {
00277         if (myPhases[i_Step]->isGreenPhase()) {
00278             unsigned int dur = (unsigned int) myPhases[i_Step]->duration;
00279             unsigned int maxdur = (unsigned int) myPhases[i_Step]->maxDuration;
00280             if (dur < maxdur) {
00281                 contentType tmp;
00282                 tmp.second = i_Step;
00283                 tmp.first = maxdur - dur;
00284                 tmp_phases.push_back(tmp);
00285                 maxLengthen = maxLengthen + tmp.first;
00286             }
00287         }
00288     }
00289     sort(tmp_phases.begin(), tmp_phases.end());
00290     //lengthens the phases acording to the difference between duration and maxDuration
00291     for (GreenPhasesVector::iterator i = tmp_phases.begin(); i != tmp_phases.end(); i++) {
00292         SUMOTime toLengthenPerPhase = 0;
00293         SUMOReal tmpdb = ((*i).first * toLengthen / SUMOReal(maxLengthen)) + (SUMOReal) 0.5;
00294         toLengthenPerPhase = static_cast<SUMOTime>(tmpdb);
00295         toLengthen = toLengthen - (unsigned int) toLengthenPerPhase;
00296         maxLengthen = maxLengthen - (*i).first;
00297         SUMOTime newDur = myPhases[(*i).second]->duration + toLengthenPerPhase;
00298         myPhases[(*i).second]->duration = newDur;
00299     }
00300 }
00301 
00302 
00303 void
00304 MSAgentbasedTrafficLightLogic::cutCycleTime(unsigned int toCut) {
00305     typedef std::pair <unsigned int, unsigned int> contentType;
00306     typedef std::vector< std::pair <unsigned int, unsigned int> > GreenPhasesVector;
00307     GreenPhasesVector tmp_phases(myPhases.size());
00308     tmp_phases.clear();
00309     unsigned maxCut = 0;  // the sum of all times, that is possible to cut
00310 
00311     /* fills the vector tmp_phases with the difference between
00312        duration and minduration and the myStep of the phases.
00313        only phases with duration > minDuration are written in the vector.
00314        sorts the vector after the difference. */
00315     for (unsigned i_Step = 0; i_Step != myPhases.size(); i_Step++) {
00316         if (myPhases[i_Step]->isGreenPhase()) {
00317             unsigned int dur = (unsigned int) myPhases[i_Step]->duration;
00318             unsigned int mindur = (unsigned int) myPhases[i_Step]->minDuration;
00319             if (dur > mindur) {
00320                 contentType tmp;
00321                 tmp.second = i_Step;
00322                 tmp.first = dur - mindur;
00323                 tmp_phases.push_back(tmp);
00324                 maxCut = maxCut + tmp.first;
00325             }
00326         }
00327     }
00328     std::sort(tmp_phases.begin(), tmp_phases.end());
00329     //cuts the phases acording to the difference between duration and minDuration
00330     for (GreenPhasesVector::iterator i = tmp_phases.begin(); i != tmp_phases.end(); i++) {
00331         SUMOTime toCutPerPhase = 0;
00332         SUMOReal tmpdb = ((*i).first * toCut / SUMOReal(maxCut)) + (SUMOReal) 0.5;
00333         toCutPerPhase = static_cast<SUMOTime>(tmpdb);
00334         toCut = toCut - (unsigned int) toCutPerPhase;
00335         maxCut = maxCut - (*i).first;
00336         SUMOTime newDur = myPhases[(*i).second]->duration - toCutPerPhase;
00337         myPhases[(*i).second]->duration = newDur;
00338     }
00339 }
00340 
00341 
00342 unsigned int
00343 MSAgentbasedTrafficLightLogic::findStepOfMaxValue() const {
00344     unsigned int StepOfMaxValue = (unsigned int) myPhases.size();
00345     SUMOReal MaxValue = -1;
00346     for (MeanDataMap::const_iterator it = myMeanDetectorData.begin(); it != myMeanDetectorData.end(); it++) {
00347         // checks whether the actual duruation is shorter than maxduration
00348         // otherwise the phase can't be lenghten
00349         unsigned int maxDur = (unsigned int) myPhases[(*it).first]->maxDuration;
00350         unsigned int actDur = (unsigned int) myPhases[(*it).first]->duration;
00351         if (actDur >= maxDur) {
00352             continue;
00353         }
00354         if ((*it).second > MaxValue) {
00355             MaxValue = (*it).second;
00356             StepOfMaxValue = (*it).first;
00357         }
00358     }
00359     return StepOfMaxValue;
00360 }
00361 
00362 
00363 unsigned int
00364 MSAgentbasedTrafficLightLogic::findStepOfMinValue() const {
00365     unsigned int StepOfMinValue = (unsigned int) myPhases.size();
00366     SUMOReal MinValue = 9999;
00367     for (MeanDataMap::const_iterator it = myMeanDetectorData.begin(); it != myMeanDetectorData.end(); it++) {
00368         // checks whether the actual duruation is longer than minduration
00369         // otherwise the phase can't be cut
00370         unsigned int minDur = (unsigned int) myPhases[(*it).first]->minDuration;
00371         unsigned int actDur = (unsigned int) myPhases[(*it).first]->duration;
00372         if (actDur <= minDur) {
00373             continue;
00374         }
00375         if ((*it).second < MinValue) {
00376             MinValue = (*it).second;
00377             StepOfMinValue = (*it).first;
00378         }
00379     }
00380     return StepOfMinValue;
00381 }
00382 
00383 
00384 /*
00385 SUMOReal
00386 MSAgentbasedTrafficLightLogic::currentForLane(E2::DetType what,
00387         MSLane *lane) const
00388 {
00389 
00390     E2DetectorMap::const_iterator i=myE2Detectors.find(lane);
00391     return (*i).second->getCurrent(what);
00392 }
00393 */
00394 
00395 
00396 /****************************************************************************/
00397 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines