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