Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2025 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 : /****************************************************************************/
14 : /// @file MSRailSignalControl.cpp
15 : /// @author Jakob Erdmann
16 : /// @date Sept 2020
17 : ///
18 : // Centralized services for rail signal control (Singleton)
19 : /****************************************************************************/
20 : #include <config.h>
21 :
22 : #include <cassert>
23 : #include <utility>
24 : #include <vector>
25 : #include <bitset>
26 : #include <microsim/MSNet.h>
27 : #include <microsim/MSRoute.h>
28 : #include <microsim/MSEdge.h>
29 : #include <microsim/MSLane.h>
30 : #include "MSRailSignal.h"
31 : #include "MSRailSignalConstraint.h"
32 : #include "MSDriveWay.h"
33 : #include "MSRailSignalControl.h"
34 :
35 :
36 : //#define DEBUG_BUILD_DEADLOCK_CHECK
37 :
38 : // ===========================================================================
39 : // static value definitions
40 : // ===========================================================================
41 : MSRailSignalControl* MSRailSignalControl::myInstance(nullptr);
42 : SVCPermissions MSRailSignalControl::mySignalizedClasses(SVC_UNSPECIFIED);
43 :
44 : // ===========================================================================
45 : // method definitions
46 : // ===========================================================================
47 1176 : MSRailSignalControl::MSRailSignalControl()
48 1176 : {}
49 :
50 : MSRailSignalControl&
51 15840564 : MSRailSignalControl::getInstance() {
52 15840564 : if (myInstance == nullptr) {
53 1176 : myInstance = new MSRailSignalControl();
54 1176 : MSNet::getInstance()->addVehicleStateListener(myInstance);
55 : }
56 15840564 : return *myInstance;
57 : }
58 :
59 : void
60 40047 : MSRailSignalControl::cleanup() {
61 40047 : delete myInstance;
62 40047 : myInstance = nullptr;
63 40047 : }
64 :
65 : void
66 187 : MSRailSignalControl::clearState() {
67 187 : if (myInstance != nullptr) {
68 : myInstance->myDriveWayCompatibility.clear();
69 10 : myInstance->myDriveWaySucc.clear();
70 10 : myInstance->myDriveWayPred.clear();
71 10 : myInstance->myWrittenDeadlocks.clear();
72 10 : myInstance->myDeadlockChecks.clear();
73 : //myInstance->myActiveSignals.clear();
74 : }
75 187 : }
76 :
77 :
78 2352 : MSRailSignalControl::~MSRailSignalControl() {
79 2352 : }
80 :
81 : void
82 16299 : MSRailSignalControl::vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to, const std::string& /*info*/) {
83 16299 : if (vehicle->isRail()) {
84 : std::string dummyMsg;
85 18047 : if ((to == MSNet::VehicleState::BUILT && (!vehicle->getParameter().wasSet(VEHPARS_FORCE_REROUTE) || vehicle->hasValidRoute(dummyMsg)))
86 15967 : || to == MSNet::VehicleState::NEWROUTE) {
87 : // @note we could delay initialization until the departure time
88 3499 : if (vehicle->getEdge()->getFunction() != SumoXMLEdgeFunc::CONNECTOR && isRailwayOrShared(vehicle->getEdge()->getPermissions())) {
89 3494 : MSRailSignal::initDriveWays(vehicle, to == MSNet::VehicleState::NEWROUTE);
90 : }
91 : }
92 : }
93 16299 : }
94 :
95 :
96 : void
97 4656 : MSRailSignalControl::addSignal(MSRailSignal* signal) {
98 4656 : mySignals.push_back(signal);
99 10574 : for (const auto& links : signal->getLinks()) {
100 11836 : for (const MSLink* link : links) {
101 5918 : mySignalizedClasses |= link->getPermissions();
102 : }
103 : }
104 4656 : }
105 :
106 :
107 : void
108 151190 : MSRailSignalControl::addWaitRelation(const SUMOVehicle* waits, const MSRailSignal* rs, const SUMOVehicle* reason, MSRailSignalConstraint* constraint) {
109 : //std::cout << time2string(SIMSTEP) << " addWaitRelation waits=" << waits->getID() << " foe=" << reason->getID() << "\n";
110 151190 : myWaitRelations[waits] = WaitRelation(rs, reason, constraint);
111 151190 : }
112 :
113 :
114 : bool
115 13241 : MSRailSignalControl::haveDeadlock(const SUMOVehicle* veh) const {
116 : std::set<const SUMOVehicle*> seen;
117 : std::vector<WaitRelation> list;
118 13241 : const SUMOVehicle* cur = veh;
119 : std::vector<MSRailSignalConstraint*> constraints;
120 : std::vector<const SUMOVehicle*> constraintBlocked;
121 : std::vector<const MSRailSignal*> constraintSignals;
122 : //std::cout << time2string(SIMSTEP) << " haveDeadlock veh=" << veh->getID() << "\n";
123 : while (seen.count(cur) == 0) {
124 : auto it = myWaitRelations.find(cur);
125 26101 : if (it != myWaitRelations.end()) {
126 12914 : if (it->second.constraint != nullptr) {
127 66 : constraints.push_back(it->second.constraint);
128 66 : constraintBlocked.push_back(cur);
129 66 : constraintSignals.push_back(it->second.railSignal);
130 : }
131 : seen.insert(cur);
132 12914 : list.push_back(it->second);
133 12914 : cur = it->second.foe;
134 : } else {
135 : return false;
136 : }
137 : }
138 54 : if (cur == veh) {
139 : const bool newDeadlock = myWrittenDeadlocks.count(seen) == 0;
140 : myWrittenDeadlocks.insert(seen);
141 54 : const OptionsCont& oc = OptionsCont::getOptions();
142 : MSRailSignalConstraint* resolved = nullptr;
143 : const SUMOVehicle* resolvedUnblocked = nullptr;
144 : const MSRailSignal* resolvedSignal = nullptr;
145 90 : if (!constraints.empty() && oc.getBool("time-to-teleport.remove-constraint")) {
146 21 : resolved = constraints.front();
147 21 : if (newDeadlock) {
148 : std::vector<std::string> vehicles;
149 57 : for (auto item : list) {
150 38 : vehicles.push_back(item.foe->getID());
151 : }
152 38 : WRITE_WARNINGF("Deactivating constraint to resolve deadlock between vehicles % at time %.", toString(vehicles), time2string(SIMSTEP));
153 19 : resolved->setActive(false);
154 19 : resolvedUnblocked = constraintBlocked.front();
155 19 : resolvedSignal = constraintSignals.front();
156 19 : }
157 : }
158 :
159 108 : if (oc.isSet("deadlock-output")) {
160 54 : if (newDeadlock) {
161 : myWrittenDeadlocks.insert(seen);
162 : std::vector<std::string> signals;
163 : std::vector<std::string> vehicles;
164 : std::vector<std::string> tripIDs;
165 154 : for (auto item : list) {
166 318 : signals.push_back(item.railSignal == nullptr ? "INSERTION" : item.railSignal->getID());
167 110 : vehicles.push_back(item.foe->getID());
168 330 : tripIDs.push_back(item.foe->getParameter().getParameter("tripId", item.foe->getID()));
169 : }
170 88 : OutputDevice& od = OutputDevice::getDeviceByOption("deadlock-output");
171 44 : if (constraints.empty()) {
172 10 : od.openTag(SUMO_TAG_DEADLOCK);
173 : } else {
174 68 : od.openTag("constraintDeadlock");
175 : }
176 44 : od.writeAttr(SUMO_ATTR_TIME, time2string(SIMSTEP));
177 44 : od.writeAttr(SUMO_ATTR_SIGNALS, signals);
178 88 : od.writeAttr("vehicles", vehicles);
179 44 : if (!constraints.empty()) {
180 68 : od.writeAttr("tripIds", tripIDs);
181 : }
182 44 : if (resolved != nullptr) {
183 38 : od.openTag("resolvedConstraint");
184 19 : od.writeAttr(SUMO_ATTR_ID, resolvedSignal->getID());
185 57 : resolved->write(od, resolvedUnblocked->getParameter().getParameter("tripId", resolvedUnblocked->getID()));
186 38 : od.closeTag();
187 : }
188 44 : od.closeTag();
189 :
190 44 : }
191 : }
192 54 : return resolved == nullptr;
193 : } else {
194 : // it's a deadlock but does not involve veh
195 : return false;
196 : }
197 13241 : }
198 :
199 :
200 : void
201 15 : MSRailSignalControl::addDeadlockCheck(std::vector<const MSRailSignal*> signals) {
202 15 : if (MSDriveWay::haveDriveWays()) {
203 0 : WRITE_WARNING("Deadlocks should be loaded before any vehicles");
204 : }
205 15 : const int n = (int)signals.size();
206 78 : for (int i = 0; i < n; i++) {
207 : std::vector<const MSRailSignal*> others;
208 342 : for (int j = 0; j < n; j++) {
209 279 : others.push_back(signals[(i + j + 1) % n]);
210 : }
211 63 : myDeadlockChecks[signals[i]] = others;
212 63 : }
213 15 : }
214 :
215 : void
216 33107 : MSRailSignalControl::addDrivewayFollower(const MSDriveWay* dw, const MSDriveWay* dw2) {
217 : //std::cout << " addDrivewayFollower " << dw->getID() << " " << dw2->getID() << "\n";
218 33107 : myDriveWaySucc[dw].insert(dw2);
219 33107 : myDriveWayPred[dw2].insert(dw);
220 33107 : }
221 :
222 :
223 : void
224 33107 : MSRailSignalControl::addDWDeadlockChecks(const MSRailSignal* rs, MSDriveWay* dw) {
225 33107 : auto itDL = MSRailSignalControl::getInstance().getDeadlockChecks().find(rs);
226 33107 : if (itDL == MSRailSignalControl::getInstance().getDeadlockChecks().end()) {
227 32987 : return;
228 : }
229 120 : const std::vector<const MSRailSignal*>& others = itDL->second;
230 : // the driveway could be part of a deadlock. check whether it would be blocked by the next signal in the circle
231 : std::vector<const MSDriveWay*> deadlockFoes;
232 120 : findDeadlockFoes(dw, others, deadlockFoes);
233 120 : }
234 :
235 :
236 : void
237 767 : MSRailSignalControl::findDeadlockFoes(const MSDriveWay* dw, const std::vector<const MSRailSignal*>& others, std::vector<const MSDriveWay*> deadlockFoes) {
238 : #ifdef DEBUG_BUILD_DEADLOCK_CHECK
239 : //std::cout << " findDLfoes dw=" << dw->getID() << " dlFoes=" << toString(deadlockFoes) << "\n";
240 : #endif
241 767 : int circleIndex = (int)deadlockFoes.size();
242 767 : if (circleIndex < (int)others.size()) {
243 580 : const MSRailSignal* other = others[circleIndex];
244 580 : deadlockFoes.push_back(dw);
245 1080 : for (const MSDriveWay* follower : myDriveWaySucc[dw]) {
246 2059 : for (MSDriveWay* foe : follower->getFoes()) {
247 1559 : if (foe->getForward().back()->getEdge().getToJunction()->getID() == other->getID()) {
248 647 : findDeadlockFoes(foe, others, deadlockFoes);
249 : }
250 : }
251 : }
252 : } else {
253 : #ifdef DEBUG_BUILD_DEADLOCK_CHECK
254 : std::cout << " add deadlock check foes=" << toString(deadlockFoes) << "\n";;
255 : #endif
256 895 : for (const MSDriveWay* dldw : deadlockFoes) {
257 708 : if (dldw->isDepartDriveway()) {
258 240 : const_cast<MSDriveWay*>(dldw)->addDWDeadlock(deadlockFoes);
259 : } else {
260 903 : for (const MSDriveWay* pred : myDriveWayPred[dldw]) {
261 435 : if (!pred->isDepartDriveway()) {
262 120 : const MSRailSignal* predRS = dynamic_cast<const MSRailSignal*>(pred->getOrigin()->getTLLogic());
263 120 : if (std::find(others.begin(), others.end(), predRS) != others.end()) {
264 : // driveways that participate in the deadlock don't need to
265 : // do checking since they cannot prevent the deadlock
266 : // (unless they are departDriveways)
267 63 : continue;
268 : }
269 57 : const_cast<MSDriveWay*>(pred)->addDWDeadlock(deadlockFoes);
270 : }
271 : }
272 : }
273 : }
274 : }
275 767 : }
276 :
277 :
278 : void
279 636174 : MSRailSignalControl::notifyApproach(const MSLink* link) {
280 636174 : const MSRailSignal* rs = dynamic_cast<const MSRailSignal*>(link->getTLLogic());
281 : assert(rs != nullptr);
282 636174 : myActiveSignals.insert(const_cast<MSRailSignal*>(rs));
283 636174 : }
284 :
285 :
286 : void
287 7451272 : MSRailSignalControl::updateSignals(SUMOTime t) {
288 : UNUSED_PARAMETER(t);
289 : // there are 4 states for a signal
290 : // 1. approached and green
291 : // 2. approached and red
292 : // 3. not approached and trains could pass
293 : // 4. not approached and trains coult not pass
294 : //
295 : // for understanding conflicts better in sumo-gui, we want to show (3) as green. This
296 : // means we have to keep updating signals in state (4) until they change to (3)
297 :
298 : //std::cout << SIMTIME << " activeSignals=" << myActiveSignals.size() << "\n";
299 22726003 : for (auto it = myActiveSignals.begin(); it != myActiveSignals.end();) {
300 15274731 : MSRailSignal* rs = *it;
301 : //std::cout << SIMTIME << " update " << rs->getID() << "\n";
302 15274731 : const bool keepActive = rs->updateCurrentPhase();
303 15274731 : if (rs->isActive()) {
304 15274716 : rs->setTrafficLightSignals(t);
305 : }
306 15274731 : if (!keepActive) {
307 : it = myActiveSignals.erase(it);
308 : } else {
309 : it++;
310 : }
311 : }
312 7451272 : }
313 :
314 :
315 : /****************************************************************************/
|