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