MSSimpleTrafficLightLogic.cpp

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

Generated on Wed May 5 00:06:32 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6