SUMO - Simulation of Urban MObility
NLJunctionControlBuilder.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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
18 // Builder of microsim-junctions and tls
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <map>
28 #include <string>
29 #include <vector>
30 #include <list>
31 #include <algorithm>
52 #include <microsim/MSGlobals.h>
53 #include <microsim/MSNet.h>
58 #include <utils/common/ToString.h>
59 #include <netbuild/NBNode.h>
60 #include "NLBuilder.h"
62 
63 
64 // ===========================================================================
65 // static members
66 // ===========================================================================
68 
69 // ===========================================================================
70 // method definitions
71 // ===========================================================================
73  myNet(net),
74  myDetectorBuilder(db),
75  myOffset(0),
76  myJunctions(0),
77  myNetIsLoaded(false) {
80 }
81 
82 
84  delete myLogicControl;
85  delete myJunctions;
86 }
87 
88 
89 void
91  const std::string& key,
92  const SumoXMLNodeType type,
93  const Position pos,
94  const PositionVector& shape,
95  const std::vector<MSLane*>& incomingLanes,
96  const std::vector<MSLane*>& internalLanes) {
97  myActiveInternalLanes = internalLanes;
98  myActiveIncomingLanes = incomingLanes;
99  myActiveID = id;
100  myActiveKey = key;
101  myType = type;
102  myPosition.set(pos);
103  myShape = shape;
104  myAdditionalParameter.clear();
105 }
106 
107 
108 void
109 NLJunctionControlBuilder::closeJunction(const std::string& basePath) {
110  if (myJunctions == 0) {
111  throw ProcessError("Information about the number of nodes was missing.");
112  }
113  MSJunction* junction = 0;
114  switch (myType) {
115  case NODETYPE_NOJUNCTION:
116  case NODETYPE_DEAD_END:
118  case NODETYPE_DISTRICT:
120  junction = buildNoLogicJunction();
121  break;
125  case NODETYPE_PRIORITY:
128  case NODETYPE_ZIPPER:
129  junction = buildLogicJunction();
130  break;
131  case NODETYPE_INTERNAL:
133  junction = buildInternalJunction();
134  }
135  break;
138  myOffset = 0;
140  myActiveProgram = "0";
142  closeTrafficLightLogic(basePath);
143  junction = buildLogicJunction();
144  break;
145  default:
146  throw InvalidArgument("False junction logic type.");
147  }
148  if (junction != 0) {
149  if (!myJunctions->add(myActiveID, junction)) {
150  throw InvalidArgument("Another junction with the id '" + myActiveID + "' exists.");
151  }
152  }
154 }
155 
156 
160  myJunctions = 0;
161  return js;
162 }
163 
164 
165 MSJunction*
169 }
170 
171 
172 MSJunction*
175  // build the junction
178  jtype);
179 }
180 
181 
182 MSJunction*
184  // build the junction
187 }
188 
189 
192  // get and check the junction logic
193  if (myLogics.find(myActiveID) == myLogics.end()) {
194  throw InvalidArgument("Missing junction logic '" + myActiveID + "'.");
195  }
196  return myLogics[myActiveID];
197 }
198 
199 
201 NLJunctionControlBuilder::getTLLogic(const std::string& id) const {
202  return getTLLogicControlToUse().get(id);
203 }
204 
205 
206 void
208  if (myActiveProgram == "off") {
209  if (myAbsDuration > 0) {
210  throw InvalidArgument("The off program for TLS '" + myActiveKey + "' has phases.");
211  }
214  throw InvalidArgument("Another logic with id '" + myActiveKey + "' and programID '" + myActiveProgram + "' exists.");
215  }
216  return;
217  }
218  SUMOTime firstEventOffset = 0;
219  int step = 0;
220  MSTrafficLightLogic* existing = 0;
221  MSSimpleTrafficLightLogic::Phases::const_iterator i = myActivePhases.begin();
222  if (myLogicType != TLTYPE_RAIL) {
223  if (myAbsDuration == 0) {
225  if (existing == 0) {
226  throw InvalidArgument("TLS program '" + myActiveProgram + "' for TLS '" + myActiveKey + "' has a duration of 0.");
227  } else {
228  // only modify the offset of an existing logic
229  myAbsDuration = existing->getDefaultCycleTime();
230  i = existing->getPhases().begin();
231  }
232  }
233  // compute the initial step and first switch time of the tls-logic
234  // a positive offset delays all phases by x (advance by absDuration - x) while a negative offset advances all phases by x seconds
235  // @note The implementation of % for negative values is implementation defined in ISO1998
236  SUMOTime offset; // the time to run the traffic light in advance
237  if (myOffset >= 0) {
239  } else {
241  }
242  while (offset >= (*i)->duration) {
243  step++;
244  offset -= (*i)->duration;
245  ++i;
246  }
247  firstEventOffset = (*i)->duration - offset + myNet.getCurrentTimeStep();
248  if (existing != 0) {
250  myNet.getCurrentTimeStep(), step, (*i)->duration - offset);
251  return;
252  }
253  }
254 
255  if (myActiveProgram == "") {
256  myActiveProgram = "default";
257  }
258  MSTrafficLightLogic* tlLogic = 0;
259  // build the tls-logic in dependance to its type
260  switch (myLogicType) {
261  case TLTYPE_SWARM_BASED:
262  firstEventOffset = DELTA_T; //this is needed because swarm needs to update the pheromone on the lanes at every step
264  break;
267  break;
268  case TLTYPE_SOTL_REQUEST:
270  break;
271  case TLTYPE_SOTL_PLATOON:
273  break;
274  case TLTYPE_SOTL_WAVE:
276  break;
277  case TLTYPE_SOTL_PHASE:
279  break;
282  break;
283  case TLTYPE_ACTUATED:
284  // @note it is unclear how to apply the given offset in the context
285  // of variable-length phases
288  myActivePhases, step, (*i)->minDuration + myNet.getCurrentTimeStep(),
289  myAdditionalParameter, basePath);
290  break;
291  case TLTYPE_DELAYBASED:
294  myActivePhases, step, (*i)->minDuration + myNet.getCurrentTimeStep(),
295  myAdditionalParameter, basePath);
296  break;
297  case TLTYPE_STATIC:
298  tlLogic =
301  myActivePhases, step, firstEventOffset,
303  break;
304  case TLTYPE_RAIL:
305  if (myType == NODETYPE_RAIL_SIGNAL) {
306  tlLogic = new MSRailSignal(getTLLogicControlToUse(),
309  } else if (myType == NODETYPE_RAIL_CROSSING) {
310  tlLogic = new MSRailCrossing(getTLLogicControlToUse(),
313  } else {
314  throw ProcessError("Invalid node type '" + toString(myType)
315  + "' for traffic light type '" + toString(myLogicType) + "'");
316  }
317  break;
318  case TLTYPE_INVALID:
319  throw ProcessError("Invalid traffic light type '" + toString(myLogicType) + "'");
320  }
321  myActivePhases.clear();
322  if (tlLogic != 0) {
323  if (getTLLogicControlToUse().add(myActiveKey, myActiveProgram, tlLogic)) {
324  if (myNetIsLoaded) {
325  tlLogic->init(myDetectorBuilder);
326  } else {
327  myLogics2PostLoadInit.push_back(tlLogic);
328  }
329  } else {
330  WRITE_ERROR("Another logic with id '" + myActiveKey + "' and programID '" + myActiveProgram + "' exists.");
331  delete tlLogic;
332  }
333  }
334 }
335 
336 
337 void
339  myActiveKey = id;
340  myActiveProgram = "";
341  myActiveLogic.clear();
342  myActiveFoes.clear();
343  myActiveConts.reset();
344  myRequestSize = NO_REQUEST_SIZE; // seems not to be used
346  myCurrentHasError = false;
347 }
348 
349 
350 void
352  const std::string& response,
353  const std::string& foes,
354  bool cont) {
355  if (myCurrentHasError) {
356  // had an error
357  return;
358  }
359  if (request >= SUMO_MAX_CONNECTIONS) {
360  // bad request
361  myCurrentHasError = true;
362  throw InvalidArgument("Junction logic '" + myActiveKey + "' is larger than allowed; recheck the network.");
363  }
365  // initialize
366  myRequestSize = (int)response.size();
367  }
368  if (static_cast<int>(response.size()) != myRequestSize) {
369  myCurrentHasError = true;
370  throw InvalidArgument("Invalid response size " + toString(response.size()) +
371  " in Junction logic '" + myActiveKey + "' (expected " + toString(myRequestSize) + ")");
372  }
373  if (static_cast<int>(foes.size()) != myRequestSize) {
374  myCurrentHasError = true;
375  throw InvalidArgument("Invalid foes size " + toString(foes.size()) +
376  " in Junction logic '" + myActiveKey + "' (expected " + toString(myRequestSize) + ")");
377  }
378  // assert that the logicitems come ordered by their request index
379  assert((int)myActiveLogic.size() == request);
380  assert((int)myActiveFoes.size() == request);
381  // add the read response for the given request index
382  myActiveLogic.push_back(std::bitset<SUMO_MAX_CONNECTIONS>(response));
383  // add the read junction-internal foes for the given request index
384  myActiveFoes.push_back(std::bitset<SUMO_MAX_CONNECTIONS>(foes));
385  // add whether the vehicle may drive a little bit further
386  myActiveConts.set(request, cont);
387  // increse number of set information
389 }
390 
391 
392 void
393 NLJunctionControlBuilder::initTrafficLightLogic(const std::string& id, const std::string& programID,
394  TrafficLightType type, SUMOTime offset) {
395  myActiveKey = id;
396  myActiveProgram = programID;
397  myActivePhases.clear();
398  myAbsDuration = 0;
400  myLogicType = type;
401  myOffset = offset;
402  myAdditionalParameter.clear();
403 }
404 
405 
406 void
407 NLJunctionControlBuilder::addPhase(SUMOTime duration, const std::string& state, SUMOTime minDuration, SUMOTime maxDuration, bool transient_notdecisional, bool commit) {
408  // build and add the phase definition to the list
409  myActivePhases.push_back(new MSPhaseDefinition(duration, minDuration, maxDuration, state, transient_notdecisional, commit));
410  // add phase duration to the absolute duration
411  myAbsDuration += duration;
412 }
413 
414 void
415 NLJunctionControlBuilder::addPhase(SUMOTime duration, const std::string& state, SUMOTime minDuration, SUMOTime maxDuration, bool transient_notdecisional, bool commit, MSPhaseDefinition::LaneIdVector& targetLanes) {
416  // build and add the phase definition to the list
417  myActivePhases.push_back(new MSPhaseDefinition(duration, minDuration, maxDuration, state, transient_notdecisional, commit, targetLanes));
418  // add phase duration to the absolute duration
419  myAbsDuration += duration;
420 }
421 
422 
423 void
424 NLJunctionControlBuilder::addPhase(SUMOTime duration, const std::string& state,
425  SUMOTime minDuration, SUMOTime maxDuration) {
426  // build and add the phase definition to the list
427  myActivePhases.push_back(new MSPhaseDefinition(duration, minDuration, maxDuration, state));
428  // add phase duration to the absolute duration
429  myAbsDuration += duration;
430 }
431 
432 
433 void
436  // We have a legacy network. junction element did not contain logicitems; read the logic later
437  return;
438  }
439  if (myCurrentHasError) {
440  // had an error before...
441  return;
442  }
444  throw InvalidArgument("The description for the junction logic '" + myActiveKey + "' is malicious.");
445  }
446  if (myLogics.count(myActiveKey) > 0) {
447  throw InvalidArgument("Junction logic '" + myActiveKey + "' was defined twice.");
448  }
452  myActiveConts);
453  myLogics[myActiveKey] = logic;
454 }
455 
456 
459  postLoadInitialization(); // must happen after edgeBuilder is finished
461  throw ProcessError("Traffic lights could not be built.");
462  }
464  myLogicControl = 0;
465  return ret;
466 }
467 
468 
469 void
470 NLJunctionControlBuilder::addParam(const std::string& key,
471  const std::string& value) {
472  myAdditionalParameter[key] = value;
473 }
474 
475 
478  if (myLogicControl != 0) {
479  return *myLogicControl;
480  }
481  return myNet.getTLSControl();
482 }
483 
484 
485 const std::string&
487  return myActiveKey;
488 }
489 
490 
491 const std::string&
493  return myActiveProgram;
494 }
495 
496 
497 void
499  for (std::vector<MSTrafficLightLogic*>::const_iterator it = myLogics2PostLoadInit.begin();
500  it != myLogics2PostLoadInit.end(); ++it) {
501  (*it)->init(myDetectorBuilder);
502  }
503  myNetIsLoaded = true;
504 }
505 
506 
507 MSJunction*
508 NLJunctionControlBuilder::retrieve(const std::string id) {
509  if (myJunctions != 0) {
510  return myJunctions->get(id);
511  } else {
512  return 0;
513  }
514 }
515 
516 /****************************************************************************/
void postLoadInitialization()
initialize junctions after all connections have been loaded
void initTrafficLightLogic(const std::string &id, const std::string &programID, TrafficLightType type, SUMOTime offset)
Begins the reading of a traffic lights logic.
Builds detectors for microsim.
virtual ~NLJunctionControlBuilder()
Destructor.
NLDetectorBuilder & myDetectorBuilder
The detector builder to use.
long long int SUMOTime
Definition: SUMOTime.h:36
An actuated traffic light logic based on time delay of approaching vehicles.
std::bitset< SUMO_MAX_CONNECTIONS > myActiveConts
The description about which lanes have an internal follower.
Storage for all programs of a single tls.
LaneVector myActiveInternalLanes
The list of the internal lanes of the currently chosen junction.
void openJunction(const std::string &id, const std::string &key, const SumoXMLNodeType type, const Position pos, const PositionVector &shape, const std::vector< MSLane *> &incomingLanes, const std::vector< MSLane *> &internalLanes)
Begins the processing of the named junction.
Class for low-level platoon policy.
A signal for rails.
Definition: MSRailSignal.h:47
MSBitSetLogic< SUMO_MAX_CONNECTIONS > MSBitsetLogic
std::string myActiveKey
The key of the currently chosen junction.
The base class for an intersection.
Definition: MSJunction.h:60
virtual void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)=0
Changes the current phase and her duration.
T get(const std::string &id) const
Retrieves an item.
MSNet & myNet
The net to use.
MSBitsetLogic::Logic myActiveLogic
The right-of-way-logic of the currently chosen bitset-logic.
void closeJunctionLogic()
Ends the building of a junction logic (row-logic)
virtual MSJunction * buildInternalJunction()
Builds an internal junction.
SUMOTime myAbsDuration
The absolute duration of a tls-control loop.
Class for low-level request policy.
Class for low-level marching policy.
#define SUMO_MAX_CONNECTIONS
the maximum number of connections across an intersection
Definition: StdDefs.h:44
SUMOTime DELTA_T
Definition: SUMOTime.cpp:35
const std::string & getActiveSubKey() const
Returns the active sub key.
void set(double x, double y)
set positions x and y
Definition: Position.h:86
Position myPosition
The position of the junction.
PositionVector myShape
The shape of the current junction.
The simulated network and simulation perfomer.
Definition: MSNet.h:85
A fixed traffic light logic.
Container for junctions; performs operations on all stored junctions.
SUMOTime myOffset
The switch offset within the tls.
A traffic lights logic which represents a tls in an off-mode.
void closeJunction(const std::string &basePath)
Closes (ends) the processing of the current junction.
MSTLLogicControl::TLSLogicVariants & getTLLogic(const std::string &id) const
Returns a previously build tls logic.
bool add(const std::string &id, T item)
Adds an item.
An actuated (adaptive) traffic light logic.
A class that stores and controls tls and switching of their programs.
SUMOTime getDefaultCycleTime() const
Returns the cycle time (in ms)
void addPhase(SUMOTime duration, const std::string &state, SUMOTime min, SUMOTime max)
Adds a phase to the currently built traffic lights logic.
A self-organizing traffic light logic based on a particular policy.
MSSimpleTrafficLightLogic::Phases myActivePhases
The current phase definitions for a simple traffic light.
TrafficLightType myLogicType
The current logic type.
std::string myActiveID
The id of the currently chosen junction.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:49
void updateParameter(const std::map< std::string, std::string > &mapArg)
Adds or updates all given parameters from the map.
MSJunctionControl * myJunctions
The junctions controls.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
virtual void init(NLDetectorBuilder &nb)
Initialises the tls with information about incoming lanes.
MSTLLogicControl & getTLSControl()
Returns the tls logics control.
Definition: MSNet.h:382
A list of positions.
virtual MSJunction * buildLogicJunction()
Builds a junction with a logic.
virtual void closeTrafficLightLogic(const std::string &basePath)
Ends the building of a traffic lights logic.
std::map< std::string, MSJunctionLogic * > myLogics
Map of loaded junction logics.
int myRequestSize
The size of the request.
std::vector< std::bitset< N > > Foes
Container holding the information which internal lanes prohibt which links Build the same way as Logi...
Definition: MSBitSetLogic.h:56
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:256
bool myNetIsLoaded
whether the network has been loaded
MSJunction * retrieve(const std::string id)
try to retrieve junction by id
A signal for rails.
static bool gUsingInternalLanes
Information whether the simulation regards internal lanes.
Definition: MSGlobals.h:69
NLJunctionControlBuilder(MSNet &net, NLDetectorBuilder &db)
Constructor.
void addParam(const std::string &key, const std::string &value)
Adds a parameter.
int myRequestItemNumber
Counter for the inserted items.
bool myCurrentHasError
Information whether the current logic had an error.
MSBitsetLogic::Foes myActiveFoes
The description about which lanes disallow other passing the junction simultaneously.
MSTLLogicControl & getTLLogicControlToUse() const
Returns the used tls control.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:248
virtual MSJunction * buildNoLogicJunction()
Builds a junction that does not use a logic.
virtual const Phases & getPhases() const =0
Returns the phases of this tls program.
bool closeNetworkReading()
Lets MSTLLogicControl know that the network has been loaded.
StringParameterMap myAdditionalParameter
Parameter map (key->value)
std::vector< std::string > LaneIdVector
SumoXMLNodeType myType
The type of the currently chosen junction.
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
void initJunctionLogic(const std::string &id)
Initialises a junction logic.
LaneVector myActiveIncomingLanes
The list of the incoming lanes of the currently chosen junction.
std::vector< std::bitset< N > > Logic
Container that holds the right of way bitsets. Each link has it&#39;s own bitset. The bits in the bitsets...
Definition: MSBitSetLogic.h:52
Class for low-level phase policy.
std::vector< MSTrafficLightLogic * > myLogics2PostLoadInit
The container for information which junctions shall be initialised using which values.
MSTLLogicControl * myLogicControl
The tls control to use (0 if net&#39;s tls control shall be used)
A junction with right-of-way - rules.
The parent class for traffic light logics.
MSJunctionLogic * getJunctionLogicSecure()
Returns the current junction logic.
MSJunctionControl * build() const
Builds the MSJunctionControl which holds all of the simulations junctions.
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
MSTLLogicControl * buildTLLogics()
Returns the built tls-logic control.
The definition of a single phase of a tls logic.
const std::string & getActiveKey() const
Returns the active key.
TrafficLightType
void addLogicItem(int request, const std::string &response, const std::string &foes, bool cont)
Adds a logic item.