SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSTrafficLightLogic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // The parent class for traffic light logics
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <string>
34 #include <iostream>
35 #include <map>
36 #include <microsim/MSLink.h>
37 #include <microsim/MSLane.h>
38 #include "MSTrafficLightLogic.h"
40 #include "MSTLLogicControl.h"
42 
43 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // CHECK_MEMORY_LEAKS
46 
47 
48 // ===========================================================================
49 // member method definitions
50 // ===========================================================================
51 /* -------------------------------------------------------------------------
52  * member method definitions
53  * ----------------------------------------------------------------------- */
55  MSTrafficLightLogic* tlLogic, SUMOTime nextSwitch)
56  : myTLControl(tlcontrol), myTLLogic(tlLogic),
57  myAssumedNextSwitch(nextSwitch), myAmValid(true) {}
58 
59 
61 
62 
63 
66  // check whether this command has been descheduled
67  if (!myAmValid) {
68  return 0;
69  }
70  //
71  bool isActive = myTLControl.isActive(myTLLogic);
72  size_t step1 = myTLLogic->getCurrentPhaseIndex();
73  SUMOTime next = myTLLogic->trySwitch(isActive);
74  size_t step2 = myTLLogic->getCurrentPhaseIndex();
75  if (step1 != step2) {
76  if (isActive) {
77  // execute any action connected to this tls
78  const MSTLLogicControl::TLSLogicVariants& vars = myTLControl.get(myTLLogic->getID());
79  // set link priorities
80  myTLLogic->setTrafficLightSignals(t);
81  // execute switch actions
83  }
84  }
85  myAssumedNextSwitch += next;
86  return next;
87 }
88 
89 
90 void
92  if (tlLogic == myTLLogic) {
93  myAmValid = false;
94  myAssumedNextSwitch = -1;
95  }
96 }
97 
98 
99 /* -------------------------------------------------------------------------
100  * member method definitions
101  * ----------------------------------------------------------------------- */
103  const std::string& id, const std::string& programID,
104  SUMOTime delay)
105  : myID(id), myProgramID(programID), myCurrentDurationIncrement(-1),
106  myDefaultCycleTime(0) {
107  mySwitchCommand = new SwitchCommand(tlcontrol, this, delay);
110 }
111 
112 
113 void
115 }
116 
117 
120 }
121 
122 
123 // ----------- Handling of controlled links
124 void
125 MSTrafficLightLogic::addLink(MSLink* link, MSLane* lane, unsigned int pos) {
126  // !!! should be done within the loader (checking necessary)
127  myLinks.reserve(pos + 1);
128  while (myLinks.size() <= pos) {
129  myLinks.push_back(LinkVector());
130  }
131  myLinks[pos].push_back(link);
132  //
133  myLanes.reserve(pos + 1);
134  while (myLanes.size() <= pos) {
135  myLanes.push_back(LaneVector());
136  }
137  myLanes[pos].push_back(lane);
138  link->setTLState((LinkState) getCurrentPhaseDef().getState()[pos], MSNet::getInstance()->getCurrentTimeStep());
139 }
140 
141 
142 void
144  myLinks = logic.myLinks;
145  myLanes = logic.myLanes;
146 }
147 
148 
149 std::map<MSLink*, LinkState>
151  std::map<MSLink*, LinkState> ret;
152  for (LinkVectorVector::const_iterator i1 = myLinks.begin(); i1 != myLinks.end(); ++i1) {
153  const LinkVector& l = (*i1);
154  for (LinkVector::const_iterator i2 = l.begin(); i2 != l.end(); ++i2) {
155  ret[*i2] = (*i2)->getState();
156  }
157  }
158  return ret;
159 }
160 
161 
162 bool
164  // get the current traffic light signal combination
165  const std::string& state = getCurrentPhaseDef().getState();
166  // go through the links
167  for (size_t i = 0; i < myLinks.size(); i++) {
168  const LinkVector& currGroup = myLinks[i];
169  LinkState ls = (LinkState) state[i];
170  for (LinkVector::const_iterator j = currGroup.begin(); j != currGroup.end(); j++) {
171  (*j)->setTLState(ls, t);
172  }
173  }
174  return true;
175 }
176 
177 
178 void
179 MSTrafficLightLogic::resetLinkStates(const std::map<MSLink*, LinkState> &vals) const {
180  for (LinkVectorVector::const_iterator i1 = myLinks.begin(); i1 != myLinks.end(); ++i1) {
181  const LinkVector& l = (*i1);
182  for (LinkVector::const_iterator i2 = l.begin(); i2 != l.end(); ++i2) {
183  assert(vals.find(*i2) != vals.end());
184  (*i2)->setTLState(vals.find(*i2)->second, MSNet::getInstance()->getCurrentTimeStep());
185  }
186  }
187 }
188 
189 
190 // ----------- Static Information Retrieval
191 int
192 MSTrafficLightLogic::getLinkIndex(const MSLink* const link) const {
193  int index = 0;
194  for (LinkVectorVector::const_iterator i1 = myLinks.begin(); i1 != myLinks.end(); ++i1, ++index) {
195  const LinkVector& l = (*i1);
196  for (LinkVector::const_iterator i2 = l.begin(); i2 != l.end(); ++i2) {
197  if ((*i2) == link) {
198  return index;
199  }
200  }
201  }
202  return -1;
203 }
204 
205 
206 
207 // ----------- Dynamic Information Retrieval
208 SUMOTime
210  return mySwitchCommand != 0 ? mySwitchCommand->getNextSwitchTime() : -1;
211 }
212 
213 
214 // ----------- Changing phases and phase durations
215 void
217  myOverridingTimes.push_back(duration);
218 }
219 
220 
221 void
224 }
225 
226 
227 
228 
229 // ----------- Algorithm parameter handling
230 void
231 MSTrafficLightLogic::setParameter(const std::map<std::string, std::string> &params) {
232  myParameter = params;
233 }
234 
235 
236 std::string
237 MSTrafficLightLogic::getParameterValue(const std::string& key) const {
238  if (myParameter.find(key) == myParameter.end()) {
239  return "";
240  }
241  return myParameter.find(key)->second;
242 }
243 
244 
245 /****************************************************************************/
246