SUMO - Simulation of Urban MObility
duarouter_main.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Main for DUAROUTER
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #ifdef HAVE_VERSION_H
34 #include <version.h>
35 #endif
36 
37 #include <xercesc/sax/SAXException.hpp>
38 #include <xercesc/sax/SAXParseException.hpp>
40 #include <iostream>
41 #include <string>
42 #include <limits.h>
43 #include <ctime>
44 #include <router/ROLoader.h>
45 #include <router/RONet.h>
46 #include <router/ROEdge.h>
50 #include <utils/vehicle/CHRouter.h>
52 #include "RODUAEdgeBuilder.h"
53 #include <router/ROFrame.h>
55 #include <utils/options/Option.h>
61 #include <utils/common/ToString.h>
62 #include <utils/xml/XMLSubSys.h>
63 #include "RODUAFrame.h"
65 
66 
67 // ===========================================================================
68 // functions
69 // ===========================================================================
70 /* -------------------------------------------------------------------------
71  * data processing methods
72  * ----------------------------------------------------------------------- */
78 void
79 initNet(RONet& net, ROLoader& loader, OptionsCont& oc) {
80  // load the net
81  RODUAEdgeBuilder builder;
82  ROEdge::setGlobalOptions(oc.getBool("weights.interpolate"));
83  loader.loadNet(net, builder);
84  // load the weights when wished/available
85  if (oc.isSet("weight-files")) {
86  loader.loadWeights(net, "weight-files", oc.getString("weight-attribute"), false, oc.getBool("weights.expand"));
87  }
88  if (oc.isSet("lane-weight-files")) {
89  loader.loadWeights(net, "lane-weight-files", oc.getString("weight-attribute"), true, oc.getBool("weights.expand"));
90  }
91 }
92 
93 
94 
98 void
99 computeRoutes(RONet& net, ROLoader& loader, OptionsCont& oc) {
100  // initialise the loader
101  loader.openRoutes(net);
102  // prepare the output
103  const std::string& filename = oc.getString("output-file");
104  std::string altFilename = filename + ".alt";
105  const int len = (int)filename.length();
106  if (len > 4 && filename.substr(len - 4) == ".xml") {
107  altFilename = filename.substr(0, len - 4) + ".alt.xml";
108  } else if (len > 4 && filename.substr(len - 4) == ".sbx") {
109  altFilename = filename.substr(0, len - 4) + ".alt.sbx";
110  }
111  // build the router
113  const std::string measure = oc.getString("weight-attribute");
114  const std::string routingAlgorithm = oc.getString("routing-algorithm");
115  const SUMOTime begin = string2time(oc.getString("begin"));
116  const SUMOTime end = string2time(oc.getString("end"));
117  if (measure == "traveltime") {
118  if (routingAlgorithm == "dijkstra") {
119  if (net.hasPermissions()) {
121  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), &ROEdge::getTravelTimeStatic);
122  } else {
124  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), &ROEdge::getTravelTimeStatic);
125  }
126  } else if (routingAlgorithm == "astar") {
127  if (net.hasPermissions()) {
129  const AStar::LookupTable* lookup = 0;
130  if (oc.isSet("astar.all-distances")) {
131  lookup = new AStar::FLT(oc.getString("astar.all-distances"), (int)ROEdge::getAllEdges().size());
132  } else if (oc.isSet("astar.landmark-distances")) {
135  begin, end, std::numeric_limits<int>::max(), 1);
136  ROVehicle defaultVehicle(SUMOVehicleParameter(), 0, net.getVehicleTypeSecure(DEFAULT_VTYPE_ID), &net);
137  lookup = new AStar::LMLT(oc.getString("astar.landmark-distances"), ROEdge::getAllEdges(), &router, &defaultVehicle,
138  oc.isSet("astar.save-landmark-distances") ? oc.getString("astar.save-landmark-distances") : "", oc.getInt("routing-threads"));
139  }
140  router = new AStar(ROEdge::getAllEdges(), oc.getBool("ignore-errors"), &ROEdge::getTravelTimeStatic, lookup);
141  } else {
143  const AStar::LookupTable* lookup = 0;
144  if (oc.isSet("astar.all-distances")) {
145  lookup = new AStar::FLT(oc.getString("astar.all-distances"), (int)ROEdge::getAllEdges().size());
146  } else if (oc.isSet("astar.landmark-distances")) {
149  begin, end, std::numeric_limits<int>::max(), 1);
150  ROVehicle defaultVehicle(SUMOVehicleParameter(), 0, net.getVehicleTypeSecure(DEFAULT_VTYPE_ID), &net);
151  lookup = new AStar::LMLT(oc.getString("astar.landmark-distances"), ROEdge::getAllEdges(), &router, &defaultVehicle,
152  oc.isSet("astar.save-landmark-distances") ? oc.getString("astar.save-landmark-distances") : "", oc.getInt("routing-threads"));
153  }
154  router = new AStar(ROEdge::getAllEdges(), oc.getBool("ignore-errors"), &ROEdge::getTravelTimeStatic, lookup);
155  }
156  } else if (routingAlgorithm == "CH") {
157  const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
158  string2time(oc.getString("weight-period")) :
160  if (net.hasPermissions()) {
162  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), &ROEdge::getTravelTimeStatic, SVC_IGNORING, weightPeriod, true);
163  } else {
165  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), &ROEdge::getTravelTimeStatic, SVC_IGNORING, weightPeriod, false);
166  }
167  } else if (routingAlgorithm == "CHWrapper") {
168  const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
169  string2time(oc.getString("weight-period")) :
173  begin, end, weightPeriod, oc.getInt("routing-threads"));
174  } else {
175  throw ProcessError("Unknown routing Algorithm '" + routingAlgorithm + "'!");
176  }
177  } else {
179  if (measure == "CO") {
180  op = &ROEdge::getEmissionEffort<PollutantsInterface::CO>;
181  } else if (measure == "CO2") {
182  op = &ROEdge::getEmissionEffort<PollutantsInterface::CO2>;
183  } else if (measure == "PMx") {
184  op = &ROEdge::getEmissionEffort<PollutantsInterface::PM_X>;
185  } else if (measure == "HC") {
186  op = &ROEdge::getEmissionEffort<PollutantsInterface::HC>;
187  } else if (measure == "NOx") {
188  op = &ROEdge::getEmissionEffort<PollutantsInterface::NO_X>;
189  } else if (measure == "fuel") {
190  op = &ROEdge::getEmissionEffort<PollutantsInterface::FUEL>;
191  } else if (measure == "electricity") {
192  op = &ROEdge::getEmissionEffort<PollutantsInterface::ELEC>;
193  } else if (measure == "noise") {
195  } else {
196  throw ProcessError("Unknown measure (weight attribute '" + measure + "')!");
197  }
198  if (net.hasPermissions()) {
200  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op, &ROEdge::getTravelTimeStatic);
201  } else {
203  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op, &ROEdge::getTravelTimeStatic);
204  }
205  }
208  // process route definitions
209  try {
210  net.openOutput(oc, altFilename);
211  loader.processRoutes(begin, end, string2time(oc.getString("route-steps")), net, provider);
212  // end the processing
213  net.cleanup();
214  } catch (ProcessError&) {
215  net.cleanup();
216  throw;
217  }
218 }
219 
220 
221 /* -------------------------------------------------------------------------
222  * main
223  * ----------------------------------------------------------------------- */
224 int
225 main(int argc, char** argv) {
227  // give some application descriptions
228  oc.setApplicationDescription("Shortest path router and DUE computer for the microscopic road traffic simulation SUMO.");
229  oc.setApplicationName("duarouter", "SUMO duarouter Version " VERSION_STRING);
230  int ret = 0;
231  RONet* net = 0;
232  try {
233  XMLSubSys::init();
235  OptionsIO::setArgs(argc, argv);
237  if (oc.processMetaOptions(argc < 2)) {
239  return 0;
240  }
241  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
243  if (!RODUAFrame::checkOptions()) {
244  throw ProcessError();
245  }
247  // load data
248  ROLoader loader(oc, false, !oc.getBool("no-step-log"));
249  net = new RONet();
250  initNet(*net, loader, oc);
251  // build routes
252  try {
253  computeRoutes(*net, loader, oc);
254  } catch (XERCES_CPP_NAMESPACE::SAXParseException& e) {
255  WRITE_ERROR(toString(e.getLineNumber()));
256  ret = 1;
257  } catch (XERCES_CPP_NAMESPACE::SAXException& e) {
258  WRITE_ERROR(TplConvert::_2str(e.getMessage()));
259  ret = 1;
260  }
261  if (MsgHandler::getErrorInstance()->wasInformed() || ret != 0) {
262  throw ProcessError();
263  }
264  } catch (const ProcessError& e) {
265  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
266  WRITE_ERROR(e.what());
267  }
268  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
269  ret = 1;
270 #ifndef _DEBUG
271  } catch (const std::exception& e) {
272  if (std::string(e.what()) != std::string("")) {
273  WRITE_ERROR(e.what());
274  }
275  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
276  ret = 1;
277  } catch (...) {
278  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
279  ret = 1;
280 #endif
281  }
282  delete net;
284  if (ret == 0) {
285  std::cout << "Success." << std::endl;
286  }
287  return ret;
288 }
289 
290 
291 
292 /****************************************************************************/
293 
Computes the shortest path through a contracted network.
Definition: CHRouter.h:69
bool hasPermissions() const
Definition: RONet.cpp:697
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:54
Computes the shortest path through a network using the Dijkstra algorithm.
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:76
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:82
int main(int argc, char **argv)
void openRoutes(RONet &net)
Builds and opens all route loaders.
Definition: ROLoader.cpp:161
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid for usage within duarouter...
Definition: RODUAFrame.cpp:163
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:65
void initNet(RONet &net, ROLoader &loader, OptionsCont &oc)
static void adaptIntermodalRouter(ROIntermodalRouter &router)
Definition: RONet.cpp:665
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)
Computes the shortest path through a network using the A* algorithm.
Definition: AStarRouter.h:85
const std::string DEFAULT_VTYPE_ID
static void close()
Closes all of an applications subsystems.
Computes the shortest path through a network using the Dijkstra algorithm.
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:62
IntermodalRouter< ROEdge, ROLane, RONode, ROVehicle > ROIntermodalRouter
Definition: RORoutable.h:51
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
void openOutput(const OptionsCont &options, const std::string altFilename="")
Opens the output for computed routes.
Definition: RONet.cpp:239
Interface for building instances of duarouter-edges.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
A vehicle as used by router.
Definition: ROVehicle.h:60
static void fillOptions()
Inserts options used by duarouter into the OptionsCont-singleton.
Definition: RODUAFrame.cpp:52
void cleanup()
closes the file output for computed routes and deletes associated threads if necessary ...
Definition: RONet.cpp:259
#define max(a, b)
Definition: polyfonts.c:65
static double getTravelTimeStatic(const ROEdge *const edge, const ROVehicle *const veh, double time)
Returns the travel time for the given edge.
Definition: ROEdge.h:405
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
The data loader.
Definition: ROLoader.h:63
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:47
#define VERSION_STRING
Definition: version.h:1
void computeRoutes(RONet &net, ROLoader &loader, OptionsCont &oc)
virtual void loadNet(RONet &toFill, ROAbstractEdgeBuilder &eb)
Loads the network.
Definition: ROLoader.cpp:120
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
The router&#39;s network representation.
Definition: RONet.h:76
Structure representing possible vehicle parameter.
static const ROEdgeVector & getAllEdges()
Returns all ROEdges.
Definition: ROEdge.cpp:320
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:191
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:85
A storage for options typed value containers)
Definition: OptionsCont.h:99
static void initRandGlobal(std::mt19937 *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:77
static double getNoiseEffort(const ROEdge *const edge, const ROVehicle *const veh, double time)
Definition: ROEdge.cpp:199
static void setGlobalOptions(const bool interpolate)
Definition: ROEdge.h:455
SUMOVTypeParameter * getVehicleTypeSecure(const std::string &id)
Retrieves the named vehicle type.
Definition: RONet.cpp:283
long long int SUMOTime
Definition: TraCIDefs.h:52
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
static void initOutputOptions()
Definition: MsgHandler.cpp:193
static std::string _2str(const int var)
convert int to string
Definition: TplConvert.h:57
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:258
vehicles ignoring classes
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
Computes the shortest path through a contracted network.
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.