SUMO - Simulation of Urban MObility
NBTrafficLightLogic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // A SUMO-compliant built logic for a traffic light
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <vector>
33 #include <bitset>
34 #include <utility>
35 #include <string>
36 #include <sstream>
37 #include <cassert>
38 #include "NBEdge.h"
39 #include "NBEdgeCont.h"
40 #include "NBTrafficLightLogic.h"
43 #include <utils/options/Option.h>
44 #include <utils/common/ToString.h>
48 
49 
50 // ===========================================================================
51 // static members
52 // ===========================================================================
55  LINKSTATE_STOP, // used for NODETYPE_TRAFFIC_LIGHT_RIGHT_ON_RED
62  };
63 
65 
66 // ===========================================================================
67 // member method definitions
68 // ===========================================================================
70  const std::string& subid, int noLinks,
71  SUMOTime offset, TrafficLightType type) :
72  Named(id), myNumLinks(noLinks), mySubID(subid),
73  myOffset(offset),
74  myType(type) {}
75 
77  Named(logic->getID()),
78  myNumLinks(logic->myNumLinks),
79  mySubID(logic->getProgramID()),
80  myOffset(logic->getOffset()),
81  myPhases(logic->myPhases.begin(), logic->myPhases.end()),
82  myType(logic->getType()) {}
83 
84 
86 
87 void
88 NBTrafficLightLogic::addStep(SUMOTime duration, const std::string& state, int index) {
89  addStep(duration, state,
92  index);
93 }
94 
95 void
96 NBTrafficLightLogic::addStep(SUMOTime duration, const std::string& state, SUMOTime minDur, SUMOTime maxDur, int index) {
97  // check state size
98  if (myNumLinks == 0) {
99  // initialize
100  myNumLinks = (int)state.size();
101  } else if ((int)state.size() != myNumLinks) {
102  throw ProcessError("When adding phase to tlLogic '" + getID() + "': state length of " + toString(state.size()) +
103  " does not match declared number of links " + toString(myNumLinks));
104  }
105  // check state contents
106  const std::string::size_type illegal = state.find_first_not_of(ALLOWED_STATES);
107  if (std::string::npos != illegal) {
108  throw ProcessError("When adding phase: illegal character '" + toString(state[illegal]) + "' in state");
109  }
110  // interpret index
111  if (index < 0 || index >= (int)myPhases.size()) {
112  // insert at the end
113  index = (int)myPhases.size();
114  }
115  myPhases.insert(myPhases.begin() + index, PhaseDefinition(duration, state, minDur, maxDur));
116 }
117 
118 
119 void
121  if (index >= (int)myPhases.size()) {
122  throw InvalidArgument("Index " + toString(index) + " out of range for logic with "
123  + toString(myPhases.size()) + " phases.");
124  }
125  myPhases.erase(myPhases.begin() + index);
126 }
127 
128 
129 void
131  if (myNumLinks > numLinks) {
132  for (PhaseDefinition& p : myPhases) {
133  p.state = p.state.substr(0, numLinks);
134  }
135  } else {
136  std::string add(numLinks - myNumLinks, (char)fill);
137  for (PhaseDefinition& p : myPhases) {
138  p.state = p.state + add;
139  }
140  }
141  myNumLinks = numLinks;
142 }
143 
144 
145 void
147  myNumLinks = 0;
148  myPhases.clear();
149 }
150 
151 
152 SUMOTime
154  SUMOTime duration = 0;
155  for (PhaseDefinitionVector::const_iterator i = myPhases.begin(); i != myPhases.end(); ++i) {
156  duration += (*i).duration;
157  }
158  return duration;
159 }
160 
161 
162 void
163 NBTrafficLightLogic::closeBuilding(bool checkVarDurations) {
164  for (int i = 0; i < (int)myPhases.size() - 1;) {
165  if (myPhases[i].state != myPhases[i + 1].state) {
166  ++i;
167  continue;
168  }
169  myPhases[i].duration += myPhases[i + 1].duration;
172  myPhases[i].minDur += myPhases[i + 1].minDur;
173  } else {
174  myPhases[i].minDur = myPhases[i + 1].minDur;
175  }
176  }
179  myPhases[i].maxDur += myPhases[i + 1].maxDur;
180  } else {
181  myPhases[i].maxDur = myPhases[i + 1].maxDur;
182  }
183  }
184  myPhases.erase(myPhases.begin() + i + 1);
185  }
186  // check if actuated lights are defined correctly
187  if (checkVarDurations) {
188  if (myType != TLTYPE_STATIC) {
189  bool found = false;
190  for (auto p : myPhases) {
193  found = true;
194  break;
195  }
196  }
197  if (!found) {
198  WRITE_WARNING("Non-static traffic light '" + getID() + "' does not define variable phase length.");
199  }
200  }
201  }
202 }
203 
204 
205 void
206 NBTrafficLightLogic::setPhaseState(int phaseIndex, int tlIndex, LinkState linkState) {
207  assert(phaseIndex < (int)myPhases.size());
208  std::string& phaseState = myPhases[phaseIndex].state;
209  assert(tlIndex < (int)phaseState.size());
210  phaseState[tlIndex] = (char)linkState;
211 }
212 
213 
214 void
216  assert(phaseIndex < (int)myPhases.size());
217  myPhases[phaseIndex].duration = duration;
218 }
219 
220 void
222  assert(phaseIndex < (int)myPhases.size());
223  myPhases[phaseIndex].minDur = duration;
224 }
225 
226 void
228  assert(phaseIndex < (int)myPhases.size());
229  myPhases[phaseIndex].maxDur = duration;
230 }
231 
232 
233 /****************************************************************************/
234 
The link has green light, may pass.
static const std::string ALLOWED_STATES
TrafficLightType getType() const
get the algorithm type (static etc..)
const std::string & getProgramID() const
Returns the ProgramID.
void setPhaseMaxDuration(int phaseIndex, SUMOTime duration)
void setPhaseState(int phaseIndex, int tlIndex, LinkState linkState)
Modifies the state for an existing phase (used by NETEDIT)
A SUMO-compliant built logic for a traffic light.
The link has green light, has to brake.
This is an uncontrolled, minor link, has to stop.
int myNumLinks
The number of participating links.
NBTrafficLightLogic(const std::string &id, const std::string &subid, int noLinks, SUMOTime offset=0, TrafficLightType type=TLTYPE_STATIC)
Constructor.
void setPhaseMinDuration(int phaseIndex, SUMOTime duration)
static const SUMOTime UNSPECIFIED_DURATION
The definition of a single phase of the logic.
The link is controlled by a tls which is off, not blinking, may pass.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
const std::string & getID() const
Returns the id.
Definition: Named.h:65
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
SUMOTime myOffset
The tls program&#39;s offset.
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic, in MSLink and GNEInternalLane.
SUMOTime getDuration() const
Returns the duration of the complete cycle.
Base class for objects which have an id.
Definition: Named.h:45
void closeBuilding(bool checkVarDurations=true)
closes the building process
SUMOTime getOffset() const
Returns the offset of first switch.
The link has yellow light, may pass.
The link is controlled by a tls which is off and blinks, has to brake.
const std::string mySubID
The tls program&#39;s subid.
~NBTrafficLightLogic()
Destructor.
The link has red light (must brake)
void setPhaseDuration(int phaseIndex, SUMOTime duration)
Modifies the duration for an existing phase (used by NETEDIT)
void setStateLength(int numLinks, LinkState fill=LINKSTATE_TL_RED)
long long int SUMOTime
Definition: TraCIDefs.h:51
TrafficLightType myType
The algorithm type for the traffic light.
The link has yellow light, has to brake anyway.
void addStep(SUMOTime duration, const std::string &state, int index=-1)
Adds a phase to the logic.
The link has red light (must brake) but indicates upcoming green.
PhaseDefinitionVector myPhases
The junction logic&#39;s storage for traffic light phase list.
TrafficLightType
static const char allowedStatesInitializer[]
all allowed characters for phase state (see SUMOXMLDefinitions)