Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2002-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 RONet.h
15 : /// @author Daniel Krajzewicz
16 : /// @author Michael Behrisch
17 : /// @author Jakob Erdmann
18 : /// @author Yun-Pang Floetteroed
19 : /// @date Sept 2002
20 : ///
21 : // The router's network representation
22 : /****************************************************************************/
23 : #pragma once
24 : #include <config.h>
25 :
26 : #include <vector>
27 : #include <utils/common/MsgHandler.h>
28 : #include <utils/common/NamedObjectCont.h>
29 : #include <utils/distribution/RandomDistributor.h>
30 : #include <utils/vehicle/SUMOVehicleParameter.h>
31 : #include <utils/vehicle/SUMOVTypeParameter.h>
32 : #include "ROLane.h"
33 : #include "RORoutable.h"
34 : #include "RORouteDef.h"
35 :
36 : #ifdef HAVE_FOX
37 : #include <utils/foxtools/MFXWorkerThread.h>
38 : #endif
39 :
40 :
41 : // ===========================================================================
42 : // class declarations
43 : // ===========================================================================
44 : class ROEdge;
45 : class RONode;
46 : class ROPerson;
47 : class ROVehicle;
48 : class ROAbstractEdgeBuilder;
49 : class OptionsCont;
50 : class OutputDevice;
51 :
52 : typedef MapMatcher<ROEdge, ROLane, RONode> ROMapMatcher;
53 :
54 : // ===========================================================================
55 : // class definitions
56 : // ===========================================================================
57 : /**
58 : * @class RONet
59 : * @brief The router's network representation.
60 : *
61 : * A router network is responsible for watching loaded edges, nodes,!!!
62 : */
63 : class RONet {
64 : public:
65 :
66 : typedef std::map<const SUMOTime, std::vector<RORoutable*> > RoutablesMap;
67 :
68 : /// @brief Constructor
69 : RONet();
70 :
71 :
72 : /** @brief Returns the pointer to the unique instance of RONet (singleton).
73 : * @return Pointer to the unique RONet-instance
74 : */
75 : static RONet* getInstance();
76 :
77 :
78 : /// @brief Destructor
79 : virtual ~RONet();
80 :
81 :
82 : /** @brief Adds a restriction for an edge type
83 : * @param[in] id The id of the type
84 : * @param[in] svc The vehicle class the restriction refers to
85 : * @param[in] speed The restricted speed
86 : */
87 : void addRestriction(const std::string& id, const SUMOVehicleClass svc, const double speed);
88 :
89 :
90 : /** @brief Returns the restrictions for an edge type
91 : * If no restrictions are present, 0 is returned.
92 : * @param[in] id The id of the type
93 : * @return The mapping of vehicle classes to maximum speeds
94 : */
95 : const std::map<SUMOVehicleClass, double>* getRestrictions(const std::string& id) const;
96 :
97 : bool hasRestrictions() const {
98 236728 : return !myRestrictions.empty();
99 : }
100 :
101 : /// @brief retriefe edge type specific routing preference
102 : double getPreference(const std::string& routingType, const SUMOVTypeParameter& pars) const;
103 :
104 : /// @brief add edge type specific routing preference
105 : void addPreference(const std::string& routingType, SUMOVehicleClass svc, double prio);
106 : /// @brief add edge type specific routing preference
107 : void addPreference(const std::string& routingType, std::string vType, double prio);
108 :
109 : /// @name Insertion and retrieval of graph parts
110 : //@{
111 :
112 : /* @brief Adds a read edge to the network
113 : *
114 : * If the edge is already known (another one with the same id exists),
115 : * an error is generated and given to msg-error-handler. The edge
116 : * is deleted in this case and false is returned.
117 : *
118 : * @param[in] edge The edge to add
119 : * @return Whether the edge was added (if not, it was deleted, too)
120 : */
121 : virtual bool addEdge(ROEdge* edge);
122 :
123 :
124 : /* @brief Adds a district and connecting edges to the network
125 : *
126 : * If the district is already known (another one with the same id exists),
127 : * an error is generated and given to msg-error-handler. The edges
128 : * are deleted in this case and false is returned.
129 : *
130 : * @param[in] id The district to add
131 : * @return Whether the district was added
132 : */
133 : bool addDistrict(const std::string id, ROEdge* source, ROEdge* sink);
134 :
135 :
136 : /* @brief Adds a district and connecting edges to the network
137 : *
138 : * If the district is already known (another one with the same id exists),
139 : * an error is generated and given to msg-error-handler. The edges
140 : * are deleted in this case and false is returned.
141 : *
142 : * @param[in] id The district to add
143 : * @return Whether the district was added
144 : */
145 : bool addDistrictEdge(const std::string tazID, const std::string edgeID, const bool isSource);
146 :
147 : /// @brief add a taz for every junction unless a taz with the same id already exists
148 : void addJunctionTaz(ROAbstractEdgeBuilder& eb);
149 :
150 : /// @brief add a taz for every junction unless a taz with the same id already exists
151 : void setBidiEdges(const std::map<ROEdge*, std::string>& bidiMap);
152 :
153 : /** @brief Retrieves all TAZ (districts) from the network
154 : *
155 : * @return The map of all districts
156 : */
157 : const std::map<std::string, std::pair<std::vector<std::string>, std::vector<std::string> > >& getDistricts() const {
158 98 : return myDistricts;
159 : }
160 :
161 : /** @brief Retrieves an edge from the network
162 : *
163 : * This is not very pretty, but necessary, though, as routes run
164 : * over instances, not over ids.
165 : *
166 : * @param[in] name The name of the edge to retrieve
167 : * @return The named edge if known, otherwise 0
168 : */
169 : ROEdge* getEdge(const std::string& name) const {
170 : return myEdges.get(name);
171 : }
172 :
173 :
174 : /** @brief Retrieves an edge from the network when the lane id is given
175 : *
176 : * @param[in] laneID The name of the lane to retrieve the edge for
177 : * @return The edge of the named lane if known, otherwise 0
178 : */
179 : ROEdge* getEdgeForLaneID(const std::string& laneID) const;
180 :
181 : /** @brief Retrieves a lane rom the network given its id
182 : *
183 : * @param[in] laneID The name of the lane to retrieve the edge for
184 : * @return The lane object
185 : */
186 : ROLane* getLane(const std::string& laneID) const;
187 :
188 : /* @brief Adds a read node to the network
189 : *
190 : * If the node is already known (another one with the same id exists),
191 : * an error is generated and given to msg-error-handler. The node
192 : * is deleted in this case
193 : *
194 : * @param[in] node The node to add
195 : */
196 : void addNode(RONode* node);
197 :
198 :
199 : /** @brief Retrieves an node from the network
200 : *
201 : * @param[in] name The name of the node to retrieve
202 : * @return The named node if known, otherwise 0
203 : * @todo Check whether a const pointer may be returned
204 : */
205 : RONode* getNode(const std::string& id) const {
206 : return myNodes.get(id);
207 : }
208 :
209 :
210 : /* @brief Adds a read stopping place (bus, train, container, parking) to the network
211 : *
212 : * If the place is already known (another one with the same id and category exists),
213 : * an error is generated and given to msg-error-handler. The stop
214 : * is deleted in this case
215 : *
216 : * @param[in] id The name of the stop to add
217 : * @param[in] category The type of stop
218 : * @param[in] stop The detailed stop description
219 : */
220 : void addStoppingPlace(const std::string& id, const SumoXMLTag category, SUMOVehicleParameter::Stop* stop);
221 :
222 : /** @brief Retrieves a stopping place from the network
223 : *
224 : * @param[in] id The name of the stop to retrieve
225 : * @param[in] category The type of stop
226 : * @return The named stop if known, otherwise 0
227 : */
228 2873 : const SUMOVehicleParameter::Stop* getStoppingPlace(const std::string& id, const SumoXMLTag category) const {
229 : if (myStoppingPlaces.count(category) > 0) {
230 : return myStoppingPlaces.find(category)->second.get(id);
231 : }
232 : return 0;
233 : }
234 :
235 : /// @brief return the name for the given stopping place id
236 : const std::string getStoppingPlaceName(const std::string& id) const;
237 :
238 : /// @brief return the element name for the given stopping place id
239 : const std::string getStoppingPlaceElement(const std::string& id) const;
240 : //@}
241 :
242 :
243 :
244 : /// @name Insertion and retrieval of vehicle types, vehicles, routes, and route definitions
245 : //@{
246 :
247 : /** @brief Checks whether the vehicle type (distribution) may be added
248 : *
249 : * This method checks also whether the default type may still be replaced
250 : * @param[in] id The id of the vehicle type (distribution) to add
251 : * @return Whether the type (distribution) may be added
252 : */
253 : bool checkVType(const std::string& id);
254 :
255 :
256 : /** @brief Adds a read vehicle type definition to the network
257 : *
258 : * If the vehicle type definition is already known (another one with
259 : * the same id exists), false is returned, and the vehicle type
260 : * is deleted.
261 : *
262 : * @param[in] def The vehicle type to add
263 : * @return Whether the vehicle type could be added
264 : */
265 : virtual bool addVehicleType(SUMOVTypeParameter* type);
266 :
267 :
268 : /** @brief Adds a vehicle type distribution
269 : *
270 : * If another vehicle type (or distribution) with the same id exists, false is returned.
271 : * Otherwise, the vehicle type distribution is added to the internal vehicle type distribution
272 : * container "myVTypeDistDict".
273 : *
274 : * This control get responsible for deletion of the added vehicle
275 : * type distribution.
276 : *
277 : * @param[in] id The id of the distribution to add
278 : * @param[in] vehTypeDistribution The vehicle type distribution to add
279 : * @return Whether the vehicle type could be added
280 : */
281 : bool addVTypeDistribution(const std::string& id, RandomDistributor<SUMOVTypeParameter*>* vehTypeDistribution);
282 :
283 :
284 : /** @brief Retrieves the named vehicle type distribution
285 : *
286 : * If the named vehicle type distribution was not added to the net before
287 : * nullptr is returned
288 : *
289 : * @param[in] id The id of the vehicle type distribution to return
290 : * @return The named vehicle type distribution
291 : */
292 : const RandomDistributor<SUMOVTypeParameter*>* getVTypeDistribution(const std::string& id) {
293 : const auto it = myVTypeDistDict.find(id);
294 192248 : return it != myVTypeDistDict.end() ? it ->second : nullptr;
295 : }
296 :
297 :
298 : /** @brief Retrieves the named vehicle type
299 : *
300 : * If the name is "" the default type is returned.
301 : * If the named vehicle type (or typeDistribution) was not added to the net before
302 : * nullptr is returned
303 : *
304 : * @param[in] id The id of the vehicle type to return
305 : * @return The named vehicle type
306 : */
307 : SUMOVTypeParameter* getVehicleTypeSecure(const std::string& id);
308 :
309 :
310 : /* @brief Adds a route definition to the network
311 : *
312 : * If the route definition is already known (another one with
313 : * the same id exists), false is returned, but the route definition
314 : * is not deleted.
315 : *
316 : * @param[in] def The route definition to add
317 : * @return Whether the route definition could be added
318 : * @todo Rename myRoutes to myRouteDefinitions
319 : */
320 : bool addRouteDef(RORouteDef* def);
321 :
322 :
323 : /** @brief Returns the named route definition
324 : *
325 : * @param[in] name The name of the route definition to retrieve
326 : * @return The named route definition if known, otherwise 0
327 : * @todo Check whether a const pointer may be returned
328 : * @todo Rename myRoutes to myRouteDefinitions
329 : */
330 : RORouteDef* getRouteDef(const std::string& name) const {
331 : return myRoutes.get(name);
332 : }
333 :
334 :
335 : /* @brief Adds a vehicle to the network
336 : *
337 : * If the vehicle is already known (another one with the same id
338 : * exists), false is returned, but the vehicle is not deleted.
339 : *
340 : * Otherwise, the number of loaded routes ("myReadRouteNo") is increased.
341 : *
342 : * @param[in] id The id of the vehicle to add
343 : * @param[in] veh The vehicle to add
344 : * @return Whether the vehicle could be added
345 : */
346 : virtual bool addVehicle(const std::string& id, ROVehicle* veh);
347 :
348 : /// @brief returns whether a vehicle with the given id was already loaded
349 : bool knowsVehicle(const std::string& id) const;
350 :
351 : /// @brief returns departure time for the given vehicle id
352 : SUMOTime getDeparture(const std::string& vehID) const;
353 :
354 : /* @brief Adds a flow of vehicles to the network
355 : *
356 : * If the flow is already known (another one with the same id
357 : * exists), false is returned, but the vehicle parameter are not deleted.
358 : *
359 : * Otherwise, the number of loaded routes ("myReadRouteNo") is increased.
360 : *
361 : * @param[in] flow The parameter of the flow to add
362 : * @return Whether the flow could be added
363 : */
364 : bool addFlow(SUMOVehicleParameter* flow, const bool randomize);
365 :
366 :
367 : /* @brief Adds a person to the network
368 : *
369 : * @param[in] person The person to add
370 : */
371 : bool addPerson(ROPerson* person);
372 :
373 :
374 : /* @brief Adds a container to the network
375 : *
376 : * @param[in] depart The departure time of the container
377 : * @param[in] desc The xml description of the container
378 : */
379 : void addContainer(const SUMOTime depart, const std::string desc);
380 : // @}
381 :
382 :
383 : /// @name Processing stored vehicle definitions
384 : //@{
385 :
386 : /** @brief Computes routes described by their definitions and saves them
387 : *
388 : * As long as a vehicle with a departure time smaller than the given
389 : * exists, its route is computed and it is written and removed from
390 : * the internal container.
391 : *
392 : * @param[in] options The options used during this process
393 : * @param[in] provider The router provider for routes computation
394 : * @param[in] time The time until which route definitions shall be processed
395 : * @return The last seen departure time>=time
396 : */
397 : SUMOTime saveAndRemoveRoutesUntil(OptionsCont& options,
398 : const RORouterProvider& provider, SUMOTime time);
399 :
400 :
401 : /// Returns the information whether further vehicles, persons or containers are stored
402 : bool furtherStored();
403 : //@}
404 :
405 :
406 : /** @brief Opens the output for computed routes
407 : *
408 : * If one of the file outputs can not be build, an IOError is thrown.
409 : *
410 : * @param[in] options The options to be asked for "output-file", "alternatives-output" and "vtype-output"
411 : */
412 : void openOutput(const OptionsCont& options);
413 :
414 :
415 : /** @brief Writes the intermodal network and weights if requested
416 : *
417 : * If one of the file outputs can not be build, an IOError is thrown.
418 : *
419 : * @param[in] options The options to be asked for "intermodal-network-output" and "intermodal-weight-output"
420 : */
421 : void writeIntermodal(const OptionsCont& options, ROIntermodalRouter& router) const;
422 :
423 :
424 : /** @brief closes the file output for computed routes and deletes associated threads if necessary */
425 : void cleanup();
426 :
427 :
428 : /// Returns the total number of edges the network contains including internal edges
429 : int getEdgeNumber() const;
430 :
431 : /// Returns the number of internal edges the network contains
432 : int getInternalEdgeNumber() const;
433 :
434 : const NamedObjectCont<ROEdge*>& getEdgeMap() const {
435 : return myEdges;
436 : }
437 :
438 : static void adaptIntermodalRouter(ROIntermodalRouter& router);
439 :
440 : bool hasPermissions() const;
441 :
442 : void setPermissionsFound();
443 :
444 : /// @brief return whether the network contains bidirectional rail edges
445 : bool hasBidiEdges() const {
446 2633 : return myHasBidiEdges;
447 : }
448 :
449 : /// @brief whether efforts were loaded from file
450 : bool hasLoadedEffort() const;
451 :
452 : OutputDevice* getRouteOutput(const bool alternative = false) {
453 : if (alternative) {
454 : return myRouteAlternativesOutput;
455 : }
456 67 : return myRoutesOutput;
457 : }
458 :
459 : #ifdef HAVE_FOX
460 : MFXWorkerThread::Pool& getThreadPool() {
461 163 : return myThreadPool;
462 : }
463 :
464 : class WorkerThread : public MFXWorkerThread, public RORouterProvider {
465 : public:
466 53 : WorkerThread(MFXWorkerThread::Pool& pool,
467 : const RORouterProvider& original)
468 53 : : MFXWorkerThread(pool), RORouterProvider(original) {}
469 106 : virtual ~WorkerThread() {
470 53 : stop();
471 106 : }
472 : };
473 :
474 : class BulkmodeTask : public MFXWorkerThread::Task {
475 : public:
476 84 : BulkmodeTask(const bool value) : myValue(value) {}
477 84 : void run(MFXWorkerThread* context) {
478 84 : static_cast<WorkerThread*>(context)->setBulkMode(myValue);
479 84 : }
480 : private:
481 : const bool myValue;
482 : private:
483 : /// @brief Invalidated assignment operator.
484 : BulkmodeTask& operator=(const BulkmodeTask&);
485 : };
486 : #endif
487 :
488 :
489 : private:
490 : void checkFlows(SUMOTime time, MsgHandler* errorHandler);
491 :
492 : void createBulkRouteRequests(const RORouterProvider& provider, const SUMOTime time, const bool removeLoops);
493 :
494 : private:
495 : /// @brief Unique instance of RONet
496 : static RONet* myInstance;
497 :
498 : /// @brief Known vehicle ids and their departure
499 : std::map<std::string, SUMOTime> myVehIDs;
500 :
501 : /// @brief Known person ids
502 : std::set<std::string> myPersonIDs;
503 :
504 : /// @brief Known nodes
505 : NamedObjectCont<RONode*> myNodes;
506 :
507 : /// @brief Known edges
508 : NamedObjectCont<ROEdge*> myEdges;
509 :
510 : /// @brief Known bus / train / container stops and parking areas
511 : std::map<SumoXMLTag, NamedObjectCont<SUMOVehicleParameter::Stop*> > myStoppingPlaces;
512 :
513 : /// @brief Known vehicle types
514 : NamedObjectCont<SUMOVTypeParameter*> myVehicleTypes;
515 :
516 : /// @brief Vehicle type distribution dictionary type
517 : typedef std::map< std::string, RandomDistributor<SUMOVTypeParameter*>* > VTypeDistDictType;
518 : /// @brief A distribution of vehicle types (probability->vehicle type)
519 : VTypeDistDictType myVTypeDistDict;
520 :
521 : /// @brief Whether the default vehicle type was already used or can still be replaced
522 : bool myDefaultVTypeMayBeDeleted;
523 :
524 : /// @brief Whether the default pedestrian type was already used or can still be replaced
525 : bool myDefaultPedTypeMayBeDeleted;
526 :
527 : /// @brief Whether the default bicycle type was already used or can still be replaced
528 : bool myDefaultBikeTypeMayBeDeleted;
529 :
530 : /// @brief Whether the default taxi type was already used or can still be replaced
531 : bool myDefaultTaxiTypeMayBeDeleted;
532 :
533 : /// @brief Whether the default rail type was already used or can still be replaced
534 : bool myDefaultRailTypeMayBeDeleted;
535 :
536 : /// @brief Known routes
537 : NamedObjectCont<RORouteDef*> myRoutes;
538 :
539 : /// @brief Known routables
540 : RoutablesMap myRoutables;
541 :
542 : /// @brief Known flows
543 : NamedObjectCont<SUMOVehicleParameter*> myFlows;
544 :
545 : /// @brief whether any flows are still active
546 : bool myHaveActiveFlows;
547 :
548 : /// @brief Known containers
549 : typedef std::multimap<const SUMOTime, const std::string> ContainerMap;
550 : ContainerMap myContainers;
551 :
552 : /// @brief vehicles to keep for public transport routing
553 : std::vector<const RORoutable*> myPTVehicles;
554 :
555 : /// @brief Departure times for randomized flows
556 : std::map<std::string, std::vector<SUMOTime> > myDepartures;
557 :
558 : /// @brief traffic assignment zones with sources and sinks
559 : std::map<std::string, std::pair<std::vector<std::string>, std::vector<std::string> > > myDistricts;
560 :
561 : /// @brief The file to write the computed routes into
562 : OutputDevice* myRoutesOutput;
563 :
564 : /// @brief The file to write the computed route alternatives into
565 : OutputDevice* myRouteAlternativesOutput;
566 :
567 : /// @brief The file to write the vehicle types into
568 : OutputDevice* myTypesOutput;
569 :
570 : /// @brief The number of read routes
571 : int myReadRouteNo;
572 :
573 : /// @brief The number of discarded routes
574 : int myDiscardedRouteNo;
575 :
576 : /// @brief The number of written routes
577 : int myWrittenRouteNo;
578 :
579 : /// @brief Whether the network contains edges which not all vehicles may pass
580 : bool myHavePermissions;
581 :
582 : /// @brief The vehicle class specific speed restrictions
583 : std::map<std::string, std::map<SUMOVehicleClass, double> > myRestrictions;
584 :
585 : /// @brief Preferences for routing
586 : std::map<SUMOVehicleClass, std::map<std::string, double> > myVClassPreferences;
587 : std::map<std::string, std::map<std::string, double> > myVTypePreferences;
588 :
589 : /// @brief The number of internal edges in the dictionary
590 : int myNumInternalEdges;
591 :
592 : /// @brief handler for ignorable error messages
593 : MsgHandler* myErrorHandler;
594 :
595 : /// @brief whether to keep the vtype distribution in output
596 : const bool myKeepVTypeDist;
597 :
598 : /// @brief whether to calculate routes for public transport
599 : const bool myDoPTRouting;
600 :
601 : /// @brief whether to preserve flows
602 : const bool myKeepFlows;
603 :
604 : /// @brief whether the network contains bidirectional railway edges
605 : bool myHasBidiEdges;
606 :
607 : #ifdef HAVE_FOX
608 : private:
609 : class RoutingTask : public MFXWorkerThread::Task {
610 : public:
611 : RoutingTask(RORoutable* v, const bool removeLoops, MsgHandler* errorHandler)
612 923 : : myRoutable(v), myRemoveLoops(removeLoops), myErrorHandler(errorHandler) {}
613 : void run(MFXWorkerThread* context);
614 : private:
615 : RORoutable* const myRoutable;
616 : const bool myRemoveLoops;
617 : MsgHandler* const myErrorHandler;
618 : private:
619 : /// @brief Invalidated assignment operator.
620 : RoutingTask& operator=(const RoutingTask&);
621 : };
622 :
623 :
624 : private:
625 : /// @brief for multi threaded routing
626 : MFXWorkerThread::Pool myThreadPool;
627 : #endif
628 :
629 : private:
630 : /// @brief Invalidated copy constructor
631 : RONet(const RONet& src);
632 :
633 : /// @brief Invalidated assignment operator
634 : RONet& operator=(const RONet& src);
635 :
636 : };
|