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// ===========================================================================
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
253 static void recheckGreen();
254
255protected:
257 bool constraintsAllow(const SUMOVehicle* veh) const;
258
259protected:
260
261 typedef std::pair<const SUMOVehicle* const, const MSLink::ApproachingVehicleInformation> Approaching;
262 typedef std::set<const MSLane*, ComparatorNumericalIdLess> LaneSet;
263 typedef std::map<const MSLane*, int, ComparatorNumericalIdLess> LaneVisitedMap;
264
265 /* The driveways (Fahrstrassen) for each link index
266 * Each link index has at least one driveway
267 * A driveway describes one possible route that passes the signal up
268 * the next secure point
269 * When a signal guards a switch (indirect guard) that signal stores two
270 * or more driveways
271 */
272 struct DriveWay {
273
275 DriveWay(bool temporary = false) :
276 myNumericalID(temporary ? -1 : myDriveWayIndex++),
278 myActive(nullptr),
279 myProtectedBidi(nullptr),
280 myCoreSize(0)
281 {}
282
285
288
291
294
296 std::vector<const MSEdge*> myRoute;
297
300
301 /* @brief the actual driveway part up to the next railsignal (halting position)
302 * This must be free of other trains */
303 std::vector<MSLane*> myForward;
304
305 /* @brief the list of bidirectional edges that can enter the forward
306 * section and which must also be free of traffic
307 * (up to the first element that could give protection) */
308 std::vector<MSLane*> myBidi;
309
310 /* @brief the list of bidirectional edges that can enter the forward
311 * section and which might contain deadlock-relevant traffic */
312 std::vector<MSLane*> myBidiExtended;
313
314 /* @brief the list of edges that merge with the forward section
315 * (found via backward search, up to the first element that could give protection) */
316 std::vector<const MSLane*> myFlank;
317
319 std::vector<const MSLane*> myConflictLanes;
320
321 /* @brief the list of switches that threaten the driveway and for which protection must be found
322 */
323 std::vector<MSLink*> myFlankSwitches;
324
325 /* @brief the list of (first) switches that could give protection from oncoming/flanking vehicles
326 * if any of them fails to do so, upstream search must be performed
327 * until protection or conflict is found
328 */
329 std::vector<MSLink*> myProtectingSwitches;
331 std::vector<MSLink*> myProtectingSwitchesBidi;
332
333 /* The conflict links for this block
334 * Conflict resolution must be performed if vehicles are approaching the
335 * current link and any of the conflict links */
336 std::vector<MSLink*> myConflictLinks;
337
339 bool conflictLaneOccupied(const std::string& joinVehicle = "", bool store = true) const;
340
342 bool deadlockLaneOccupied(bool store = true) const;
343
345 bool reserve(const Approaching& closest, MSEdgeVector& occupied);
346
348 bool hasLinkConflict(const Approaching& closest, MSLink* foeLink) const;
349
351 static bool mustYield(const Approaching& veh, const Approaching& foe);
352
354 bool conflictLinkApproached() const;
355
357 bool findProtection(const Approaching& veh, MSLink* link) const;
358
360 bool overlap(const DriveWay& other) const;
361
363 bool flankConflict(const DriveWay& other) const;
364
366 void writeBlocks(OutputDevice& od) const;
367
368 /* @brief determine route that identifies this driveway (a subset of the
369 * vehicle route)
370 * collects:
371 * myRoute
372 * myForward
373 * myBidi
374 * myProtectedBidi
375 *
376 * returns edge that is assumed to safe from oncoming-deadlock or nullptr
377 */
378 void buildRoute(MSLink* origin, double length, MSRouteIterator next, MSRouteIterator end, LaneVisitedMap& visited);
379
381 void checkFlanks(const std::vector<MSLane*>& lanes, const LaneVisitedMap& visited, bool allFoes);
382
384 void checkCrossingFlanks(MSLink* dwLink, const LaneVisitedMap& visited);
385
387 void findFlankProtection(MSLink* link, double length, LaneVisitedMap& visited, MSLink* origLink);
388 };
389
390 /* The driveways for each link
391 */
392 struct LinkInfo {
394 LinkInfo(MSLink* link);
395
397
400
402 std::vector<DriveWay> myDriveways;
403
405 std::string getID() const;
406
409
412
414 void reroute(SUMOVehicle* veh, const MSEdgeVector& occupied);
415
417 void reset();
418
421 };
422
424 std::vector<LinkInfo> myLinkInfos;
425
426 /* @brief retrieve driveway with the given numerical id
427 * @note: throws exception if the driveway does not exist at this rail signal */
428 const DriveWay& retrieveDriveWay(int numericalID) const;
429
431 static Approaching getClosest(MSLink* link);
432
434 static std::string getTLLinkID(MSLink* link);
435
437 static std::string getJunctionLinkID(MSLink* link);
438
440 static std::string getClickableTLLinkID(MSLink* link);
441
443 static std::string describeLinks(std::vector<MSLink*> links);
444
446 static std::string formatVisitedMap(const LaneVisitedMap& visited);
447
449 static void appendMapIndex(LaneVisitedMap& map, const MSLane* lane);
450
451protected:
452
458
461
464
467
469 std::map<std::string, std::vector<MSRailSignalConstraint*> > myConstraints;
470
471 static int myNumWarnings;
472
474 static std::vector<std::pair<MSLink*, int> > mySwitchedGreenFlanks;
475 static std::map<std::pair<int, int>, bool> myDriveWayCompatibility;
476 static int myDriveWayIndex;
477
478protected:
480 void storeTraCIVehicles(int linkIndex);
481
483
484 static bool myStoreVehicles;
488 static std::string myConstraintInfo;
490
491
492};
long long int SUMOTime
Definition: GUI.h:36
std::vector< MSEdge * > MSEdgeVector
Definition: MSEdge.h:73
ConstMSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:54
#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:486
std::string getBlockingVehicleIDs() const
Phases myPhases
The list of phases this logic uses.
Definition: MSRailSignal.h:457
static VehicleVector myPriorityVehicles
Definition: MSRailSignal.h:487
int myPhaseIndex
MSTrafficLightLogic requires that the phase index changes whenever signals change their state.
Definition: MSRailSignal.h:463
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:488
MSPhaseDefinition myCurrentPhase
The current phase.
Definition: MSRailSignal.h:460
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:424
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:476
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:485
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:469
static std::map< std::pair< int, int >, bool > myDriveWayCompatibility
Definition: MSRailSignal.h:475
std::pair< const SUMOVehicle *const, const MSLink::ApproachingVehicleInformation > Approaching
Definition: MSRailSignal.h:261
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:262
static std::string getJunctionLinkID(MSLink *link)
return junctionID_junctionLinkIndex
static int myNumWarnings
Definition: MSRailSignal.h:471
SUMOTime trySwitch() override
Switches to the next phase.
const DriveWay & retrieveDriveWay(int numericalID) const
static bool myStoreVehicles
Definition: MSRailSignal.h:484
std::map< const MSLane *, int, ComparatorNumericalIdLess > LaneVisitedMap
Definition: MSRailSignal.h:263
bool myMovingBlock
whether the signal is in moving block mode (only protects from oncoming and flanking trains)
Definition: MSRailSignal.h:466
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:474
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
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
void checkFlanks(const std::vector< MSLane * > &lanes, const LaneVisitedMap &visited, bool allFoes)
find switches that threaten 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:287
std::vector< MSLane * > myBidiExtended
Definition: MSRailSignal.h:312
std::vector< MSLink * > myFlankSwitches
Definition: MSRailSignal.h:323
int myCoreSize
number of edges in myRoute where overlap with other driveways is forbidden
Definition: MSRailSignal.h:299
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:293
std::vector< MSLink * > myProtectingSwitchesBidi
subset of myProtectingSwitches that protects from oncoming trains
Definition: MSRailSignal.h:331
std::vector< const MSLane * > myConflictLanes
the lanes that must be clear of trains before this signal can switch to green
Definition: MSRailSignal.h:319
bool overlap(const DriveWay &other) const
Wether this driveway (route) overlaps with the given one.
int myNumericalID
global driveway index
Definition: MSRailSignal.h:284
std::vector< MSLink * > myConflictLinks
Definition: MSRailSignal.h:336
void checkCrossingFlanks(MSLink *dwLink, const LaneVisitedMap &visited)
find links that cross the driveway without entering it
std::vector< MSLane * > myBidi
Definition: MSRailSignal.h:308
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:316
DriveWay(bool temporary=false)
Constructor.
Definition: MSRailSignal.h:275
std::vector< const MSEdge * > myRoute
list of edges for matching against train routes
Definition: MSRailSignal.h:296
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:329
std::vector< MSLane * > myForward
Definition: MSRailSignal.h:303
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:290
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.