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  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), &ROEdge::getTravelTimeStatic);
130  } else {
132  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), &ROEdge::getTravelTimeStatic);
133  }
134  } else if (routingAlgorithm == "CH") {
135  const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
136  string2time(oc.getString("weight-period")) :
138  if (net.hasPermissions()) {
140  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), &ROEdge::getTravelTimeStatic, SVC_IGNORING, weightPeriod, true);
141  } else {
143  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), &ROEdge::getTravelTimeStatic, SVC_IGNORING, weightPeriod, false);
144  }
145  } else if (routingAlgorithm == "CHWrapper") {
146  const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
147  string2time(oc.getString("weight-period")) :
151  begin, end, weightPeriod, oc.getInt("routing-threads"));
152  } else {
153  throw ProcessError("Unknown routing Algorithm '" + routingAlgorithm + "'!");
154  }
155  } else {
157  if (measure == "CO") {
158  op = &ROEdge::getEmissionEffort<PollutantsInterface::CO>;
159  } else if (measure == "CO2") {
160  op = &ROEdge::getEmissionEffort<PollutantsInterface::CO2>;
161  } else if (measure == "PMx") {
162  op = &ROEdge::getEmissionEffort<PollutantsInterface::PM_X>;
163  } else if (measure == "HC") {
164  op = &ROEdge::getEmissionEffort<PollutantsInterface::HC>;
165  } else if (measure == "NOx") {
166  op = &ROEdge::getEmissionEffort<PollutantsInterface::NO_X>;
167  } else if (measure == "fuel") {
168  op = &ROEdge::getEmissionEffort<PollutantsInterface::FUEL>;
169  } else if (measure == "electricity") {
170  op = &ROEdge::getEmissionEffort<PollutantsInterface::ELEC>;
171  } else if (measure == "noise") {
173  } else {
174  throw ProcessError("Unknown measure (weight attribute '" + measure + "')!");
175  }
176  if (net.hasPermissions()) {
178  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op, &ROEdge::getTravelTimeStatic);
179  } else {
181  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op, &ROEdge::getTravelTimeStatic);
182  }
183  }
186  // process route definitions
187  try {
188  net.openOutput(oc, altFilename);
189  loader.processRoutes(begin, end, string2time(oc.getString("route-steps")), net, provider);
190  // end the processing
191  net.cleanup();
192  } catch (ProcessError&) {
193  net.cleanup();
194  throw;
195  }
196 }
197 
198 
199 /* -------------------------------------------------------------------------
200  * main
201  * ----------------------------------------------------------------------- */
202 int
203 main(int argc, char** argv) {
205  // give some application descriptions
206  oc.setApplicationDescription("Shortest path router and DUE computer for the microscopic road traffic simulation SUMO.");
207  oc.setApplicationName("duarouter", "SUMO duarouter Version " VERSION_STRING);
208  int ret = 0;
209  RONet* net = 0;
210  try {
211  XMLSubSys::init();
213  OptionsIO::setArgs(argc, argv);
215  if (oc.processMetaOptions(argc < 2)) {
217  return 0;
218  }
219  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
221  if (!RODUAFrame::checkOptions()) {
222  throw ProcessError();
223  }
225  // load data
226  ROLoader loader(oc, false, !oc.getBool("no-step-log"));
227  net = new RONet();
228  initNet(*net, loader, oc);
229  // build routes
230  try {
231  computeRoutes(*net, loader, oc);
232  } catch (XERCES_CPP_NAMESPACE::SAXParseException& e) {
233  WRITE_ERROR(toString(e.getLineNumber()));
234  ret = 1;
235  } catch (XERCES_CPP_NAMESPACE::SAXException& e) {
236  WRITE_ERROR(TplConvert::_2str(e.getMessage()));
237  ret = 1;
238  }
239  if (MsgHandler::getErrorInstance()->wasInformed() || ret != 0) {
240  throw ProcessError();
241  }
242  } catch (const ProcessError& e) {
243  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
244  WRITE_ERROR(e.what());
245  }
246  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
247  ret = 1;
248 #ifndef _DEBUG
249  } catch (const std::exception& e) {
250  if (std::string(e.what()) != std::string("")) {
251  WRITE_ERROR(e.what());
252  }
253  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
254  ret = 1;
255  } catch (...) {
256  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
257  ret = 1;
258 #endif
259  }
260  delete net;
262  if (ret == 0) {
263  std::cout << "Success." << std::endl;
264  }
265  return ret;
266 }
267 
268 
269 
270 /****************************************************************************/
271 
Computes the shortest path through a contracted network.
Definition: CHRouter.h:69
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
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: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:142
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.
Computes the shortest path through a network using the A* algorithm.
Definition: AStarRouter.h:74
bool hasPermissions() const
Definition: RONet.cpp:697
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
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:237
static void initRandGlobal(MTRand *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:64
Interface for building instances of duarouter-edges.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
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:257
#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:393
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
The data loader.
Definition: ROLoader.h:63
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:47
void computeRoutes(RONet &net, ROLoader &loader, OptionsCont &oc)
virtual void loadNet(RONet &toFill, ROAbstractEdgeBuilder &eb)
Loads the network.
Definition: ROLoader.cpp:120
#define VERSION_STRING
Definition: config.h:210
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
The router&#39;s network representation.
Definition: RONet.h:76
static const ROEdgeVector & getAllEdges()
Returns all ROEdges.
Definition: ROEdge.cpp:278
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:197
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 double getNoiseEffort(const ROEdge *const edge, const ROVehicle *const veh, double time)
Definition: ROEdge.cpp:179
static void setGlobalOptions(const bool interpolate)
Definition: ROEdge.h:437
long long int SUMOTime
Definition: TraCIDefs.h:52
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:264
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.