Eclipse SUMO - Simulation of Urban MObility
NBTrafficLightDefinition.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3// Copyright (C) 2002-2023 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
76 enum TLColor {
87 };
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
298 return myControlledLinks;
299 }
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
353 virtual int getMaxIndex() = 0;
354
356 virtual int getMaxValidIndex() {
357 return getMaxIndex();
358 }
359
365 int computeBrakingTime(double minDecel) const;
366
368 virtual bool usingSignalGroups() const {
369 return false;
370 };
371
373 std::string getDescription() const;
374
376 virtual void finalChecks() const {}
377
378protected:
380 static const std::string DummyID;
381
386 virtual NBTrafficLightLogic* myCompute(int brakingTime) = 0;
387
388
392 virtual void collectLinks() = 0;
393
394
397 virtual void collectEdges();
398
399
400 // @return whether this traffic light is invalid and should be computed
401 virtual bool amInvalid() const;
402
405
406protected:
408 std::vector<NBNode*> myControlledNodes;
409
412
415
418
420 std::set<std::string> myControlledInnerEdges;
421
423 std::string mySubID;
424
427
430
432 struct StreamPair {
433 StreamPair(const NBEdge* _from1, const NBEdge* _to1, const NBEdge* _from2, const NBEdge* _to2):
434 from1(_from1),
435 to1(_to1),
436 from2(_from2),
437 to2(_to2) {}
438
439 bool operator==(const StreamPair& o) const {
440 return (from1 == o.from1 && to1 == o.to1
441 && from2 == o.from2 && to2 == o.to2);
442 }
443
444 bool operator<(const StreamPair& o) const {
445 if (from1 != o.from1) {
446 return from1 < o.from1;
447 }
448 if (to1 != o.to1) {
449 return to1 < o.to1;
450 }
451 if (from2 != o.from2) {
452 return from2 < o.from2;
453 }
454 return to2 < o.to2;
455 }
456
457 const NBEdge* from1;
458 const NBEdge* to1;
459 const NBEdge* from2;
460 const NBEdge* to2;
461 };
462 typedef std::set<StreamPair> NeedsContRelation;
465
466 typedef std::set<std::pair<int, int> > RightOnRedConflicts;
469
470private:
471 static std::set<NBEdge*> collectReachable(EdgeVector outer, const EdgeVector& within, bool checkControlled);
472
473
474};
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
TrafficLightType
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_GREEN
Signal shows green.
@ TLCOLOR_YELLOW
Signal shows yellow.
@ TLCOLOR_REDYELLOW
Signal shows red/yellow (unused)
std::vector< NBNode * > myControlledNodes
The container with participating nodes.
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)
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
NBTrafficLightDefinition(const std::string &id, const std::vector< NBNode * > &junctions, const std::string &programID, SUMOTime offset, TrafficLightType type)
Constructor.
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.
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...
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.
Definition: Parameterised.h:41
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)