SUMO - Simulation of Urban MObility

MSActuatedTrafficLightLogic.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // An actuated (adaptive) 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 <microsim/MSEventControl.h>
00034 #include <microsim/output/MSInductLoop.h>
00035 #include <microsim/MSNet.h>
00036 #include "MSTrafficLightLogic.h"
00037 #include "MSActuatedTrafficLightLogic.h"
00038 #include <microsim/MSLane.h>
00039 #include <netload/NLDetectorBuilder.h>
00040 #include <utils/common/TplConvert.h>
00041 
00042 #ifdef CHECK_MEMORY_LEAKS
00043 #include <foreign/nvwa/debug_new.h>
00044 #endif // CHECK_MEMORY_LEAKS
00045 
00046 
00047 // ===========================================================================
00048 // method definitions
00049 // ===========================================================================
00050 MSActuatedTrafficLightLogic::MSActuatedTrafficLightLogic(MSTLLogicControl& tlcontrol,
00051         const std::string& id, const std::string& programID,
00052         const Phases& phases,
00053         unsigned int step, SUMOTime delay, const std::map<std::string, std::string> &parameter)
00054     : MSSimpleTrafficLightLogic(tlcontrol, id, programID, phases, step, delay),
00055       myContinue(false) {
00056     myMaxGap = SUMOReal(3.1);
00057     if (parameter.find("max-gap")!=parameter.end()) {
00058         myMaxGap = TplConvert<char>::_2SUMOReal(parameter.find("max-gap")->second.c_str());
00059     }
00060     myPassingTime = SUMOReal(1.9);
00061     if (parameter.find("passing-time")!=parameter.end()) {
00062         myPassingTime = TplConvert<char>::_2SUMOReal(parameter.find("passing-time")->second.c_str());
00063     }
00064     myDetectorGap = SUMOReal(3.0);
00065     if (parameter.find("detector-gap")!=parameter.end()) {
00066         myDetectorGap = TplConvert<char>::_2SUMOReal(parameter.find("detector-gap")->second.c_str());
00067     }
00068 }
00069 
00070 
00071 void
00072 MSActuatedTrafficLightLogic::init(NLDetectorBuilder& nb) {
00073     SUMOReal det_offset = TplConvert<char>::_2SUMOReal(myParameter.find("detector_offset")->second.c_str());
00074     // change values for setting the loops and lanestate-detectors, here
00075     //SUMOTime inductLoopInterval = 1; //
00076     LaneVectorVector::const_iterator i2;
00077     LaneVector::const_iterator i;
00078     // build the induct loops
00079     for (i2=myLanes.begin(); i2!=myLanes.end(); ++i2) {
00080         const LaneVector& lanes = *i2;
00081         for (i=lanes.begin(); i!=lanes.end(); i++) {
00082             MSLane* lane = (*i);
00083             SUMOReal length = lane->getLength();
00084             SUMOReal speed = lane->getMaxSpeed();
00085             SUMOReal inductLoopPosition = myDetectorGap * speed;
00086             // check whether the lane is long enough
00087             SUMOReal ilpos = length - inductLoopPosition;
00088             if (ilpos<0) {
00089                 ilpos = 0;
00090             }
00091             // Build the induct loop and set it into the container
00092             std::string id = "TLS" + myID + "_" + myProgramID + "_InductLoopOn_" + lane->getID();
00093             if (myInductLoops.find(lane)==myInductLoops.end()) {
00094                 myInductLoops[lane] = static_cast<MSInductLoop*>(nb.createInductLoop(id, lane, ilpos, false));
00095             }
00096         }
00097         // build the lane state-detectors
00098         for (i=lanes.begin(); i!=lanes.end(); i++) {
00099             MSLane* lane = (*i);
00100             SUMOReal length = lane->getLength();
00101             // check whether the position is o.k. (not longer than the lane)
00102             SUMOReal lslen = det_offset;
00103             if (lslen>length) {
00104                 lslen = length;
00105             }
00106         }
00107     }
00108 }
00109 
00110 
00111 MSActuatedTrafficLightLogic::~MSActuatedTrafficLightLogic() {
00112     for (InductLoopMap::iterator i=myInductLoops.begin(); i!=myInductLoops.end(); ++i) {
00113         delete(*i).second;
00114     }
00115 }
00116 
00117 
00118 // ------------ Switching and setting current rows
00119 SUMOTime
00120 MSActuatedTrafficLightLogic::trySwitch(bool) {
00121     // checks if the actual phase should be continued
00122     gapControl();
00123     if (myContinue) {
00124         return duration();
00125     }
00126     // increment the index to the current phase
00127     myStep++;
00128     assert(myStep<=myPhases.size());
00129     if (myStep==myPhases.size()) {
00130         myStep = 0;
00131     }
00132     //stores the time the phase started
00133     myPhases[myStep]->myLastSwitch = MSNet::getInstance()->getCurrentTimeStep();
00134     // set the next event
00135     return duration();
00136 }
00137 
00138 
00139 // ------------ "actuated" algorithm methods
00140 SUMOTime
00141 MSActuatedTrafficLightLogic::duration() const {
00142     if (myContinue) {
00143         return 1;
00144     }
00145     assert(myPhases.size()>myStep);
00146     if (!getCurrentPhaseDef().isGreenPhase()) {
00147         return getCurrentPhaseDef().duration;
00148     }
00149     // define the duration depending from the number of waiting vehicles of the actual phase
00150     int newduration = (int) getCurrentPhaseDef().minDuration;
00151     const std::string& state = getCurrentPhaseDef().getState();
00152     for (unsigned int i=0; i<(unsigned int) state.size(); i++) {
00153         if (state[i]==LINKSTATE_TL_GREEN_MAJOR||state[i]==LINKSTATE_TL_GREEN_MINOR) {
00154             const std::vector<MSLane*> &lanes = getLanesAt(i);
00155             if (lanes.empty()) {
00156                 break;
00157             }
00158             for (LaneVector::const_iterator j=lanes.begin(); j!=lanes.end(); j++) {
00159                 InductLoopMap::const_iterator k = myInductLoops.find(*j);
00160                 SUMOReal waiting = (SUMOReal)(*k).second->getCurrentPassedNumber();
00161                 SUMOReal tmpdur =  myPassingTime * waiting;
00162                 if (tmpdur > newduration) {
00163                     // here we cut the decimal places, because we have to return an integer
00164                     newduration = (int) tmpdur;
00165                 }
00166                 if (newduration > (int) getCurrentPhaseDef().maxDuration)  {
00167                     return getCurrentPhaseDef().maxDuration;
00168                 }
00169             }
00170         }
00171     }
00172     return newduration;
00173 }
00174 
00175 
00176 void
00177 MSActuatedTrafficLightLogic::gapControl() {
00178     //intergreen times should not be lenghtend
00179     assert(myPhases.size()>myStep);
00180     if (!getCurrentPhaseDef().isGreenPhase()) {
00181         myContinue = false;
00182         return;
00183     }
00184 
00185     // Checks, if the maxDuration is kept. No phase should longer send than maxDuration.
00186     SUMOTime actDuration = MSNet::getInstance()->getCurrentTimeStep() - myPhases[myStep]->myLastSwitch;
00187     if (actDuration >= getCurrentPhaseDef().maxDuration) {
00188         myContinue = false;
00189         return;
00190     }
00191 
00192     // now the gapcontrol starts
00193     const std::string& state = getCurrentPhaseDef().getState();
00194     for (unsigned int i=0; i<(unsigned int) state.size(); i++)  {
00195         if (state[i]==LINKSTATE_TL_GREEN_MAJOR||state[i]==LINKSTATE_TL_GREEN_MINOR) {
00196             const std::vector<MSLane*> &lanes = getLanesAt(i);
00197             if (lanes.empty())    {
00198                 break;
00199             }
00200             for (LaneVector::const_iterator j=lanes.begin(); j!=lanes.end(); j++) {
00201                 if (myInductLoops.find(*j)==myInductLoops.end()) {
00202                     continue;
00203                 }
00204                 SUMOReal actualGap =
00205                     myInductLoops.find(*j)->second->getTimestepsSinceLastDetection();
00206                 if (actualGap < myMaxGap) {
00207                     myContinue = true;
00208                     return;
00209                 }
00210             }
00211         }
00212     }
00213     myContinue = false;
00214 }
00215 
00216 
00217 
00218 /****************************************************************************/
00219 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines