SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-sim.org/
12 // Copyright (C) 2001-2013 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>
51 #include "RODUAEdgeBuilder.h"
52 #include <router/ROFrame.h>
54 #include <utils/options/Option.h>
60 #include <utils/common/ToString.h>
61 #include <utils/xml/XMLSubSys.h>
62 #include "RODUAFrame.h"
64 
65 #ifdef HAVE_INTERNAL // catchall for internal stuff
66 #include <internal/BulkStarRouter.h>
67 #include <internal/CHRouter.h>
68 #include <internal/CHRouterWrapper.h>
69 #endif // have HAVE_INTERNAL
70 
71 #ifdef CHECK_MEMORY_LEAKS
72 #include <foreign/nvwa/debug_new.h>
73 #endif // CHECK_MEMORY_LEAKS
74 
75 
76 // ===========================================================================
77 // functions
78 // ===========================================================================
79 /* -------------------------------------------------------------------------
80  * data processing methods
81  * ----------------------------------------------------------------------- */
87 void
88 initNet(RONet& net, ROLoader& loader, OptionsCont& oc) {
89  // load the net
90  RODUAEdgeBuilder builder(oc.getBool("weights.expand"), oc.getBool("weights.interpolate"));
91  loader.loadNet(net, builder);
92  // load the weights when wished/available
93  if (oc.isSet("weight-files")) {
94  loader.loadWeights(net, "weight-files", oc.getString("weight-attribute"), false);
95  }
96  if (oc.isSet("lane-weight-files")) {
97  loader.loadWeights(net, "lane-weight-files", oc.getString("weight-attribute"), true);
98  }
99 }
100 
101 
102 
106 void
108  // initialise the loader
109  loader.openRoutes(net);
110  // prepare the output
111  const std::string& filename = oc.getString("output-file");
112  std::string altFilename = filename + ".alt";
113  const size_t len = filename.length();
114  if (len > 4 && filename.substr(len - 4) == ".xml") {
115  altFilename = filename.substr(0, len - 4) + ".alt.xml";
116  } else if (len > 4 && filename.substr(len - 4) == ".sbx") {
117  altFilename = filename.substr(0, len - 4) + ".alt.sbx";
118  }
119  // build the router
121  const std::string measure = oc.getString("weight-attribute");
122  const std::string routingAlgorithm = oc.getString("routing-algorithm");
123  if (measure == "traveltime") {
124  if (routingAlgorithm == "dijkstra") {
125  if (net.hasRestrictions()) {
127  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime);
128  } else {
130  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime);
131  }
132  } else if (routingAlgorithm == "astar") {
133  if (net.hasRestrictions()) {
135  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime);
136  } else {
138  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime);
139  }
140 #ifdef HAVE_INTERNAL // catchall for internal stuff
141  } else if (routingAlgorithm == "bulkstar") {
142  if (net.hasRestrictions()) {
143  router = new BulkStarRouterTT<ROEdge, ROVehicle, prohibited_withRestrictions<ROEdge, ROVehicle> >(
145  } else {
146  router = new BulkStarRouterTT<ROEdge, ROVehicle, prohibited_noRestrictions<ROEdge, ROVehicle> >(
148  }
149 
150  } else if (routingAlgorithm == "CH") {
151  // defaultVehicle is only in constructor and may be safely deleted
152  // it is mainly needed for its maximum speed. @todo XXX make this configurable
154  const SUMOTime begin = string2time(oc.getString("begin"));
155  const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
156  string2time(oc.getString("weight-period")) :
158  if (net.hasRestrictions()) {
159  router = new CHRouter<ROEdge, ROVehicle, prohibited_withRestrictions<ROEdge, ROVehicle> >(
160  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime, &defaultVehicle, begin, weightPeriod, true);
161  } else {
162  router = new CHRouter<ROEdge, ROVehicle, prohibited_noRestrictions<ROEdge, ROVehicle> >(
163  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime, &defaultVehicle, begin, weightPeriod, false);
164  }
165 
166  } else if (routingAlgorithm == "CHWrapper") {
167  const SUMOTime begin = string2time(oc.getString("begin"));
168  const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
169  string2time(oc.getString("weight-period")) :
171 
172  router = new CHRouterWrapper<ROEdge, ROVehicle, prohibited_withRestrictions<ROEdge, ROVehicle> >(
173  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime, begin, weightPeriod);
174 
175 #endif // have HAVE_INTERNAL
176  } else {
177  throw ProcessError("Unknown routing Algorithm '" + routingAlgorithm + "'!");
178  }
179 
180  } else {
182  if (measure == "CO") {
183  op = &ROEdge::getCOEffort;
184  } else if (measure == "CO2") {
185  op = &ROEdge::getCO2Effort;
186  } else if (measure == "PMx") {
187  op = &ROEdge::getPMxEffort;
188  } else if (measure == "HC") {
189  op = &ROEdge::getHCEffort;
190  } else if (measure == "NOx") {
191  op = &ROEdge::getNOxEffort;
192  } else if (measure == "fuel") {
193  op = &ROEdge::getFuelEffort;
194  } else if (measure == "noise") {
196  } else {
197  net.closeOutput();
198  throw ProcessError("Unknown measure (weight attribute '" + measure + "')!");
199  }
200  if (net.hasRestrictions()) {
202  net.getEdgeNo(), oc.getBool("ignore-errors"), op, &ROEdge::getTravelTime);
203  } else {
205  net.getEdgeNo(), oc.getBool("ignore-errors"), op, &ROEdge::getTravelTime);
206  }
207  }
208  net.openOutput(filename, altFilename, oc.getString("vtype-output"));
209  // process route definitions
210  try {
211  if (routingAlgorithm == "bulkstar") {
212 #ifdef HAVE_INTERNAL // catchall for internal stuff
213  // need to load all routes for spatial aggregation
214  loader.processAllRoutesWithBulkRouter(string2time(oc.getString("begin")), string2time(oc.getString("end")), net, *router);
215 #endif
216  } else {
217  loader.processRoutes(string2time(oc.getString("begin")), string2time(oc.getString("end")), net, *router);
218  }
219  // end the processing
220  net.closeOutput();
221  delete router;
223  } catch (ProcessError&) {
224  net.closeOutput();
225  delete router;
227  throw;
228  }
229 }
230 
231 
232 /* -------------------------------------------------------------------------
233  * main
234  * ----------------------------------------------------------------------- */
235 int
236 main(int argc, char** argv) {
238  // give some application descriptions
239  oc.setApplicationDescription("Shortest path router and DUE computer for the microscopic road traffic simulation SUMO.");
240  oc.setApplicationName("duarouter", "SUMO duarouter Version " + (std::string)VERSION_STRING);
241  int ret = 0;
242  RONet* net = 0;
243  try {
244  XMLSubSys::init();
246  OptionsIO::getOptions(true, argc, argv);
247  if (oc.processMetaOptions(argc < 2)) {
249  return 0;
250  }
251  XMLSubSys::setValidation(oc.getBool("xml-validation"));
253  if (!RODUAFrame::checkOptions()) {
254  throw ProcessError();
255  }
257  // load data
258  ROLoader loader(oc, false, !oc.getBool("no-step-log"));
259  net = new RONet();
260  initNet(*net, loader, oc);
261  // build routes
262  try {
263  computeRoutes(*net, loader, oc);
264  } catch (XERCES_CPP_NAMESPACE::SAXParseException& e) {
265  WRITE_ERROR(toString(e.getLineNumber()));
266  ret = 1;
267  } catch (XERCES_CPP_NAMESPACE::SAXException& e) {
268  WRITE_ERROR(TplConvert::_2str(e.getMessage()));
269  ret = 1;
270  }
271  if (MsgHandler::getErrorInstance()->wasInformed() || ret != 0) {
272  throw ProcessError();
273  }
274  } catch (const ProcessError& e) {
275  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
276  WRITE_ERROR(e.what());
277  }
278  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
279  ret = 1;
280 #ifndef _DEBUG
281  } catch (const std::exception& e) {
282  if (std::string(e.what()) != std::string("")) {
283  WRITE_ERROR(e.what());
284  }
285  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
286  ret = 1;
287  } catch (...) {
288  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
289  ret = 1;
290 #endif
291  }
292  delete net;
294  if (ret == 0) {
295  std::cout << "Success." << std::endl;
296  }
297  return ret;
298 }
299 
300 
301 
302 /****************************************************************************/
303 
void processRoutes(SUMOTime start, SUMOTime end, RONet &net, SUMOAbstractRouter< ROEdge, ROVehicle > &router)
Loads routes from all previously build route loaders.
Definition: ROLoader.cpp:188
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:57
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
static void getOptions(bool loadConfig, int argc=0, char **argv=0)
Parses the command line arguments and loads the configuration optionally.
Definition: OptionsIO.cpp:64
void openRoutes(RONet &net)
Builds and opens all route loaders.
Definition: ROLoader.cpp:163
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid for usage within duarouter...
Definition: RODUAFrame.cpp:140
void initNet(RONet &net, ROLoader &loader, OptionsCont &oc)
unsigned int getEdgeNo() const
Returns the number of edges thenetwork contains.
Definition: RONet.cpp:382
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)
bool loadWeights(RONet &net, const std::string &optionName, const std::string &measure, bool useLanes)
Loads the net weights.
Definition: ROLoader.cpp:258
SUMOReal getPMxEffort(const ROVehicle *const veh, SUMOReal time) const
Definition: ROEdge.cpp:208
SUMOReal getCO2Effort(const ROVehicle *const veh, SUMOReal time) const
Definition: ROEdge.cpp:196
const std::string DEFAULT_VTYPE_ID
static void close()
Closes all of an applications subsystems.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
static void initRandGlobal(MTRand *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:68
void openOutput(const std::string &filename, const std::string altFilename, const std::string typeFilename)
Opens the output for computed routes.
Definition: RONet.cpp:106
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:57
void closeOutput()
closes the file output for computed routes
Definition: RONet.cpp:125
static void fillOptions()
Inserts options used by duarouter into the OptionsCont-singleton.
Definition: RODUAFrame.cpp:56
#define max(a, b)
Definition: polyfonts.c:61
static std::string _2str(const E *const data)
Definition: TplConvert.h:56
SUMOReal getHCEffort(const ROVehicle *const veh, SUMOReal time) const
Definition: ROEdge.cpp:220
The data loader.
Definition: ROLoader.h:62
SUMOReal getNOxEffort(const ROVehicle *const veh, SUMOReal time) const
Definition: ROEdge.cpp:232
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:48
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:51
void computeRoutes(RONet &net, ROLoader &loader, OptionsCont &oc)
virtual void loadNet(RONet &toFill, ROAbstractEdgeBuilder &eb)
Loads the network.
Definition: ROLoader.cpp:128
#define VERSION_STRING
Definition: config.h:233
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:201
static void setValidation(bool enableValidation)
Enables or disables validation.
Definition: XMLSubSys.cpp:68
The router&#39;s network representation.
Definition: RONet.h:65
Structure representing possible vehicle parameter.
SUMOReal getFuelEffort(const ROVehicle *const veh, SUMOReal time) const
Definition: ROEdge.cpp:244
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:89
A storage for options typed value containers)
Definition: OptionsCont.h:108
bool hasRestrictions() const
Definition: RONet.cpp:394
int SUMOTime
Definition: SUMOTime.h:43
static void cleanup()
SUMOReal getNoiseEffort(const ROVehicle *const veh, SUMOReal time) const
Definition: ROEdge.cpp:256
SUMOReal getCOEffort(const ROVehicle *const veh, SUMOReal time) const
Definition: ROEdge.cpp:184
SUMOVTypeParameter * getVehicleTypeSecure(const std::string &id)
Retrieves the named vehicle type.
Definition: RONet.cpp:143
SUMOReal getTravelTime(const ROVehicle *const veh, SUMOReal time) const
Returns the travel time for this edge.
Definition: ROEdge.cpp:146
SUMOReal getMinimumTravelTime(const ROVehicle *const veh) const
Returns the travel time for this edge without using any stored timeLine.
Definition: ROEdge.cpp:178
static void initOutputOptions()
Definition: MsgHandler.cpp:193
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
int main(int argc, char *argv[])
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.