SUMO - Simulation of Urban MObility
NBTrafficLightLogicCont.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A container for traffic light definitions and built programs
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 #include <map>
33 #include <string>
34 #include <algorithm>
36 #include <utils/common/ToString.h>
40 #include "NBTrafficLightLogic.h"
42 #include "NBOwnTLDef.h"
43 #include "NBEdgeCont.h"
44 #include "NBNodeCont.h"
45 
46 
47 // ===========================================================================
48 // static members
49 // ===========================================================================
51 
52 // ===========================================================================
53 // method definitions
54 // ===========================================================================
56 
57 
59  clear();
60 }
61 
62 
63 void
65  // check whether any offsets shall be manipulated by setting
66  // them to half of the duration
67  if (oc.isSet("tls.half-offset")) {
68  std::vector<std::string> ids = oc.getStringVector("tls.half-offset");
69  myHalfOffsetTLS.insert(ids.begin(), ids.end());
70  }
71  // check whether any offsets shall be manipulated by setting
72  // them to a quarter of the duration
73  if (oc.isSet("tls.quarter-offset")) {
74  std::vector<std::string> ids = oc.getStringVector("tls.quarter-offset");
75  myHalfOffsetTLS.insert(ids.begin(), ids.end());
76  }
77 }
78 
79 
80 bool
82  myExtracted.erase(logic);
83  if (myDefinitions.count(logic->getID())) {
84  if (myDefinitions[logic->getID()].count(logic->getProgramID())) {
85  if (forceInsert) {
86  const Program2Def& programs = myDefinitions[logic->getID()];
87  IDSupplier idS("", 0);
88  for (Program2Def::const_iterator it_prog = programs.begin(); it_prog != programs.end(); it_prog++) {
89  idS.avoid(it_prog->first);
90  }
91  logic->setProgramID(idS.getNext());
92  } else {
93  return false;
94  }
95  }
96  } else {
97  myDefinitions[logic->getID()] = Program2Def();
98  }
99  myDefinitions[logic->getID()][logic->getProgramID()] = logic;
100  return true;
101 }
102 
103 
104 bool
106  if (myDefinitions.count(id)) {
107  // delete all programs
108  for (Program2Def::iterator i = myDefinitions[id].begin(); i != myDefinitions[id].end(); i++) {
109  delete i->second;
110  }
111  myDefinitions.erase(id);
112  // also delete any logics that were already computed
113  if (myComputed.count(id)) {
114  for (Program2Logic::iterator i = myComputed[id].begin(); i != myComputed[id].end(); i++) {
115  delete i->second;
116  }
117  myComputed.erase(id);
118  }
119  return true;
120  } else {
121  return false;
122  }
123 }
124 
125 
126 bool
127 NBTrafficLightLogicCont::removeProgram(const std::string id, const std::string programID, bool del) {
128  if (myDefinitions.count(id) && myDefinitions[id].count(programID)) {
129  if (del) {
130  delete myDefinitions[id][programID];
131  }
132  myDefinitions[id].erase(programID);
133  return true;
134  } else {
135  return false;
136  }
137 }
138 
139 
140 void
142  myExtracted.insert(definition);
143  removeProgram(definition->getID(), definition->getProgramID(), false);
144 }
145 
146 
147 std::pair<int, int>
149  // clean previous logics
150  Logics logics = getComputed();
151  for (Logics::iterator it = logics.begin(); it != logics.end(); it++) {
152  delete *it;
153  }
154  myComputed.clear();
155 
156  int numPrograms = 0;
157  Definitions definitions = getDefinitions();
158  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
159  if (computeSingleLogic(oc, *it)) {
160  numPrograms++;
161  }
162  }
163  return std::pair<int, int>((int)myComputed.size(), numPrograms);
164 }
165 
166 
167 bool
169  if (def->getNodes().size() == 0) {
170  return false;
171  }
172  const std::string& id = def->getID();
173  const std::string& programID = def->getProgramID();
174  // build program
175  NBTrafficLightLogic* built = def->compute(oc);
176  if (built == 0) {
177  WRITE_WARNING("Could not build program '" + programID + "' for traffic light '" + id + "'");
178  return false;
179  }
180  // compute offset
181  SUMOTime T = built->getDuration();
182  if (myHalfOffsetTLS.count(id)) {
183  built->setOffset((SUMOTime)(T / 2.));
184  }
185  if (myQuarterOffsetTLS.count(id)) {
186  built->setOffset((SUMOTime)(T / 4.));
187  }
188  // and insert the result after computation
189  // make sure we don't leak memory if computeSingleLogic is called externally
190  if (myComputed[id][programID] != 0) {
191  delete myComputed[id][programID];
192  }
193  myComputed[id][programID] = built;
194  return true;
195 }
196 
197 
198 void
200  Definitions definitions = getDefinitions();
201  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
202  delete *it;
203  }
204  myDefinitions.clear();
205  Logics logics = getComputed();
206  for (Logics::iterator it = logics.begin(); it != logics.end(); it++) {
207  delete *it;
208  }
209  myComputed.clear();
210  for (std::set<NBTrafficLightDefinition*>::iterator it = myExtracted.begin(); it != myExtracted.end(); it++) {
211  delete *it;
212  }
213  myExtracted.clear();
214 }
215 
216 
217 void
219  const EdgeVector& outgoing) {
220  Definitions definitions = getDefinitions();
221  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
222  (*it)->remapRemoved(removed, incoming, outgoing);
223  }
224 }
225 
226 
227 void
229  NBEdge* by, int byLane) {
230  Definitions definitions = getDefinitions();
231  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
232  (*it)->replaceRemoved(removed, removedLane, by, byLane);
233  }
234 }
235 
236 
238 NBTrafficLightLogicCont::getDefinition(const std::string& id, const std::string& programID) const {
239  Id2Defs::const_iterator i = myDefinitions.find(id);
240  if (i != myDefinitions.end()) {
241  Program2Def programs = i->second;
242  Program2Def::const_iterator i2 = programs.find(programID);
243  if (i2 != programs.end()) {
244  return i2->second;
245  }
246  }
247  return 0;
248 }
249 
251 NBTrafficLightLogicCont::getPrograms(const std::string& id) const {
252  Id2Defs::const_iterator it = myDefinitions.find(id);
253  if (it != myDefinitions.end()) {
254  return it->second;
255  } else {
256  return EmptyDefinitions;
257  }
258 }
259 
260 
262 NBTrafficLightLogicCont::getLogic(const std::string& id, const std::string& programID) const {
263  Id2Logics::const_iterator i = myComputed.find(id);
264  if (i != myComputed.end()) {
265  Program2Logic programs = i->second;
266  Program2Logic::const_iterator i2 = programs.find(programID);
267  if (i2 != programs.end()) {
268  return i2->second;
269  }
270  }
271  return 0;
272 }
273 
274 
275 void
277  Definitions definitions = getDefinitions();
278  // set the information about all participants, first
279  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
280  (*it)->setParticipantsInformation();
281  }
282  // clear previous information because tlDefs may have been removed in NETEDIT
284  // insert the information about the tl-controlling
285  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
286  (*it)->setTLControllingInformation();
287  }
288  // handle rail signals which are not instantiated as normal definitions
289  for (std::map<std::string, NBNode*>::const_iterator it = nc.begin(); it != nc.end(); it ++) {
290  NBNode* n = it->second;
292  NBOwnTLDef dummy(n->getID(), n, 0, TLTYPE_STATIC);
294  dummy.setTLControllingInformation();
295  n->setCrossingTLIndices(dummy.getID(), (int)dummy.getControlledLinks().size());
296  n->removeTrafficLight(&dummy);
297  }
298  }
299 }
300 
301 
304  Logics result;
305  for (Id2Logics::const_iterator it_id = myComputed.begin(); it_id != myComputed.end(); it_id++) {
306  const Program2Logic& programs = it_id->second;
307  for (Program2Logic::const_iterator it_prog = programs.begin(); it_prog != programs.end(); it_prog++) {
308  result.push_back(it_prog->second);
309  }
310  }
311  return result;
312 }
313 
314 
317  Definitions result;
318  for (Id2Defs::const_iterator it_id = myDefinitions.begin(); it_id != myDefinitions.end(); it_id++) {
319  const Program2Def& programs = it_id->second;
320  for (Program2Def::const_iterator it_prog = programs.begin(); it_prog != programs.end(); it_prog++) {
321  result.push_back(it_prog->second);
322  }
323  }
324  return result;
325 }
326 
327 
328 /****************************************************************************/
329 
const std::map< std::string, NBTrafficLightDefinition * > & getPrograms(const std::string &id) const
Returns all programs for the given tl-id.
std::map< std::string, NBTrafficLightLogic * > Program2Logic
Definition of internal the container types.
bool removeProgram(const std::string id, const std::string programID, bool del=true)
Removes a program of a logic definition from the dictionary.
std::map< std::string, NBNode * >::const_iterator begin() const
Returns the pointer to the begin of the stored nodes.
Definition: NBNodeCont.h:114
static const Program2Def EmptyDefinitions
std::map< std::string, NBNode * >::const_iterator end() const
Returns the pointer to the end of the stored nodes.
Definition: NBNodeCont.h:119
A SUMO-compliant built logic for a traffic light.
The representation of a single edge during network building.
Definition: NBEdge.h:71
NBTrafficLightLogic * compute(OptionsCont &oc)
Computes the traffic light logic.
void avoid(const std::string &id)
make sure that the given id is never supplied
Definition: IDSupplier.cpp:67
The base class for traffic light logic definitions.
void setOffset(SUMOTime offset)
Sets the offset of this tls.
std::vector< NBTrafficLightDefinition * > Definitions
const std::string & getID() const
Returns the id.
Definition: Named.h:66
void removeTrafficLight(NBTrafficLightDefinition *tlDef)
Removes the given traffic light from this node.
Definition: NBNode.cpp:320
void extract(NBTrafficLightDefinition *definition)
Extracts a traffic light definition from myDefinitions but keeps it in myExtracted for eventual * del...
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
std::set< NBTrafficLightDefinition * > myExtracted
The container for extracted definitions.
std::set< std::string > myQuarterOffsetTLS
List of tls which shall have an offset of T/2.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
std::map< std::string, NBTrafficLightDefinition * > Program2Def
std::string getNext()
Returns the next id.
Definition: IDSupplier.cpp:59
NBTrafficLightDefinition * getDefinition(const std::string &id, const std::string &programID) const
Returns the named definition.
void remapRemoved(NBEdge *removed, const EdgeVector &incoming, const EdgeVector &outgoing)
Replaces occurences of the removed edge in incoming/outgoing edges of all definitions.
void setCrossingTLIndices(const std::string &tlID, int startIndex)
set tl indices of this nodes crossing starting at the given index
Definition: NBNode.cpp:2588
void applyOptions(OptionsCont &oc)
Initialises the storage by applying given options.
std::set< std::string > myHalfOffsetTLS
List of tls which shall have an offset of T/2.
void clearControllingTLInformation() const
Clears information about controlling traffic lights for all connenections of all edges.
Definition: NBEdgeCont.cpp:600
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:66
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String) ...
void setProgramID(const std::string &programID)
Sets the programID.
const std::string & getProgramID() const
Returns the ProgramID.
std::vector< NBTrafficLightLogic * > Logics
SUMOTime getDuration() const
Returns the duration of the complete cycle.
Id2Defs myDefinitions
The container for tl-ids to their definitions.
std::vector< NBTrafficLightLogic * > getComputed() const
Returns a list of all computed logics.
bool computeSingleLogic(OptionsCont &oc, NBTrafficLightDefinition *def)
Computes a specific traffic light logic (using by NETEDIT)
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:41
const std::vector< NBNode * > & getNodes() const
Returns the list of controlled nodes.
A storage for options typed value containers)
Definition: OptionsCont.h:99
SumoXMLNodeType getType() const
Returns the type of this node.
Definition: NBNode.h:257
Represents a single node (junction) during network building.
Definition: NBNode.h:75
bool removeFully(const std::string id)
Removes a logic definition (and all programs) from the dictionary.
bool insert(NBTrafficLightDefinition *logic, bool forceInsert=false)
Adds a logic definition to the dictionary.
long long int SUMOTime
Definition: TraCIDefs.h:52
void clear()
Destroys all stored definitions and logics.
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:63
void replaceRemoved(NBEdge *removed, int removedLane, NBEdge *by, int byLane)
Replaces occurences of the removed edge/lane in all definitions by the given edge.
A traffic light logics which must be computed (only nodes/edges are given)
Definition: NBOwnTLDef.h:54
NBTrafficLightLogic * getLogic(const std::string &id, const std::string &programID) const
Returns the computed logic for the given name.
void setParticipantsInformation()
Builds the list of participating nodes/edges/links.
Definition: NBOwnTLDef.cpp:523
void setTLControllingInformation(const NBEdgeCont &ec, const NBNodeCont &nc)
Informs the edges about being controlled by a tls.
Id2Logics myComputed
The container for previously computed tl-logics.
std::pair< int, int > computeLogics(OptionsCont &oc)
Computes the traffic light logics using the stored definitions and stores the results.
Definitions getDefinitions() const
Returns a list of all definitions (convenience for easier iteration)