Eclipse 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-2019 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(nullptr),
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 == nullptr) {
111  throw ProcessError("Information about the number of nodes was missing.");
112  }
113  MSJunction* junction = nullptr;
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 != nullptr) {
149  if (!myJunctions->add(myActiveID, junction)) {
150  throw InvalidArgument("Another junction with the id '" + myActiveID + "' exists.");
151  }
152  }
154 }
155 
156 
160  myJunctions = nullptr;
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 = nullptr;
221  MSSimpleTrafficLightLogic::Phases::const_iterator i = myActivePhases.begin();
223  if (myAbsDuration == 0) {
225  if (existing == nullptr) {
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 != nullptr) {
250  myNet.getCurrentTimeStep(), step, (*i)->duration - offset);
251  return;
252  }
253  }
254 
255  if (myActiveProgram == "") {
256  myActiveProgram = "default";
257  }
258  MSTrafficLightLogic* tlLogic = nullptr;
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:
300  myActivePhases, step, firstEventOffset,
302  break;
303  case TLTYPE_RAIL_SIGNAL:
304  tlLogic = new MSRailSignal(getTLLogicControlToUse(),
307  break;
309  tlLogic = new MSRailCrossing(getTLLogicControlToUse(),
312  break;
313  case TLTYPE_OFF:
315  break;
316  case TLTYPE_INVALID:
317  throw ProcessError("Invalid traffic light type '" + toString(myLogicType) + "'");
318  }
319  myActivePhases.clear();
320  if (tlLogic != nullptr) {
321  if (getTLLogicControlToUse().add(myActiveKey, myActiveProgram, tlLogic)) {
322  if (myNetIsLoaded) {
323  tlLogic->init(myDetectorBuilder);
324  } else {
325  myLogics2PostLoadInit.push_back(tlLogic);
326  }
327  } else {
328  WRITE_ERROR("Another logic with id '" + myActiveKey + "' and programID '" + myActiveProgram + "' exists.");
329  delete tlLogic;
330  }
331  }
332 }
333 
334 
335 void
337  myActiveKey = id;
338  myActiveProgram = "";
339  myActiveLogic.clear();
340  myActiveFoes.clear();
341  myActiveConts.reset();
342  myRequestSize = NO_REQUEST_SIZE; // seems not to be used
344  myCurrentHasError = false;
345 }
346 
347 
348 void
350  const std::string& response,
351  const std::string& foes,
352  bool cont) {
353  if (myCurrentHasError) {
354  // had an error
355  return;
356  }
357  if (request >= SUMO_MAX_CONNECTIONS) {
358  // bad request
359  myCurrentHasError = true;
360  throw InvalidArgument("Junction logic '" + myActiveKey + "' is larger than allowed; recheck the network.");
361  }
363  // initialize
364  myRequestSize = (int)response.size();
365  }
366  if (static_cast<int>(response.size()) != myRequestSize) {
367  myCurrentHasError = true;
368  throw InvalidArgument("Invalid response size " + toString(response.size()) +
369  " in Junction logic '" + myActiveKey + "' (expected " + toString(myRequestSize) + ")");
370  }
371  if (static_cast<int>(foes.size()) != myRequestSize) {
372  myCurrentHasError = true;
373  throw InvalidArgument("Invalid foes size " + toString(foes.size()) +
374  " in Junction logic '" + myActiveKey + "' (expected " + toString(myRequestSize) + ")");
375  }
376  // assert that the logicitems come ordered by their request index
377  assert((int)myActiveLogic.size() == request);
378  assert((int)myActiveFoes.size() == request);
379  // add the read response for the given request index
380  myActiveLogic.push_back(std::bitset<SUMO_MAX_CONNECTIONS>(response));
381  // add the read junction-internal foes for the given request index
382  myActiveFoes.push_back(std::bitset<SUMO_MAX_CONNECTIONS>(foes));
383  // add whether the vehicle may drive a little bit further
384  myActiveConts.set(request, cont);
385  // increse number of set information
387 }
388 
389 
390 void
391 NLJunctionControlBuilder::initTrafficLightLogic(const std::string& id, const std::string& programID,
392  TrafficLightType type, SUMOTime offset) {
393  myActiveKey = id;
394  myActiveProgram = programID;
395  myActivePhases.clear();
396  myAbsDuration = 0;
398  myLogicType = type;
399  myOffset = offset;
400  myAdditionalParameter.clear();
401 }
402 
403 
404 void
405 NLJunctionControlBuilder::addPhase(SUMOTime duration, const std::string& state, const std::vector<int>& nextPhases, SUMOTime minDuration, SUMOTime maxDuration, const std::string& name, bool transient_notdecisional, bool commit, MSPhaseDefinition::LaneIdVector* targetLanes) {
406  // build and add the phase definition to the list
407  myActivePhases.push_back(new MSPhaseDefinition(duration, state, minDuration, maxDuration, nextPhases, name, transient_notdecisional, commit, targetLanes));
408  // add phase duration to the absolute duration
409  myAbsDuration += duration;
410 }
411 
412 
413 void
414 NLJunctionControlBuilder::addPhase(SUMOTime duration, const std::string& state, const std::vector<int>& nextPhases,
415  SUMOTime minDuration, SUMOTime maxDuration, const std::string& name) {
416  // build and add the phase definition to the list
417  myActivePhases.push_back(new MSPhaseDefinition(duration, state, minDuration, maxDuration, nextPhases, name));
418  // add phase duration to the absolute duration
419  myAbsDuration += duration;
420 }
421 
422 
423 void
426  // We have a legacy network. junction element did not contain logicitems; read the logic later
427  return;
428  }
429  if (myCurrentHasError) {
430  // had an error before...
431  return;
432  }
434  throw InvalidArgument("The description for the junction logic '" + myActiveKey + "' is malicious.");
435  }
436  if (myLogics.count(myActiveKey) > 0) {
437  throw InvalidArgument("Junction logic '" + myActiveKey + "' was defined twice.");
438  }
442  myActiveConts);
443  myLogics[myActiveKey] = logic;
444 }
445 
446 
449  postLoadInitialization(); // must happen after edgeBuilder is finished
451  throw ProcessError("Traffic lights could not be built.");
452  }
454  myLogicControl = nullptr;
455  return ret;
456 }
457 
458 
459 void
460 NLJunctionControlBuilder::addParam(const std::string& key,
461  const std::string& value) {
462  myAdditionalParameter[key] = value;
463 }
464 
465 
468  if (myLogicControl != nullptr) {
469  return *myLogicControl;
470  }
471  return myNet.getTLSControl();
472 }
473 
474 
475 const std::string&
477  return myActiveKey;
478 }
479 
480 
481 const std::string&
483  return myActiveProgram;
484 }
485 
486 
487 void
489  for (MSTrafficLightLogic* const logic : myLogics2PostLoadInit) {
490  logic->init(myDetectorBuilder);
491  }
492  myNetIsLoaded = true;
493 }
494 
495 
496 MSJunction*
497 NLJunctionControlBuilder::retrieve(const std::string id) {
498  if (myJunctions != nullptr) {
499  return myJunctions->get(id);
500  } else {
501  return nullptr;
502  }
503 }
504 
505 /****************************************************************************/
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:35
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:61
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:43
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:87
Position myPosition
The position of the junction.
PositionVector myShape
The shape of the current junction.
The simulated network and simulation perfomer.
Definition: MSNet.h:92
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)
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:48
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:410
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:284
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:245
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.
void addPhase(SUMOTime duration, const std::string &state, const std::vector< int > &nextPhases, SUMOTime min, SUMOTime max, const std::string &name)
Adds a phase to the currently built traffic lights logic.
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.