Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
NLJunctionControlBuilder.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
21// Builder of microsim-junctions and tls
22/****************************************************************************/
23#include <config.h>
24
25#include <map>
26#include <string>
27#include <vector>
28#include <list>
29#include <algorithm>
33#include <microsim/MSGlobals.h>
34#include <microsim/MSNet.h>
56#include "NLBuilder.h"
59
60
61// ===========================================================================
62// static members
63// ===========================================================================
65
66// ===========================================================================
67// method definitions
68// ===========================================================================
70 myNet(net),
71 myDetectorBuilder(db),
72 myOffset(0),
73 myJunctions(new MSJunctionControl()),
74 myNetIsLoaded(false) {
76}
77
78
83
84
85void
87 const std::string& key,
88 const SumoXMLNodeType type,
89 const Position pos,
90 const PositionVector& shape,
91 const std::vector<MSLane*>& incomingLanes,
92 const std::vector<MSLane*>& internalLanes,
93 const std::string& name) {
94 myActiveInternalLanes = internalLanes;
95 myActiveIncomingLanes = incomingLanes;
96 myActiveID = id;
97 myActiveKey = key;
98 myType = type;
99 myPosition.set(pos);
100 myShape = shape;
101 myActiveName = name;
102 myAdditionalParameter.clear();
103}
104
105
106void
107NLJunctionControlBuilder::closeJunction(const std::string& basePath) {
108 if (myCurrentHasError) {
109 // had an error before...
110 return;
111 }
113 throw InvalidArgument("The description for the junction logic '" + myActiveKey + "' is malicious.");
114 }
115 if (myJunctions == nullptr) {
116 throw ProcessError(TL("Information about the number of nodes was missing."));
117 }
118 MSJunction* junction = nullptr;
119 switch (myType) {
125 if (!myActiveLogic.empty()) {
126 WRITE_WARNINGF(TL("Ignoring junction logic for junction '%'."), myActiveID)
127 }
128 junction = buildNoLogicJunction();
129 break;
139 break;
142 if (!myActiveLogic.empty()) {
143 WRITE_WARNINGF(TL("Ignoring junction logic for junction '%'."), myActiveID)
144 }
145 junction = buildInternalJunction();
146 }
147 break;
150 myOffset = 0;
152 myActiveProgram = "0";
154 closeTrafficLightLogic(basePath);
156 break;
157 default:
158 throw InvalidArgument("False junction logic type.");
159 }
160 if (junction != nullptr) {
161 if (!myJunctions->add(myActiveID, junction)) {
162 delete junction;
163 throw InvalidArgument("Another junction with the id '" + myActiveID + "' exists.");
164 }
166 }
167}
168
169
173 myJunctions = nullptr;
174 return js;
175}
176
177
183
184
190
191
198
199
201NLJunctionControlBuilder::getTLLogic(const std::string& id) const {
202 return getTLLogicControlToUse().get(id);
203}
204
205
206void
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 MSSimpleTrafficLightLogic::Phases::const_iterator i = myActivePhases.begin();
222 if (existing != nullptr && (existing->getLogicType() == TrafficLightType::RAIL_SIGNAL || existing->getLogicType() == TrafficLightType::RAIL_CROSSING)) {
224 return;
225 } else {
227 if (myAbsDuration == 0) {
228 if (existing == nullptr) {
229 throw InvalidArgument("TLS program '" + myActiveProgram + "' for TLS '" + myActiveKey + "' has a duration of 0.");
230 } else {
231 // only modify the offset of an existing logic
233 i = existing->getPhases().begin();
234 }
235 } else if (existing != nullptr) {
236 throw InvalidArgument("Another logic with id '" + myActiveKey + "' and programID '" + myActiveProgram + "' exists.");
237 }
238 // compute the initial step and first switch time of the tls-logic
239 // a positive offset delays all phases by x (advance by absDuration - x) while a negative offset advances all phases by x seconds
240 // @note The implementation of % for negative values is implementation defined in ISO1998
241 SUMOTime offset; // the time to run the traffic light in advance
242 if (myOffset >= 0) {
244 } else {
246 }
247 while (offset >= (*i)->duration) {
248 step++;
249 offset -= (*i)->duration;
250 ++i;
251 }
252 firstEventOffset = (*i)->duration - offset + myNet.getCurrentTimeStep();
253 if (existing != nullptr) {
255 myNet.getCurrentTimeStep(), step, (*i)->duration - offset);
256 // parameters that are used when initializing a logic will not take
257 // effect but parameters that are checked at runtime can be used
258 // here (i.e. device.glosa.range)
261 // value must be available when calling setMesoTypes and before setting the rest in postLoadInitialization
263 }
264 return;
265 }
266 }
267 }
268
269 if (myActiveProgram == "") {
270 myActiveProgram = "default";
271 }
272 MSTrafficLightLogic* tlLogic = nullptr;
273 // build the tls-logic in dependence to its type
274 switch (myLogicType) {
276 firstEventOffset = DELTA_T; //this is needed because swarm needs to update the pheromone on the lanes at every step
278 break;
281 break;
284 break;
287 break;
290 break;
293 break;
296 break;
298 // @note it is unclear how to apply the given offset in the context
299 // of variable-length phases
302 myActivePhases, step, (*i)->minDuration + myNet.getCurrentTimeStep(),
304 break;
306 tlLogic = new NEMALogic(getTLLogicControlToUse(),
308 myActivePhases, step, (*i)->minDuration + myNet.getCurrentTimeStep(),
309 myAdditionalParameter, basePath);
310 break;
314 myActivePhases, step, (*i)->minDuration + myNet.getCurrentTimeStep(),
315 myAdditionalParameter, basePath);
316 break;
321 myActivePhases, step, firstEventOffset,
323 break;
325 tlLogic = new MSRailSignal(getTLLogicControlToUse(),
328 break;
333 break;
336 break;
338 throw ProcessError(TLF("Invalid traffic light type '%'", toString(myLogicType)));
339 }
340 myActivePhases.clear();
341 if (tlLogic != nullptr) {
343 if (myNetIsLoaded) {
344 myAdditionalLogics.push_back(tlLogic);
346 // special case: intialize earlier because signals are already used when
347 // loading train routes in additional files
348 myRailSignals.push_back(tlLogic);
349 } else {
350 myNetworkLogics.push_back(tlLogic);
351 }
352 } else {
353 WRITE_ERRORF(TL("Another logic with id '%' and programID '%' exists."), myActiveKey, myActiveProgram);
354 }
355 }
356}
357
358
359void
361 myActiveKey = id;
362 myActiveProgram = "";
363 myActiveLogic.clear();
364 myActiveFoes.clear();
365 myActiveConts.reset();
366 myRequestSize = NO_REQUEST_SIZE; // seems not to be used
368 myCurrentHasError = false;
369}
370
371
372void
374 const std::string& response,
375 const std::string& foes,
376 bool cont) {
377 if (myCurrentHasError) {
378 // had an error
379 return;
380 }
381 if (request >= SUMO_MAX_CONNECTIONS) {
382 // bad request
383 myCurrentHasError = true;
384 throw InvalidArgument("Junction logic '" + myActiveKey + "' is larger than allowed; recheck the network.");
385 }
387 // initialize
388 myRequestSize = (int)response.size();
389 }
390 if (static_cast<int>(response.size()) != myRequestSize) {
391 myCurrentHasError = true;
392 throw InvalidArgument("Invalid response size " + toString(response.size()) +
393 " in Junction logic '" + myActiveKey + "' (expected " + toString(myRequestSize) + ")");
394 }
395 if (static_cast<int>(foes.size()) != myRequestSize) {
396 myCurrentHasError = true;
397 throw InvalidArgument("Invalid foes size " + toString(foes.size()) +
398 " in Junction logic '" + myActiveKey + "' (expected " + toString(myRequestSize) + ")");
399 }
400 // assert that the logicitems come ordered by their request index
401 assert((int)myActiveLogic.size() == request);
402 assert((int)myActiveFoes.size() == request);
403 // add the read response for the given request index
404 myActiveLogic.push_back(std::bitset<SUMO_MAX_CONNECTIONS>(response));
405 // add the read junction-internal foes for the given request index
406 myActiveFoes.push_back(std::bitset<SUMO_MAX_CONNECTIONS>(foes));
407 // add whether the vehicle may drive a little bit further
408 myActiveConts.set(request, cont);
409 // increse number of set information
411}
412
413
414void
415NLJunctionControlBuilder::initTrafficLightLogic(const std::string& id, const std::string& programID,
416 TrafficLightType type, SUMOTime offset) {
417 myActiveKey = id;
418 myActiveProgram = programID;
419 myActivePhases.clear();
420 myActiveConditions.clear();
421 myActiveAssignments.clear();
422 myActiveFunctions.clear();
423 myAbsDuration = 0;
425 myLogicType = type;
426 myOffset = offset;
427 myAdditionalParameter.clear();
428}
429
430
431void
433 // build and add the phase definition to the list
434 myActivePhases.push_back(phase);
435 // add phase duration to the absolute duration
436 myAbsDuration += phase->duration;
437}
438
439
440bool
441NLJunctionControlBuilder::addCondition(const std::string& id, const std::string& value) {
442 if (myActiveConditions.count(id) == 0) {
443 myActiveConditions[id] = value;
444 return true;
445 } else {
446 return false;
447 }
448}
449
450
451void
452NLJunctionControlBuilder::addAssignment(const std::string& id, const std::string& check, const std::string& value) {
453 if (myActiveFunction.id == "") {
454 myActiveAssignments.push_back(std::make_tuple(id, check, value));
455 } else {
456 myActiveFunction.assignments.push_back(std::make_tuple(id, check, value));
457 }
458}
459
460
461void
462NLJunctionControlBuilder::addFunction(const std::string& id, int nArgs) {
463 myActiveFunction.id = id;
464 myActiveFunction.nArgs = nArgs;
465}
466
467
468void
474
475
479 throw ProcessError(TL("Traffic lights could not be built."));
480 }
481 for (MSTrafficLightLogic* const logic : myRailSignals) {
482 logic->init(myDetectorBuilder);
483 }
485 myNetIsLoaded = true;
486 myLogicControl = nullptr;
487 return ret;
488}
489
490
491void
493 const std::string& value) {
494 myAdditionalParameter[key] = value;
495}
496
497
500 if (myLogicControl != nullptr) {
501 return *myLogicControl;
502 }
503 return myNet.getTLSControl();
504}
505
506
507const std::string&
511
512
513const std::string&
517
518
519void
521 for (MSTrafficLightLogic* const logic : myNetworkLogics) {
522 logic->init(myDetectorBuilder);
523 }
524 for (MSTrafficLightLogic* const logic : myAdditionalLogics) {
525 logic->init(myDetectorBuilder);
526 }
527 // delay parameter loading until initialization
528 for (auto item : myLogicParams) {
529 item.first->updateParameters(item.second);
530 }
531}
532
533
536 if (myJunctions != nullptr) {
537 return myJunctions->get(id);
538 } else {
539 return nullptr;
540 }
541}
542
543
544/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
MSBitSetLogic< SUMO_MAX_CONNECTIONS > MSBitsetLogic
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:287
#define WRITE_ERRORF(...)
Definition MsgHandler.h:296
#define TL(string)
Definition MsgHandler.h:304
#define TLF(string,...)
Definition MsgHandler.h:306
SUMOTime DELTA_T
Definition SUMOTime.cpp:38
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
#define SUMO_MAX_CONNECTIONS
the maximum number of connections across an intersection
Definition StdDefs.h:45
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:49
static const std::string OVERRIDE_TLS_PENALTIES
special param value
Definition MESegment.h:55
An actuated (adaptive) traffic light logic.
An actuated traffic light logic based on time delay of approaching vehicles.
static bool gUsingInternalLanes
Information whether the simulation regards internal lanes.
Definition MSGlobals.h:81
Container for junctions; performs operations on all stored junctions.
The base class for an intersection.
Definition MSJunction.h:58
The simulated network and simulation perfomer.
Definition MSNet.h:89
MSTLLogicControl & getTLSControl()
Returns the tls logics control.
Definition MSNet.h:465
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition MSNet.h:334
A traffic lights logic which represents a tls in an off-mode.
The definition of a single phase of a tls logic.
SUMOTime duration
The duration of the phase.
A signal for rails.
A signal for rails.
A junction with right-of-way - rules.
Class for low-level marching policy.
Class for low-level phase policy.
Class for low-level platoon policy.
A self-organizing traffic light logic based on a particular policy.
Class for low-level request policy.
A fixed traffic light logic.
Storage for all programs of a single tls.
A class that stores and controls tls and switching of their programs.
bool closeNetworkReading()
Lets MSTLLogicControl know that the network has been loaded.
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
The parent class for traffic light logics.
virtual void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)=0
Changes the current phase and her duration.
TrafficLightType getLogicType() const
Returns the type of the logic.
virtual const Phases & getPhases() const =0
Returns the phases of this tls program.
SUMOTime getDefaultCycleTime() const
Returns the cycle time (in ms)
A NEMA (adaptive) traffic light logic based on E2Detector.
Builds detectors for microsim.
PositionVector myShape
The shape of the current junction.
NLJunctionControlBuilder(MSNet &net, NLDetectorBuilder &db)
Constructor.
int myRequestItemNumber
Counter for the inserted items.
SumoXMLNodeType myType
The type of the currently chosen junction.
std::vector< MSTrafficLightLogic * > myNetworkLogics
The container for information which junctions shall be initialised using which values.
MSTLLogicControl::TLSLogicVariants & getTLLogic(const std::string &id) const
Returns a previously build tls logic.
std::string myActiveName
the name of the current junction
virtual ~NLJunctionControlBuilder()
Destructor.
void closeFunction()
closes a switching condition function to the traffic lights logic currently build
MSJunction * retrieve(const std::string id)
try to retrieve junction by id
MSActuatedTrafficLightLogic::AssignmentMap myActiveAssignments
The current assignments for an actuated traffic light.
MSBitsetLogic::Foes myActiveFoes
The description about which lanes disallow other passing the junction simultaneously.
std::string myActiveID
The id of the currently chosen junction.
MSActuatedTrafficLightLogic::ConditionMap myActiveConditions
The current switching conditions for an actuated traffic light.
SUMOTime myOffset
The switch offset within the tls.
MSTLLogicControl * myLogicControl
The tls control to use (0 if net's tls control shall be used)
std::string myActiveKey
The key of the currently chosen junction.
TrafficLightType myLogicType
The current logic type.
bool myCurrentHasError
Information whether the current logic had an error.
MSTLLogicControl * buildTLLogics()
Returns the built tls-logic control.
MSJunctionControl * build() const
Builds the MSJunctionControl which holds all of the simulations junctions.
int myRequestSize
The size of the request.
void addPhase(MSPhaseDefinition *phase)
Adds a phase to the currently built traffic lights logic.
MSTLLogicControl & getTLLogicControlToUse() const
Returns the used tls control.
void addFunction(const std::string &id, int nArgs)
adds a switching condition function to the traffic lights logic currently build
LaneVector myActiveIncomingLanes
The list of the incoming lanes of the currently chosen junction.
const std::string & getActiveSubKey() const
Returns the active sub key.
bool addCondition(const std::string &id, const std::string &value)
Adds a condition to the currently built traffic lights logic.
StringParameterMap myAdditionalParameter
Parameter map (key->value)
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, const std::string &name)
Begins the processing of the named junction.
void addParam(const std::string &key, const std::string &value)
Adds a parameter.
LaneVector myActiveInternalLanes
The list of the internal lanes of the currently chosen junction.
void initJunctionLogic(const std::string &id)
Initialises a junction logic.
void initTrafficLightLogic(const std::string &id, const std::string &programID, TrafficLightType type, SUMOTime offset)
Begins the reading of a traffic lights logic.
MSActuatedTrafficLightLogic::FunctionMap myActiveFunctions
The current functions for an actuated traffic light.
NLDetectorBuilder & myDetectorBuilder
The detector builder to use.
virtual MSJunction * buildNoLogicJunction()
Builds a junction that does not use a logic.
virtual MSJunction * buildInternalJunction()
Builds an internal junction.
MSJunctionControl * myJunctions
The junctions controls.
std::vector< MSTrafficLightLogic * > myAdditionalLogics
const std::string & getActiveKey() const
Returns the active key.
Position myPosition
The position of the junction.
void addAssignment(const std::string &id, const std::string &check, const std::string &value)
Adds an assignment to the currently built traffic lights logic.
void postLoadInitialization()
initialize junctions after all connections have been loaded
std::map< MSTrafficLightLogic *, StringParameterMap > myLogicParams
std::vector< MSTrafficLightLogic * > myRailSignals
void addLogicItem(int request, const std::string &response, const std::string &foes, bool cont)
Adds a logic item.
MSActuatedTrafficLightLogic::Function myActiveFunction
The current function for an actuated traffic light.
MSBitsetLogic::Logic myActiveLogic
The right-of-way-logic of the currently chosen bitset-logic.
virtual MSJunction * buildLogicJunction(MSJunctionLogic *const logic)
Builds a junction with a logic.
SUMOTime myAbsDuration
The absolute duration of a tls-control loop.
void closeJunction(const std::string &basePath)
Closes (ends) the processing of the current junction.
MSSimpleTrafficLightLogic::Phases myActivePhases
The current phase definitions for a simple traffic light.
bool myNetIsLoaded
whether the network has been loaded
virtual void closeTrafficLightLogic(const std::string &basePath)
Ends the building of a traffic lights logic.
std::bitset< SUMO_MAX_CONNECTIONS > myActiveConts
The description about which lanes have an internal follower.
T get(const std::string &id) const
Retrieves an item.
bool add(const std::string &id, T item)
Adds an item.
virtual void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
void updateParameters(const Parameterised::Map &mapArg)
Adds or updates all given parameters from the map.
A point in 2D or 3D with translation and scaling methods.
Definition Position.h:37
void set(double x, double y)
set positions x and y
Definition Position.h:82
A list of positions.