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-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/****************************************************************************/
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
55#include <utils/xml/XMLSubSys.h>
56#include <router/ROFrame.h>
57#include <router/ROLoader.h>
58#include <router/RONet.h>
59#include <router/ROEdge.h>
60#include "RODUAEdgeBuilder.h"
61#include "RODUAFrame.h"
62
63
64// ===========================================================================
65// functions
66// ===========================================================================
67/* -------------------------------------------------------------------------
68 * data processing methods
69 * ----------------------------------------------------------------------- */
75void
76initNet(RONet& net, ROLoader& loader, OptionsCont& oc) {
77 // load the net
78 RODUAEdgeBuilder builder;
79 ROEdge::setGlobalOptions(oc.getBool("weights.interpolate"));
80 loader.loadNet(net, builder);
81 // load the weights when wished/available
82 if (oc.isSet("weight-files")) {
83 loader.loadWeights(net, "weight-files", oc.getString("weight-attribute"), false, oc.getBool("weights.expand"));
84 }
85 if (oc.isSet("lane-weight-files")) {
86 loader.loadWeights(net, "lane-weight-files", oc.getString("weight-attribute"), true, oc.getBool("weights.expand"));
87 }
88}
89
90
94void
96 // initialise the loader
97 loader.openRoutes(net);
98 // build the router
101 const std::string measure = oc.getString("weight-attribute");
102 const std::string routingAlgorithm = oc.getString("routing-algorithm");
103 const double priorityFactor = oc.getFloat("weights.priority-factor");
104 const SUMOTime begin = string2time(oc.getString("begin"));
105 const SUMOTime end = oc.isDefault("end") ? SUMOTime_MAX : string2time(oc.getString("end"));
107
108 if (oc.isSet("restriction-params") &&
109 (routingAlgorithm == "CH" || routingAlgorithm == "CHWrapper")) {
110 throw ProcessError(TLF("Routing algorithm '%' does not support restriction-params", routingAlgorithm));
111 }
112
113 if (measure == "traveltime" && priorityFactor == 0) {
114 if (routingAlgorithm == "dijkstra") {
115 router = new DijkstraRouter<ROEdge, ROVehicle>(ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction, nullptr, false, nullptr, net.hasPermissions(), oc.isSet("restriction-params"));
116 } else if (routingAlgorithm == "astar") {
118 std::shared_ptr<const AStar::LookupTable> lookup;
119 if (oc.isSet("astar.all-distances")) {
120 lookup = std::make_shared<const AStar::FLT>(oc.getString("astar.all-distances"), (int)ROEdge::getAllEdges().size());
121 } else if (oc.isSet("astar.landmark-distances")) {
122 /* CHRouterWrapper<ROEdge, ROVehicle> chrouter(
123 ROEdge::getAllEdges(), true, &ROEdge::getTravelTimeStatic,
124 begin, end, SUMOTime_MAX, 1); */
126 std::vector<ReversedEdge<ROEdge, ROVehicle>*> reversed;
127 for (ROEdge* edge : ROEdge::getAllEdges()) {
128 reversed.push_back(edge->getReversedRoutingEdge());
129 }
130 for (ReversedEdge<ROEdge, ROVehicle>* redge : reversed) {
131 redge->init();
132 }
134 ROVehicle defaultVehicle(SUMOVehicleParameter(), nullptr, net.getVehicleTypeSecure(DEFAULT_VTYPE_ID), &net);
135 ROMapMatcher* mapMatcher = dynamic_cast<ROMapMatcher*>(loader.getRouteHandler());
136 lookup = std::make_shared<const AStar::LMLT>(oc.getString("astar.landmark-distances"), ROEdge::getAllEdges(), &forward, &backward, &defaultVehicle,
137 oc.isSet("astar.save-landmark-distances") ? oc.getString("astar.save-landmark-distances") : "", oc.getInt("routing-threads"), mapMatcher);
138 }
139 router = new AStar(ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction, lookup, net.hasPermissions(), oc.isSet("restriction-params"));
140 } else if (routingAlgorithm == "CH" && !net.hasPermissions()) {
141 const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
142 string2time(oc.getString("weight-period")) :
144 router = new CHRouter<ROEdge, ROVehicle>(
145 ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction, SVC_IGNORING, weightPeriod, net.hasPermissions(), oc.isSet("restriction-params"));
146 } else if (routingAlgorithm == "CHWrapper" || routingAlgorithm == "CH") {
147 // use CHWrapper instead of CH if the net has permissions
148 const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
149 string2time(oc.getString("weight-period")) :
152 ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction,
153 begin, end, weightPeriod, net.hasPermissions(), oc.getInt("routing-threads"));
154 } else if (routingAlgorithm == "arcflag") {
156 constexpr auto NUMBER_OF_LEVELS = 5; //or 4 or 8
157 ROVehicle defaultVehicle(SUMOVehicleParameter(), nullptr, net.getVehicleTypeSecure(DEFAULT_VTYPE_ID), &net);
159 partition->init(&defaultVehicle);
163 partition, oc.getBool("ignore-errors"), ttFunction, reversedTtFunction, (oc.isSet("weight-files") ? string2time(oc.getString("weight-period")) : SUMOTime_MAX),
164 nullptr, nullptr, net.hasPermissions(), oc.isSet("restriction-params"));
165 } else {
166 throw ProcessError(TLF("Unknown routing Algorithm '%'!", routingAlgorithm));
167 }
168 } else {
169 if (measure == "traveltime") {
170 if (ROEdge::initPriorityFactor(priorityFactor)) {
172 }
173 } else if (measure == "CO") {
174 op = &ROEdge::getEmissionEffort<PollutantsInterface::CO>;
175 } else if (measure == "CO2") {
176 op = &ROEdge::getEmissionEffort<PollutantsInterface::CO2>;
177 } else if (measure == "PMx") {
178 op = &ROEdge::getEmissionEffort<PollutantsInterface::PM_X>;
179 } else if (measure == "HC") {
180 op = &ROEdge::getEmissionEffort<PollutantsInterface::HC>;
181 } else if (measure == "NOx") {
182 op = &ROEdge::getEmissionEffort<PollutantsInterface::NO_X>;
183 } else if (measure == "fuel") {
184 op = &ROEdge::getEmissionEffort<PollutantsInterface::FUEL>;
185 } else if (measure == "electricity") {
186 op = &ROEdge::getEmissionEffort<PollutantsInterface::ELEC>;
187 } else if (measure == "noise") {
189 } else {
191 }
192 if (measure != "traveltime" && !net.hasLoadedEffort()) {
193 WRITE_WARNINGF(TL("No weight data was loaded for attribute '%'."), measure);
194 }
196 ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op, ttFunction, false, nullptr, net.hasPermissions(), oc.isSet("restriction-params"));
197 }
198 const int carWalk = SUMOVehicleParserHelper::parseCarWalkTransfer(oc);
199 double taxiWait = STEPS2TIME(string2time(OptionsCont::getOptions().getString("persontrip.taxi.waiting-time")));
200
201 RailwayRouter<ROEdge, ROVehicle>* railRouter = nullptr;
202 if (net.hasBidiEdges()) {
203 railRouter = new RailwayRouter<ROEdge, ROVehicle>(ROEdge::getAllEdges(), true, op, ttFunction, false, net.hasPermissions(),
204 oc.isSet("restriction-params"),
205 oc.getFloat("railway.max-train-length"));
206 }
208 new ROIntermodalRouter(RONet::adaptIntermodalRouter, carWalk, taxiWait, routingAlgorithm),
209 railRouter);
210 // process route definitions
211 try {
212 net.openOutput(oc);
213 loader.processRoutes(begin, end, string2time(oc.getString("route-steps")), net, provider);
214 net.writeIntermodal(oc, provider.getIntermodalRouter());
215 // end the processing
216 net.cleanup();
217 } catch (ProcessError&) {
218 net.cleanup();
219 throw;
220 }
221}
222
223
224/* -------------------------------------------------------------------------
225 * main
226 * ----------------------------------------------------------------------- */
227int
228main(int argc, char** argv) {
230 oc.setApplicationDescription(TL("Shortest path router and DUE computer for the microscopic, multi-modal traffic simulation SUMO."));
231 oc.setApplicationName("duarouter", "Eclipse SUMO duarouter Version " VERSION_STRING);
232 int ret = 0;
233 RONet* net = nullptr;
234 try {
237 OptionsIO::setArgs(argc, argv);
239 if (oc.processMetaOptions(argc < 2)) {
241 return 0;
242 }
244 XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"), oc.getString("xml-validation.routes"));
245#ifdef HAVE_FOX
246 if (oc.getInt("routing-threads") > 1) {
247 // make the output aware of threading
249 }
250#endif
253 throw ProcessError();
254 }
256 // load data
257 ROLoader loader(oc, false, !oc.getBool("no-step-log"));
258 net = new RONet();
259 initNet(*net, loader, oc);
260 // build routes
261 try {
262 computeRoutes(*net, loader, oc);
263 } catch (XERCES_CPP_NAMESPACE::SAXParseException& e) {
264 WRITE_ERROR(toString(e.getLineNumber()));
265 ret = 1;
266 } catch (XERCES_CPP_NAMESPACE::SAXException& e) {
267 WRITE_ERROR(StringUtils::transcode(e.getMessage()));
268 ret = 1;
269 }
270 if (MsgHandler::getErrorInstance()->wasInformed() || ret != 0) {
271 throw ProcessError();
272 }
273 } catch (const ProcessError& e) {
274 if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
275 WRITE_ERROR(e.what());
276 }
277 MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
278 ret = 1;
279#ifndef _DEBUG
280 } catch (const std::exception& e) {
281 if (std::string(e.what()) != std::string("")) {
282 WRITE_ERROR(e.what());
283 }
284 MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
285 ret = 1;
286 } catch (...) {
287 MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
288 ret = 1;
289#endif
290 }
291 delete net;
293 if (ret == 0) {
294 std::cout << "Success." << std::endl;
295 }
296 return ret;
297}
298
299
300/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:296
#define WRITE_ERROR(msg)
Definition MsgHandler.h:304
#define TL(string)
Definition MsgHandler.h:315
#define TLF(string,...)
Definition MsgHandler.h:317
IntermodalRouter< ROEdge, ROLane, RONode, ROVehicle > ROIntermodalRouter
Definition RORoutable.h:41
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition SUMOTime.cpp:46
#define STEPS2TIME(x)
Definition SUMOTime.h:55
#define SUMOTime_MAX
Definition SUMOTime.h:34
const std::string DEFAULT_VTYPE_ID
@ SVC_IGNORING
vehicles ignoring classes
double gWeightsRandomFactor
Definition StdDefs.cpp:32
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
Computes the shortest path through a network using the A* algorithm.
Definition AStarRouter.h:76
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:58
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition OptionsIO.cpp:74
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:72
static double getStoredEffort(const ROEdge *const edge, const ROVehicle *const, double time)
Definition ROEdge.h:486
static bool initPriorityFactor(double priorityFactor)
initialize priority factor range
Definition ROEdge.cpp:464
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:446
static double getNoiseEffort(const ROEdge *const edge, const ROVehicle *const veh, double time)
Definition ROEdge.cpp:235
static double getTravelTimeStaticRandomized(const ROEdge *const edge, const ROVehicle *const veh, double time)
Definition ROEdge.h:436
static void setGlobalOptions(const bool interpolate)
Definition ROEdge.h:501
static double getTravelTimeStatic(const ROEdge *const edge, const ROVehicle *const veh, double time)
Returns the travel time for the given edge.
Definition ROEdge.h:432
static const ROEdgeVector & getAllEdges()
Returns all ROEdges.
Definition ROEdge.cpp:372
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:254
SUMORouteHandler * getRouteHandler()
Definition ROLoader.cpp:308
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:193
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:163
The router's network representation.
Definition RONet.h:63
SUMOVTypeParameter * getVehicleTypeSecure(const std::string &id)
Retrieves the named vehicle type.
Definition RONet.cpp:364
bool hasBidiEdges() const
return whether the network contains bidirectional rail edges
Definition RONet.h:434
void cleanup()
closes the file output for computed routes and deletes associated threads if necessary
Definition RONet.cpp:340
void openOutput(const OptionsCont &options)
Opens the output for computed routes.
Definition RONet.cpp:292
void writeIntermodal(const OptionsCont &options, ROIntermodalRouter &router) const
Writes the intermodal network and weights if requested.
Definition RONet.cpp:321
static void adaptIntermodalRouter(ROIntermodalRouter &router)
Definition RONet.cpp:820
bool hasPermissions() const
Definition RONet.cpp:865
bool hasLoadedEffort() const
whether efforts were loaded from file
Definition RONet.cpp:876
A vehicle as used by router.
Definition ROVehicle.h:50
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=false)
static std::string transcode(const XMLCh *const data)
converts a 0-terminated XMLCh* array (usually UTF-16, stemming from Xerces) into std::string in UTF-8
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:83
static void init()
Initialises the xml-subsystem.
Definition XMLSubSys.cpp:56
int main(int argc, char **argv)
void initNet(RONet &net, ROLoader &loader, OptionsCont &oc)
void computeRoutes(RONet &net, ROLoader &loader, OptionsCont &oc)