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