Eclipse SUMO - Simulation of Urban MObility
MSRailSignal.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-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// A rail signal logic
21/****************************************************************************/
22#pragma once
23#include <config.h>
24
25#include <vector>
26#include <microsim/MSRoute.h>
29
30
31// ===========================================================================
32// class declarations
33// ===========================================================================
34class MSLink;
37
38
39// ===========================================================================
40// class definitions
41// ===========================================================================
47public:
56 const std::string& id, const std::string& programID, SUMOTime delay,
57 const Parameterised::Map& parameters);
58
59
64 void init(NLDetectorBuilder& nb) override;
65
66
69
71 void setParameter(const std::string& key, const std::string& value) override;
72
78 void addLink(MSLink* link, MSLane* lane, int pos) override;
79
82
87 void adaptLinkInformationFrom(const MSTrafficLightLogic& logic) override;
89
90
93
94
104 void updateCurrentPhase();
105
110 SUMOTime trySwitch() override;
111
113
114
117
122 int getPhaseNumber() const override;
123
128 const Phases& getPhases() const override;
129
138 const MSPhaseDefinition& getPhase(int givenstep) const override;
139
141 bool getsMajorGreen(int /*linkIndex*/) const override {
142 return true;
143 }
145
146
149
154 int getCurrentPhaseIndex() const override;
155
159 const MSPhaseDefinition& getCurrentPhaseDef() const override;
161
162
165
170 SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const override;
171
177 SUMOTime getOffsetFromIndex(int index) const override;
178
184 int getIndexFromOffset(SUMOTime offset) const override;
186
187
190
198 void changeStepAndDuration(MSTLLogicControl& tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration) override {
199 UNUSED_PARAMETER(tlcontrol);
200 UNUSED_PARAMETER(simStep);
201 UNUSED_PARAMETER(step);
202 UNUSED_PARAMETER(stepDuration);
203 }
205
207 VehicleVector getBlockingVehicles(int linkIndex) override;
208 std::string getBlockingVehicleIDs() const;
209
211 VehicleVector getRivalVehicles(int linkIndex) override;
212 std::string getRivalVehicleIDs() const;
213
215 VehicleVector getPriorityVehicles(int linkIndex) override;
216 std::string getPriorityVehicleIDs() const;
217
219 std::string getConstraintInfo(int linkIndex);
220 std::string getConstraintInfo() const;
221
223 void writeBlocks(OutputDevice& od) const;
224
226 void addConstraint(const std::string& tripId, MSRailSignalConstraint* constraint);
227
230 const std::map<std::string, std::vector<MSRailSignalConstraint*> >& getConstraints() const {
231 return myConstraints;
232 }
233
235 bool removeConstraint(const std::string& tripId, MSRailSignalConstraint* constraint);
236 void removeConstraints();
238
240 void updateDriveway(int numericalID);
241
242 /* @brief return whether vehicle insertion must be delayed for an oncoming train
243 * @param[in] link The rail signal link before which the vehicle is being inserted
244 * @param[in] veh The vehicle being inserted
245 * @param[in] brakeBeforeSignal Whether the vehicle may brake before the signal,
246 * Returns true if the vehicle has to brake before the signal
247 */
248 static bool hasOncomingRailTraffic(MSLink* link, const MSVehicle* ego, bool& brakeBeforeSignal);
249
250 static bool hasInsertionConstraint(MSLink* link, const MSVehicle* veh, std::string& info, bool& isInsertionOrder);
251
252 static void initDriveWays(const SUMOVehicle* ego, bool update);
253
255 static void recheckGreen();
256
257protected:
259 bool constraintsAllow(const SUMOVehicle* veh) const;
260
261protected:
262
263 typedef std::pair<const SUMOVehicle* const, const MSLink::ApproachingVehicleInformation> Approaching;
264 typedef std::set<const MSLane*, ComparatorNumericalIdLess> LaneSet;
265 typedef std::map<const MSLane*, int, ComparatorNumericalIdLess> LaneVisitedMap;
266
267 /* The driveways (Fahrstrassen) for each link index
268 * Each link index has at least one driveway
269 * A driveway describes one possible route that passes the signal up
270 * the next secure point
271 * When a signal guards a switch (indirect guard) that signal stores two
272 * or more driveways
273 */
274 struct DriveWay {
275
277 DriveWay(bool temporary = false) :
278 myNumericalID(temporary ? -1 : myDriveWayIndex++),
280 myActive(nullptr),
281 myProtectedBidi(nullptr),
282 myCoreSize(0),
283 myFoundSignal(false),
284 myFoundReversal(false)
285 {}
286
289
292
295
298
300 std::vector<const MSEdge*> myRoute;
301
304
308
309 /* @brief the actual driveway part up to the next railsignal (halting position)
310 * This must be free of other trains */
311 std::vector<const MSLane*> myForward;
312
313 /* @brief the list of bidirectional edges that can enter the forward
314 * section and which must also be free of traffic
315 * (up to the first element that could give protection) */
316 std::vector<const MSLane*> myBidi;
317
318 /* @brief the list of bidirectional edges that can enter the forward
319 * section and which might contain deadlock-relevant traffic */
320 std::vector<const MSLane*> myBidiExtended;
321
322 /* @brief the list of edges that merge with the forward section
323 * (found via backward search, up to the first element that could give protection) */
324 std::vector<const MSLane*> myFlank;
325
327 std::vector<const MSLane*> myConflictLanes;
328
329 /* @brief the list of switches that threaten the driveway and for which protection must be found
330 */
331 std::vector<MSLink*> myFlankSwitches;
332
333 /* @brief the list of (first) switches that could give protection from oncoming/flanking vehicles
334 * if any of them fails to do so, upstream search must be performed
335 * until protection or conflict is found
336 */
337 std::vector<MSLink*> myProtectingSwitches;
339 std::vector<MSLink*> myProtectingSwitchesBidi;
340
341 /* The conflict links for this block
342 * Conflict resolution must be performed if vehicles are approaching the
343 * current link and any of the conflict links */
344 std::vector<MSLink*> myConflictLinks;
345
347 bool conflictLaneOccupied(const std::string& joinVehicle = "", bool store = true, const SUMOVehicle* ego = nullptr) const;
348
350 bool deadlockLaneOccupied(bool store = true) const;
351
353 bool reserve(const Approaching& closest, MSEdgeVector& occupied);
354
356 bool hasLinkConflict(const Approaching& closest, MSLink* foeLink) const;
357
359 static bool mustYield(const Approaching& veh, const Approaching& foe);
360
362 bool conflictLinkApproached() const;
363
365 bool findProtection(const Approaching& veh, MSLink* link) const;
366
368 bool overlap(const DriveWay& other) const;
369
371 bool flankConflict(const DriveWay& other) const;
372
374 void writeBlocks(OutputDevice& od) const;
375
376 /* @brief determine route that identifies this driveway (a subset of the
377 * vehicle route)
378 * collects:
379 * myRoute
380 * myForward
381 * myBidi
382 * myProtectedBidi
383 *
384 * returns edge that is assumed to safe from oncoming-deadlock or nullptr
385 */
386 void buildRoute(MSLink* origin, double length, MSRouteIterator next, MSRouteIterator end, LaneVisitedMap& visited);
387
388 /* @brief find switches that threaten this driveway
389 * @param[out] flankSwitches collect the switches
390 */
391 void checkFlanks(const MSLink* originLink, const std::vector<const MSLane*>& lanes, const LaneVisitedMap& visited, bool allFoes, std::vector<MSLink*>& flankSwitches) const;
392
393 /* @brief find links that cross the driveway without entering it
394 * @param[out] flankSwitches collect the switches
395 */
396 void checkCrossingFlanks(MSLink* dwLink, const LaneVisitedMap& visited, std::vector<MSLink*>& flankSwitches) const;
397
398 /* @brief find upstream protection from the given link
399 * @param[out] flank: the stored flank lanes
400 */
401 void findFlankProtection(MSLink* link, double length, LaneVisitedMap& visited, MSLink* origLink, std::vector<const MSLane*>& flank);
402 };
403
404 /* The driveways for each link
405 */
406 struct LinkInfo {
408 LinkInfo(MSLink* link);
409
411
413 std::vector<DriveWay> myDriveways;
414
416 std::string getID() const;
417
420
423
425 void reroute(SUMOVehicle* veh, const MSEdgeVector& occupied);
426
428 void reset();
429
432 };
433
435 std::vector<LinkInfo> myLinkInfos;
436
437 /* @brief retrieve driveway with the given numerical id
438 * @note: throws exception if the driveway does not exist at this rail signal */
439 const DriveWay& retrieveDriveWay(int numericalID) const;
440
442 static Approaching getClosest(MSLink* link);
443
445 static std::string getTLLinkID(MSLink* link);
446
448 static std::string getJunctionLinkID(MSLink* link);
449
451 static std::string getClickableTLLinkID(MSLink* link);
452
454 static std::string describeLinks(std::vector<MSLink*> links);
455
457 static std::string formatVisitedMap(const LaneVisitedMap& visited);
458
460 static void appendMapIndex(LaneVisitedMap& map, const MSLane* lane);
461
462protected:
463
469
472
475
478
480 std::map<std::string, std::vector<MSRailSignalConstraint*> > myConstraints;
481
482 static int myNumWarnings;
483
485 static std::vector<std::pair<MSLink*, int> > mySwitchedGreenFlanks;
486 static std::map<std::pair<int, int>, bool> myDriveWayCompatibility;
487 static int myDriveWayIndex;
488
489protected:
491 void storeTraCIVehicles(int linkIndex);
492
494
495 static bool myStoreVehicles;
499 static std::string myConstraintInfo;
501
502
503};
long long int SUMOTime
Definition: GUI.h:36
std::vector< MSEdge * > MSEdgeVector
Definition: MSEdge.h:73
ConstMSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:56
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:30
A road/street connecting two junctions.
Definition: MSEdge.h:77
Representation of a lane in the micro simulation.
Definition: MSLane.h:84
The definition of a single phase of a tls logic.
A base class for constraints.
A signal for rails.
Definition: MSRailSignal.h:46
bool constraintsAllow(const SUMOVehicle *veh) const
whether the given vehicle is free to drive
static VehicleVector myRivalVehicles
Definition: MSRailSignal.h:497
std::string getBlockingVehicleIDs() const
Phases myPhases
The list of phases this logic uses.
Definition: MSRailSignal.h:468
static VehicleVector myPriorityVehicles
Definition: MSRailSignal.h:498
int myPhaseIndex
MSTrafficLightLogic requires that the phase index changes whenever signals change their state.
Definition: MSRailSignal.h:474
SUMOTime getOffsetFromIndex(int index) const override
Returns the position (start of a phase during a cycle) from of a given step.
void setParameter(const std::string &key, const std::string &value) override
Sets a parameter and updates internal constants.
static std::string myConstraintInfo
Definition: MSRailSignal.h:499
MSPhaseDefinition myCurrentPhase
The current phase.
Definition: MSRailSignal.h:471
void addConstraint(const std::string &tripId, MSRailSignalConstraint *constraint)
register contraint for signal switching
static std::string getClickableTLLinkID(MSLink *link)
return logicID_linkIndex in a way that allows clicking in sumo-gui
std::vector< LinkInfo > myLinkInfos
data storage for every link at this node (more than one when directly guarding a switch)
Definition: MSRailSignal.h:435
SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const override
Returns the index of the logic at the given simulation step.
std::string getPriorityVehicleIDs() const
MSRailSignal(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, SUMOTime delay, const Parameterised::Map &parameters)
Constructor.
static void appendMapIndex(LaneVisitedMap &map, const MSLane *lane)
append to map by map index and avoid undefined behavior
const std::map< std::string, std::vector< MSRailSignalConstraint * > > & getConstraints() const
Definition: MSRailSignal.h:230
VehicleVector getBlockingVehicles(int linkIndex) override
return vehicles that block the intersection/rail signal for vehicles that wish to pass the given link...
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration) override
Changes the current phase and her duration.
Definition: MSRailSignal.h:198
VehicleVector getRivalVehicles(int linkIndex) override
return vehicles that approach the intersection/rail signal and are in conflict with vehicles that wis...
static int myDriveWayIndex
Definition: MSRailSignal.h:487
static std::string describeLinks(std::vector< MSLink * > links)
print link descriptions
void writeBlocks(OutputDevice &od) const
write rail signal block output for all links and driveways
bool getsMajorGreen(int) const override
whether the given link index ever turns 'G'
Definition: MSRailSignal.h:141
~MSRailSignal()
Destructor.
void adaptLinkInformationFrom(const MSTrafficLightLogic &logic) override
Applies information about controlled links and lanes from the given logic.
static VehicleVector myBlockingVehicles
Definition: MSRailSignal.h:496
void removeConstraints()
void storeTraCIVehicles(int linkIndex)
update vehicle lists for traci calls
int getIndexFromOffset(SUMOTime offset) const override
Returns the step (the phasenumber) of a given position of the cycle.
static Approaching getClosest(MSLink *link)
get the closest vehicle approaching the given link
void init(NLDetectorBuilder &nb) override
Initialises the rail signal with information about adjacent rail signals.
std::map< std::string, std::vector< MSRailSignalConstraint * > > myConstraints
map from tripId to constraint list
Definition: MSRailSignal.h:480
static std::map< std::pair< int, int >, bool > myDriveWayCompatibility
Definition: MSRailSignal.h:486
std::pair< const SUMOVehicle *const, const MSLink::ApproachingVehicleInformation > Approaching
Definition: MSRailSignal.h:263
int getPhaseNumber() const override
Returns the number of phases.
const MSPhaseDefinition & getPhase(int givenstep) const override
Returns the definition of the phase from the given position within the plan.
static void recheckGreen()
final check for driveway compatibility of signals that switched green in this step
std::set< const MSLane *, ComparatorNumericalIdLess > LaneSet
Definition: MSRailSignal.h:264
static std::string getJunctionLinkID(MSLink *link)
return junctionID_junctionLinkIndex
static int myNumWarnings
Definition: MSRailSignal.h:482
SUMOTime trySwitch() override
Switches to the next phase.
const DriveWay & retrieveDriveWay(int numericalID) const
static bool myStoreVehicles
Definition: MSRailSignal.h:495
std::map< const MSLane *, int, ComparatorNumericalIdLess > LaneVisitedMap
Definition: MSRailSignal.h:265
bool myMovingBlock
whether the signal is in moving block mode (only protects from oncoming and flanking trains)
Definition: MSRailSignal.h:477
bool removeConstraint(const std::string &tripId, MSRailSignalConstraint *constraint)
remove contraint for signal switching
static bool hasOncomingRailTraffic(MSLink *link, const MSVehicle *ego, bool &brakeBeforeSignal)
static std::vector< std::pair< MSLink *, int > > mySwitchedGreenFlanks
list of signals that switched green along with driveway index
Definition: MSRailSignal.h:485
const Phases & getPhases() const override
Returns the phases of this tls program.
const MSPhaseDefinition & getCurrentPhaseDef() const override
Returns the definition of the current phase.
void updateDriveway(int numericalID)
update driveway for extended deadlock protection
std::string getConstraintInfo() const
static std::string formatVisitedMap(const LaneVisitedMap &visited)
print link descriptions
static void initDriveWays(const SUMOVehicle *ego, bool update)
void updateCurrentPhase()
returns the state of the signal that actually required
void addLink(MSLink *link, MSLane *lane, int pos) override
Adds a link on building.
std::string getRivalVehicleIDs() const
static bool hasInsertionConstraint(MSLink *link, const MSVehicle *veh, std::string &info, bool &isInsertionOrder)
VehicleVector getPriorityVehicles(int linkIndex) override
return vehicles that approach the intersection/rail signal and have priority over vehicles that wish ...
static std::string getTLLinkID(MSLink *link)
return logicID_linkIndex
int getCurrentPhaseIndex() const override
Returns the current index within the program.
A class that stores and controls tls and switching of their programs.
The parent class for traffic light logics.
std::vector< const SUMOVehicle * > VehicleVector
list of vehicles
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:77
Builds detectors for microsim.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
std::map< std::string, std::string > Map
parameters map
Definition: Parameterised.h:45
Representation of a vehicle.
Definition: SUMOVehicle.h:62
std::vector< const MSLane * > myForward
Definition: MSRailSignal.h:311
void writeBlocks(OutputDevice &od) const
Write block items for this driveway.
void buildRoute(MSLink *origin, double length, MSRouteIterator next, MSRouteIterator end, LaneVisitedMap &visited)
double myMaxFlankLength
the maximum flank length searched while building this driveway
Definition: MSRailSignal.h:291
void checkFlanks(const MSLink *originLink, const std::vector< const MSLane * > &lanes, const LaneVisitedMap &visited, bool allFoes, std::vector< MSLink * > &flankSwitches) const
std::vector< MSLink * > myFlankSwitches
Definition: MSRailSignal.h:331
int myCoreSize
number of edges in myRoute where overlap with other driveways is forbidden
Definition: MSRailSignal.h:303
bool deadlockLaneOccupied(bool store=true) const
whether any of myBidiExtended is occupied by a vehicle that targets myBidi
const MSEdge * myProtectedBidi
switch assumed safe from bidi-traffic
Definition: MSRailSignal.h:297
void findFlankProtection(MSLink *link, double length, LaneVisitedMap &visited, MSLink *origLink, std::vector< const MSLane * > &flank)
std::vector< MSLink * > myProtectingSwitchesBidi
subset of myProtectingSwitches that protects from oncoming trains
Definition: MSRailSignal.h:339
std::vector< const MSLane * > myConflictLanes
the lanes that must be clear of trains before this signal can switch to green
Definition: MSRailSignal.h:327
bool overlap(const DriveWay &other) const
Wether this driveway (route) overlaps with the given one.
int myNumericalID
global driveway index
Definition: MSRailSignal.h:288
std::vector< MSLink * > myConflictLinks
Definition: MSRailSignal.h:344
std::vector< const MSLane * > myBidi
Definition: MSRailSignal.h:316
std::vector< const MSLane * > myBidiExtended
Definition: MSRailSignal.h:320
bool conflictLaneOccupied(const std::string &joinVehicle="", bool store=true, const SUMOVehicle *ego=nullptr) const
whether any of myConflictLanes is occupied (vehicles that are the target of a join must be ignored)
std::vector< const MSLane * > myFlank
Definition: MSRailSignal.h:324
DriveWay(bool temporary=false)
Constructor.
Definition: MSRailSignal.h:277
std::vector< const MSEdge * > myRoute
list of edges for matching against train routes
Definition: MSRailSignal.h:300
void checkCrossingFlanks(MSLink *dwLink, const LaneVisitedMap &visited, std::vector< MSLink * > &flankSwitches) const
bool hasLinkConflict(const Approaching &closest, MSLink *foeLink) const
Whether the approaching vehicle is prevent from driving by another vehicle approaching the given link...
bool findProtection(const Approaching &veh, MSLink *link) const
find protection for the given vehicle starting at a switch
std::vector< MSLink * > myProtectingSwitches
Definition: MSRailSignal.h:337
bool myFoundSignal
whether this driveway ends its forward section with a rail signal (and thus comprises a full block)
Definition: MSRailSignal.h:306
bool conflictLinkApproached() const
Whether any of the conflict links have approaching vehicles.
bool reserve(const Approaching &closest, MSEdgeVector &occupied)
attempt reserve this driveway for the given vehicle
const SUMOVehicle * myActive
whether the current signal is switched green for a train approaching this block
Definition: MSRailSignal.h:294
bool flankConflict(const DriveWay &other) const
Wether there is a flank conflict with the given driveway.
static bool mustYield(const Approaching &veh, const Approaching &foe)
Whether veh must yield to the foe train.