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-2013 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 <cassert>
34 #include <string>
35 #include <iostream>
36 #include <map>
37 #include <microsim/MSLink.h>
38 #include <microsim/MSLane.h>
39 #include "MSTrafficLightLogic.h"
41 #include "MSTLLogicControl.h"
43 
44 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
47 
48 
49 // ===========================================================================
50 // member method definitions
51 // ===========================================================================
52 /* -------------------------------------------------------------------------
53  * member method definitions
54  * ----------------------------------------------------------------------- */
56  MSTrafficLightLogic* tlLogic, SUMOTime nextSwitch)
57  : myTLControl(tlcontrol), myTLLogic(tlLogic),
58  myAssumedNextSwitch(nextSwitch), myAmValid(true) {}
59 
60 
62 
63 
64 
67  // check whether this command has been descheduled
68  if (!myAmValid) {
69  return 0;
70  }
71  //
72  bool isActive = myTLControl.isActive(myTLLogic);
73  size_t step1 = myTLLogic->getCurrentPhaseIndex();
74  SUMOTime next = myTLLogic->trySwitch(isActive);
75  size_t step2 = myTLLogic->getCurrentPhaseIndex();
76  if (step1 != step2) {
77  if (isActive) {
78  // execute any action connected to this tls
79  const MSTLLogicControl::TLSLogicVariants& vars = myTLControl.get(myTLLogic->getID());
80  // set link priorities
81  myTLLogic->setTrafficLightSignals(t);
82  // execute switch actions
84  }
85  }
86  myAssumedNextSwitch += next;
87  return next;
88 }
89 
90 
91 void
93  if (tlLogic == myTLLogic) {
94  myAmValid = false;
95  myAssumedNextSwitch = -1;
96  }
97 }
98 
99 
100 /* -------------------------------------------------------------------------
101  * member method definitions
102  * ----------------------------------------------------------------------- */
104  MSTLLogicControl& tlcontrol,
105  const std::string& id,
106  const std::string& programID,
107  SUMOTime delay,
108  const ParameterMap& parameters) :
109  myParameter(parameters),
110  myID(id),
111  myProgramID(programID),
113  myDefaultCycleTime(0) {
114  mySwitchCommand = new SwitchCommand(tlcontrol, this, delay);
117 }
118 
119 
120 void
122 }
123 
124 
126  // no need to do something about mySwitchCommand here,
127  // it is handled by the event control
128 }
129 
130 
131 // ----------- Handling of controlled links
132 void
133 MSTrafficLightLogic::addLink(MSLink* link, MSLane* lane, unsigned int pos) {
134  // !!! should be done within the loader (checking necessary)
135  myLinks.reserve(pos + 1);
136  while (myLinks.size() <= pos) {
137  myLinks.push_back(LinkVector());
138  }
139  myLinks[pos].push_back(link);
140  //
141  myLanes.reserve(pos + 1);
142  while (myLanes.size() <= pos) {
143  myLanes.push_back(LaneVector());
144  }
145  myLanes[pos].push_back(lane);
146  link->setTLState((LinkState) getCurrentPhaseDef().getState()[pos], MSNet::getInstance()->getCurrentTimeStep());
147 }
148 
149 
150 void
152  myLinks = logic.myLinks;
153  myLanes = logic.myLanes;
154 }
155 
156 
157 std::map<MSLink*, LinkState>
159  std::map<MSLink*, LinkState> ret;
160  for (LinkVectorVector::const_iterator i1 = myLinks.begin(); i1 != myLinks.end(); ++i1) {
161  const LinkVector& l = (*i1);
162  for (LinkVector::const_iterator i2 = l.begin(); i2 != l.end(); ++i2) {
163  ret[*i2] = (*i2)->getState();
164  }
165  }
166  return ret;
167 }
168 
169 
170 bool
172  // get the current traffic light signal combination
173  const std::string& state = getCurrentPhaseDef().getState();
174  // go through the links
175  for (size_t i = 0; i < myLinks.size(); i++) {
176  const LinkVector& currGroup = myLinks[i];
177  LinkState ls = (LinkState) state[i];
178  for (LinkVector::const_iterator j = currGroup.begin(); j != currGroup.end(); j++) {
179  (*j)->setTLState(ls, t);
180  }
181  }
182  return true;
183 }
184 
185 
186 void
187 MSTrafficLightLogic::resetLinkStates(const std::map<MSLink*, LinkState>& vals) const {
188  for (LinkVectorVector::const_iterator i1 = myLinks.begin(); i1 != myLinks.end(); ++i1) {
189  const LinkVector& l = (*i1);
190  for (LinkVector::const_iterator i2 = l.begin(); i2 != l.end(); ++i2) {
191  assert(vals.find(*i2) != vals.end());
192  (*i2)->setTLState(vals.find(*i2)->second, MSNet::getInstance()->getCurrentTimeStep());
193  }
194  }
195 }
196 
197 
198 // ----------- Static Information Retrieval
199 int
200 MSTrafficLightLogic::getLinkIndex(const MSLink* const link) const {
201  int index = 0;
202  for (LinkVectorVector::const_iterator i1 = myLinks.begin(); i1 != myLinks.end(); ++i1, ++index) {
203  const LinkVector& l = (*i1);
204  for (LinkVector::const_iterator i2 = l.begin(); i2 != l.end(); ++i2) {
205  if ((*i2) == link) {
206  return index;
207  }
208  }
209  }
210  return -1;
211 }
212 
213 
214 
215 // ----------- Dynamic Information Retrieval
216 SUMOTime
218  return mySwitchCommand != 0 ? mySwitchCommand->getNextSwitchTime() : -1;
219 }
220 
221 
222 // ----------- Changing phases and phase durations
223 void
225  myOverridingTimes.push_back(duration);
226 }
227 
228 
229 void
232 }
233 
234 
235 
236 
237 // ----------- Algorithm parameter handling
238 void
240  myParameter = params;
241 }
242 
243 
244 std::string
245 MSTrafficLightLogic::getParameterValue(const std::string& key) const {
246  if (myParameter.find(key) == myParameter.end()) {
247  return "";
248  }
249  return myParameter.find(key)->second;
250 }
251 
252 
253 /****************************************************************************/
254