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-2020 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 
75  void addLink(MSLink* link, MSLane* lane, int pos);
76 
79 
86 
87 
90 
91 
101  void updateCurrentPhase();
102 
108 
110 
111 
114 
119  int getPhaseNumber() const;
120 
121 
126  const Phases& getPhases() const;
127 
136  const MSPhaseDefinition& getPhase(int givenstep) const;
138 
139 
142 
147  int getCurrentPhaseIndex() const;
148 
149 
153  const MSPhaseDefinition& getCurrentPhaseDef() const;
155 
156 
159 
164  SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const;
165 
166 
172  SUMOTime getOffsetFromIndex(int index) const;
173 
174 
180  int getIndexFromOffset(SUMOTime offset) const;
182 
183 
186 
194  void changeStepAndDuration(MSTLLogicControl& tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration) {
195  UNUSED_PARAMETER(tlcontrol);
196  UNUSED_PARAMETER(simStep);
197  UNUSED_PARAMETER(step);
198  UNUSED_PARAMETER(stepDuration);
199  }
201 
203  VehicleVector getBlockingVehicles(int linkIndex);
204  std::string getBlockingVehicleIDs() const;
205 
207  VehicleVector getRivalVehicles(int linkIndex);
208  std::string getRivalVehicleIDs() const;
209 
211  VehicleVector getPriorityVehicles(int linkIndex);
212  std::string getPriorityVehicleIDs() const;
213 
215  std::string getConstraintInfo(int linkIndex);
216  std::string getConstraintInfo() const;
217 
219  void writeBlocks(OutputDevice& od) const;
220 
222  void addConstraint(const std::string& tripId, MSRailSignalConstraint* constraint);
223 
225  void addInsertionConstraint(const std::string& tripId, MSRailSignalConstraint* constraint);
226 
228  void updateDriveway(int numericalID);
229 
230  static bool hasOncomingRailTraffic(MSLink* link);
231  static bool hasInsertionConstraint(MSLink* link, const MSVehicle* veh);
232 
234  static void recheckGreen();
235 
236 protected:
238  bool constraintsAllow(const SUMOVehicle* veh) const;
239 
240 protected:
241 
242  typedef std::pair<const SUMOVehicle* const, const MSLink::ApproachingVehicleInformation> Approaching;
243  typedef std::set<const MSLane*, ComparatorNumericalIdLess> LaneSet;
244  typedef std::map<const MSLane*, int, ComparatorNumericalIdLess> LaneVisitedMap;
245 
246  /* The driveways (Fahrstrassen) for each link index
247  * Each link index has at least one driveway
248  * A driveway describes one possible route that passes the signal up
249  * the next secure point
250  * When a signal guards a switch (indirect guard) that signal stores two
251  * or more driveways
252  */
253  struct DriveWay {
254 
256  DriveWay(bool temporary = false) :
257  myNumericalID(temporary ? -1 : myDriveWayIndex++),
258  myMaxFlankLength(0),
259  myActive(nullptr),
260  myProtectedBidi(nullptr),
261  myCoreSize(0)
262  {}
263 
266 
269 
272 
275 
277  std::vector<const MSEdge*> myRoute;
278 
281 
282  /* @brief the actual driveway part up to the next railsignal (halting position)
283  * This must be free of other trains */
284  std::vector<MSLane*> myForward;
285 
286  /* @brief the list of bidirectional edges that can enter the forward
287  * section and which must also be free of traffic
288  * (up to the first element that could give protection) */
289  std::vector<MSLane*> myBidi;
290 
291  /* @brief the list of bidirectional edges that can enter the forward
292  * section and which might contain deadlock-relevant traffic */
293  std::vector<MSLane*> myBidiExtended;
294 
295  /* @brief the list of edges that merge with the forward section
296  * (found via backward search, up to the first element that could give protection) */
297  std::vector<const MSLane*> myFlank;
298 
300  std::vector<const MSLane*> myConflictLanes;
301 
302  /* @brief the list of switches that threaten the driveway and for which protection must be found
303  */
304  std::vector<MSLink*> myFlankSwitches;
305 
306  /* @brief the list of (first) switches that could give protection from oncoming/flanking vehicles
307  * if any of them fails to do so, upstream search must be performed
308  * until protection or conflict is found
309  */
310  std::vector<MSLink*> myProtectingSwitches;
311 
312  /* The conflict links for this block
313  * Conflict resolution must be performed if vehicles are approaching the
314  * current link and any of the conflict links */
315  std::vector<MSLink*> myConflictLinks;
316 
318  bool conflictLaneOccupied(const std::string& joinVehicle = "", bool store = true) const;
319 
321  bool deadlockLaneOccupied(bool store = true) const;
322 
324  bool reserve(const Approaching& closest, MSEdgeVector& occupied);
325 
327  bool hasLinkConflict(const Approaching& closest, MSLink* foeLink) const;
328 
330  static bool mustYield(const Approaching& veh, const Approaching& foe);
331 
333  bool conflictLinkApproached() const;
334 
336  bool findProtection(const Approaching& veh, MSLink* link) const;
337 
339  bool overlap(const DriveWay& other) const;
340 
342  bool flankConflict(const DriveWay& other) const;
343 
345  void writeBlocks(OutputDevice& od) const;
346 
347  /* @brief determine route that identifies this driveway (a subset of the
348  * vehicle route)
349  * collects:
350  * myRoute
351  * myForward
352  * myBidi
353  * myProtectedBidi
354  *
355  * returns edge that is assumed to safe from oncoming-deadlock or nullptr
356  */
357  void buildRoute(MSLink* origin, double length, MSRouteIterator next, MSRouteIterator end, LaneVisitedMap& visited);
358 
360  void checkFlanks(const std::vector<MSLane*>& lanes, const LaneVisitedMap& visited, bool allFoes);
361 
363  void checkCrossingFlanks(MSLink* dwLink, const LaneVisitedMap& visited);
364 
366  void findFlankProtection(MSLink* link, double length, LaneVisitedMap& visited, MSLink* origLink);
367  };
368 
369  /* The driveways for each link
370  */
371  struct LinkInfo {
373  LinkInfo(MSLink* link);
374 
376 
379 
381  std::vector<DriveWay> myDriveways;
382 
384  std::string getID() const;
385 
388 
391 
393  void reroute(SUMOVehicle* veh, const MSEdgeVector& occupied);
394 
397  };
398 
400  std::vector<LinkInfo> myLinkInfos;
401 
402  /* @brief retrieve driveway with the given numerical id
403  * @note: throws exception if the driveway does not exist at this rail signal */
404  const DriveWay& retrieveDriveWay(int numericalID) const;
405 
407  static Approaching getClosest(MSLink* link);
408 
410  static std::string getTLLinkID(MSLink* link);
411 
413  static std::string getJunctionLinkID(MSLink* link);
414 
416  static std::string getClickableTLLinkID(MSLink* link);
417 
419  static std::string describeLinks(std::vector<MSLink*> links);
420 
422  static std::string formatVisitedMap(const LaneVisitedMap& visited);
423 
424 protected:
425 
431 
434 
437 
439  std::map<std::string, std::vector<MSRailSignalConstraint*> > myConstraints;
440  std::map<std::string, std::vector<MSRailSignalConstraint*> > myInsertionConstraints;
441 
442  static int myNumWarnings;
443 
445  static std::vector<std::pair<MSLink*, int> > mySwitchedGreenFlanks;
446  static std::map<std::pair<int, int>, bool> myDriveWayCompatibility;
447  static int myDriveWayIndex;
448 
449 protected:
451  void storeTraCIVehicles(int linkIndex);
452 
454 
455  static bool myStoreVehicles;
459  static std::string myConstraintInfo;
461 
462 
463 };
std::vector< MSEdge * > MSEdgeVector
Definition: MSEdge.h:73
ConstMSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:54
long long int SUMOTime
Definition: SUMOTime.h:31
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:29
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:457
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:430
std::map< std::string, std::vector< MSRailSignalConstraint * > > myInsertionConstraints
Definition: MSRailSignal.h:440
static VehicleVector myPriorityVehicles
Definition: MSRailSignal.h:458
int myPhaseIndex
MSTrafficLightLogic requires that the phase index changes whenever signals change their state.
Definition: MSRailSignal.h:436
int getPhaseNumber() const
Returns the number of phases.
static std::string myConstraintInfo
Definition: MSRailSignal.h:459
MSPhaseDefinition myCurrentPhase
The current phase.
Definition: MSRailSignal.h:433
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:400
std::string getPriorityVehicleIDs() const
static int myDriveWayIndex
Definition: MSRailSignal.h:447
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:456
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
std::map< std::string, std::vector< MSRailSignalConstraint * > > myConstraints
map from tripId to constraint list
Definition: MSRailSignal.h:439
static std::map< std::pair< int, int >, bool > myDriveWayCompatibility
Definition: MSRailSignal.h:446
std::pair< const SUMOVehicle *const, const MSLink::ApproachingVehicleInformation > Approaching
Definition: MSRailSignal.h:242
SUMOTime trySwitch()
Switches to the next phase.
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:243
static std::string getJunctionLinkID(MSLink *link)
return junctionID_junctionLinkIndex
static int myNumWarnings
Definition: MSRailSignal.h:442
const DriveWay & retrieveDriveWay(int numericalID) const
static bool myStoreVehicles
Definition: MSRailSignal.h:455
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:244
static std::vector< std::pair< MSLink *, int > > mySwitchedGreenFlanks
list of signals that switched green along with driveway index
Definition: MSRailSignal.h:445
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.
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)
Changes the current phase and her duration.
Definition: MSRailSignal.h:194
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 hasInsertionConstraint(MSLink *link, const MSVehicle *veh)
static bool hasOncomingRailTraffic(MSLink *link)
std::string getRivalVehicleIDs() const
int getCurrentPhaseIndex() const
Returns the current index within the program.
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:77
Builds detectors for microsim.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:60
Representation of a vehicle.
Definition: SUMOVehicle.h:58
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:268
std::vector< MSLane * > myBidiExtended
Definition: MSRailSignal.h:293
std::vector< MSLink * > myFlankSwitches
Definition: MSRailSignal.h:304
int myCoreSize
number of edges in myRoute where overlap with other driveways is forbidden
Definition: MSRailSignal.h:280
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:274
std::vector< const MSLane * > myConflictLanes
the lanes that must be clear of trains before this signal can switch to green
Definition: MSRailSignal.h:300
bool overlap(const DriveWay &other) const
Wether this driveway (route) overlaps with the given one.
int myNumericalID
global driveway index
Definition: MSRailSignal.h:265
std::vector< MSLink * > myConflictLinks
Definition: MSRailSignal.h:315
void checkCrossingFlanks(MSLink *dwLink, const LaneVisitedMap &visited)
find links that cross the driveway without entering it
std::vector< MSLane * > myBidi
Definition: MSRailSignal.h:289
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:297
DriveWay(bool temporary=false)
Constructor.
Definition: MSRailSignal.h:256
std::vector< const MSEdge * > myRoute
list of edges for matching against train routes
Definition: MSRailSignal.h:277
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:310
std::vector< MSLane * > myForward
Definition: MSRailSignal.h:284
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:271
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.