SUMO - Simulation of Urban MObility

MSTrafficLightLogic.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // The parent class for traffic light logics
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 <string>
00031 #include <iostream>
00032 #include <map>
00033 #include <microsim/MSLink.h>
00034 #include <microsim/MSLane.h>
00035 #include "MSTrafficLightLogic.h"
00036 #include <microsim/MSEventControl.h>
00037 #include "MSTLLogicControl.h"
00038 #include <microsim/MSJunctionLogic.h>
00039 
00040 #ifdef CHECK_MEMORY_LEAKS
00041 #include <foreign/nvwa/debug_new.h>
00042 #endif // CHECK_MEMORY_LEAKS
00043 
00044 
00045 // ===========================================================================
00046 // member method definitions
00047 // ===========================================================================
00048 /* -------------------------------------------------------------------------
00049  * member method definitions
00050  * ----------------------------------------------------------------------- */
00051 MSTrafficLightLogic::SwitchCommand::SwitchCommand(MSTLLogicControl& tlcontrol,
00052         MSTrafficLightLogic* tlLogic, SUMOTime nextSwitch)
00053     : myTLControl(tlcontrol), myTLLogic(tlLogic),
00054       myAssumedNextSwitch(nextSwitch), myAmValid(true) {}
00055 
00056 
00057 MSTrafficLightLogic::SwitchCommand::~SwitchCommand() {}
00058 
00059 
00060 
00061 SUMOTime
00062 MSTrafficLightLogic::SwitchCommand::execute(SUMOTime t) {
00063     // check whether this command has been descheduled
00064     if (!myAmValid) {
00065         return 0;
00066     }
00067     //
00068     bool isActive = myTLControl.isActive(myTLLogic);
00069     size_t step1 = myTLLogic->getCurrentPhaseIndex();
00070     SUMOTime next = myTLLogic->trySwitch(isActive);
00071     size_t step2 = myTLLogic->getCurrentPhaseIndex();
00072     if (step1!=step2) {
00073         if (isActive) {
00074             // execute any action connected to this tls
00075             const MSTLLogicControl::TLSLogicVariants& vars = myTLControl.get(myTLLogic->getID());
00076             // set link priorities
00077             myTLLogic->setTrafficLightSignals(t);
00078             // execute switch actions
00079             vars.executeOnSwitchActions();
00080         }
00081     }
00082     myAssumedNextSwitch += next;
00083     return next;
00084 }
00085 
00086 
00087 void
00088 MSTrafficLightLogic::SwitchCommand::deschedule(MSTrafficLightLogic* tlLogic) {
00089     if (tlLogic==myTLLogic) {
00090         myAmValid = false;
00091         myAssumedNextSwitch = -1;
00092     }
00093 }
00094 
00095 
00096 /* -------------------------------------------------------------------------
00097  * member method definitions
00098  * ----------------------------------------------------------------------- */
00099 MSTrafficLightLogic::MSTrafficLightLogic(MSTLLogicControl& tlcontrol,
00100         const std::string& id, const std::string& programID,
00101         SUMOTime delay)
00102     : myID(id), myProgramID(programID), myCurrentDurationIncrement(-1),
00103       myDefaultCycleTime(0) {
00104     mySwitchCommand = new SwitchCommand(tlcontrol, this, delay);
00105     MSNet::getInstance()->getBeginOfTimestepEvents().addEvent(
00106         mySwitchCommand, delay, MSEventControl::NO_CHANGE);
00107 }
00108 
00109 
00110 void
00111 MSTrafficLightLogic::init(NLDetectorBuilder&) {
00112 }
00113 
00114 
00115 MSTrafficLightLogic::~MSTrafficLightLogic() {
00116     mySwitchCommand->deschedule(this);
00117 }
00118 
00119 
00120 // ----------- Handling of controlled links
00121 void
00122 MSTrafficLightLogic::addLink(MSLink* link, MSLane* lane, unsigned int pos) {
00123     // !!! should be done within the loader (checking necessary)
00124     myLinks.reserve(pos+1);
00125     while (myLinks.size()<=pos) {
00126         myLinks.push_back(LinkVector());
00127     }
00128     myLinks[pos].push_back(link);
00129     //
00130     myLanes.reserve(pos+1);
00131     while (myLanes.size()<=pos) {
00132         myLanes.push_back(LaneVector());
00133     }
00134     myLanes[pos].push_back(lane);
00135     link->setTLState((LinkState) getCurrentPhaseDef().getState()[pos], MSNet::getInstance()->getCurrentTimeStep());
00136 }
00137 
00138 
00139 void
00140 MSTrafficLightLogic::adaptLinkInformationFrom(const MSTrafficLightLogic& logic) {
00141     myLinks = logic.myLinks;
00142     myLanes = logic.myLanes;
00143 }
00144 
00145 
00146 std::map<MSLink*, LinkState>
00147 MSTrafficLightLogic::collectLinkStates() const {
00148     std::map<MSLink*, LinkState> ret;
00149     for (LinkVectorVector::const_iterator i1=myLinks.begin(); i1!=myLinks.end(); ++i1) {
00150         const LinkVector& l = (*i1);
00151         for (LinkVector::const_iterator i2=l.begin(); i2!=l.end(); ++i2) {
00152             ret[*i2] = (*i2)->getState();
00153         }
00154     }
00155     return ret;
00156 }
00157 
00158 
00159 bool
00160 MSTrafficLightLogic::setTrafficLightSignals(SUMOTime t) const {
00161     // get the current traffic light signal combination
00162     const std::string& state = getCurrentPhaseDef().getState();
00163     // go through the links
00164     for (size_t i=0; i<myLinks.size(); i++) {
00165         const LinkVector& currGroup = myLinks[i];
00166         LinkState ls = (LinkState) state[i];
00167         for (LinkVector::const_iterator j=currGroup.begin(); j!=currGroup.end(); j++) {
00168             (*j)->setTLState(ls, t);
00169         }
00170     }
00171     return true;
00172 }
00173 
00174 
00175 void
00176 MSTrafficLightLogic::resetLinkStates(const std::map<MSLink*, LinkState> &vals) const {
00177     for (LinkVectorVector::const_iterator i1=myLinks.begin(); i1!=myLinks.end(); ++i1) {
00178         const LinkVector& l = (*i1);
00179         for (LinkVector::const_iterator i2=l.begin(); i2!=l.end(); ++i2) {
00180             assert(vals.find(*i2)!=vals.end());
00181             (*i2)->setTLState(vals.find(*i2)->second, MSNet::getInstance()->getCurrentTimeStep());
00182         }
00183     }
00184 }
00185 
00186 
00187 // ----------- Static Information Retrieval
00188 int
00189 MSTrafficLightLogic::getLinkIndex(const MSLink* const link) const {
00190     int index = 0;
00191     for (LinkVectorVector::const_iterator i1=myLinks.begin(); i1!=myLinks.end(); ++i1, ++index) {
00192         const LinkVector& l = (*i1);
00193         for (LinkVector::const_iterator i2=l.begin(); i2!=l.end(); ++i2) {
00194             if ((*i2)==link) {
00195                 return index;
00196             }
00197         }
00198     }
00199     return -1;
00200 }
00201 
00202 
00203 
00204 // ----------- Dynamic Information Retrieval
00205 SUMOTime
00206 MSTrafficLightLogic::getNextSwitchTime() const {
00207     return mySwitchCommand!=0 ? mySwitchCommand->getNextSwitchTime() : -1;
00208 }
00209 
00210 
00211 // ----------- Changing phases and phase durations
00212 void
00213 MSTrafficLightLogic::addOverridingDuration(SUMOTime duration) {
00214     myOverridingTimes.push_back(duration);
00215 }
00216 
00217 
00218 void
00219 MSTrafficLightLogic::setCurrentDurationIncrement(SUMOTime delay) {
00220     myCurrentDurationIncrement = delay;
00221 }
00222 
00223 
00224 
00225 
00226 // ----------- Algorithm parameter handling
00227 void
00228 MSTrafficLightLogic::setParameter(const std::map<std::string, std::string> &params) {
00229     myParameter = params;
00230 }
00231 
00232 
00233 std::string
00234 MSTrafficLightLogic::getParameterValue(const std::string& key) const {
00235     if (myParameter.find(key)==myParameter.end()) {
00236         return "";
00237     }
00238     return myParameter.find(key)->second;
00239 }
00240 
00241 
00242 /****************************************************************************/
00243 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines