Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2026 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 RONet.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Jakob Erdmann
17 : /// @author Michael Behrisch
18 : /// @date Sept 2002
19 : ///
20 : // The router's network representation
21 : /****************************************************************************/
22 : #include <config.h>
23 :
24 : #include <algorithm>
25 : #include <utils/router/RouteCostCalculator.h>
26 : #include <utils/vehicle/SUMOVTypeParameter.h>
27 : #include <utils/router/SUMOAbstractRouter.h>
28 : #include <utils/options/OptionsCont.h>
29 : #include <utils/common/UtilExceptions.h>
30 : #include <utils/common/ToString.h>
31 : #include <utils/common/StringUtils.h>
32 : #include <utils/common/RandHelper.h>
33 : #include <utils/common/SUMOVehicleClass.h>
34 : #include <utils/iodevices/OutputDevice.h>
35 : #include "ROEdge.h"
36 : #include "ROLane.h"
37 : #include "RONode.h"
38 : #include "ROPerson.h"
39 : #include "RORoute.h"
40 : #include "RORouteDef.h"
41 : #include "ROVehicle.h"
42 : #include "ROAbstractEdgeBuilder.h"
43 : #include "RONet.h"
44 :
45 :
46 : // ===========================================================================
47 : // static member definitions
48 : // ===========================================================================
49 : RONet* RONet::myInstance = nullptr;
50 :
51 :
52 : // ===========================================================================
53 : // method definitions
54 : // ===========================================================================
55 : RONet*
56 5902811 : RONet::getInstance(void) {
57 5902811 : if (myInstance != nullptr) {
58 5902811 : return myInstance;
59 : }
60 0 : throw ProcessError(TL("A network was not yet constructed."));
61 : }
62 :
63 :
64 4312 : RONet::RONet() :
65 4312 : myVehicleTypes(), myDefaultVTypeMayBeDeleted(true),
66 4312 : myDefaultPedTypeMayBeDeleted(true),
67 4312 : myDefaultBikeTypeMayBeDeleted(true),
68 4312 : myDefaultTaxiTypeMayBeDeleted(true),
69 4312 : myDefaultRailTypeMayBeDeleted(true),
70 4312 : myHaveActiveFlows(true),
71 4312 : myRoutesOutput(nullptr), myRouteAlternativesOutput(nullptr), myTypesOutput(nullptr),
72 4312 : myReadRouteNo(0), myDiscardedRouteNo(0), myWrittenRouteNo(0),
73 4312 : myHavePermissions(false),
74 4312 : myHaveParamRestrictions(false),
75 4312 : myNumInternalEdges(0),
76 4312 : myErrorHandler(OptionsCont::getOptions().exists("ignore-errors")
77 8177 : && OptionsCont::getOptions().getBool("ignore-errors") ? MsgHandler::getWarningInstance() : MsgHandler::getErrorInstance()),
78 4312 : myKeepVTypeDist(OptionsCont::getOptions().exists("keep-vtype-distributions")
79 8177 : && OptionsCont::getOptions().getBool("keep-vtype-distributions")),
80 4312 : myDoPTRouting(!OptionsCont::getOptions().exists("ptline-routing")
81 7711 : || OptionsCont::getOptions().getBool("ptline-routing")),
82 4312 : myKeepFlows(OptionsCont::getOptions().exists("keep-flows")
83 7711 : && OptionsCont::getOptions().getBool("keep-flows")),
84 4312 : myHasBidiEdges(false),
85 15422 : myMaxTraveltime(OptionsCont::getOptions().exists("max-traveltime") ? STEPS2TIME(string2time(OptionsCont::getOptions().getString("max-traveltime"))) : -1) {
86 4312 : if (myInstance != nullptr) {
87 0 : throw ProcessError(TL("A network was already constructed."));
88 : }
89 4312 : SUMOVTypeParameter* type = new SUMOVTypeParameter(DEFAULT_VTYPE_ID, SVC_PASSENGER);
90 4312 : type->onlyReferenced = true;
91 4312 : myVehicleTypes.add(type->id, type);
92 :
93 4312 : SUMOVTypeParameter* defPedType = new SUMOVTypeParameter(DEFAULT_PEDTYPE_ID, SVC_PEDESTRIAN);
94 4312 : defPedType->onlyReferenced = true;
95 4312 : defPedType->parametersSet |= VTYPEPARS_VEHICLECLASS_SET;
96 4312 : myVehicleTypes.add(defPedType->id, defPedType);
97 :
98 4312 : SUMOVTypeParameter* defBikeType = new SUMOVTypeParameter(DEFAULT_BIKETYPE_ID, SVC_BICYCLE);
99 4312 : defBikeType->onlyReferenced = true;
100 4312 : defBikeType->parametersSet |= VTYPEPARS_VEHICLECLASS_SET;
101 4312 : myVehicleTypes.add(defBikeType->id, defBikeType);
102 :
103 4312 : SUMOVTypeParameter* defTaxiType = new SUMOVTypeParameter(DEFAULT_TAXITYPE_ID, SVC_TAXI);
104 4312 : defTaxiType->onlyReferenced = true;
105 4312 : defTaxiType->parametersSet |= VTYPEPARS_VEHICLECLASS_SET;
106 4312 : myVehicleTypes.add(defTaxiType->id, defTaxiType);
107 :
108 4312 : SUMOVTypeParameter* defRailType = new SUMOVTypeParameter(DEFAULT_RAILTYPE_ID, SVC_RAIL);
109 4312 : defRailType->onlyReferenced = true;
110 4312 : defRailType->parametersSet |= VTYPEPARS_VEHICLECLASS_SET;
111 4312 : myVehicleTypes.add(defRailType->id, defRailType);
112 :
113 4312 : myInstance = this;
114 4312 : }
115 :
116 :
117 8167 : RONet::~RONet() {
118 4515 : for (const auto& routables : myRoutables) {
119 426 : for (RORoutable* const r : routables.second) {
120 213 : const ROVehicle* const veh = dynamic_cast<const ROVehicle*>(r);
121 : // delete routes and the vehicle
122 213 : if (veh != nullptr && veh->getRouteDefinition()->getID()[0] == '!') {
123 204 : if (!myRoutes.remove(veh->getRouteDefinition()->getID())) {
124 76 : delete veh->getRouteDefinition();
125 : }
126 : }
127 213 : delete r;
128 : }
129 : }
130 4338 : for (const RORoutable* const r : myPTVehicles) {
131 36 : const ROVehicle* const veh = dynamic_cast<const ROVehicle*>(r);
132 : // delete routes and the vehicle
133 36 : if (veh != nullptr && veh->getRouteDefinition()->getID()[0] == '!') {
134 36 : if (!myRoutes.remove(veh->getRouteDefinition()->getID())) {
135 8 : delete veh->getRouteDefinition();
136 : }
137 : }
138 36 : delete r;
139 : }
140 : myRoutables.clear();
141 4346 : for (const auto& vTypeDist : myVTypeDistDict) {
142 88 : delete vTypeDist.second;
143 : }
144 29677 : }
145 :
146 :
147 : void
148 17 : RONet::addSpeedRestriction(const std::string& id, const SUMOVehicleClass svc, const double speed) {
149 17 : mySpeedRestrictions[id][svc] = speed;
150 17 : }
151 :
152 :
153 : double
154 1286 : RONet::getPreference(const std::string& routingType, const SUMOVTypeParameter& pars) const {
155 1286 : if (gRoutingPreferences) {
156 1286 : auto it = myVTypePreferences.find(pars.id);
157 1286 : if (it != myVTypePreferences.end()) {
158 : auto it2 = it->second.find(routingType);
159 86 : if (it2 != it->second.end()) {
160 22 : return it2->second;
161 : }
162 : }
163 : auto it3 = myVClassPreferences.find(pars.vehicleClass);
164 1264 : if (it3 != myVClassPreferences.end()) {
165 : auto it4 = it3->second.find(routingType);
166 176 : if (it4 != it3->second.end()) {
167 47 : return it4->second;
168 : }
169 : }
170 : // fallback to generel preferences
171 1217 : it = myVTypePreferences.find("");
172 1217 : if (it != myVTypePreferences.end()) {
173 : auto it2 = it->second.find(routingType);
174 774 : if (it2 != it->second.end()) {
175 140 : return it2->second;
176 : }
177 : }
178 : }
179 : return 1;
180 : }
181 :
182 :
183 : void
184 24 : RONet::addPreference(const std::string& routingType, SUMOVehicleClass svc, double prio) {
185 24 : myVClassPreferences[svc][routingType] = prio;
186 24 : gRoutingPreferences = true;
187 24 : }
188 :
189 :
190 : void
191 32 : RONet::addPreference(const std::string& routingType, std::string vType, double prio) {
192 32 : myVTypePreferences[vType][routingType] = prio;
193 32 : gRoutingPreferences = true;
194 32 : }
195 :
196 :
197 :
198 : const std::map<SUMOVehicleClass, double>*
199 66 : RONet::getRestrictions(const std::string& id) const {
200 : std::map<std::string, std::map<SUMOVehicleClass, double> >::const_iterator i = mySpeedRestrictions.find(id);
201 66 : if (i == mySpeedRestrictions.end()) {
202 : return nullptr;
203 : }
204 26 : return &i->second;
205 : }
206 :
207 :
208 : bool
209 329257 : RONet::addEdge(ROEdge* edge) {
210 329257 : if (!myEdges.add(edge->getID(), edge)) {
211 36 : WRITE_ERRORF(TL("The edge '%' occurs at least twice."), edge->getID());
212 12 : delete edge;
213 12 : return false;
214 : }
215 329245 : if (edge->isInternal()) {
216 161204 : myNumInternalEdges += 1;
217 : }
218 : return true;
219 : }
220 :
221 :
222 : bool
223 1818 : RONet::addDistrict(const std::string id, ROEdge* source, ROEdge* sink) {
224 : if (myDistricts.count(id) > 0) {
225 0 : WRITE_ERRORF(TL("The TAZ '%' occurs at least twice."), id);
226 0 : delete source;
227 0 : delete sink;
228 0 : return false;
229 : }
230 : sink->setFunction(SumoXMLEdgeFunc::CONNECTOR);
231 1818 : if (!addEdge(sink)) {
232 : return false;
233 : }
234 : source->setFunction(SumoXMLEdgeFunc::CONNECTOR);
235 1818 : if (!addEdge(source)) {
236 : return false;
237 : }
238 : sink->setOtherTazConnector(source);
239 : source->setOtherTazConnector(sink);
240 3636 : myDistricts[id] = std::make_pair(std::vector<std::string>(), std::vector<std::string>());
241 1818 : return true;
242 : }
243 :
244 :
245 : bool
246 1383 : RONet::addDistrictEdge(const std::string tazID, const std::string edgeID, const bool isSource) {
247 : if (myDistricts.count(tazID) == 0) {
248 0 : WRITE_ERRORF(TL("The TAZ '%' is unknown."), tazID);
249 0 : return false;
250 : }
251 : ROEdge* edge = getEdge(edgeID);
252 1383 : if (edge == nullptr) {
253 0 : WRITE_ERRORF(TL("The edge '%' for TAZ '%' is unknown."), edgeID, tazID);
254 0 : return false;
255 : }
256 1383 : if (isSource) {
257 2073 : getEdge(tazID + "-source")->addSuccessor(edge);
258 691 : myDistricts[tazID].first.push_back(edgeID);
259 : } else {
260 2076 : edge->addSuccessor(getEdge(tazID + "-sink"));
261 692 : myDistricts[tazID].second.push_back(edgeID);
262 : }
263 : return true;
264 : }
265 :
266 :
267 : void
268 101 : RONet::addJunctionTaz(ROAbstractEdgeBuilder& eb) {
269 1544 : for (auto item : myNodes) {
270 : const std::string tazID = item.first;
271 8 : if (myDistricts.count(tazID) != 0) {
272 24 : WRITE_WARNINGF(TL("A TAZ with id '%' already exists. Not building junction TAZ."), tazID);
273 8 : continue;
274 : }
275 1435 : const std::string sourceID = tazID + "-source";
276 1435 : const std::string sinkID = tazID + "-sink";
277 : // sink must be added before source
278 2870 : ROEdge* sink = eb.buildEdge(sinkID, nullptr, nullptr, 0, "", "");
279 2870 : ROEdge* source = eb.buildEdge(sourceID, nullptr, nullptr, 0, "", "");
280 : sink->setOtherTazConnector(source);
281 : source->setOtherTazConnector(sink);
282 2870 : if (!addDistrict(tazID, source, sink)) {
283 : continue;
284 : }
285 1435 : auto& district = myDistricts[tazID];
286 1435 : const RONode* junction = item.second;
287 12452 : for (const ROEdge* edge : junction->getIncoming()) {
288 11017 : if (!edge->isInternal()) {
289 3129 : const_cast<ROEdge*>(edge)->addSuccessor(sink);
290 3129 : district.second.push_back(edge->getID());
291 : }
292 : }
293 12452 : for (const ROEdge* edge : junction->getOutgoing()) {
294 11017 : if (!edge->isInternal()) {
295 3129 : source->addSuccessor(const_cast<ROEdge*>(edge));
296 3129 : district.first.push_back(edge->getID());
297 : }
298 : }
299 : }
300 101 : }
301 :
302 :
303 : void
304 3581 : RONet::setBidiEdges(const std::map<ROEdge*, std::string>& bidiMap) {
305 15609 : for (const auto& item : bidiMap) {
306 12028 : ROEdge* bidi = myEdges.get(item.second);
307 12028 : if (bidi == nullptr) {
308 0 : WRITE_ERRORF(TL("The bidi edge '%' is not known."), item.second);
309 : }
310 12028 : item.first->setBidiEdge(bidi);
311 12028 : myHasBidiEdges = true;
312 : }
313 3581 : }
314 :
315 :
316 : void
317 73684 : RONet::addNode(RONode* node) {
318 73684 : if (!myNodes.add(node->getID(), node)) {
319 0 : WRITE_ERRORF(TL("The node '%' occurs at least twice."), node->getID());
320 0 : delete node;
321 : }
322 73684 : }
323 :
324 :
325 : void
326 1968 : RONet::addStoppingPlace(const std::string& id, const SumoXMLTag category, SUMOVehicleParameter::Stop* stop) {
327 3556 : if (!myStoppingPlaces[category == SUMO_TAG_TRAIN_STOP ? SUMO_TAG_BUS_STOP : category].add(id, stop)) {
328 12 : WRITE_ERRORF(TL("The % '%' occurs at least twice."), toString(category), id);
329 4 : delete stop;
330 : }
331 1968 : }
332 :
333 :
334 : bool
335 281049 : RONet::addRouteDef(RORouteDef* def) {
336 281049 : return myRoutes.add(def->getID(), def);
337 : }
338 :
339 :
340 : void
341 2882 : RONet::openOutput(const OptionsCont& options) {
342 8644 : if (options.isSet("output-file") && options.getString("output-file") != "") {
343 2881 : myRoutesOutput = &OutputDevice::getDevice(options.getString("output-file"));
344 2875 : if (myRoutesOutput->isNull()) {
345 1 : myRoutesOutput = nullptr;
346 : } else {
347 5748 : myRoutesOutput->writeXMLHeader("routes", "routes_file.xsd");
348 : }
349 : }
350 8442 : if (options.exists("alternatives-output") && options.isSet("alternatives-output")
351 13440 : && !(options.exists("write-trips") && options.getBool("write-trips"))) {
352 2496 : myRouteAlternativesOutput = &OutputDevice::getDevice(options.getString("alternatives-output"));
353 2496 : if (myRouteAlternativesOutput->isNull()) {
354 132 : myRouteAlternativesOutput = nullptr;
355 : } else {
356 4728 : myRouteAlternativesOutput->writeXMLHeader("routes", "routes_file.xsd");
357 : }
358 : }
359 5752 : if (options.isSet("vtype-output")) {
360 32 : myTypesOutput = &OutputDevice::getDevice(options.getString("vtype-output"));
361 64 : myTypesOutput->writeXMLHeader("routes", "routes_file.xsd");
362 : }
363 2876 : }
364 :
365 :
366 : void
367 2658 : RONet::writeIntermodal(const OptionsCont& options, ROIntermodalRouter& router) const {
368 5316 : if (options.exists("intermodal-network-output") && options.isSet("intermodal-network-output")) {
369 32 : OutputDevice::createDeviceByOption("intermodal-network-output", "intermodal");
370 32 : router.writeNetwork(OutputDevice::getDevice(options.getString("intermodal-network-output")));
371 : }
372 5316 : if (options.exists("intermodal-weight-output") && options.isSet("intermodal-weight-output")) {
373 16 : OutputDevice::createDeviceByOption("intermodal-weight-output", "weights", "meandata_file.xsd");
374 8 : OutputDevice& dev = OutputDevice::getDeviceByOption("intermodal-weight-output");
375 8 : dev.openTag(SUMO_TAG_INTERVAL);
376 8 : dev.writeAttr(SUMO_ATTR_ID, "intermodalweights");
377 8 : dev.writeAttr(SUMO_ATTR_BEGIN, 0);
378 8 : dev.writeAttr(SUMO_ATTR_END, SUMOTime_MAX);
379 8 : router.writeWeights(dev);
380 16 : dev.closeTag();
381 : }
382 2658 : }
383 :
384 :
385 : void
386 2881 : RONet::cleanup() {
387 : // end writing
388 2881 : if (myRoutesOutput != nullptr) {
389 2874 : myRoutesOutput->close();
390 : }
391 : // only if opened
392 2881 : if (myRouteAlternativesOutput != nullptr) {
393 2364 : myRouteAlternativesOutput->close();
394 : }
395 : // only if opened
396 2881 : if (myTypesOutput != nullptr) {
397 32 : myTypesOutput->close();
398 : }
399 : RouteCostCalculator<RORoute, ROEdge, ROVehicle>::cleanup();
400 : #ifdef HAVE_FOX
401 2881 : if (myThreadPool.size() > 0) {
402 30 : myThreadPool.clear();
403 : }
404 : #endif
405 2881 : }
406 :
407 :
408 :
409 : SUMOVTypeParameter*
410 495990 : RONet::getVehicleTypeSecure(const std::string& id) {
411 : // check whether the type was already known
412 : SUMOVTypeParameter* type = myVehicleTypes.get(id);
413 495990 : if (id == DEFAULT_VTYPE_ID) {
414 455335 : myDefaultVTypeMayBeDeleted = false;
415 40655 : } else if (id == DEFAULT_PEDTYPE_ID) {
416 5340 : myDefaultPedTypeMayBeDeleted = false;
417 35315 : } else if (id == DEFAULT_BIKETYPE_ID) {
418 54 : myDefaultBikeTypeMayBeDeleted = false;
419 35261 : } else if (id == DEFAULT_TAXITYPE_ID) {
420 93 : myDefaultTaxiTypeMayBeDeleted = false;
421 35168 : } else if (id == DEFAULT_RAILTYPE_ID) {
422 0 : myDefaultRailTypeMayBeDeleted = false;
423 : }
424 495990 : if (type != nullptr) {
425 : return type;
426 : }
427 : VTypeDistDictType::iterator it2 = myVTypeDistDict.find(id);
428 1478 : if (it2 != myVTypeDistDict.end()) {
429 1148 : return it2->second->get();
430 : }
431 330 : if (id == "") {
432 : // ok, no vehicle type or an unknown type was given within the user input
433 : // return the default type
434 0 : myDefaultVTypeMayBeDeleted = false;
435 : return myVehicleTypes.get(DEFAULT_VTYPE_ID);
436 : }
437 : return type;
438 : }
439 :
440 :
441 : bool
442 2109 : RONet::checkVType(const std::string& id) {
443 2109 : if (id == DEFAULT_VTYPE_ID) {
444 68 : if (myDefaultVTypeMayBeDeleted) {
445 68 : myVehicleTypes.remove(id);
446 68 : myDefaultVTypeMayBeDeleted = false;
447 : } else {
448 : return false;
449 : }
450 2041 : } else if (id == DEFAULT_PEDTYPE_ID) {
451 0 : if (myDefaultPedTypeMayBeDeleted) {
452 0 : myVehicleTypes.remove(id);
453 0 : myDefaultPedTypeMayBeDeleted = false;
454 : } else {
455 : return false;
456 : }
457 2041 : } else if (id == DEFAULT_BIKETYPE_ID) {
458 0 : if (myDefaultBikeTypeMayBeDeleted) {
459 0 : myVehicleTypes.remove(id);
460 0 : myDefaultBikeTypeMayBeDeleted = false;
461 : } else {
462 : return false;
463 : }
464 2041 : } else if (id == DEFAULT_TAXITYPE_ID) {
465 0 : if (myDefaultTaxiTypeMayBeDeleted) {
466 0 : myVehicleTypes.remove(id);
467 0 : myDefaultTaxiTypeMayBeDeleted = false;
468 : } else {
469 : return false;
470 : }
471 2041 : } else if (id == DEFAULT_RAILTYPE_ID) {
472 0 : if (myDefaultRailTypeMayBeDeleted) {
473 0 : myVehicleTypes.remove(id);
474 0 : myDefaultRailTypeMayBeDeleted = false;
475 : } else {
476 : return false;
477 : }
478 : } else {
479 2041 : if (myVehicleTypes.get(id) != 0 || myVTypeDistDict.find(id) != myVTypeDistDict.end()) {
480 0 : return false;
481 : }
482 : }
483 : return true;
484 : }
485 :
486 :
487 : bool
488 2065 : RONet::addVehicleType(SUMOVTypeParameter* type) {
489 2065 : if (checkVType(type->id)) {
490 2065 : myVehicleTypes.add(type->id, type);
491 : } else {
492 0 : WRITE_ERRORF(TL("The vehicle type '%' occurs at least twice."), type->id);
493 0 : delete type;
494 0 : return false;
495 : }
496 2065 : return true;
497 : }
498 :
499 :
500 : bool
501 44 : RONet::addVTypeDistribution(const std::string& id, RandomDistributor<SUMOVTypeParameter*>* vehTypeDistribution) {
502 44 : if (checkVType(id)) {
503 44 : myVTypeDistDict[id] = vehTypeDistribution;
504 44 : return true;
505 : }
506 0 : delete vehTypeDistribution;
507 : return false;
508 : }
509 :
510 :
511 : bool
512 297012 : RONet::addVehicle(const std::string& id, ROVehicle* veh) {
513 297012 : if (myVehIDs.find(id) == myVehIDs.end()) {
514 593984 : myVehIDs[id] = veh->getParameter().departProcedure == DepartDefinition::TRIGGERED ? -1 : veh->getDepartureTime();
515 :
516 297008 : if (veh->isPublicTransport()) {
517 44 : if (!veh->isPartOfFlow()) {
518 36 : myPTVehicles.push_back(veh);
519 : }
520 44 : if (!myDoPTRouting) {
521 : return true;
522 : }
523 : }
524 296972 : myRoutables[veh->getDepart()].push_back(veh);
525 296972 : return true;
526 : }
527 12 : WRITE_ERRORF(TL("Another vehicle with the id '%' exists."), id);
528 4 : delete veh;
529 : return false;
530 : }
531 :
532 :
533 : bool
534 256 : RONet::knowsVehicle(const std::string& id) const {
535 256 : return myVehIDs.find(id) != myVehIDs.end();
536 : }
537 :
538 : SUMOTime
539 28 : RONet::getDeparture(const std::string& vehID) const {
540 : auto it = myVehIDs.find(vehID);
541 28 : if (it != myVehIDs.end()) {
542 28 : return it->second;
543 : } else {
544 0 : throw ProcessError(TLF("Requesting departure time for unknown vehicle '%'", vehID));
545 : }
546 : }
547 :
548 :
549 : bool
550 2554 : RONet::addFlow(SUMOVehicleParameter* flow, const bool randomize) {
551 2554 : if (randomize && flow->repetitionOffset >= 0) {
552 10 : myDepartures[flow->id].reserve(flow->repetitionNumber);
553 110 : for (int i = 0; i < flow->repetitionNumber; ++i) {
554 100 : myDepartures[flow->id].push_back(flow->depart + RandHelper::rand(flow->repetitionNumber * flow->repetitionOffset));
555 : }
556 10 : std::sort(myDepartures[flow->id].begin(), myDepartures[flow->id].end());
557 10 : std::reverse(myDepartures[flow->id].begin(), myDepartures[flow->id].end());
558 2544 : } else if (flow->repetitionOffset < 0) {
559 : // init poisson flow (but only the timing)
560 349 : flow->incrementFlow(1);
561 349 : flow->repetitionsDone--;
562 : }
563 2554 : const bool added = myFlows.add(flow->id, flow);
564 2554 : if (added) {
565 2550 : myHaveActiveFlows = true;
566 : }
567 2554 : return added;
568 : }
569 :
570 :
571 : bool
572 3345 : RONet::addPerson(ROPerson* person) {
573 : if (myPersonIDs.count(person->getID()) == 0) {
574 : myPersonIDs.insert(person->getID());
575 3345 : myRoutables[person->getDepart()].push_back(person);
576 3345 : return true;
577 : }
578 0 : WRITE_ERRORF(TL("Another person with the id '%' exists."), person->getID());
579 0 : return false;
580 : }
581 :
582 :
583 : void
584 56 : RONet::addContainer(const SUMOTime depart, const std::string desc) {
585 56 : myContainers.insert(std::pair<const SUMOTime, const std::string>(depart, desc));
586 56 : }
587 :
588 :
589 : void
590 13306 : RONet::checkFlows(SUMOTime time, MsgHandler* errorHandler) {
591 13306 : myHaveActiveFlows = false;
592 51246 : for (const auto& i : myFlows) {
593 37940 : SUMOVehicleParameter* const pars = i.second;
594 37940 : if (pars->line != "" && !myDoPTRouting) {
595 737 : continue;
596 : }
597 37203 : if (myKeepFlows) {
598 796 : if (pars->repetitionsDone < pars->repetitionNumber) {
599 : // each each flow only once
600 790 : pars->repetitionsDone = pars->repetitionNumber;
601 790 : const SUMOVTypeParameter* type = getVehicleTypeSecure(pars->vtypeid);
602 790 : if (type == nullptr) {
603 0 : type = getVehicleTypeSecure(DEFAULT_VTYPE_ID);
604 : } else {
605 : auto dist = getVTypeDistribution(pars->vtypeid);
606 0 : if (dist != nullptr) {
607 0 : WRITE_WARNINGF("Keeping flow '%' with a vTypeDistribution can lead to invalid routes if the distribution contains different vClasses", pars->id);
608 : }
609 : }
610 1580 : RORouteDef* route = getRouteDef(pars->routeid)->copy(pars->routeid, pars->depart);
611 790 : ROVehicle* veh = new ROVehicle(*pars, route, type, this, errorHandler);
612 790 : addVehicle(pars->id, veh);
613 : }
614 36407 : } else if (pars->repetitionProbability > 0) {
615 108 : if (pars->repetitionEnd > pars->depart && pars->repetitionsDone < pars->repetitionNumber) {
616 96 : myHaveActiveFlows = true;
617 : }
618 : const SUMOTime origDepart = pars->depart;
619 1020 : while (pars->depart < time && pars->repetitionsDone < pars->repetitionNumber) {
620 928 : if (pars->repetitionEnd <= pars->depart) {
621 : break;
622 : }
623 : // only call rand if all other conditions are met
624 912 : if (RandHelper::rand() < (pars->repetitionProbability * TS)) {
625 168 : SUMOVehicleParameter* newPars = new SUMOVehicleParameter(*pars);
626 336 : newPars->id = pars->id + "." + toString(pars->repetitionsDone);
627 168 : newPars->depart = pars->depart;
628 168 : for (StopParVector::iterator stop = newPars->stops.begin(); stop != newPars->stops.end(); ++stop) {
629 0 : if (stop->until >= 0) {
630 0 : stop->until += pars->depart - origDepart;
631 : }
632 : }
633 168 : pars->repetitionsDone++;
634 : // try to build the vehicle
635 168 : const SUMOVTypeParameter* type = getVehicleTypeSecure(pars->vtypeid);
636 168 : if (type == nullptr) {
637 32 : type = getVehicleTypeSecure(DEFAULT_VTYPE_ID);
638 136 : } else if (!myKeepVTypeDist) {
639 : // fix the type id in case we used a distribution
640 136 : newPars->vtypeid = type->id;
641 : }
642 168 : const SUMOTime stopOffset = pars->routeid[0] == '!' ? pars->depart - origDepart : pars->depart;
643 336 : RORouteDef* route = getRouteDef(pars->routeid)->copy("!" + newPars->id, stopOffset);
644 168 : ROVehicle* veh = new ROVehicle(*newPars, route, type, this, errorHandler);
645 168 : addVehicle(newPars->id, veh);
646 168 : delete newPars;
647 : }
648 912 : pars->depart += DELTA_T;
649 : }
650 : } else {
651 36299 : SUMOTime depart = static_cast<SUMOTime>(pars->depart + pars->repetitionTotalOffset);
652 54062 : while (pars->repetitionsDone < pars->repetitionNumber && pars->repetitionEnd >= depart) {
653 28485 : myHaveActiveFlows = true;
654 28485 : depart = static_cast<SUMOTime>(pars->depart + pars->repetitionTotalOffset);
655 28485 : if (myDepartures.find(pars->id) != myDepartures.end()) {
656 110 : depart = myDepartures[pars->id].back();
657 : }
658 28485 : if (depart >= time + DELTA_T) {
659 : break;
660 : }
661 17763 : if (myDepartures.find(pars->id) != myDepartures.end()) {
662 100 : myDepartures[pars->id].pop_back();
663 : }
664 17763 : SUMOVehicleParameter* newPars = new SUMOVehicleParameter(*pars);
665 35526 : newPars->id = pars->id + "." + toString(pars->repetitionsDone);
666 17763 : newPars->depart = depart;
667 17923 : for (StopParVector::iterator stop = newPars->stops.begin(); stop != newPars->stops.end(); ++stop) {
668 160 : if (stop->until >= 0) {
669 40 : stop->until += depart - pars->depart;
670 : }
671 160 : if (stop->arrival >= 0) {
672 40 : stop->arrival += depart - pars->depart;
673 : }
674 : }
675 17763 : pars->incrementFlow(1);
676 : // try to build the vehicle
677 17763 : const SUMOVTypeParameter* type = getVehicleTypeSecure(pars->vtypeid);
678 17763 : if (type == nullptr) {
679 62 : type = getVehicleTypeSecure(DEFAULT_VTYPE_ID);
680 17701 : } else if (!myKeepVTypeDist) {
681 : // fix the type id in case we used a distribution
682 17661 : newPars->vtypeid = type->id;
683 : }
684 17763 : const SUMOTime stopOffset = pars->routeid[0] == '!' ? depart - pars->depart : depart;
685 35526 : RORouteDef* route = getRouteDef(pars->routeid)->copy("!" + newPars->id, stopOffset);
686 17763 : ROVehicle* veh = new ROVehicle(*newPars, route, type, this, errorHandler);
687 17763 : addVehicle(newPars->id, veh);
688 17763 : delete newPars;
689 : }
690 : }
691 : }
692 13306 : }
693 :
694 :
695 : void
696 56 : RONet::createBulkRouteRequests(const RORouterProvider& provider, const SUMOTime time, const bool removeLoops) {
697 : std::map<const int, std::vector<RORoutable*> > bulkVehs;
698 : int numBulked = 0;
699 84 : for (RoutablesMap::const_iterator i = myRoutables.begin(); i != myRoutables.end(); ++i) {
700 56 : if (i->first >= time) {
701 : break;
702 : }
703 539 : for (RORoutable* const routable : i->second) {
704 511 : const ROEdge* const depEdge = routable->getDepartEdge();
705 511 : bulkVehs[depEdge->getNumericalID()].push_back(routable);
706 511 : RORoutable* const first = bulkVehs[depEdge->getNumericalID()].front();
707 511 : numBulked++;
708 511 : if (first->getMaxSpeed() != routable->getMaxSpeed()) {
709 6 : WRITE_WARNINGF(TL("Bulking different maximum speeds ('%' and '%') may lead to suboptimal routes."), first->getID(), routable->getID());
710 : }
711 511 : if (first->getVClass() != routable->getVClass()) {
712 6 : WRITE_WARNINGF(TL("Bulking different vehicle classes ('%' and '%') may lead to invalid routes."), first->getID(), routable->getID());
713 : }
714 : }
715 : }
716 : #ifdef HAVE_FOX
717 : int workerIndex = 0;
718 : #endif
719 56 : if ((int)bulkVehs.size() < numBulked) {
720 56 : WRITE_MESSAGE(TLF("Using bulk-mode for % entities from % origins", numBulked, bulkVehs.size()));
721 : }
722 157 : for (std::map<const int, std::vector<RORoutable*> >::const_iterator i = bulkVehs.begin(); i != bulkVehs.end(); ++i) {
723 : #ifdef HAVE_FOX
724 101 : if (myThreadPool.size() > 0) {
725 : bool bulk = true;
726 132 : for (RORoutable* const r : i->second) {
727 120 : myThreadPool.add(new RoutingTask(r, removeLoops, myErrorHandler), workerIndex);
728 120 : if (bulk) {
729 12 : myThreadPool.add(new BulkmodeTask(true), workerIndex);
730 : bulk = false;
731 : }
732 : }
733 12 : myThreadPool.add(new BulkmodeTask(false), workerIndex);
734 12 : workerIndex++;
735 12 : if (workerIndex == (int)myThreadPool.size()) {
736 : workerIndex = 0;
737 : }
738 12 : continue;
739 12 : }
740 : #endif
741 480 : for (RORoutable* const r : i->second) {
742 391 : r->computeRoute(provider, removeLoops, myErrorHandler);
743 391 : provider.setBulkMode(true);
744 : }
745 89 : provider.setBulkMode(false);
746 : }
747 56 : }
748 :
749 :
750 : SUMOTime
751 19428 : RONet::saveAndRemoveRoutesUntil(OptionsCont& options, const RORouterProvider& provider,
752 : SUMOTime time) {
753 19428 : MsgHandler* mh = (options.getBool("ignore-errors") ?
754 19428 : MsgHandler::getWarningInstance() : MsgHandler::getErrorInstance());
755 19428 : if (myHaveActiveFlows) {
756 13306 : checkFlows(time, mh);
757 : }
758 : SUMOTime lastTime = -1;
759 19428 : const bool removeLoops = options.getBool("remove-loops");
760 : #ifdef HAVE_FOX
761 38856 : const int maxNumThreads = options.getInt("routing-threads");
762 : #endif
763 19428 : if (myRoutables.size() != 0) {
764 18814 : if (options.getBool("bulk-routing")) {
765 : #ifdef HAVE_FOX
766 64 : while ((int)myThreadPool.size() < maxNumThreads) {
767 8 : new WorkerThread(myThreadPool, provider);
768 : }
769 : #endif
770 56 : createBulkRouteRequests(provider, time, removeLoops);
771 : } else {
772 251275 : for (RoutablesMap::const_iterator i = myRoutables.begin(); i != myRoutables.end(); ++i) {
773 248474 : if (i->first >= time) {
774 : break;
775 : }
776 541533 : for (RORoutable* const routable : i->second) {
777 : #ifdef HAVE_FOX
778 : // add task
779 299609 : if (maxNumThreads > 0) {
780 : const int numThreads = (int)myThreadPool.size();
781 827 : if (numThreads == 0) {
782 : // This is the very first routing. Since at least the CHRouter needs initialization
783 : // before it gets cloned, we do not do this in parallel
784 24 : routable->computeRoute(provider, removeLoops, myErrorHandler);
785 24 : new WorkerThread(myThreadPool, provider);
786 : } else {
787 : // add thread if necessary
788 803 : if (numThreads < maxNumThreads && myThreadPool.isFull()) {
789 18 : new WorkerThread(myThreadPool, provider);
790 : }
791 803 : myThreadPool.add(new RoutingTask(routable, removeLoops, myErrorHandler));
792 : }
793 827 : continue;
794 827 : }
795 : #endif
796 298782 : routable->computeRoute(provider, removeLoops, myErrorHandler);
797 : }
798 : }
799 : }
800 : #ifdef HAVE_FOX
801 9391 : myThreadPool.waitAll();
802 : #endif
803 : }
804 57692 : const double scale = options.exists("scale-suffix") ? options.getFloat("scale") : 1;
805 : // write all vehicles (and additional structures)
806 261412 : while (myRoutables.size() != 0 || myContainers.size() != 0) {
807 : // get the next vehicle, person or container
808 : RoutablesMap::iterator routables = myRoutables.begin();
809 248570 : const SUMOTime routableTime = routables == myRoutables.end() ? SUMOTime_MAX : routables->first;
810 : ContainerMap::iterator container = myContainers.begin();
811 248570 : const SUMOTime containerTime = container == myContainers.end() ? SUMOTime_MAX : container->first;
812 : // check whether it shall not yet be computed
813 248570 : if (routableTime >= time && containerTime >= time) {
814 : lastTime = MIN2(routableTime, containerTime);
815 : break;
816 : }
817 : const SUMOTime minTime = MIN2(routableTime, containerTime);
818 242000 : if (routableTime == minTime) {
819 : // check whether to print the output
820 241952 : if (lastTime != routableTime && lastTime != -1) {
821 : // report writing progress
822 235696 : if (options.getInt("stats-period") >= 0 && ((int)routableTime % options.getInt("stats-period")) == 0) {
823 0 : WRITE_MESSAGE("Read: " + toString(myVehIDs.size()) + ", Discarded: " + toString(myDiscardedRouteNo) + ", Written: " + toString(myWrittenRouteNo));
824 : }
825 : }
826 : lastTime = routableTime;
827 542056 : for (const RORoutable* const r : routables->second) {
828 : // ok, check whether it has been routed
829 300104 : if (r->getRoutingSuccess()) {
830 : // write the route
831 280903 : int quota = getScalingQuota(scale, myWrittenRouteNo);
832 280903 : r->write(myRoutesOutput, myRouteAlternativesOutput, myTypesOutput, options, quota);
833 280903 : myWrittenRouteNo++;
834 : } else {
835 19201 : myDiscardedRouteNo++;
836 : }
837 : // we need to keep individual public transport vehicles but not the flows
838 300104 : if (!r->isPublicTransport() || r->isPartOfFlow()) {
839 : // delete routes and the vehicle
840 300104 : const ROVehicle* const veh = dynamic_cast<const ROVehicle*>(r);
841 300104 : if (veh != nullptr && veh->getRouteDefinition()->getID()[0] == '!') {
842 296768 : if (r->isPartOfFlow() || !myRoutes.remove(veh->getRouteDefinition()->getID())) {
843 19912 : delete veh->getRouteDefinition();
844 : }
845 : }
846 300104 : delete r;
847 : }
848 : }
849 : myRoutables.erase(routables);
850 : }
851 242000 : if (containerTime == minTime) {
852 48 : myRoutesOutput->writePreformattedTag(container->second);
853 48 : if (myRouteAlternativesOutput != nullptr) {
854 : myRouteAlternativesOutput->writePreformattedTag(container->second);
855 : }
856 : myContainers.erase(container);
857 : }
858 : }
859 19412 : return lastTime;
860 : }
861 :
862 :
863 : bool
864 41639 : RONet::furtherStored() {
865 41639 : return myRoutables.size() > 0 || (myFlows.size() > 0 && myHaveActiveFlows) || myContainers.size() > 0;
866 : }
867 :
868 :
869 : int
870 123 : RONet::getEdgeNumber() const {
871 123 : return myEdges.size();
872 : }
873 :
874 :
875 : int
876 1 : RONet::getInternalEdgeNumber() const {
877 1 : return myNumInternalEdges;
878 : }
879 :
880 :
881 : ROEdge*
882 9035 : RONet::getEdgeForLaneID(const std::string& laneID) const {
883 18070 : return getEdge(SUMOXMLDefinitions::getEdgeIDFromLane(laneID));
884 : }
885 :
886 :
887 : ROLane*
888 1140 : RONet::getLane(const std::string& laneID) const {
889 1140 : int laneIndex = SUMOXMLDefinitions::getIndexFromLane(laneID);
890 1140 : return getEdgeForLaneID(laneID)->getLanes()[laneIndex];
891 : }
892 :
893 :
894 : void
895 597 : RONet::adaptIntermodalRouter(ROIntermodalRouter& router) {
896 597 : double taxiWait = STEPS2TIME(string2time(OptionsCont::getOptions().getString("persontrip.taxi.waiting-time")));
897 884 : for (const auto& stopType : myInstance->myStoppingPlaces) {
898 : // add access to all stopping places
899 287 : const SumoXMLTag element = stopType.first;
900 2062 : for (const auto& stop : stopType.second) {
901 1775 : router.getNetwork()->addAccess(stop.first, myInstance->getEdgeForLaneID(stop.second->lane),
902 1775 : stop.second->startPos, stop.second->endPos, 0., element, false, taxiWait);
903 : // add access to all public transport stops
904 1775 : if (element == SUMO_TAG_BUS_STOP) {
905 3831 : for (const auto& a : stop.second->accessPos) {
906 2076 : router.getNetwork()->addAccess(stop.first, myInstance->getEdgeForLaneID(std::get<0>(a)),
907 : std::get<1>(a), std::get<1>(a), std::get<2>(a), SUMO_TAG_BUS_STOP, true, taxiWait);
908 : }
909 : }
910 : }
911 : }
912 : // fill the public transport router with pre-parsed public transport lines
913 1350 : for (const auto& i : myInstance->myFlows) {
914 753 : if (i.second->line != "") {
915 741 : const RORouteDef* const route = myInstance->getRouteDef(i.second->routeid);
916 : const StopParVector* addStops = nullptr;
917 1482 : if (route != nullptr && route->getFirstRoute() != nullptr) {
918 : addStops = &route->getFirstRoute()->getStops();
919 : }
920 741 : router.getNetwork()->addSchedule(*i.second, addStops);
921 : }
922 : }
923 633 : for (const RORoutable* const veh : myInstance->myPTVehicles) {
924 : // add single vehicles with line attribute which are not part of a flow
925 : // no need to add route stops here, they have been added to the vehicle before
926 36 : router.getNetwork()->addSchedule(veh->getParameter());
927 : }
928 : // add access to transfer from walking to taxi-use
929 597 : if ((router.getCarWalkTransfer() & ModeChangeOptions::TAXI_PICKUP_ANYWHERE) != 0) {
930 81703 : for (const ROEdge* edge : ROEdge::getAllEdges()) {
931 81118 : if (!edge->isTazConnector() && (edge->getPermissions() & SVC_PEDESTRIAN) != 0 && (edge->getPermissions() & SVC_TAXI) != 0) {
932 22486 : router.getNetwork()->addCarAccess(edge, SVC_TAXI, taxiWait);
933 : }
934 : }
935 : }
936 597 : }
937 :
938 :
939 : bool
940 4180031 : RONet::hasPermissions() const {
941 4180031 : return myHavePermissions;
942 : }
943 :
944 :
945 : void
946 253014 : RONet::setPermissionsFound() {
947 253014 : myHavePermissions = true;
948 253014 : }
949 :
950 : bool
951 12 : RONet::hasLoadedEffort() const {
952 38 : for (const auto& item : myEdges) {
953 35 : if (item.second->hasStoredEffort()) {
954 : return true;
955 : }
956 : }
957 : return false;
958 : }
959 :
960 : const std::string
961 1619 : RONet::getStoppingPlaceName(const std::string& id) const {
962 1811 : for (const auto& mapItem : myStoppingPlaces) {
963 : SUMOVehicleParameter::Stop* stop = mapItem.second.get(id);
964 1619 : if (stop != nullptr) {
965 : // see RONetHandler::parseStoppingPlace
966 : return stop->busstop;
967 : }
968 : }
969 0 : return "";
970 : }
971 :
972 : const std::string
973 1499 : RONet::getStoppingPlaceElement(const std::string& id) const {
974 1643 : for (const auto& mapItem : myStoppingPlaces) {
975 : SUMOVehicleParameter::Stop* stop = mapItem.second.get(id);
976 1499 : if (stop != nullptr) {
977 : // see RONetHandler::parseStoppingPlace
978 : return stop->actType;
979 : }
980 : }
981 0 : return toString(SUMO_TAG_BUS_STOP);
982 : }
983 :
984 :
985 : void
986 4 : RONet::addProhibition(const ROEdge* edge, const RouterProhibition& prohibition) {
987 : if (myProhibitions.count(edge) != 0) {
988 0 : throw ProcessError(TLF("Already loaded prohibition for edge '%'. (Only one prohibition per edge is supported)", edge->getID()));
989 : }
990 4 : myProhibitions[edge] = prohibition;
991 4 : myHavePermissions = true;
992 4 : }
993 :
994 :
995 : void
996 4 : RONet::addLaneProhibition(const ROLane* lane, const RouterProhibition& prohibition) {
997 : if (myLaneProhibitions.count(lane) != 0) {
998 0 : throw ProcessError(TLF("Already loaded prohibition for lane '%'. (Only one prohibition per lane is supported)", lane->getID()));
999 : }
1000 : assert(prohibition.end > prohibition.begin);
1001 4 : myLaneProhibitions[lane] = prohibition;
1002 4 : myLaneProhibitionTimes[prohibition.begin].insert(lane);
1003 4 : myHavePermissions = true;
1004 4 : }
1005 :
1006 :
1007 : void
1008 295775 : RONet::updateLaneProhibitions(SUMOTime begin) {
1009 295775 : const double beginS = STEPS2TIME(begin);
1010 295783 : while (myLaneProhibitionTimes.size() > 0 && myLaneProhibitionTimes.begin()->first <= beginS) {
1011 : const double t = myLaneProhibitionTimes.begin()->first;
1012 16 : for (const ROLane* const lane : myLaneProhibitionTimes.begin()->second) {
1013 : const SVCPermissions orig = lane->getPermissions();
1014 : assert(myLaneProhibitions.count(lane) != 0);
1015 8 : RouterProhibition& rp = myLaneProhibitions[lane];
1016 8 : const_cast<ROLane*>(lane)->setPermissions(rp.permissions);
1017 8 : lane->getEdge().resetSuccessors();
1018 8 : for (ROEdge* pred : lane->getEdge().getPredecessors()) {
1019 0 : pred->resetSuccessors();
1020 : }
1021 8 : if (t == rp.begin) {
1022 : // schedule restoration of original permissions. This works
1023 : // without a stack because there is at most one prohibition per lane
1024 4 : myLaneProhibitionTimes[rp.end].insert(lane);
1025 4 : rp.permissions = orig;
1026 : }
1027 : }
1028 : myLaneProhibitionTimes.erase(myLaneProhibitionTimes.begin());
1029 : }
1030 295775 : }
1031 :
1032 :
1033 : #ifdef HAVE_FOX
1034 : // ---------------------------------------------------------------------------
1035 : // RONet::RoutingTask-methods
1036 : // ---------------------------------------------------------------------------
1037 : void
1038 923 : RONet::RoutingTask::run(MFXWorkerThread* context) {
1039 923 : myRoutable->computeRoute(*static_cast<WorkerThread*>(context), myRemoveLoops, myErrorHandler);
1040 923 : }
1041 : #endif
1042 :
1043 :
1044 : /****************************************************************************/
|