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