SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00011 // A fixed 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 <cassert> 00036 #include <utility> 00037 #include <vector> 00038 #include <bitset> 00039 #include <sstream> 00040 #include <microsim/MSEventControl.h> 00041 #include "MSTrafficLightLogic.h" 00042 #include "MSSimpleTrafficLightLogic.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 MSSimpleTrafficLightLogic::MSSimpleTrafficLightLogic(MSTLLogicControl& tlcontrol, 00053 const std::string& id, const std::string& subid, const Phases& phases, 00054 unsigned int step, SUMOTime delay) 00055 : MSTrafficLightLogic(tlcontrol, id, subid, delay), myPhases(phases), 00056 myStep(step) { 00057 for (size_t i = 0; i < myPhases.size(); i++) { 00058 myDefaultCycleTime += myPhases[i]->duration; 00059 } 00060 } 00061 00062 00063 MSSimpleTrafficLightLogic::~MSSimpleTrafficLightLogic() { 00064 deletePhases(); 00065 } 00066 00067 00068 // ------------ Switching and setting current rows 00069 SUMOTime 00070 MSSimpleTrafficLightLogic::trySwitch(bool) { 00071 // check whether the current duration shall be increased 00072 if (myCurrentDurationIncrement > 0) { 00073 SUMOTime delay = myCurrentDurationIncrement; 00074 myCurrentDurationIncrement = 0; 00075 return delay; 00076 } 00077 00078 // increment the index 00079 myStep++; 00080 // if the last phase was reached ... 00081 if (myStep >= myPhases.size()) { 00082 // ... set the index to the first phase 00083 myStep = 0; 00084 } 00085 assert(myPhases.size() > myStep); 00086 //stores the time the phase started 00087 myPhases[myStep]->myLastSwitch = MSNet::getInstance()->getCurrentTimeStep(); 00088 // check whether the next duration was overridden 00089 if (myOverridingTimes.size() > 0) { 00090 SUMOTime nextDuration = myOverridingTimes[0]; 00091 myOverridingTimes.erase(myOverridingTimes.begin()); 00092 return nextDuration; 00093 } 00094 // return offset to the next switch 00095 return myPhases[myStep]->duration; 00096 } 00097 00098 00099 // ------------ Static Information Retrieval 00100 unsigned int 00101 MSSimpleTrafficLightLogic::getPhaseNumber() const { 00102 return (unsigned int) myPhases.size(); 00103 } 00104 00105 00106 const MSSimpleTrafficLightLogic::Phases& 00107 MSSimpleTrafficLightLogic::getPhases() const { 00108 return myPhases; 00109 } 00110 00111 00112 MSSimpleTrafficLightLogic::Phases& 00113 MSSimpleTrafficLightLogic::getPhases() { 00114 return myPhases; 00115 } 00116 00117 00118 const MSPhaseDefinition& 00119 MSSimpleTrafficLightLogic::getPhase(unsigned int givenStep) const { 00120 assert(myPhases.size() > givenStep); 00121 return *myPhases[givenStep]; 00122 } 00123 00124 00125 // ------------ Dynamic Information Retrieval 00126 unsigned int 00127 MSSimpleTrafficLightLogic::getCurrentPhaseIndex() const { 00128 return myStep; 00129 } 00130 00131 00132 const MSPhaseDefinition& 00133 MSSimpleTrafficLightLogic::getCurrentPhaseDef() const { 00134 return *myPhases[myStep]; 00135 } 00136 00137 00138 // ------------ Conversion between time and phase 00139 SUMOTime 00140 MSSimpleTrafficLightLogic::getPhaseIndexAtTime(SUMOTime simStep) const { 00141 SUMOTime position = 0; 00142 if (myStep > 0) { 00143 for (unsigned int i = 0; i < myStep; i++) { 00144 position = position + getPhase(i).duration; 00145 } 00146 } 00147 position = position + simStep - getPhase(myStep).myLastSwitch; 00148 position = position % myDefaultCycleTime; 00149 assert(position <= myDefaultCycleTime); 00150 return position; 00151 } 00152 00153 00154 SUMOTime 00155 MSSimpleTrafficLightLogic::getOffsetFromIndex(unsigned int index) const { 00156 assert(index < myPhases.size()); 00157 if (index == 0) { 00158 return 0; 00159 } 00160 unsigned int pos = 0; 00161 for (unsigned int i = 0; i < index; i++) { 00162 pos += getPhase(i).duration; 00163 } 00164 return pos; 00165 } 00166 00167 00168 unsigned int 00169 MSSimpleTrafficLightLogic::getIndexFromOffset(SUMOTime offset) const { 00170 assert(offset <= myDefaultCycleTime); 00171 if (offset == myDefaultCycleTime) { 00172 return 0; 00173 } 00174 SUMOTime testPos = 0; 00175 for (unsigned int i = 0; i < myPhases.size(); i++) { 00176 testPos = testPos + getPhase(i).duration; 00177 if (testPos > offset) { 00178 return i; 00179 } 00180 if (testPos == offset) { 00181 assert(myPhases.size() > (i + 1)); 00182 return (i + 1); 00183 } 00184 } 00185 return 0; 00186 } 00187 00188 00189 // ------------ Changing phases and phase durations 00190 void 00191 MSSimpleTrafficLightLogic::changeStepAndDuration(MSTLLogicControl& tlcontrol, 00192 SUMOTime simStep, unsigned int step, SUMOTime stepDuration) { 00193 mySwitchCommand->deschedule(this); 00194 mySwitchCommand = new SwitchCommand(tlcontrol, this, stepDuration + simStep); 00195 myStep = step; 00196 MSNet::getInstance()->getBeginOfTimestepEvents().addEvent( 00197 mySwitchCommand, stepDuration + simStep, 00198 MSEventControl::ADAPT_AFTER_EXECUTION); 00199 } 00200 00201 00202 void 00203 MSSimpleTrafficLightLogic::setPhases(const Phases& phases, unsigned int step) { 00204 assert(step < phases.size()); 00205 deletePhases(); 00206 myPhases = phases; 00207 myStep = step; 00208 } 00209 00210 00211 void 00212 MSSimpleTrafficLightLogic::deletePhases() { 00213 for (size_t i = 0; i < myPhases.size(); i++) { 00214 delete myPhases[i]; 00215 } 00216 } 00217 00218 00219 /****************************************************************************/ 00220