SUMO - Simulation of Urban MObility
NBTrafficLightLogic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A SUMO-compliant built logic for a traffic light
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2017 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 <vector>
34 #include <bitset>
35 #include <utility>
36 #include <string>
37 #include <sstream>
38 #include <cassert>
39 #include "NBEdge.h"
40 #include "NBEdgeCont.h"
41 #include "NBTrafficLightLogic.h"
44 #include <utils/options/Option.h>
45 #include <utils/common/ToString.h>
49 
50 
51 // ===========================================================================
52 // static members
53 // ===========================================================================
56  LINKSTATE_STOP, // used for NODETYPE_TRAFFIC_LIGHT_RIGHT_ON_RED
63  };
64 
66 
67 // ===========================================================================
68 // member method definitions
69 // ===========================================================================
71  const std::string& subid, int noLinks,
72  SUMOTime offset, TrafficLightType type) :
73  Named(id), myNumLinks(noLinks), mySubID(subid),
74  myOffset(offset),
75  myType(type) {}
76 
78  Named(logic->getID()),
79  myNumLinks(logic->myNumLinks),
80  mySubID(logic->getProgramID()),
81  myOffset(logic->getOffset()),
82  myPhases(logic->myPhases.begin(), logic->myPhases.end()),
83  myType(logic->getType()) {}
84 
85 
87 
88 void
89 NBTrafficLightLogic::addStep(SUMOTime duration, const std::string& state, int index) {
90  addStep(duration, state,
93  index);
94 }
95 
96 void
97 NBTrafficLightLogic::addStep(SUMOTime duration, const std::string& state, SUMOTime minDur, SUMOTime maxDur, int index) {
98  // check state size
99  if (myNumLinks == 0) {
100  // initialize
101  myNumLinks = (int)state.size();
102  } else if ((int)state.size() != myNumLinks) {
103  throw ProcessError("When adding phase to tlLogic '" + getID() + "': state length of " + toString(state.size()) +
104  " does not match declared number of links " + toString(myNumLinks));
105  }
106  // check state contents
107  const std::string::size_type illegal = state.find_first_not_of(ALLOWED_STATES);
108  if (std::string::npos != illegal) {
109  throw ProcessError("When adding phase: illegal character '" + toString(state[illegal]) + "' in state");
110  }
111  // interpret index
112  if (index < 0 || index >= (int)myPhases.size()) {
113  // insert at the end
114  index = (int)myPhases.size();
115  }
116  myPhases.insert(myPhases.begin() + index, PhaseDefinition(duration, state, minDur, maxDur));
117 }
118 
119 
120 void
122  if (index >= (int)myPhases.size()) {
123  throw InvalidArgument("Index " + toString(index) + " out of range for logic with "
124  + toString(myPhases.size()) + " phases.");
125  }
126  myPhases.erase(myPhases.begin() + index);
127 }
128 
129 
130 void
132  myNumLinks = 0;
133  myPhases.clear();
134 }
135 
136 
137 SUMOTime
139  SUMOTime duration = 0;
140  for (PhaseDefinitionVector::const_iterator i = myPhases.begin(); i != myPhases.end(); ++i) {
141  duration += (*i).duration;
142  }
143  return duration;
144 }
145 
146 
147 void
148 NBTrafficLightLogic::closeBuilding(bool checkVarDurations) {
149  for (int i = 0; i < (int)myPhases.size() - 1;) {
150  if (myPhases[i].state != myPhases[i + 1].state) {
151  ++i;
152  continue;
153  }
154  myPhases[i].duration += myPhases[i + 1].duration;
157  myPhases[i].minDur += myPhases[i + 1].minDur;
158  } else {
159  myPhases[i].minDur = myPhases[i + 1].minDur;
160  }
161  }
164  myPhases[i].maxDur += myPhases[i + 1].maxDur;
165  } else {
166  myPhases[i].maxDur = myPhases[i + 1].maxDur;
167  }
168  }
169  myPhases.erase(myPhases.begin() + i + 1);
170  }
171  // check if actuated lights are defined correctly
172  if (checkVarDurations) {
173  if (myType != TLTYPE_STATIC) {
174  bool found = false;
175  for (auto p : myPhases) {
178  found = true;
179  break;
180  }
181  }
182  if (!found) {
183  WRITE_WARNING("Non-static traffic light '" + getID() + "' does not define variable phase length.");
184  }
185  }
186  }
187 }
188 
189 
190 void
191 NBTrafficLightLogic::setPhaseState(int phaseIndex, int tlIndex, LinkState linkState) {
192  assert(phaseIndex < (int)myPhases.size());
193  std::string& phaseState = myPhases[phaseIndex].state;
194  assert(tlIndex < (int)phaseState.size());
195  phaseState[tlIndex] = (char)linkState;
196 }
197 
198 
199 void
201  assert(phaseIndex < (int)myPhases.size());
202  myPhases[phaseIndex].duration = duration;
203 }
204 
205 void
207  assert(phaseIndex < (int)myPhases.size());
208  myPhases[phaseIndex].minDur = duration;
209 }
210 
211 void
213  assert(phaseIndex < (int)myPhases.size());
214  myPhases[phaseIndex].maxDur = duration;
215 }
216 
217 
218 /****************************************************************************/
219 
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:200
const std::string & getID() const
Returns the id.
Definition: Named.h:66
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
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:46
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)
long long int SUMOTime
Definition: TraCIDefs.h:52
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)