Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
NBTrafficLightDefinition.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2002-2024 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/****************************************************************************/
20// The base class for traffic light logic definitions
21/****************************************************************************/
22#pragma once
23#include <config.h>
24
25#include <vector>
26#include <string>
27#include <bitset>
28#include <utility>
29#include <set>
31#include <utils/common/Named.h>
36#include "NBCont.h"
37#include "NBConnection.h"
38#include "NBConnectionDefs.h"
40
41
42// ===========================================================================
43// class declarations
44// ===========================================================================
45class NBNode;
46class OptionsCont;
48
49
50// ===========================================================================
51// class definitions
52// ===========================================================================
66public:
67
68 static const std::string DefaultProgramID;
70 static const int MIN_YELLOW_SECONDS;
71
88
89
97 NBTrafficLightDefinition(const std::string& id,
98 const std::vector<NBNode*>& junctions,
99 const std::string& programID,
100 SUMOTime offset,
101 TrafficLightType type);
102
103
111 NBTrafficLightDefinition(const std::string& id,
112 NBNode* junction,
113 const std::string& programID,
114 SUMOTime offset,
115 TrafficLightType type);
116
117
124 NBTrafficLightDefinition(const std::string& id, const std::string& programID,
125 SUMOTime offset,
126 TrafficLightType type);
127
128
131
141
144
148 virtual void addNode(NBNode* node);
149
150
154 virtual void removeNode(NBNode* node);
155
160 virtual void removeConnection(const NBConnection& conn, bool reconstruct = true) {
161 UNUSED_PARAMETER(conn);
162 UNUSED_PARAMETER(reconstruct);
163 }
164
168 const std::vector<NBNode*>& getNodes() const {
169 return myControlledNodes;
170 }
172
173
184 bool mustBrake(const NBEdge* const from, const NBEdge* const to) const;
185
186
194 bool mustBrake(const NBConnection& possProhibited,
195 const NBConnection& possProhibitor,
196 bool regardNonSignalisedLowerPriority) const;
197
207 bool mustBrake(const NBEdge* const possProhibitedFrom, const NBEdge* const possProhibitedTo,
208 const NBEdge* const possProhibitorFrom, const NBEdge* const possProhibitorTo,
209 bool regardNonSignalisedLowerPriority) const;
210
211
222 bool forbids(const NBEdge* const possProhibitorFrom, const NBEdge* const possProhibitorTo,
223 const NBEdge* const possProhibitedFrom, const NBEdge* const possProhibitedTo,
224 bool regardNonSignalisedLowerPriority,
225 bool sameNodeOnly = false) const;
226
227
235 bool foes(const NBEdge* const from1, const NBEdge* const to1,
236 const NBEdge* const from2, const NBEdge* const to2) const;
237
238
241 virtual void setTLControllingInformation() const = 0;
242
243
246 virtual void setParticipantsInformation();
247
248
252 void addControlledInnerEdges(const std::vector<std::string>& edges);
253
256 std::vector<std::string> getControlledInnerEdges() const;
257
263 virtual void remapRemoved(NBEdge* removed,
264 const EdgeVector& incoming, const EdgeVector& outgoing) = 0;
265
266
274 virtual void replaceRemoved(NBEdge* removed, int removedLane,
275 NBEdge* by, int byLane, bool incoming) = 0;
276
278 virtual void shiftTLConnectionLaneIndex(NBEdge* edge, int offset, int threshold = -1) {
279 UNUSED_PARAMETER(edge);
280 UNUSED_PARAMETER(offset);
281 UNUSED_PARAMETER(threshold);
282 }
283
287 const EdgeVector& getIncomingEdges() const;
288
289
292 return myControlledLinks;
293 }
294
295
300
301
305 const std::string& getProgramID() const {
306 return mySubID;
307 };
308
309
313 virtual void setProgramID(const std::string& programID) {
314 mySubID = programID;
315 }
316
319 }
320
325 return myOffset;
326 }
327
328
331 return myType;
332 }
333
335 virtual void setType(TrafficLightType type) {
336 myType = type;
337 }
338
339 /* @brief computes whether the given stream may have green minor while the
340 * other stream has green major in the same phase
341 */
342 bool needsCont(const NBEdge* fromE, const NBEdge* toE, const NBEdge* otherFromE, const NBEdge* otherToE) const;
343
345 virtual bool rightOnRedConflict(int index, int foeIndex) const;
346
347 /* initialize myNeedsContRelation and set myNeedsContRelationReady to true
348 * This information is a byproduct of NBOwnTLDef::myCompute. All other
349 * subclasses instantiate a private instance of NBOwnTLDef to answer this query */
350 virtual void initNeedsContRelation() const;
351
352 virtual void initRightOnRedConflicts() const;
353
355 virtual int getMaxIndex() = 0;
356
358 virtual int getMaxValidIndex() {
359 return getMaxIndex();
360 }
361
367 int computeBrakingTime(double minDecel) const;
368
370 virtual bool usingSignalGroups() const {
371 return false;
372 };
373
375 std::string getDescription() const;
376
378 virtual void finalChecks() const {}
379
381 static const std::string OSM_DIRECTION;
382 static const std::string OSM_SIGNAL_DIRECTION;
383
384protected:
386 static const std::string DummyID;
387
392 virtual NBTrafficLightLogic* myCompute(int brakingTime) = 0;
393
394
398 virtual void collectLinks() = 0;
399
400
403 virtual void collectEdges();
404
405
406 // @return whether this traffic light is invalid and should be computed
407 virtual bool amInvalid() const;
408
411
412protected:
414 std::vector<NBNode*> myControlledNodes;
415
418
421
424
426 std::set<std::string> myControlledInnerEdges;
427
429 std::string mySubID;
430
433
436
438 struct StreamPair {
439 StreamPair(const NBEdge* _from1, const NBEdge* _to1, const NBEdge* _from2, const NBEdge* _to2):
440 from1(_from1),
441 to1(_to1),
442 from2(_from2),
443 to2(_to2) {}
444
445 bool operator==(const StreamPair& o) const {
446 return (from1 == o.from1 && to1 == o.to1
447 && from2 == o.from2 && to2 == o.to2);
448 }
449
450 bool operator<(const StreamPair& o) const {
451 if (from1 != o.from1) {
452 return from1 < o.from1;
453 }
454 if (to1 != o.to1) {
455 return to1 < o.to1;
456 }
457 if (from2 != o.from2) {
458 return from2 < o.from2;
459 }
460 return to2 < o.to2;
461 }
462
463 const NBEdge* from1;
464 const NBEdge* to1;
465 const NBEdge* from2;
466 const NBEdge* to2;
467 };
468 typedef std::set<StreamPair> NeedsContRelation;
471
472 typedef std::set<std::pair<int, int> > RightOnRedConflicts;
475
476private:
477 static std::set<NBEdge*> collectReachable(EdgeVector outer, const EdgeVector& within, bool checkControlled);
478
479 static bool railSignalUncontrolled(const NBEdge* in, const NBEdge* out);
480
481};
long long int SUMOTime
Definition GUI.h:36
std::vector< NBConnection > NBConnectionVector
Definition of a connection vector.
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition NBCont.h:42
TrafficLightLayout
#define UNUSED_PARAMETER(x)
Definition StdDefs.h:30
The representation of a single edge during network building.
Definition NBEdge.h:92
Represents a single node (junction) during network building.
Definition NBNode.h:66
The base class for traffic light logic definitions.
const std::vector< NBNode * > & getNodes() const
Returns the list of controlled nodes.
virtual bool rightOnRedConflict(int index, int foeIndex) const
whether the given index must yield to the foeIndex while turning right on a red light
virtual void remapRemoved(NBEdge *removed, const EdgeVector &incoming, const EdgeVector &outgoing)=0
Replaces occurrences of the removed edge in incoming/outgoing edges of all definitions.
virtual ~NBTrafficLightDefinition()
Destructor.
const std::string & getProgramID() const
Returns the ProgramID.
virtual void finalChecks() const
perform optional final checks
std::set< std::pair< int, int > > RightOnRedConflicts
void addControlledInnerEdges(const std::vector< std::string > &edges)
Adds the given ids into the list of inner edges controlled by the tls.
const EdgeVector & getIncomingEdges() const
Returns the list of incoming edges (must be build first)
bool needsCont(const NBEdge *fromE, const NBEdge *toE, const NBEdge *otherFromE, const NBEdge *otherToE) const
TLColor
An enumeration of possible tl-signal states.
@ TLCOLOR_BLINK
Signal is blinking yellow.
@ TLCOLOR_YELLOW
Signal shows yellow.
@ TLCOLOR_REDYELLOW
Signal shows red/yellow (unused)
std::vector< NBNode * > myControlledNodes
The container with participating nodes.
virtual void initRightOnRedConflicts() const
TrafficLightType getType() const
get the algorithm type (static etc..)
virtual void setProgramID(const std::string &programID)
Sets the programID.
virtual void removeNode(NBNode *node)
Removes the given node from the list of controlled nodes.
EdgeVector myIncomingEdges
The list of incoming edges.
virtual bool usingSignalGroups() const
whether this definition uses signal group (multiple connections with the same link index)
virtual void addNode(NBNode *node)
Adds a node to the traffic light logic.
NBConnectionVector & getControlledLinks()
returns the controlled links (non const version)
NBTrafficLightLogic * compute(const OptionsCont &oc)
Computes the traffic light logic.
virtual void shiftTLConnectionLaneIndex(NBEdge *edge, int offset, int threshold=-1)
patches (loaded) signal plans by modifying lane indices
std::vector< std::string > getControlledInnerEdges() const
Retrieve the ids of edges explicitly controlled by the tls.
virtual NBTrafficLightLogic * myCompute(int brakingTime)=0
Computes the traffic light logic finally in dependence to the type.
SUMOTime getOffset()
Returns the offset.
static std::set< NBEdge * > collectReachable(EdgeVector outer, const EdgeVector &within, bool checkControlled)
static const std::string OSM_DIRECTION
processing parameter for rail signal edges and nodes
RightOnRedConflicts myRightOnRedConflicts
TrafficLightType myType
The algorithm type for the traffic light.
EdgeVector myEdgesWithin
The list of edges within the area controlled by the tls.
static const std::string DummyID
id for temporary definitions
const NBConnectionVector & getControlledLinks() const
returns the controlled links (depends on previous call to collectLinks)
virtual void collectLinks()=0
Collects the links participating in this traffic light.
int computeBrakingTime(double minDecel) const
Computes the time vehicles may need to brake.
bool forbids(const NBEdge *const possProhibitorFrom, const NBEdge *const possProhibitorTo, const NBEdge *const possProhibitedFrom, const NBEdge *const possProhibitedTo, bool regardNonSignalisedLowerPriority, bool sameNodeOnly=false) const
Returns the information whether "prohibited" flow must let "prohibitor" flow pass.
std::set< StreamPair > NeedsContRelation
virtual void setTLControllingInformation() const =0
Informs edges about being controlled by a tls.
NBConnectionVector myControlledLinks
The list of controlled links.
static const std::string DefaultProgramID
bool mustBrake(const NBEdge *const from, const NBEdge *const to) const
Returns the information whether the described flow must let any other flow pass.
virtual void setType(TrafficLightType type)
set the algorithm type (static etc..)
virtual void initNeedsContRelation() const
virtual void setParticipantsInformation()
Builds the list of participating nodes/edges/links.
virtual int getMaxIndex()=0
Returns the maximum index controlled by this traffic light and assigned to a connection.
void collectAllLinks(NBConnectionVector &into)
helper method for use in NBOwnTLDef and NBLoadedSUMOTLDef
SUMOTime myOffset
The offset in the program.
static const SUMOTime UNSPECIFIED_DURATION
bool foes(const NBEdge *const from1, const NBEdge *const to1, const NBEdge *const from2, const NBEdge *const to2) const
Returns the information whether the given flows cross.
virtual int getMaxValidIndex()
Returns the maximum index controlled by this traffic light.
static const std::string OSM_SIGNAL_DIRECTION
virtual void replaceRemoved(NBEdge *removed, int removedLane, NBEdge *by, int byLane, bool incoming)=0
Replaces a removed edge/lane.
std::string getDescription() const
get ID and programID together (for convenient debugging)
virtual TrafficLightLayout getLayout() const
virtual void removeConnection(const NBConnection &conn, bool reconstruct=true)
removes the given connection from the traffic light if recontruct=true, reconstructs the logic and in...
static bool railSignalUncontrolled(const NBEdge *in, const NBEdge *out)
std::string mySubID
The tls program's subid.
virtual void collectEdges()
Build the list of participating edges.
std::set< std::string > myControlledInnerEdges
Set of inner edges that shall be controlled, though.
A SUMO-compliant built logic for a traffic light.
Base class for objects which have an id.
Definition Named.h:54
A storage for options typed value containers)
Definition OptionsCont.h:89
An upper class for objects with additional parameters.
data structure for caching needsCont information
bool operator==(const StreamPair &o) const
bool operator<(const StreamPair &o) const
StreamPair(const NBEdge *_from1, const NBEdge *_to1, const NBEdge *_from2, const NBEdge *_to2)