Eclipse SUMO - Simulation of Urban MObility
duarouter_main.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2022 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>
40 #include <utils/common/ToString.h>
41 #ifdef HAVE_FOX
43 #endif
45 #include <utils/options/Option.h>
50 #include <utils/router/CHRouter.h>
52 #include <utils/xml/XMLSubSys.h>
53 #include <router/ROFrame.h>
54 #include <router/ROLoader.h>
55 #include <router/RONet.h>
56 #include <router/ROEdge.h>
57 #include "RODUAEdgeBuilder.h"
58 #include "RODUAFrame.h"
59 
60 
61 // ===========================================================================
62 // functions
63 // ===========================================================================
64 /* -------------------------------------------------------------------------
65  * data processing methods
66  * ----------------------------------------------------------------------- */
72 void
73 initNet(RONet& net, ROLoader& loader, OptionsCont& oc) {
74  // load the net
75  RODUAEdgeBuilder builder;
76  ROEdge::setGlobalOptions(oc.getBool("weights.interpolate"));
77  loader.loadNet(net, builder);
78  // load the weights when wished/available
79  if (oc.isSet("weight-files")) {
80  loader.loadWeights(net, "weight-files", oc.getString("weight-attribute"), false, oc.getBool("weights.expand"));
81  }
82  if (oc.isSet("lane-weight-files")) {
83  loader.loadWeights(net, "lane-weight-files", oc.getString("weight-attribute"), true, oc.getBool("weights.expand"));
84  }
85 }
86 
87 
91 void
92 computeRoutes(RONet& net, ROLoader& loader, OptionsCont& oc) {
93  // initialise the loader
94  loader.openRoutes(net);
95  // build the router
98  const std::string measure = oc.getString("weight-attribute");
99  const std::string routingAlgorithm = oc.getString("routing-algorithm");
100  const double priorityFactor = oc.getFloat("weights.priority-factor");
101  const SUMOTime begin = string2time(oc.getString("begin"));
102  const SUMOTime end = oc.isDefault("end") ? SUMOTime_MAX : string2time(oc.getString("end"));
104 
105  if (oc.isSet("restriction-params") &&
106  (routingAlgorithm == "CH" || routingAlgorithm == "CHWrapper")) {
107  throw ProcessError("Routing algorithm '" + routingAlgorithm + "' does not support restriction-params");
108  }
109 
110  if (measure == "traveltime" && priorityFactor == 0) {
111  if (routingAlgorithm == "dijkstra") {
112  router = new DijkstraRouter<ROEdge, ROVehicle>(ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction, nullptr, false, nullptr, net.hasPermissions(), oc.isSet("restriction-params"));
113  } else if (routingAlgorithm == "astar") {
114  typedef AStarRouter<ROEdge, ROVehicle> AStar;
115  std::shared_ptr<const AStar::LookupTable> lookup;
116  if (oc.isSet("astar.all-distances")) {
117  lookup = std::make_shared<const AStar::FLT>(oc.getString("astar.all-distances"), (int)ROEdge::getAllEdges().size());
118  } else if (oc.isSet("astar.landmark-distances")) {
119  /* CHRouterWrapper<ROEdge, ROVehicle> chrouter(
120  ROEdge::getAllEdges(), true, &ROEdge::getTravelTimeStatic,
121  begin, end, SUMOTime_MAX, 1); */
123  std::vector<ReversedEdge<ROEdge, ROVehicle>*> reversed;
124  for (ROEdge* edge : ROEdge::getAllEdges()) {
125  reversed.push_back(edge->getReversedRoutingEdge());
126  }
127  for (ReversedEdge<ROEdge, ROVehicle>* redge : reversed) {
128  redge->init();
129  }
131  ROVehicle defaultVehicle(SUMOVehicleParameter(), nullptr, net.getVehicleTypeSecure(DEFAULT_VTYPE_ID), &net);
132  lookup = std::make_shared<const AStar::LMLT>(oc.getString("astar.landmark-distances"), ROEdge::getAllEdges(), &forward, &backward, &defaultVehicle,
133  oc.isSet("astar.save-landmark-distances") ? oc.getString("astar.save-landmark-distances") : "", oc.getInt("routing-threads"));
134  }
135  router = new AStar(ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction, lookup, net.hasPermissions(), oc.isSet("restriction-params"));
136  } else if (routingAlgorithm == "CH" && !net.hasPermissions()) {
137  const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
138  string2time(oc.getString("weight-period")) :
139  SUMOTime_MAX);
140  router = new CHRouter<ROEdge, ROVehicle>(
141  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction, SVC_IGNORING, weightPeriod, net.hasPermissions(), oc.isSet("restriction-params"));
142  } else if (routingAlgorithm == "CHWrapper" || routingAlgorithm == "CH") {
143  // use CHWrapper instead of CH if the net has permissions
144  const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
145  string2time(oc.getString("weight-period")) :
146  SUMOTime_MAX);
148  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction,
149  begin, end, weightPeriod, net.hasPermissions(), oc.getInt("routing-threads"));
150  } else {
151  throw ProcessError("Unknown routing Algorithm '" + routingAlgorithm + "'!");
152  }
153  } else {
154  if (measure == "traveltime") {
155  if (ROEdge::initPriorityFactor(priorityFactor)) {
157  }
158  } else if (measure == "CO") {
159  op = &ROEdge::getEmissionEffort<PollutantsInterface::CO>;
160  } else if (measure == "CO2") {
161  op = &ROEdge::getEmissionEffort<PollutantsInterface::CO2>;
162  } else if (measure == "PMx") {
163  op = &ROEdge::getEmissionEffort<PollutantsInterface::PM_X>;
164  } else if (measure == "HC") {
165  op = &ROEdge::getEmissionEffort<PollutantsInterface::HC>;
166  } else if (measure == "NOx") {
167  op = &ROEdge::getEmissionEffort<PollutantsInterface::NO_X>;
168  } else if (measure == "fuel") {
169  op = &ROEdge::getEmissionEffort<PollutantsInterface::FUEL>;
170  } else if (measure == "electricity") {
171  op = &ROEdge::getEmissionEffort<PollutantsInterface::ELEC>;
172  } else if (measure == "noise") {
174  } else {
176  }
177  if (measure != "traveltime" && !net.hasLoadedEffort()) {
178  WRITE_WARNING("No weight data was loaded for attribute '" + measure + "'.");
179  }
181  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op, ttFunction, false, nullptr, net.hasPermissions(), oc.isSet("restriction-params"));
182  }
183  int carWalk = 0;
184  for (const std::string& opt : oc.getStringVector("persontrip.transfer.car-walk")) {
185  if (opt == "parkingAreas") {
187  } else if (opt == "ptStops") {
189  } else if (opt == "allJunctions") {
191  }
192  }
193  for (const std::string& opt : oc.getStringVector("persontrip.transfer.taxi-walk")) {
194  if (opt == "ptStops") {
196  } else if (opt == "allJunctions") {
198  }
199  }
200  for (const std::string& opt : oc.getStringVector("persontrip.transfer.walk-taxi")) {
201  if (opt == "ptStops") {
203  } else if (opt == "allJunctions") {
205  }
206  }
207  double taxiWait = STEPS2TIME(string2time(OptionsCont::getOptions().getString("persontrip.taxi.waiting-time")));
208 
209  RailwayRouter<ROEdge, ROVehicle>* railRouter = nullptr;
210  if (net.hasBidiEdges()) {
211  railRouter = new RailwayRouter<ROEdge, ROVehicle>(ROEdge::getAllEdges(), true, op, ttFunction, false, net.hasPermissions(),
212  oc.isSet("restriction-params"),
213  oc.getFloat("railway.max-train-length"));
214  }
216  new ROIntermodalRouter(RONet::adaptIntermodalRouter, carWalk, taxiWait, routingAlgorithm),
217  railRouter);
218  // process route definitions
219  try {
220  net.openOutput(oc);
221  loader.processRoutes(begin, end, string2time(oc.getString("route-steps")), net, provider);
222  net.writeIntermodal(oc, provider.getIntermodalRouter());
223  // end the processing
224  net.cleanup();
225  } catch (ProcessError&) {
226  net.cleanup();
227  throw;
228  }
229 }
230 
231 
232 /* -------------------------------------------------------------------------
233  * main
234  * ----------------------------------------------------------------------- */
235 int
236 main(int argc, char** argv) {
239  // give some application descriptions
240  oc.setApplicationDescription("Shortest path router and DUE computer for the microscopic, multi-modal traffic simulation SUMO.");
241  oc.setApplicationName("duarouter", "Eclipse SUMO duarouter Version " VERSION_STRING);
242  int ret = 0;
243  RONet* net = nullptr;
244  try {
245  XMLSubSys::init();
247  OptionsIO::setArgs(argc, argv);
249  if (oc.processMetaOptions(argc < 2)) {
251  return 0;
252  }
254  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"), oc.getString("xml-validation.routes"));
255 #ifdef HAVE_FOX
256  if (oc.getInt("routing-threads") > 1) {
257  // make the output aware of threading
259  }
260 #endif
262  if (!RODUAFrame::checkOptions()) {
263  throw ProcessError();
264  }
266  // load data
267  ROLoader loader(oc, false, !oc.getBool("no-step-log"));
268  net = new RONet();
269  initNet(*net, loader, oc);
270  // build routes
271  try {
272  computeRoutes(*net, loader, oc);
273  } catch (XERCES_CPP_NAMESPACE::SAXParseException& e) {
274  WRITE_ERROR(toString(e.getLineNumber()));
275  ret = 1;
276  } catch (XERCES_CPP_NAMESPACE::SAXException& e) {
277  WRITE_ERROR(StringUtils::transcode(e.getMessage()));
278  ret = 1;
279  }
280  if (MsgHandler::getErrorInstance()->wasInformed() || ret != 0) {
281  throw ProcessError();
282  }
283  } catch (const ProcessError& e) {
284  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
285  WRITE_ERROR(e.what());
286  }
287  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
288  ret = 1;
289 #ifndef _DEBUG
290  } catch (const std::exception& e) {
291  if (std::string(e.what()) != std::string("")) {
292  WRITE_ERROR(e.what());
293  }
294  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
295  ret = 1;
296  } catch (...) {
297  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
298  ret = 1;
299 #endif
300  }
301  delete net;
303  if (ret == 0) {
304  std::cout << "Success." << std::endl;
305  }
306  return ret;
307 }
308 
309 
310 /****************************************************************************/
long long int SUMOTime
Definition: GUI.h:35
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:274
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:265
IntermodalRouter< ROEdge, ROLane, RONode, ROVehicle > ROIntermodalRouter
Definition: RORoutable.h:41
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:45
#define STEPS2TIME(x)
Definition: SUMOTime.h:54
#define SUMOTime_MAX
Definition: SUMOTime.h:33
@ SVC_IGNORING
vehicles ignoring classes
const std::string DEFAULT_VTYPE_ID
double gWeightsRandomFactor
Definition: StdDefs.cpp:30
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.
@ TAXI_PICKUP_ANYWHERE
taxi customer may be picked up anywhere
@ TAXI_DROPOFF_ANYWHERE
taxi customer may exit anywhere
@ PARKING_AREAS
parking areas
@ ALL_JUNCTIONS
junctions with edges allowing the additional mode
@ TAXI_PICKUP_PT
taxi customer may be picked up at public transport stop
@ PT_STOPS
public transport stops and access
@ TAXI_DROPOFF_PT
taxi customer may be picked up at public transport stop
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:79
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:116
static void setupI18n(const std::string &locale="")
set up gettext stuff
Definition: MsgHandler.cpp:228
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:255
static void setFactory(Factory func)
Sets the factory function to use for new MsgHandlers.
Definition: MsgHandler.h:65
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)
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:59
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.
Definition: RODUAFrame.cpp:43
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid for usage within duarouter.
Definition: RODUAFrame.cpp:184
A basic edge for routing applications.
Definition: ROEdge.h:70
static double getStoredEffort(const ROEdge *const edge, const ROVehicle *const, double time)
Definition: ROEdge.h:472
static bool initPriorityFactor(double priorityFactor)
initialize priority factor range
Definition: ROEdge.cpp:444
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:432
static double getNoiseEffort(const ROEdge *const edge, const ROVehicle *const veh, double time)
Definition: ROEdge.cpp:215
static double getTravelTimeStaticRandomized(const ROEdge *const edge, const ROVehicle *const veh, double time)
Definition: ROEdge.h:422
static void setGlobalOptions(const bool interpolate)
Definition: ROEdge.h:487
static double getTravelTimeStatic(const ROEdge *const edge, const ROVehicle *const veh, double time)
Returns the travel time for the given edge.
Definition: ROEdge.h:418
static const ROEdgeVector & getAllEdges()
Returns all ROEdges.
Definition: ROEdge.cpp:352
The data loader.
Definition: ROLoader.h:53
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:256
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:195
virtual void loadNet(RONet &toFill, ROAbstractEdgeBuilder &eb)
Loads the network.
Definition: ROLoader.cpp:112
void openRoutes(RONet &net)
Builds and opens all route loaders.
Definition: ROLoader.cpp:165
The router's network representation.
Definition: RONet.h:62
SUMOVTypeParameter * getVehicleTypeSecure(const std::string &id)
Retrieves the named vehicle type.
Definition: RONet.cpp:353
bool hasBidiEdges() const
return whether the network contains bidirectional rail edges
Definition: RONet.h:420
void cleanup()
closes the file output for computed routes and deletes associated threads if necessary
Definition: RONet.cpp:329
void openOutput(const OptionsCont &options)
Opens the output for computed routes.
Definition: RONet.cpp:281
void writeIntermodal(const OptionsCont &options, ROIntermodalRouter &router) const
Writes the intermodal network and weights if requested.
Definition: RONet.cpp:310
static void adaptIntermodalRouter(ROIntermodalRouter &router)
Definition: RONet.cpp:784
bool hasPermissions() const
Definition: RONet.cpp:829
bool hasLoadedEffort() const
whether efforts were loaded from file
Definition: RONet.cpp:840
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.
Definition: RandHelper.cpp:87
IntermodalRouter< E, L, N, V > & getIntermodalRouter() const
Structure representing possible vehicle parameter.
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
Definition: StringUtils.h:140
static void close()
Closes all of an applications subsystems.
static bool checkOptions()
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:65
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:54
int main(int argc, char **argv)
void initNet(RONet &net, ROLoader &loader, OptionsCont &oc)
void computeRoutes(RONet &net, ROLoader &loader, OptionsCont &oc)