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