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.sourceforge.net/
12 // Copyright (C) 2001-2012 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  net.openOutput(oc.getString("output-file"), true, oc.getString("vtype-output"));
112  // build the router
114  const std::string measure = oc.getString("weight-attribute");
115  const std::string routingAlgorithm = oc.getString("routing-algorithm");
116  if (measure == "traveltime") {
117  if (routingAlgorithm == "dijkstra") {
118  if (net.hasRestrictions()) {
120  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime);
121  } else {
123  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime);
124  }
125  } else if (routingAlgorithm == "astar") {
126  if (net.hasRestrictions()) {
128  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime);
129  } else {
131  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime);
132  }
133 #ifdef HAVE_INTERNAL // catchall for internal stuff
134  } else if (routingAlgorithm == "bulkstar") {
135  if (net.hasRestrictions()) {
136  router = new BulkStarRouterTT<ROEdge, ROVehicle, prohibited_withRestrictions<ROEdge, ROVehicle> >(
138  } else {
139  router = new BulkStarRouterTT<ROEdge, ROVehicle, prohibited_noRestrictions<ROEdge, ROVehicle> >(
141  }
142 
143  } else if (routingAlgorithm == "CH") {
144  // defaultVehicle is only in constructor and may be safely deleted
145  // it is mainly needed for its maximum speed. @todo XXX make this configurable
147  const SUMOTime begin = string2time(oc.getString("begin"));
148  const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
149  string2time(oc.getString("weight-period")) :
151  if (net.hasRestrictions()) {
152  router = new CHRouter<ROEdge, ROVehicle, prohibited_withRestrictions<ROEdge, ROVehicle> >(
153  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime, &defaultVehicle, begin, weightPeriod, true);
154  } else {
155  router = new CHRouter<ROEdge, ROVehicle, prohibited_noRestrictions<ROEdge, ROVehicle> >(
156  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime, &defaultVehicle, begin, weightPeriod, false);
157  }
158 
159  } else if (routingAlgorithm == "CHWrapper") {
160  const SUMOTime begin = string2time(oc.getString("begin"));
161  const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
162  string2time(oc.getString("weight-period")) :
164 
165  if (!net.hasRestrictions()) {
166  WRITE_WARNING("CHWrapper is only needed for a restricted network");
167  }
168  router = new CHRouterWrapper<ROEdge, ROVehicle, prohibited_withRestrictions<ROEdge, ROVehicle> >(
169  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTime, begin, weightPeriod);
170 
171 #endif // have HAVE_INTERNAL
172  } else {
173  throw ProcessError("Unknown routing Algorithm '" + routingAlgorithm + "'!");
174  }
175 
176  } else {
178  if (measure == "CO") {
179  op = &ROEdge::getCOEffort;
180  } else if (measure == "CO2") {
181  op = &ROEdge::getCO2Effort;
182  } else if (measure == "PMx") {
183  op = &ROEdge::getPMxEffort;
184  } else if (measure == "HC") {
185  op = &ROEdge::getHCEffort;
186  } else if (measure == "NOx") {
187  op = &ROEdge::getNOxEffort;
188  } else if (measure == "fuel") {
189  op = &ROEdge::getFuelEffort;
190  } else if (measure == "noise") {
192  } else {
193  net.closeOutput();
194  throw ProcessError("Unknown measure (weight attribute '" + measure + "')!");
195  }
196  if (net.hasRestrictions()) {
198  net.getEdgeNo(), oc.getBool("ignore-errors"), op, &ROEdge::getTravelTime);
199  } else {
201  net.getEdgeNo(), oc.getBool("ignore-errors"), op, &ROEdge::getTravelTime);
202  }
203  }
204  // process route definitions
205  try {
206  if (routingAlgorithm == "bulkstar") {
207 #ifdef HAVE_INTERNAL // catchall for internal stuff
208  // need to load all routes for spatial aggregation
209  loader.processAllRoutesWithBulkRouter(string2time(oc.getString("begin")), string2time(oc.getString("end")), net, *router);
210 #endif
211  } else if (!oc.getBool("unsorted-input")) {
212  // the routes are sorted - process stepwise
213  loader.processRoutesStepWise(string2time(oc.getString("begin")), string2time(oc.getString("end")), net, *router);
214  } else {
215  // the routes are not sorted: load all and process
216  loader.processAllRoutes(string2time(oc.getString("begin")), string2time(oc.getString("end")), net, *router);
217  }
218  // end the processing
219  net.closeOutput();
220  delete router;
222  } catch (ProcessError&) {
223  net.closeOutput();
224  delete router;
226  throw;
227  }
228 }
229 
230 
231 /* -------------------------------------------------------------------------
232  * main
233  * ----------------------------------------------------------------------- */
234 int
235 main(int argc, char** argv) {
237  // give some application descriptions
238  oc.setApplicationDescription("Shortest path router and DUE computer for the microscopic road traffic simulation SUMO.");
239  oc.setApplicationName("duarouter", "SUMO duarouter Version " + (std::string)VERSION_STRING);
240  int ret = 0;
241  RONet* net = 0;
242  try {
243  XMLSubSys::init();
245  OptionsIO::getOptions(true, argc, argv);
246  if (oc.processMetaOptions(argc < 2)) {
248  return 0;
249  }
250  XMLSubSys::setValidation(oc.getBool("xml-validation"));
252  if (!RODUAFrame::checkOptions()) {
253  throw ProcessError();
254  }
256  // load data
257  ROLoader loader(oc, false);
258  net = new RONet();
259  initNet(*net, loader, oc);
260  // build routes
261  try {
262  computeRoutes(*net, loader, oc);
263  } catch (XERCES_CPP_NAMESPACE::SAXParseException& e) {
264  WRITE_ERROR(toString(e.getLineNumber()));
265  ret = 1;
266  } catch (XERCES_CPP_NAMESPACE::SAXException& e) {
267  WRITE_ERROR(TplConvert::_2str(e.getMessage()));
268  ret = 1;
269  }
270  if (MsgHandler::getErrorInstance()->wasInformed() || ret != 0) {
271  throw ProcessError();
272  }
273  } catch (const ProcessError& e) {
274  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
275  WRITE_ERROR(e.what());
276  }
277  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
278  ret = 1;
279 #ifndef _DEBUG
280  } catch (const std::exception& e) {
281  if (std::string(e.what()) != std::string("")) {
282  WRITE_ERROR(e.what());
283  }
284  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
285  ret = 1;
286  } catch (...) {
287  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
288  ret = 1;
289 #endif
290  }
291  delete net;
293  if (ret == 0) {
294  std::cout << "Success." << std::endl;
295  }
296  return ret;
297 }
298 
299 
300 
301 /****************************************************************************/
302