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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // Main for DUAROUTER
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #ifdef HAVE_VERSION_H
27 #include <version.h>
28 #endif
29 
30 #include <iostream>
31 #include <string>
32 #include <limits.h>
33 #include <ctime>
34 #include <memory>
35 #include <xercesc/sax/SAXException.hpp>
36 #include <xercesc/sax/SAXParseException.hpp>
42 #include <utils/common/ToString.h>
44 #include <utils/options/Option.h>
49 #include <utils/router/CHRouter.h>
51 #include <utils/xml/XMLSubSys.h>
52 #include <router/ROFrame.h>
53 #include <router/ROLoader.h>
54 #include <router/RONet.h>
55 #include <router/ROEdge.h>
56 #include "RODUAEdgeBuilder.h"
57 #include "RODUAFrame.h"
58 
59 
60 // ===========================================================================
61 // functions
62 // ===========================================================================
63 /* -------------------------------------------------------------------------
64  * data processing methods
65  * ----------------------------------------------------------------------- */
71 void
72 initNet(RONet& net, ROLoader& loader, OptionsCont& oc) {
73  // load the net
74  RODUAEdgeBuilder builder;
75  ROEdge::setGlobalOptions(oc.getBool("weights.interpolate"));
76  loader.loadNet(net, builder);
77  // load the weights when wished/available
78  if (oc.isSet("weight-files")) {
79  loader.loadWeights(net, "weight-files", oc.getString("weight-attribute"), false, oc.getBool("weights.expand"));
80  }
81  if (oc.isSet("lane-weight-files")) {
82  loader.loadWeights(net, "lane-weight-files", oc.getString("weight-attribute"), true, oc.getBool("weights.expand"));
83  }
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 SUMOTime begin = string2time(oc.getString("begin"));
101  const SUMOTime end = string2time(oc.getString("end"));
102  if (measure == "traveltime") {
103  if (routingAlgorithm == "dijkstra") {
104  if (net.hasPermissions()) {
106  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction);
107  } else {
109  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction);
110  }
111  } else if (routingAlgorithm == "astar") {
112  if (net.hasPermissions()) {
114  std::shared_ptr<const AStar::LookupTable> lookup;
115  if (oc.isSet("astar.all-distances")) {
116  lookup = std::make_shared<const AStar::FLT>(oc.getString("astar.all-distances"), (int)ROEdge::getAllEdges().size());
117  } else if (oc.isSet("astar.landmark-distances")) {
120  begin, end, std::numeric_limits<int>::max(), 1);
121  ROVehicle defaultVehicle(SUMOVehicleParameter(), nullptr, net.getVehicleTypeSecure(DEFAULT_VTYPE_ID), &net);
122  lookup = std::make_shared<const AStar::LMLT>(oc.getString("astar.landmark-distances"), ROEdge::getAllEdges(), &router, &defaultVehicle,
123  oc.isSet("astar.save-landmark-distances") ? oc.getString("astar.save-landmark-distances") : "", oc.getInt("routing-threads"));
124  }
125  router = new AStar(ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction, lookup);
126  } else {
128  std::shared_ptr<const AStar::LookupTable> lookup;
129  if (oc.isSet("astar.all-distances")) {
130  lookup = std::make_shared<const AStar::FLT>(oc.getString("astar.all-distances"), (int)ROEdge::getAllEdges().size());
131  } else if (oc.isSet("astar.landmark-distances")) {
134  begin, end, std::numeric_limits<int>::max(), 1);
135  ROVehicle defaultVehicle(SUMOVehicleParameter(), nullptr, net.getVehicleTypeSecure(DEFAULT_VTYPE_ID), &net);
136  lookup = std::make_shared<const AStar::LMLT>(oc.getString("astar.landmark-distances"), ROEdge::getAllEdges(), &router, &defaultVehicle,
137  oc.isSet("astar.save-landmark-distances") ? oc.getString("astar.save-landmark-distances") : "", oc.getInt("routing-threads"));
138  }
139  router = new AStar(ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction, lookup);
140  }
141  } else if (routingAlgorithm == "CH") {
142  const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
143  string2time(oc.getString("weight-period")) :
144  std::numeric_limits<int>::max());
145  if (net.hasPermissions()) {
147  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), &ROEdge::getTravelTimeStatic, SVC_IGNORING, weightPeriod, true);
148  } else {
150  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), &ROEdge::getTravelTimeStatic, SVC_IGNORING, weightPeriod, false);
151  }
152  } else if (routingAlgorithm == "CHWrapper") {
153  const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
154  string2time(oc.getString("weight-period")) :
155  std::numeric_limits<int>::max());
158  begin, end, weightPeriod, oc.getInt("routing-threads"));
159  } else {
160  throw ProcessError("Unknown routing Algorithm '" + routingAlgorithm + "'!");
161  }
162  } else {
164  if (measure == "CO") {
165  op = &ROEdge::getEmissionEffort<PollutantsInterface::CO>;
166  } else if (measure == "CO2") {
167  op = &ROEdge::getEmissionEffort<PollutantsInterface::CO2>;
168  } else if (measure == "PMx") {
169  op = &ROEdge::getEmissionEffort<PollutantsInterface::PM_X>;
170  } else if (measure == "HC") {
171  op = &ROEdge::getEmissionEffort<PollutantsInterface::HC>;
172  } else if (measure == "NOx") {
173  op = &ROEdge::getEmissionEffort<PollutantsInterface::NO_X>;
174  } else if (measure == "fuel") {
175  op = &ROEdge::getEmissionEffort<PollutantsInterface::FUEL>;
176  } else if (measure == "electricity") {
177  op = &ROEdge::getEmissionEffort<PollutantsInterface::ELEC>;
178  } else if (measure == "noise") {
180  } else {
181  throw ProcessError("Unknown measure (weight attribute '" + measure + "')!");
182  }
183  if (net.hasPermissions()) {
185  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op, ttFunction);
186  } else {
188  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op, ttFunction);
189  }
190  }
191  int carWalk = 0;
192  for (const std::string& opt : oc.getStringVector("persontrip.transfer.car-walk")) {
193  if (opt == "parkingAreas") {
195  } else if (opt == "ptStops") {
197  } else if (opt == "allJunctions") {
199  }
200  }
202  new ROIntermodalRouter(RONet::adaptIntermodalRouter, carWalk, routingAlgorithm));
203  // process route definitions
204  try {
205  net.openOutput(oc);
206  loader.processRoutes(begin, end, string2time(oc.getString("route-steps")), net, provider);
207  net.writeIntermodal(oc, provider.getIntermodalRouter());
208  // end the processing
209  net.cleanup();
210  } catch (ProcessError&) {
211  net.cleanup();
212  throw;
213  }
214 }
215 
216 
217 /* -------------------------------------------------------------------------
218  * main
219  * ----------------------------------------------------------------------- */
220 int
221 main(int argc, char** argv) {
223  // give some application descriptions
224  oc.setApplicationDescription("Shortest path router and DUE computer for the microscopic, multi-modal traffic simulation SUMO.");
225  oc.setApplicationName("duarouter", "Eclipse SUMO duarouter Version " VERSION_STRING);
226  int ret = 0;
227  RONet* net = nullptr;
228  try {
229  XMLSubSys::init();
231  OptionsIO::setArgs(argc, argv);
233  if (oc.processMetaOptions(argc < 2)) {
235  return 0;
236  }
237  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
240  throw ProcessError();
241  }
243  // load data
244  ROLoader loader(oc, false, !oc.getBool("no-step-log"));
245  net = new RONet();
246  initNet(*net, loader, oc);
247  // build routes
248  try {
249  computeRoutes(*net, loader, oc);
250  } catch (XERCES_CPP_NAMESPACE::SAXParseException& e) {
251  WRITE_ERROR(toString(e.getLineNumber()));
252  ret = 1;
253  } catch (XERCES_CPP_NAMESPACE::SAXException& e) {
254  WRITE_ERROR(StringUtils::transcode(e.getMessage()));
255  ret = 1;
256  }
257  if (MsgHandler::getErrorInstance()->wasInformed() || ret != 0) {
258  throw ProcessError();
259  }
260  } catch (const ProcessError& e) {
261  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
262  WRITE_ERROR(e.what());
263  }
264  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
265  ret = 1;
266 #ifndef _DEBUG
267  } catch (const std::exception& e) {
268  if (std::string(e.what()) != std::string("")) {
269  WRITE_ERROR(e.what());
270  }
271  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
272  ret = 1;
273  } catch (...) {
274  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
275  ret = 1;
276 #endif
277  }
278  delete net;
280  if (ret == 0) {
281  std::cout << "Success." << std::endl;
282  }
283  return ret;
284 }
285 
286 
287 
288 /****************************************************************************/
289 
Computes the shortest path through a contracted network.
Definition: CHRouter.h:63
IntermodalRouter< E, L, N, V > & getIntermodalRouter() const
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:48
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:81
public transport stops and access
long long int SUMOTime
Definition: SUMOTime.h:35
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:76
int main(int argc, char **argv)
void writeIntermodal(const OptionsCont &options, ROIntermodalRouter &router) const
Writes the intermodal network and weights if requested.
Definition: RONet.cpp:236
void openRoutes(RONet &net)
Builds and opens all route loaders.
Definition: ROLoader.cpp:156
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid for usage within duarouter...
Definition: RODUAFrame.cpp:178
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:59
void initNet(RONet &net, ROLoader &loader, OptionsCont &oc)
static void adaptIntermodalRouter(ROIntermodalRouter &router)
Definition: RONet.cpp:661
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
Computes the shortest path through a network using the A* algorithm.
Definition: AStarRouter.h:78
bool hasPermissions() const
Definition: RONet.cpp:693
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const std::string DEFAULT_VTYPE_ID
static void close()
Closes all of an applications subsystems.
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:55
IntermodalRouter< ROEdge, ROLane, RONode, ROVehicle > ROIntermodalRouter
Definition: RORoutable.h:44
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
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:132
Interface for building instances of duarouter-edges.
void openOutput(const OptionsCont &options)
Opens the output for computed routes.
Definition: RONet.cpp:215
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
A vehicle as used by router.
Definition: ROVehicle.h:53
static void fillOptions()
Inserts options used by duarouter into the OptionsCont-singleton.
Definition: RODUAFrame.cpp:45
void cleanup()
closes the file output for computed routes and deletes associated threads if necessary ...
Definition: RONet.cpp:255
static double getTravelTimeStatic(const ROEdge *const edge, const ROVehicle *const veh, double time)
Returns the travel time for the given edge.
Definition: ROEdge.h:396
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
Computes the shortest path through a network using the Dijkstra algorithm.
The data loader.
Definition: ROLoader.h:56
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:42
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String) ...
static bool checkOptions()
checks shared options and sets StdDefs
void computeRoutes(RONet &net, ROLoader &loader, OptionsCont &oc)
virtual void loadNet(RONet &toFill, ROAbstractEdgeBuilder &eb)
Loads the network.
Definition: ROLoader.cpp:114
double gWeightsRandomFactor
Definition: StdDefs.cpp:31
junctions with edges allowing the additional mode
#define VERSION_STRING
Definition: config.h:207
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
The router&#39;s network representation.
Definition: RONet.h:64
Structure representing possible vehicle parameter.
static const ROEdgeVector & getAllEdges()
Returns all ROEdges.
Definition: ROEdge.cpp:336
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:186
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:118
A storage for options typed value containers)
Definition: OptionsCont.h:90
static void initRandGlobal(std::mt19937 *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:72
static double getNoiseEffort(const ROEdge *const edge, const ROVehicle *const veh, double time)
Definition: ROEdge.cpp:209
static void setGlobalOptions(const bool interpolate)
Definition: ROEdge.h:450
SUMOVTypeParameter * getVehicleTypeSecure(const std::string &id)
Retrieves the named vehicle type.
Definition: RONet.cpp:279
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:208
static double getTravelTimeStaticRandomized(const ROEdge *const edge, const ROVehicle *const veh, double time)
Definition: ROEdge.h:400
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:247
vehicles ignoring classes
Computes the shortest path through a contracted network.
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.