Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
duarouter_main.cpp
Go to the documentation of this file.
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/****************************************************************************/
20// Main for DUAROUTER
21/****************************************************************************/
22#include <config.h>
23
24#ifdef HAVE_VERSION_H
25#include <version.h>
26#endif
27
28#include <iostream>
29#include <string>
30#include <limits.h>
31#include <ctime>
32#include <memory>
33#include <xercesc/sax/SAXException.hpp>
34#include <xercesc/sax/SAXParseException.hpp>
42#ifdef HAVE_FOX
44#endif
56#include <utils/xml/XMLSubSys.h>
57#include <router/ROFrame.h>
58#include <router/ROLoader.h>
59#include <router/RONet.h>
60#include <router/ROEdge.h>
61#include "RODUAEdgeBuilder.h"
62#include <router/ROLane.h>
63#include <router/RONode.h>
64#include <router/ROCCHMetrics.h>
65#include "RODUAFrame.h"
66
67
68// ===========================================================================
69// functions
70// ===========================================================================
71
72/* -------------------------------------------------------------------------
73 * data processing methods
74 * ----------------------------------------------------------------------- */
80void
81initNet(RONet& net, ROLoader& loader, OptionsCont& oc) {
82 // load the net
83 RODUAEdgeBuilder builder;
84 ROEdge::setGlobalOptions(oc.getBool("weights.interpolate"));
85 loader.loadNet(net, builder);
86 // load the weights when wished/available
87 if (oc.isSet("weight-files")) {
88 loader.loadWeights(net, "weight-files", oc.getString("weight-attribute"), false, oc.getBool("weights.expand"));
89 }
90 if (oc.isSet("lane-weight-files")) {
91 loader.loadWeights(net, "lane-weight-files", oc.getString("weight-attribute"), true, oc.getBool("weights.expand"));
92 }
93 if (oc.getBool("skip-new-routes")) {
95 }
96}
97
98
102void
104 // initialise the loader
105 loader.openRoutes(net);
106 // build the router
109 const std::string measure = oc.getString("weight-attribute");
110 const std::string routingAlgorithm = oc.getString("routing-algorithm");
111 const double priorityFactor = oc.getFloat("weights.priority-factor");
112 const SUMOTime begin = string2time(oc.getString("begin"));
113 const SUMOTime end = oc.isDefault("end") ? SUMOTime_MAX : string2time(oc.getString("end"));
115 if (measure == "traveltime") {
116 if (priorityFactor != 0) {
117 if (ROEdge::initPriorityFactor(priorityFactor)) {
118 if (gWeightsRandomFactor > 0) {
120 } else {
122 }
123 }
124 }
125 }
126
127 if (oc.isSet("restriction-params") &&
128 (routingAlgorithm == "CH" || routingAlgorithm == "CHWrapper")) {
129 throw ProcessError(TLF("Routing algorithm '%' does not support restriction-params", routingAlgorithm));
130 }
131
132 if (measure == "traveltime") {
133 if (routingAlgorithm == "dijkstra") {
134 router = new DijkstraRouter<ROEdge, ROVehicle>(ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op, nullptr, false, nullptr, net.hasPermissions(), oc.isSet("restriction-params"));
135 } else if (routingAlgorithm == "astar") {
137 std::shared_ptr<const AStar::LookupTable> lookup;
138 if (oc.isSet("astar.all-distances")) {
139 lookup = std::make_shared<const AStar::FLT>(oc.getString("astar.all-distances"), (int)ROEdge::getAllEdges().size());
140 } else if (oc.isSet("astar.landmark-distances")) {
141 /* CHRouterWrapper<ROEdge, ROVehicle> chrouter(
142 ROEdge::getAllEdges(), true, &ROEdge::getTravelTimeStatic,
143 begin, end, SUMOTime_MAX, 1); */
145 std::vector<ReversedEdge<ROEdge, ROVehicle>*> reversed;
146 for (ROEdge* edge : ROEdge::getAllEdges()) {
147 reversed.push_back(edge->getReversedRoutingEdge());
148 }
149 for (ReversedEdge<ROEdge, ROVehicle>* redge : reversed) {
150 redge->init();
151 }
153 ROVehicle defaultVehicle(SUMOVehicleParameter(), nullptr, net.getVehicleTypeSecure(DEFAULT_VTYPE_ID), &net);
154 ROMapMatcher* mapMatcher = dynamic_cast<ROMapMatcher*>(loader.getRouteHandler());
155 lookup = std::make_shared<const AStar::LMLT>(oc.getString("astar.landmark-distances"), ROEdge::getAllEdges(), &forward, &backward, &defaultVehicle,
156 oc.isSet("astar.save-landmark-distances") ? oc.getString("astar.save-landmark-distances") : "", oc.getInt("routing-threads"), mapMatcher);
157 }
158 router = new AStar(ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op, lookup, net.hasPermissions(), oc.isSet("restriction-params"));
159 } else if (routingAlgorithm == "CH" && !net.hasPermissions() && !gRoutingPreferences && !net.hasSpeedRestrictions()) {
160 const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
161 string2time(oc.getString("weight-period")) :
163 router = new CHRouter<ROEdge, ROVehicle>(
164 ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op, SVC_IGNORING, weightPeriod, net.hasPermissions(), oc.isSet("restriction-params"));
165 } else if (routingAlgorithm == "CHWrapper" || routingAlgorithm == "CH") {
166 // use CHWrapper instead of CH if the net has permissions, preferences or speed restriction
167 const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
168 string2time(oc.getString("weight-period")) :
171 ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op,
172 begin, end, weightPeriod, net.hasPermissions(), oc.getInt("routing-threads"));
173 } else if (routingAlgorithm == "CCH") {
174 // One weight-independent hierarchy over the union graph; one
175 // customized metric per (vehicle type, weight period), built
176 // lazily on first query (see ROCCHMetrics.h). With
177 // time-dependent weight files this follows CHRouterWrapper's
178 // per-period semantics, but repeats only the metric
179 // customization, never the topology build.
180 // Because the metrics are keyed by vehicle type, the type and
181 // class specific routing preferences of the effort function are
182 // captured exactly. weights.random-factor freezes one random
183 // realization into each metric, the same approximation
184 // CHRouterWrapper makes when building its hierarchies.
185 const bool hasWeights = oc.isSet("weight-files") || oc.isSet("lane-weight-files");
186 const SUMOTime weightPeriod = hasWeights ? string2time(oc.getString("weight-period")) : SUMOTime_MAX;
187 // Process-lifetime singletons: the topology and metric store are
188 // shared by every router clone until duarouter exits.
189 ROCCHGraph* cchGraph = new ROCCHGraph(ROEdge::getAllEdges());
190 ROCCHMetrics::init(cchGraph, op, begin, weightPeriod);
192 ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op,
193 nullptr, net.hasPermissions(), oc.isSet("restriction-params"));
194 // restriction-params are handled natively: each distinct
195 // restriction profile gets its own masked metric (see
196 // ROCCHMetrics::get); the period-end hook lets queries whose
197 // trip crosses a weight-period boundary re-query on the next
198 // period's metric.
200 cchGraph, &ROCCHMetrics::get, op,
201 oc.getBool("ignore-errors"), fallback,
202 hasWeights ? &ROCCHMetrics::periodEnd : nullptr);
203 } else if (routingAlgorithm == "arcflag") {
205 constexpr auto NUMBER_OF_LEVELS = 5; //or 4 or 8
206 ROVehicle defaultVehicle(SUMOVehicleParameter(), nullptr, net.getVehicleTypeSecure(DEFAULT_VTYPE_ID), &net);
208 partition->init(&defaultVehicle);
211 partition, oc.getBool("ignore-errors"), ttFunction, reversedTtFunction, (oc.isSet("weight-files") ? string2time(oc.getString("weight-period")) : SUMOTime_MAX),
212 nullptr, nullptr, net.hasPermissions(), oc.isSet("restriction-params"));
213 } else {
214 throw ProcessError(TLF("Unknown routing algorithm '%'!", routingAlgorithm));
215 }
216 } else {
217 if (measure == "CO") {
218 op = &ROEdge::getEmissionEffort<PollutantsInterface::CO>;
219 } else if (measure == "CO2") {
220 op = &ROEdge::getEmissionEffort<PollutantsInterface::CO2>;
221 } else if (measure == "PMx") {
222 op = &ROEdge::getEmissionEffort<PollutantsInterface::PM_X>;
223 } else if (measure == "HC") {
224 op = &ROEdge::getEmissionEffort<PollutantsInterface::HC>;
225 } else if (measure == "NOx") {
226 op = &ROEdge::getEmissionEffort<PollutantsInterface::NO_X>;
227 } else if (measure == "fuel") {
228 op = &ROEdge::getEmissionEffort<PollutantsInterface::FUEL>;
229 } else if (measure == "electricity") {
230 op = &ROEdge::getEmissionEffort<PollutantsInterface::ELEC>;
231 } else if (measure == "noise") {
233 } else {
235 }
236 if (measure != "traveltime" && !net.hasLoadedEffort()) {
237 WRITE_WARNINGF(TL("No weight data was loaded for attribute '%'."), measure);
238 }
240 ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op, ttFunction, false, nullptr, net.hasPermissions(), oc.isSet("restriction-params"));
241 }
242 const int carWalk = SUMOVehicleParserHelper::parseCarWalkTransfer(oc, true);
243 double taxiWait = STEPS2TIME(string2time(OptionsCont::getOptions().getString("persontrip.taxi.waiting-time")));
244
245 RailwayRouter<ROEdge, ROVehicle>* railRouter = nullptr;
246 if (net.hasBidiEdges()) {
247 railRouter = new RailwayRouter<ROEdge, ROVehicle>(ROEdge::getAllEdges(), true, op, ttFunction, false, net.hasPermissions(),
248 oc.isSet("restriction-params"),
249 oc.getFloat("railway.max-train-length"),
250 oc.getFloat("weights.reversal-penalty"));
251 }
253 new ROIntermodalRouter(RONet::adaptIntermodalRouter, carWalk, taxiWait, routingAlgorithm),
254 railRouter);
255 // process route definitions
256 try {
257 net.openOutput(oc);
258 loader.processRoutes(begin, end, string2time(oc.getString("route-steps")), net, provider);
259 net.writeIntermodal(oc, provider.getIntermodalRouter());
260 // end the processing
261 net.cleanup();
262 } catch (ProcessError&) {
263 net.cleanup();
264 throw;
265 }
266}
267
268
269/* -------------------------------------------------------------------------
270 * main
271 * ----------------------------------------------------------------------- */
272int
273main(int argc, char** argv) {
275 oc.setApplicationDescription(TL("Shortest path router and DUE computer for the microscopic, multi-modal traffic simulation SUMO."));
276 oc.setApplicationName("duarouter", "Eclipse SUMO duarouter " VERSION_STRING);
277 int ret = 0;
278 RONet* net = nullptr;
279 try {
282 OptionsIO::setArgs(argc, argv);
284 if (oc.processMetaOptions(argc < 2)) {
286 return 0;
287 }
289 XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"), oc.getString("xml-validation.routes"));
290#ifdef HAVE_FOX
291 if (oc.getInt("routing-threads") > 1) {
292 // make the output aware of threading
294 }
295#endif
298 throw ProcessError();
299 }
301 // load data
302 ROLoader loader(oc, false, !oc.getBool("no-step-log"));
303 net = new RONet();
304 initNet(*net, loader, oc);
305 // build routes
306 try {
307 computeRoutes(*net, loader, oc);
308 } catch (XERCES_CPP_NAMESPACE::SAXParseException& e) {
309 WRITE_ERROR(toString(e.getLineNumber()));
310 ret = 1;
311 } catch (XERCES_CPP_NAMESPACE::SAXException& e) {
312 WRITE_ERROR(XMLSubSys::transcode(e.getMessage()));
313 ret = 1;
314 }
315 if (MsgHandler::getErrorInstance()->wasInformed() || ret != 0) {
316 throw ProcessError();
317 }
318 } catch (const ProcessError& e) {
319 if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
320 WRITE_ERROR(e.what());
321 }
322 MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
323 ret = 1;
324#ifndef _DEBUG
325 } catch (const std::exception& e) {
326 if (std::string(e.what()) != std::string("")) {
327 WRITE_ERROR(e.what());
328 }
329 MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
330 ret = 1;
331 } catch (...) {
332 MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
333 ret = 1;
334#endif
335 }
336 delete net;
338 if (ret == 0) {
339 std::cout << "Success." << std::endl;
340 }
341 return ret;
342}
343
344
345/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:287
#define WRITE_ERROR(msg)
Definition MsgHandler.h:295
#define TL(string)
Definition MsgHandler.h:304
#define TLF(string,...)
Definition MsgHandler.h:306
CCHGraph< ROEdge, ROVehicle > ROCCHGraph
the ROEdge instantiation of the CCH graph mapping
IntermodalRouter< ROEdge, ROLane, RONode, ROVehicle > ROIntermodalRouter
Definition RORoutable.h:42
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition SUMOTime.cpp:46
#define STEPS2TIME(x)
Definition SUMOTime.h:58
#define SUMOTime_MAX
Definition SUMOTime.h:34
const std::string DEFAULT_VTYPE_ID
@ SVC_IGNORING
vehicles ignoring classes
bool gRoutingPreferences
Definition StdDefs.cpp:37
double gWeightsRandomFactor
Definition StdDefs.cpp:35
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:49
Computes the shortest path through a network using the A* algorithm.
Definition AStarRouter.h:76
Metric-independent RoutingKit CCH topology over the PURE road graph.
Definition CCHGraph.h:97
Contraction-hierarchy router over the edge graph mapped by GRAPH.
Definition CCHRouter.h:83
Computes the shortest path through a contracted network.
Definition CHRouter.h:59
Computes the shortest path through a contracted network.
Computes the shortest path through a network using the Dijkstra algorithm.
The edge type representing backward edges with flipped nodes.
Definition FlippedEdge.h:43
Partitions the router's network wrt a k-d tree subdivision scheme.
void init(const V *const vehicle)
Initialize the k-d tree wrt to the given vehicle's permissions.
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
static void initOutputOptions()
init output options
static void setFactory(Factory func)
Sets the factory function to use for new MsgHandlers.
Definition MsgHandler.h:64
static MsgHandler * create(MsgType type)
A storage for options typed value containers)
Definition OptionsCont.h:89
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
static OptionsCont & getOptions()
Retrieves the options.
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition OptionsIO.cpp:59
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition OptionsIO.cpp:75
static SUMOTime periodEnd(SUMOTime time)
the end of the weight period containing the given time (SUMOTime_MAX when the weights are static); CC...
static void init(const ROCCHGraph *graph, ROCCHGraph::EffortOperation effort, SUMOTime begin, SUMOTime weightPeriod)
install the shared graph, effort function and weight-period grid (call once, before routing); weightP...
static const RoutingKit::CustomizableContractionHierarchyMetric * get(SUMOVehicleClass vClass, SUMOTime time, const ROVehicle *veh)
the metric for the vehicle's type at a query time, built on first use; matches CCHRouter::MetricProvi...
Interface for building instances of duarouter-edges.
static void fillOptions()
Inserts options used by duarouter into the OptionsCont-singleton.
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid for usage within duarouter.
A basic edge for routing applications.
Definition ROEdge.h:73
static double getStoredEffort(const ROEdge *const edge, const ROVehicle *const, double time)
Definition ROEdge.h:512
static bool initPriorityFactor(double priorityFactor)
initialize priority factor range
Definition ROEdge.cpp:469
static double getTravelTimeStaticPriorityFactor(const ROEdge *const edge, const ROVehicle *const veh, double time)
Return traveltime weighted by edge priority (scaled penalty for low-priority edges)
Definition ROEdge.h:439
static double getNoiseEffort(const ROEdge *const edge, const ROVehicle *const veh, double time)
Definition ROEdge.cpp:239
static double getTravelTimeStaticRandomized(const ROEdge *const edge, const ROVehicle *const veh, double time)
Definition ROEdge.h:427
static double getTravelTimeStaticPriorityFactorRandomized(const ROEdge *const edge, const ROVehicle *const veh, double time)
Return traveltime weighted by edge priority (scaled penalty for low-priority edges)
Definition ROEdge.h:449
static void setGlobalOptions(const bool interpolate)
Definition ROEdge.h:527
static double getTravelTimeStatic(const ROEdge *const edge, const ROVehicle *const veh, double time)
Returns the travel time for the given edge.
Definition ROEdge.h:423
static const ROEdgeVector & getAllEdges()
Returns all ROEdges.
Definition ROEdge.cpp:375
The data loader.
Definition ROLoader.h:54
bool loadWeights(RONet &net, const std::string &optionName, const std::string &measure, const bool useLanes, const bool boundariesOverride)
Loads the net weights.
Definition ROLoader.cpp:260
SUMORouteHandler * getRouteHandler()
Definition ROLoader.cpp:314
void processRoutes(const SUMOTime start, const SUMOTime end, const SUMOTime increment, RONet &net, const RORouterProvider &provider)
Loads routes from all previously build route loaders.
Definition ROLoader.cpp:199
virtual void loadNet(RONet &toFill, ROAbstractEdgeBuilder &eb)
Loads the network.
Definition ROLoader.cpp:108
void openRoutes(RONet &net)
Builds and opens all route loaders.
Definition ROLoader.cpp:169
The router's network representation.
Definition RONet.h:63
SUMOVTypeParameter * getVehicleTypeSecure(const std::string &id)
Retrieves the named vehicle type.
Definition RONet.cpp:410
bool hasBidiEdges() const
return whether the network contains bidirectional rail edges
Definition RONet.h:458
void cleanup()
closes the file output for computed routes and deletes associated threads if necessary
Definition RONet.cpp:386
void openOutput(const OptionsCont &options)
Opens the output for computed routes.
Definition RONet.cpp:341
void writeIntermodal(const OptionsCont &options, ROIntermodalRouter &router) const
Writes the intermodal network and weights if requested.
Definition RONet.cpp:367
static void adaptIntermodalRouter(ROIntermodalRouter &router)
Definition RONet.cpp:902
bool hasSpeedRestrictions() const
Definition RONet.h:99
bool hasPermissions() const
Definition RONet.cpp:947
bool hasLoadedEffort() const
whether efforts were loaded from file
Definition RONet.cpp:958
static void setSkipNew()
Definition RORouteDef.h:156
A vehicle as used by router.
Definition ROVehicle.h:51
static void initRandGlobal(SumoRNG *which=nullptr)
Reads the given random number options and initialises the random number generator in accordance.
the edge type representing backward edges
IntermodalRouter< E, L, N, V > & getIntermodalRouter() const
Structure representing possible vehicle parameter.
static int parseCarWalkTransfer(const OptionsCont &oc, const bool hasTaxi)
static void close()
Closes all of an applications subsystems.
static bool checkOptions(OptionsCont &oc)
checks shared options and sets StdDefs
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme, const std::string &routeValidationScheme)
Enables or disables validation.
Definition XMLSubSys.cpp:88
static std::string transcode(const XMLCh *const data, int length=-1)
converts a 0-terminated XMLCh* array (usually UTF-16, stemming from Xerces) into std::string in UTF-8
static void init()
Initialises the xml-subsystem.
Definition XMLSubSys.cpp:61
int main(int argc, char **argv)
void initNet(RONet &net, ROLoader &loader, OptionsCont &oc)
void computeRoutes(RONet &net, ROLoader &loader, OptionsCont &oc)