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-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 // 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 // ===========================================================================
34 class MSLink;
35 class MSPhaseDefinition;
37 
38 
39 // ===========================================================================
40 // class definitions
41 // ===========================================================================
47 public:
55  MSRailSignal(MSTLLogicControl& tlcontrol,
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 
68  ~MSRailSignal();
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 
254  typedef std::pair<const SUMOVehicle* const, const MSLink::ApproachingVehicleInformation> Approaching;
255  typedef std::set<const MSLane*, ComparatorNumericalIdLess> LaneSet;
256  typedef std::map<const MSLane*, int, ComparatorNumericalIdLess> LaneVisitedMap;
257 
258  /* The driveways (Fahrstrassen) for each link index
259  * Each link index has at least one driveway
260  * A driveway describes one possible route that passes the signal up
261  * the next secure point
262  * When a signal guards a switch (indirect guard) that signal stores two
263  * or more driveways
264  */
265  struct DriveWay {
266 
268  DriveWay(bool temporary = false) :
269  myNumericalID(temporary ? -1 : myDriveWayIndex++),
270  myMaxFlankLength(0),
271  myActive(nullptr),
272  myProtectedBidi(nullptr),
273  myCoreSize(0),
274  myFoundSignal(false),
275  myFoundReversal(false)
276  {}
277 
280 
283 
286 
289 
291  std::vector<const MSEdge*> myRoute;
292 
295 
299 
300  /* @brief the actual driveway part up to the next railsignal (halting position)
301  * This must be free of other trains */
302  std::vector<const MSLane*> myForward;
303 
304  /* @brief the list of bidirectional edges that can enter the forward
305  * section and which must also be free of traffic
306  * (up to the first element that could give protection) */
307  std::vector<const MSLane*> myBidi;
308 
309  /* @brief the list of bidirectional edges that can enter the forward
310  * section and which might contain deadlock-relevant traffic */
311  std::vector<const MSLane*> myBidiExtended;
312 
313  /* @brief the list of edges that merge with the forward section
314  * (found via backward search, up to the first element that could give protection) */
315  std::vector<const MSLane*> myFlank;
316 
318  std::vector<const MSLane*> myConflictLanes;
319 
320  /* @brief the list of switches that threaten the driveway and for which protection must be found
321  */
322  std::vector<MSLink*> myFlankSwitches;
323 
324  /* @brief the list of (first) switches that could give protection from oncoming/flanking vehicles
325  * if any of them fails to do so, upstream search must be performed
326  * until protection or conflict is found
327  */
328  std::vector<MSLink*> myProtectingSwitches;
330  std::vector<MSLink*> myProtectingSwitchesBidi;
331 
332  /* The conflict links for this block
333  * Conflict resolution must be performed if vehicles are approaching the
334  * current link and any of the conflict links */
335  std::vector<MSLink*> myConflictLinks;
336 
338  bool conflictLaneOccupied(const std::string& joinVehicle = "", bool store = true, const SUMOVehicle* ego = nullptr) const;
339 
341  bool deadlockLaneOccupied(bool store = true) const;
342 
344  bool reserve(const Approaching& closest, MSEdgeVector& occupied);
345 
347  bool hasLinkConflict(const Approaching& closest, MSLink* foeLink) const;
348 
350  static bool mustYield(const Approaching& veh, const Approaching& foe);
351 
353  bool conflictLinkApproached() const;
354 
356  bool findProtection(const Approaching& veh, MSLink* link) const;
357 
359  bool overlap(const DriveWay& other) const;
360 
362  bool flankConflict(const DriveWay& other) const;
363 
365  void writeBlocks(OutputDevice& od) const;
366 
367  /* @brief determine route that identifies this driveway (a subset of the
368  * vehicle route)
369  * collects:
370  * myRoute
371  * myForward
372  * myBidi
373  * myProtectedBidi
374  *
375  * returns edge that is assumed to safe from oncoming-deadlock or nullptr
376  */
377  void buildRoute(MSLink* origin, double length, MSRouteIterator next, MSRouteIterator end, LaneVisitedMap& visited);
378 
379  /* @brief find switches that threaten this driveway
380  * @param[out] flankSwitches collect the switches
381  */
382  void checkFlanks(const MSLink* originLink, const std::vector<const MSLane*>& lanes, const LaneVisitedMap& visited, bool allFoes, std::vector<MSLink*>& flankSwitches) const;
383 
384  /* @brief find links that cross the driveway without entering it
385  * @param[out] flankSwitches collect the switches
386  */
387  void checkCrossingFlanks(MSLink* dwLink, const LaneVisitedMap& visited, std::vector<MSLink*>& flankSwitches) const;
388 
389  /* @brief find upstream protection from the given link
390  * @param[out] flank: the stored flank lanes
391  */
392  void findFlankProtection(MSLink* link, double length, LaneVisitedMap& visited, MSLink* origLink, std::vector<const MSLane*>& flank);
393  };
394 
395  /* @brief retrieve driveway with the given numerical id
396  * @note: throws exception if the driveway does not exist at this rail signal */
397  const DriveWay& retrieveDriveWay(int numericalID) const;
398 
400  static Approaching getClosest(MSLink* link);
401 
402 protected:
404  bool constraintsAllow(const SUMOVehicle* veh) const;
405 
406 protected:
407 
408  /* The driveways for each link
409  */
410  struct LinkInfo {
412  LinkInfo(MSLink* link);
413 
415 
417  std::vector<DriveWay> myDriveways;
418 
420  std::string getID() const;
421 
424 
427 
429  void reroute(SUMOVehicle* veh, const MSEdgeVector& occupied);
430 
432  void reset();
433 
436  };
437 
439  std::vector<LinkInfo> myLinkInfos;
440 
442  static std::string getTLLinkID(MSLink* link);
443 
445  static std::string getJunctionLinkID(MSLink* link);
446 
448  static std::string getClickableTLLinkID(MSLink* link);
449 
451  static std::string describeLinks(std::vector<MSLink*> links);
452 
454  static std::string formatVisitedMap(const LaneVisitedMap& visited);
455 
457  static void appendMapIndex(LaneVisitedMap& map, const MSLane* lane);
458 
459 protected:
460 
466 
469 
472 
475 
477  std::map<std::string, std::vector<MSRailSignalConstraint*> > myConstraints;
478 
479  static int myNumWarnings;
480 
481  static int myDriveWayIndex;
482 
483 protected:
485  void storeTraCIVehicles(int linkIndex);
486 
488 
489  static bool myStoreVehicles;
493  static std::string myConstraintInfo;
495 
496 
497 };
long long int SUMOTime
Definition: GUI.h:35
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:491
std::string getBlockingVehicleIDs() const
Phases myPhases
The list of phases this logic uses.
Definition: MSRailSignal.h:465
static VehicleVector myPriorityVehicles
Definition: MSRailSignal.h:492
int myPhaseIndex
MSTrafficLightLogic requires that the phase index changes whenever signals change their state.
Definition: MSRailSignal.h:471
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:493
MSPhaseDefinition myCurrentPhase
The current phase.
Definition: MSRailSignal.h:468
void addConstraint(const std::string &tripId, MSRailSignalConstraint *constraint)
register constraint 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:439
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
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:481
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:490
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:477
std::pair< const SUMOVehicle *const, const MSLink::ApproachingVehicleInformation > Approaching
Definition: MSRailSignal.h:254
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.
std::set< const MSLane *, ComparatorNumericalIdLess > LaneSet
Definition: MSRailSignal.h:255
static std::string getJunctionLinkID(MSLink *link)
return junctionID_junctionLinkIndex
static int myNumWarnings
Definition: MSRailSignal.h:479
SUMOTime trySwitch() override
Switches to the next phase.
const DriveWay & retrieveDriveWay(int numericalID) const
static bool myStoreVehicles
Definition: MSRailSignal.h:489
std::map< const MSLane *, int, ComparatorNumericalIdLess > LaneVisitedMap
Definition: MSRailSignal.h:256
bool myMovingBlock
whether the signal is in moving block mode (only protects from oncoming and flanking trains)
Definition: MSRailSignal.h:474
bool removeConstraint(const std::string &tripId, MSRailSignalConstraint *constraint)
remove constraint for signal switching
static bool hasOncomingRailTraffic(MSLink *link, const MSVehicle *ego, bool &brakeBeforeSignal)
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
const std::map< std::string, std::vector< MSRailSignalConstraint * > > & getConstraints() const
Definition: MSRailSignal.h:230
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:60
std::vector< const MSLane * > myForward
Definition: MSRailSignal.h:302
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:282
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:322
int myCoreSize
number of edges in myRoute where overlap with other driveways is forbidden
Definition: MSRailSignal.h:294
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:288
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:330
std::vector< const MSLane * > myConflictLanes
the lanes that must be clear of trains before this signal can switch to green
Definition: MSRailSignal.h:318
bool overlap(const DriveWay &other) const
Wether this driveway (route) overlaps with the given one.
int myNumericalID
global driveway index
Definition: MSRailSignal.h:279
std::vector< MSLink * > myConflictLinks
Definition: MSRailSignal.h:335
std::vector< const MSLane * > myBidi
Definition: MSRailSignal.h:307
std::vector< const MSLane * > myBidiExtended
Definition: MSRailSignal.h:311
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:315
DriveWay(bool temporary=false)
Constructor.
Definition: MSRailSignal.h:268
std::vector< const MSEdge * > myRoute
list of edges for matching against train routes
Definition: MSRailSignal.h:291
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:328
bool myFoundSignal
whether this driveway ends its forward section with a rail signal (and thus comprises a full block)
Definition: MSRailSignal.h:297
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:285
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.