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.org/sumo
3 // Copyright (C) 2002-2022 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 std::map<std::string, std::string>& parameters);
58 
59 
64  void init(NLDetectorBuilder& nb);
65 
66 
68  ~MSRailSignal();
69 
71  void setParameter(const std::string& key, const std::string& value);
72 
78  void addLink(MSLink* link, MSLane* lane, int pos);
79 
82 
89 
90 
93 
94 
104  void updateCurrentPhase();
105 
111 
113 
114 
117 
122  int getPhaseNumber() const;
123 
124 
129  const Phases& getPhases() const;
130 
139  const MSPhaseDefinition& getPhase(int givenstep) const;
141 
142 
145 
150  int getCurrentPhaseIndex() const;
151 
152 
156  const MSPhaseDefinition& getCurrentPhaseDef() const;
158 
159 
162 
167  SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const;
168 
169 
175  SUMOTime getOffsetFromIndex(int index) const;
176 
177 
183  int getIndexFromOffset(SUMOTime offset) const;
185 
186 
189 
197  void changeStepAndDuration(MSTLLogicControl& tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration) {
198  UNUSED_PARAMETER(tlcontrol);
199  UNUSED_PARAMETER(simStep);
200  UNUSED_PARAMETER(step);
201  UNUSED_PARAMETER(stepDuration);
202  }
204 
206  VehicleVector getBlockingVehicles(int linkIndex);
207  std::string getBlockingVehicleIDs() const;
208 
210  VehicleVector getRivalVehicles(int linkIndex);
211  std::string getRivalVehicleIDs() const;
212 
214  VehicleVector getPriorityVehicles(int linkIndex);
215  std::string getPriorityVehicleIDs() const;
216 
218  std::string getConstraintInfo(int linkIndex);
219  std::string getConstraintInfo() const;
220 
222  void writeBlocks(OutputDevice& od) const;
223 
225  void addConstraint(const std::string& tripId, MSRailSignalConstraint* constraint);
226 
228  void addInsertionConstraint(const std::string& tripId, MSRailSignalConstraint* constraint);
229 
232  const std::map<std::string, std::vector<MSRailSignalConstraint*> >& getConstraints() const {
233  return myConstraints;
234  }
235 
236  const std::map<std::string, std::vector<MSRailSignalConstraint*> >& getInsertionConstraints() const {
237  return myInsertionConstraints;
238  }
239 
241  bool removeConstraint(const std::string& tripId, MSRailSignalConstraint* constraint);
242  void removeConstraints();
244 
246  void updateDriveway(int numericalID);
247 
248  static bool hasOncomingRailTraffic(MSLink* link);
249  static bool hasInsertionConstraint(MSLink* link, const MSVehicle* veh, std::string& info);
250 
252  static void recheckGreen();
253 
254 protected:
256  bool constraintsAllow(const SUMOVehicle* veh) const;
257 
258 protected:
259 
260  typedef std::pair<const SUMOVehicle* const, const MSLink::ApproachingVehicleInformation> Approaching;
261  typedef std::set<const MSLane*, ComparatorNumericalIdLess> LaneSet;
262  typedef std::map<const MSLane*, int, ComparatorNumericalIdLess> LaneVisitedMap;
263 
264  /* The driveways (Fahrstrassen) for each link index
265  * Each link index has at least one driveway
266  * A driveway describes one possible route that passes the signal up
267  * the next secure point
268  * When a signal guards a switch (indirect guard) that signal stores two
269  * or more driveways
270  */
271  struct DriveWay {
272 
274  DriveWay(bool temporary = false) :
275  myNumericalID(temporary ? -1 : myDriveWayIndex++),
276  myMaxFlankLength(0),
277  myActive(nullptr),
278  myProtectedBidi(nullptr),
279  myCoreSize(0)
280  {}
281 
284 
287 
290 
293 
295  std::vector<const MSEdge*> myRoute;
296 
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<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<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<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;
329 
330  /* The conflict links for this block
331  * Conflict resolution must be performed if vehicles are approaching the
332  * current link and any of the conflict links */
333  std::vector<MSLink*> myConflictLinks;
334 
336  bool conflictLaneOccupied(const std::string& joinVehicle = "", bool store = true) const;
337 
339  bool deadlockLaneOccupied(bool store = true) const;
340 
342  bool reserve(const Approaching& closest, MSEdgeVector& occupied);
343 
345  bool hasLinkConflict(const Approaching& closest, MSLink* foeLink) const;
346 
348  static bool mustYield(const Approaching& veh, const Approaching& foe);
349 
351  bool conflictLinkApproached() const;
352 
354  bool findProtection(const Approaching& veh, MSLink* link) const;
355 
357  bool overlap(const DriveWay& other) const;
358 
360  bool flankConflict(const DriveWay& other) const;
361 
363  void writeBlocks(OutputDevice& od) const;
364 
365  /* @brief determine route that identifies this driveway (a subset of the
366  * vehicle route)
367  * collects:
368  * myRoute
369  * myForward
370  * myBidi
371  * myProtectedBidi
372  *
373  * returns edge that is assumed to safe from oncoming-deadlock or nullptr
374  */
375  void buildRoute(MSLink* origin, double length, MSRouteIterator next, MSRouteIterator end, LaneVisitedMap& visited);
376 
378  void checkFlanks(const std::vector<MSLane*>& lanes, const LaneVisitedMap& visited, bool allFoes);
379 
381  void checkCrossingFlanks(MSLink* dwLink, const LaneVisitedMap& visited);
382 
384  void findFlankProtection(MSLink* link, double length, LaneVisitedMap& visited, MSLink* origLink);
385  };
386 
387  /* The driveways for each link
388  */
389  struct LinkInfo {
391  LinkInfo(MSLink* link);
392 
394 
397 
399  std::vector<DriveWay> myDriveways;
400 
402  std::string getID() const;
403 
406 
409 
411  void reroute(SUMOVehicle* veh, const MSEdgeVector& occupied);
412 
414  void reset();
415 
418  };
419 
421  std::vector<LinkInfo> myLinkInfos;
422 
423  /* @brief retrieve driveway with the given numerical id
424  * @note: throws exception if the driveway does not exist at this rail signal */
425  const DriveWay& retrieveDriveWay(int numericalID) const;
426 
428  static Approaching getClosest(MSLink* link);
429 
431  static std::string getTLLinkID(MSLink* link);
432 
434  static std::string getJunctionLinkID(MSLink* link);
435 
437  static std::string getClickableTLLinkID(MSLink* link);
438 
440  static std::string describeLinks(std::vector<MSLink*> links);
441 
443  static std::string formatVisitedMap(const LaneVisitedMap& visited);
444 
445 protected:
446 
452 
455 
458 
461 
463  std::map<std::string, std::vector<MSRailSignalConstraint*> > myConstraints;
464  std::map<std::string, std::vector<MSRailSignalConstraint*> > myInsertionConstraints;
465 
466  static int myNumWarnings;
467 
469  static std::vector<std::pair<MSLink*, int> > mySwitchedGreenFlanks;
470  static std::map<std::pair<int, int>, bool> myDriveWayCompatibility;
471  static int myDriveWayIndex;
472 
473 protected:
475  void storeTraCIVehicles(int linkIndex);
476 
478 
479  static bool myStoreVehicles;
483  static std::string myConstraintInfo;
485 
486 
487 };
std::vector< MSEdge * > MSEdgeVector
Definition: MSEdge.h:73
ConstMSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:54
long long int SUMOTime
Definition: SUMOTime.h:32
#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:82
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:481
std::string getBlockingVehicleIDs() const
int getIndexFromOffset(SUMOTime offset) const
Returns the step (the phasenumber) of a given position of the cycle.
Phases myPhases
The list of phases this logic uses.
Definition: MSRailSignal.h:451
std::map< std::string, std::vector< MSRailSignalConstraint * > > myInsertionConstraints
Definition: MSRailSignal.h:464
static VehicleVector myPriorityVehicles
Definition: MSRailSignal.h:482
int myPhaseIndex
MSTrafficLightLogic requires that the phase index changes whenever signals change their state.
Definition: MSRailSignal.h:457
int getPhaseNumber() const
Returns the number of phases.
static std::string myConstraintInfo
Definition: MSRailSignal.h:483
MSPhaseDefinition myCurrentPhase
The current phase.
Definition: MSRailSignal.h:454
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:421
std::string getPriorityVehicleIDs() const
static int myDriveWayIndex
Definition: MSRailSignal.h:471
static std::string describeLinks(std::vector< MSLink * > links)
print link descriptions
VehicleVector getRivalVehicles(int linkIndex)
return vehicles that approach the intersection/rail signal and are in conflict with vehicles that wis...
void writeBlocks(OutputDevice &od) const
write rail signal block output for all links and driveways
~MSRailSignal()
Destructor.
VehicleVector getPriorityVehicles(int linkIndex)
return vehicles that approach the intersection/rail signal and have priority over vehicles that wish ...
const Phases & getPhases() const
Returns the phases of this tls program.
static VehicleVector myBlockingVehicles
Definition: MSRailSignal.h:480
void removeConstraints()
void storeTraCIVehicles(int linkIndex)
update vehicle lists for traci calls
SUMOTime getOffsetFromIndex(int index) const
Returns the position (start of a phase during a cycle) from of a given step.
static Approaching getClosest(MSLink *link)
get the closest vehicle approaching the given link
void setParameter(const std::string &key, const std::string &value)
Sets a parameter and updates internal constants.
std::map< std::string, std::vector< MSRailSignalConstraint * > > myConstraints
map from tripId to constraint list
Definition: MSRailSignal.h:463
static std::map< std::pair< int, int >, bool > myDriveWayCompatibility
Definition: MSRailSignal.h:470
std::pair< const SUMOVehicle *const, const MSLink::ApproachingVehicleInformation > Approaching
Definition: MSRailSignal.h:260
SUMOTime trySwitch()
Switches to the next phase.
const std::map< std::string, std::vector< MSRailSignalConstraint * > > & getInsertionConstraints() const
Definition: MSRailSignal.h:236
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:261
static std::string getJunctionLinkID(MSLink *link)
return junctionID_junctionLinkIndex
static int myNumWarnings
Definition: MSRailSignal.h:466
const DriveWay & retrieveDriveWay(int numericalID) const
static bool myStoreVehicles
Definition: MSRailSignal.h:479
void addInsertionConstraint(const std::string &tripId, MSRailSignalConstraint *constraint)
register contraint for vehicle insertion
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.
std::map< const MSLane *, int, ComparatorNumericalIdLess > LaneVisitedMap
Definition: MSRailSignal.h:262
bool myMovingBlock
whether the signal is in moving block mode (only protects from oncoming and flanking trains)
Definition: MSRailSignal.h:460
bool removeConstraint(const std::string &tripId, MSRailSignalConstraint *constraint)
remove contraint for signal switching
static std::vector< std::pair< MSLink *, int > > mySwitchedGreenFlanks
list of signals that switched green along with driveway index
Definition: MSRailSignal.h:469
void addLink(MSLink *link, MSLane *lane, int pos)
Adds a link on building.
SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const
Returns the index of the logic at the given simulation step.
MSRailSignal(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, SUMOTime delay, const std::map< std::string, std::string > &parameters)
Constructor.
void init(NLDetectorBuilder &nb)
Initialises the rail signal with information about adjacent rail signals.
void updateDriveway(int numericalID)
update driveway for extended deadlock protection
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
std::string getConstraintInfo() const
static std::string formatVisitedMap(const LaneVisitedMap &visited)
print link descriptions
void adaptLinkInformationFrom(const MSTrafficLightLogic &logic)
Applies information about controlled links and lanes from the given logic.
const std::map< std::string, std::vector< MSRailSignalConstraint * > > & getConstraints() const
Definition: MSRailSignal.h:232
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)
Changes the current phase and her duration.
Definition: MSRailSignal.h:197
VehicleVector getBlockingVehicles(int linkIndex)
return vehicles that block the intersection/rail signal for vehicles that wish to pass the given link...
void updateCurrentPhase()
returns the state of the signal that actually required
static bool hasOncomingRailTraffic(MSLink *link)
std::string getRivalVehicleIDs() const
int getCurrentPhaseIndex() const
Returns the current index within the program.
static bool hasInsertionConstraint(MSLink *link, const MSVehicle *veh, std::string &info)
static std::string getTLLinkID(MSLink *link)
return logicID_linkIndex
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:75
Builds detectors for microsim.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
Representation of a vehicle.
Definition: SUMOVehicle.h:60
void checkFlanks(const std::vector< MSLane * > &lanes, const LaneVisitedMap &visited, bool allFoes)
find switches that threathen this driveway
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:286
std::vector< MSLane * > myBidiExtended
Definition: MSRailSignal.h:311
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:298
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:292
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:283
std::vector< MSLink * > myConflictLinks
Definition: MSRailSignal.h:333
void checkCrossingFlanks(MSLink *dwLink, const LaneVisitedMap &visited)
find links that cross the driveway without entering it
std::vector< MSLane * > myBidi
Definition: MSRailSignal.h:307
void findFlankProtection(MSLink *link, double length, LaneVisitedMap &visited, MSLink *origLink)
find upstream protection from the given link
bool conflictLaneOccupied(const std::string &joinVehicle="", bool store=true) 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:274
std::vector< const MSEdge * > myRoute
list of edges for matching against train routes
Definition: MSRailSignal.h:295
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
std::vector< MSLane * > myForward
Definition: MSRailSignal.h:302
bool conflictLinkApproached() const
Whether any of the conflict linkes 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:289
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.