SUMO - Simulation of Urban MObility
marouter_main.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Main for MAROUTER
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2015 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #ifdef HAVE_VERSION_H
35 #include <version.h>
36 #endif
37 
38 #include <xercesc/sax/SAXException.hpp>
39 #include <xercesc/sax/SAXParseException.hpp>
41 #include <iostream>
42 #include <string>
43 #include <limits.h>
44 #include <ctime>
45 #include <vector>
46 #include <router/ROLoader.h>
47 #include <router/RONet.h>
48 #include <router/ROEdge.h>
54 #include <utils/vehicle/CHRouter.h>
56 #include <router/ROFrame.h>
58 #include <utils/options/Option.h>
64 #include <utils/common/ToString.h>
65 #include <utils/xml/XMLSubSys.h>
67 #include <utils/options/Option.h>
70 #include <od/ODCell.h>
71 #include <od/ODDistrict.h>
72 #include <od/ODDistrictCont.h>
73 #include <od/ODDistrictHandler.h>
74 #include <od/ODMatrix.h>
76 
77 #include "ROMAFrame.h"
78 #include "ROMAAssignments.h"
79 #include "ROMAEdgeBuilder.h"
80 
81 #ifdef CHECK_MEMORY_LEAKS
82 #include <foreign/nvwa/debug_new.h>
83 #endif // CHECK_MEMORY_LEAKS
84 
85 
86 // ===========================================================================
87 // functions
88 // ===========================================================================
89 /* -------------------------------------------------------------------------
90  * data processing methods
91  * ----------------------------------------------------------------------- */
97 void
98 initNet(RONet& net, ROLoader& loader, OptionsCont& oc) {
99  // load the net
100  ROMAEdgeBuilder builder(oc.getBool("weights.expand"), oc.getBool("weights.interpolate"));
101  loader.loadNet(net, builder);
102  // load the weights when wished/available
103  if (oc.isSet("weight-files")) {
104  loader.loadWeights(net, "weight-files", oc.getString("weight-attribute"), false);
105  }
106  if (oc.isSet("lane-weight-files")) {
107  loader.loadWeights(net, "lane-weight-files", oc.getString("weight-attribute"), true);
108  }
109 }
110 
111 SUMOReal
112 getTravelTime(const ROEdge* const edge, const ROVehicle* const /* veh */, SUMOReal /* time */) {
113  return edge->getLength() / edge->getSpeed();
114 }
115 
116 
120 void
122  std::ofstream outFile(oc.getString("all-pairs-output").c_str(), std::ios::binary);
123  // build the router
125  Dijkstra router(net.getEdgeNo(), oc.getBool("ignore-errors"), &getTravelTime);
126  ConstROEdgeVector into;
127  const int numInternalEdges = net.getInternalEdgeNumber();
128  const int numTotalEdges = (int)net.getEdgeNo();
129  for (int i = numInternalEdges; i < numTotalEdges; i++) {
130  const Dijkstra::EdgeInfo& ei = router.getEdgeInfo(i);
131  if (ei.edge->getType() != ROEdge::ET_INTERNAL) {
132  router.compute(ei.edge, 0, 0, 0, into);
133  for (int j = numInternalEdges; j < numTotalEdges; j++) {
134  FileHelpers::writeFloat(outFile, router.getEdgeInfo(j).traveltime);
135  }
136  }
137  }
138 }
139 
143 void
145  // build the router
147  const std::string measure = oc.getString("weight-attribute");
148  const std::string routingAlgorithm = oc.getString("routing-algorithm");
149  if (measure == "traveltime") {
150  if (routingAlgorithm == "dijkstra") {
151  if (net.hasRestrictions()) {
152  if (oc.getInt("paths") > 1) {
154  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROMAAssignments::getPenalizedTT);
155  } else {
157  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTimeStatic);
158  }
159  } else {
160  if (oc.getInt("paths") > 1) {
162  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROMAAssignments::getPenalizedTT);
163  } else {
165  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTimeStatic);
166  }
167  }
168  } else if (routingAlgorithm == "astar") {
169  if (net.hasRestrictions()) {
170  if (oc.getInt("paths") > 1) {
172  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROMAAssignments::getPenalizedTT);
173  } else {
175  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTimeStatic);
176  }
177  } else {
178  if (oc.getInt("paths") > 1) {
180  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROMAAssignments::getPenalizedTT);
181  } else {
183  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTimeStatic);
184  }
185  }
186  } else if (routingAlgorithm == "bulkstar") {
187  if (net.hasRestrictions()) {
190  } else {
193  }
194  } else if (routingAlgorithm == "CH") {
195  const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
196  string2time(oc.getString("weight-period")) :
198  if (net.hasRestrictions()) {
200  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTimeStatic, SVC_IGNORING, weightPeriod, true);
201  } else {
203  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTimeStatic, SVC_IGNORING, weightPeriod, false);
204  }
205  } else if (routingAlgorithm == "CHWrapper") {
206  const SUMOTime begin = string2time(oc.getString("begin"));
207  const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
208  string2time(oc.getString("weight-period")) :
210 
212  net.getEdgeNo(), oc.getBool("ignore-errors"), &ROEdge::getTravelTimeStatic, begin, weightPeriod);
213  } else {
214  throw ProcessError("Unknown routing Algorithm '" + routingAlgorithm + "'!");
215  }
216 
217  } else {
219  if (measure == "CO") {
220  op = &ROEdge::getEmissionEffort<PollutantsInterface::CO>;
221  } else if (measure == "CO2") {
222  op = &ROEdge::getEmissionEffort<PollutantsInterface::CO2>;
223  } else if (measure == "PMx") {
224  op = &ROEdge::getEmissionEffort<PollutantsInterface::PM_X>;
225  } else if (measure == "HC") {
226  op = &ROEdge::getEmissionEffort<PollutantsInterface::HC>;
227  } else if (measure == "NOx") {
228  op = &ROEdge::getEmissionEffort<PollutantsInterface::NO_X>;
229  } else if (measure == "fuel") {
230  op = &ROEdge::getEmissionEffort<PollutantsInterface::FUEL>;
231  } else if (measure == "noise") {
233  } else {
234  throw ProcessError("Unknown measure (weight attribute '" + measure + "')!");
235  }
236  if (net.hasRestrictions()) {
237  if (oc.getInt("paths") > 1) {
240  } else {
242  net.getEdgeNo(), oc.getBool("ignore-errors"), op, &ROEdge::getTravelTimeStatic);
243  }
244  } else {
245  if (oc.getInt("paths") > 1) {
248  } else {
250  net.getEdgeNo(), oc.getBool("ignore-errors"), op, &ROEdge::getTravelTimeStatic);
251  }
252  }
253  }
254  // prepare the output
255  net.openOutput(oc.isSet("output-file") ? oc.getString("output-file") : "",
256  oc.isSet("flow-output") ? oc.getString("flow-output") : "", "");
257  // process route definitions
258  try {
259  if (oc.isSet("timeline")) {
260  matrix.applyCurve(matrix.parseTimeLine(oc.getStringVector("timeline"), oc.getBool("timeline.day-in-hours")));
261  }
262  ROVehicle defaultVehicle(SUMOVehicleParameter(), 0, net.getVehicleTypeSecure(DEFAULT_VTYPE_ID), &net);
263  ROMAAssignments a(string2time(oc.getString("begin")), string2time(oc.getString("end")), net, matrix, *router);
264  const std::string assignMethod = oc.getString("assignment-method");
265  if (assignMethod == "incremental") {
266  a.incremental(oc.getInt("max-iterations"));
267  } else if (assignMethod == "SUE") {
268  a.sue(oc.getInt("max-iterations"), oc.getInt("max-inner-iterations"),
269  oc.getInt("paths"), oc.getFloat("paths.penalty"), oc.getFloat("tolerance"), oc.getString("route-choice-method"));
270  }
271  // update path costs and output
272  bool haveOutput = false;
273  OutputDevice* dev = net.getRouteOutput();
274  if (dev != 0) {
275  for (std::vector<ODCell*>::const_iterator i = matrix.getCells().begin(); i != matrix.getCells().end(); ++i) {
276  const ODCell* const c = *i;
278  for (std::vector<RORoute*>::const_iterator j = c->pathsVector.begin(); j != c->pathsVector.end(); ++j) {
279  (*j)->setCosts(router->recomputeCosts((*j)->getEdgeVector(), &defaultVehicle, string2time(oc.getString("begin"))));
280  (*j)->writeXMLDefinition(*dev, 0, true, false);
281  }
282  dev->closeTag();
283  }
284  haveOutput = true;
285  }
286  dev = net.getRouteOutput(true);
287  if (dev != 0) {
288  int num = 0;
289  for (std::vector<ODCell*>::const_iterator i = matrix.getCells().begin(); i != matrix.getCells().end(); ++i) {
290  const ODCell* const c = *i;
291  dev->openTag(SUMO_TAG_FLOW).writeAttr(SUMO_ATTR_ID, oc.getString("prefix") + toString(num++));
294  matrix.writeDefaultAttrs(*dev, oc.getBool("ignore-vehicle-type"), c);
296  for (std::vector<RORoute*>::const_iterator j = c->pathsVector.begin(); j != c->pathsVector.end(); ++j) {
297  (*j)->setCosts(router->recomputeCosts((*j)->getEdgeVector(), &defaultVehicle, string2time(oc.getString("begin"))));
298  (*j)->writeXMLDefinition(*dev, 0, true, false);
299  }
300  dev->closeTag();
301  dev->closeTag();
302  }
303  haveOutput = true;
304  }
305  if (!haveOutput) {
306  throw ProcessError("No output file given.");
307  }
308  // end the processing
309  net.cleanup(router);
310  } catch (ProcessError&) {
311  net.cleanup(router);
312  throw;
313  }
314 }
315 
316 
317 /* -------------------------------------------------------------------------
318  * main
319  * ----------------------------------------------------------------------- */
320 int
321 main(int argc, char** argv) {
323  oc.setApplicationDescription("Import O/D-matrices for macroscopic traffic assignment");
324  oc.setApplicationName("marouter", "SUMO marouter Version " + getBuildName(VERSION_STRING));
325  int ret = 0;
326  RONet* net = 0;
327  try {
328  XMLSubSys::init();
330  OptionsIO::getOptions(true, argc, argv);
331  if (oc.processMetaOptions(argc < 2)) {
333  return 0;
334  }
335  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
337  if (!ROMAFrame::checkOptions()) {
338  throw ProcessError();
339  }
341  // load data
342  ROLoader loader(oc, false, false);
343  net = new RONet();
344  initNet(*net, loader, oc);
345  if (oc.isSet("all-pairs-output")) {
346  computeAllPairs(*net, oc);
347  if (net->getDistricts().empty()) {
348  delete net;
350  if (ret == 0) {
351  std::cout << "Success." << std::endl;
352  }
353  return ret;
354  }
355  }
356  if (net->getDistricts().empty()) {
357  throw ProcessError("No districts loaded.");
358  }
359  // load districts
360  ODDistrictCont districts;
361  districts.makeDistricts(net->getDistricts());
362  // load the matrix
363  ODMatrix matrix(districts);
364  matrix.loadMatrix(oc);
365  if (matrix.getNoLoaded() == 0) {
366  throw ProcessError("No vehicles loaded.");
367  }
368  if (MsgHandler::getErrorInstance()->wasInformed() && !oc.getBool("ignore-errors")) {
369  throw ProcessError("Loading failed.");
370  }
372  WRITE_MESSAGE(toString(matrix.getNoLoaded()) + " vehicles loaded.");
373 
374  // build routes and parse the incremental rates if the incremental method is choosen.
375  try {
376  computeRoutes(*net, oc, matrix);
377  } catch (XERCES_CPP_NAMESPACE::SAXParseException& e) {
378  WRITE_ERROR(toString(e.getLineNumber()));
379  ret = 1;
380  } catch (XERCES_CPP_NAMESPACE::SAXException& e) {
381  WRITE_ERROR(TplConvert::_2str(e.getMessage()));
382  ret = 1;
383  }
384  if (MsgHandler::getErrorInstance()->wasInformed() || ret != 0) {
385  throw ProcessError();
386  }
387  } catch (const ProcessError& e) {
388  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
389  WRITE_ERROR(e.what());
390  }
391  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
392  ret = 1;
393  }
394 
395  delete net;
397  if (ret == 0) {
398  std::cout << "Success." << std::endl;
399  }
400  return ret;
401 }
402 
403 
404 
405 /****************************************************************************/
406 
SUMOReal getNoLoaded() const
Returns the number of loaded vehicles.
Definition: ODMatrix.cpp:432
Computes the shortest path through a contracted network.
Definition: CHRouter.h:74
void writeDefaultAttrs(OutputDevice &dev, const bool noVtype, const ODCell *const cell)
Helper function for flow and trip output writing the depart and arrival attributes.
Definition: ODMatrix.cpp:141
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
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) ...
const std::vector< ODCell * > & getCells()
Definition: ODMatrix.h:219
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:58
Computes the shortest path through a network using the Dijkstra algorithm.
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
OutputDevice * getRouteOutput(const bool alternative=false)
Definition: RONet.h:394
const std::map< std::string, std::pair< std::vector< std::string >, std::vector< std::string > > > & getDistricts() const
Retrieves all TAZ (districts) from the network.
Definition: RONet.h:126
void computeRoutes(RONet &net, OptionsCont &oc, ODMatrix &matrix)
assignment methods
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
size_t getEdgeNo() const
Returns the total number of edges the network contains including internal edges.
Definition: RONet.cpp:529
static SUMOReal getNoiseEffort(const ROEdge *const edge, const ROVehicle *const veh, SUMOReal time)
Definition: ROEdge.cpp:171
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:69
Interface for building instances of duarouter-edges.
void makeDistricts(const std::map< std::string, std::pair< std::vector< std::string >, std::vector< std::string > > > &districts)
create districts from description
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)
int main(int argc, char **argv)
bool loadWeights(RONet &net, const std::string &optionName, const std::string &measure, bool useLanes)
Loads the net weights.
Definition: ROLoader.cpp:273
SUMOReal getTravelTime(const ROEdge *const edge, const ROVehicle *const , SUMOReal)
static std::ostream & writeFloat(std::ostream &strm, SUMOReal value)
Writes a float binary.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:61
Computes the shortest path through a network using the Dijkstra algorithm.
Definition: AStarRouter.h:71
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid for usage within duarouter...
Definition: ROMAFrame.cpp:272
void computeAllPairs(RONet &net, OptionsCont &oc)
An internal edge which models vehicles driving across a junction. This is currently not used for rout...
Definition: ROEdge.h:93
SUMOReal getFloat(const std::string &name) const
Returns the SUMOReal-value of the named option (only for Option_Float)
std::vector< const ROEdge * > ConstROEdgeVector
Definition: ROEdge.h:59
std::vector< RORoute * > pathsVector
the list of paths / routes
Definition: ODCell.h:76
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 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 loadMatrix(OptionsCont &oc)
read a VISUM-matrix with the V Format
Definition: ODMatrix.cpp:477
void openOutput(const std::string &filename, const std::string altFilename, const std::string typeFilename)
Opens the output for computed routes.
Definition: RONet.cpp:172
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
#define max(a, b)
Definition: polyfonts.c:65
A single O/D-matrix cell.
Definition: ODCell.h:56
void initNet(RONet &net, ROLoader &loader, OptionsCont &oc)
std::string origin
Name of the origin district.
Definition: ODCell.h:67
static SUMOReal getPenalizedTT(const ROEdge *const e, const ROVehicle *const v, SUMOReal t)
Returns the traveltime on an edge including penalties.
static std::string _2str(const E *const data)
Definition: TplConvert.h:56
An O/D (origin/destination) matrix.
Definition: ODMatrix.h:74
The data loader.
Definition: ROLoader.h:63
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
const std::string getBuildName(const std::string &version)
attach some build flags to the version string
Definition: StdDefs.cpp:88
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:48
A container for districts.
SUMOReal vehicleNumber
The number of vehicles.
Definition: ODCell.h:58
static SUMOReal getTravelTime(const ROEdge *const e, const ROVehicle *const v, SUMOReal t)
Returns the traveltime on an edge without penalties.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
static SUMOReal getPenalizedEffort(const ROEdge *const e, const ROVehicle *const v, SUMOReal t)
Returns the effort to pass an edge including penalties.
SUMOTime begin
The begin time this cell describes.
Definition: ODCell.h:61
virtual void loadNet(RONet &toFill, ROAbstractEdgeBuilder &eb)
Loads the network.
Definition: ROLoader.cpp:125
A basic edge for routing applications.
Definition: ROEdge.h:73
int getInternalEdgeNumber() const
Returns the number of internal edges the network contains.
Definition: RONet.cpp:535
#define VERSION_STRING
Definition: config.h:230
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
static void fillOptions()
Inserts options used by duarouter into the OptionsCont-singleton.
Definition: ROMAFrame.cpp:57
The router's network representation.
Definition: RONet.h:72
Structure representing possible vehicle parameter.
SUMOReal getLength() const
Returns the length of the edge.
Definition: ROEdge.h:183
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:547
void applyCurve(const Distribution_Points &ps)
Splits the stored cells dividing them on the given time line.
Definition: ODMatrix.cpp:465
int SUMOTime
Definition: SUMOTime.h:43
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
virtual SUMOReal recomputeCosts(const std::vector< const E * > &edges, const V *const v, SUMOTime msTime) const =0
std::string destination
Name of the destination district.
Definition: ODCell.h:70
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:218
SUMOVTypeParameter * getVehicleTypeSecure(const std::string &id)
Retrieves the named vehicle type.
Definition: RONet.cpp:217
SUMOTime end
The end time this cell describes.
Definition: ODCell.h:64
SUMOReal getMinimumTravelTime(const ROVehicle *const veh) const
Returns a lower bound for the travel time on this edge without using any stored timeLine.
Definition: ROEdge.h:372
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
void clear()
Clears information whether an error occured previously.
Definition: MsgHandler.cpp:149
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
void cleanup(SUMOAbstractRouter< ROEdge, ROVehicle > *router)
closes the file output for computed routes and deletes routers and associated threads if necessary ...
Definition: RONet.cpp:191
static void initOutputOptions()
Definition: MsgHandler.cpp:197
SUMOReal getSpeed() const
Returns the speed allowed on this edge.
Definition: ROEdge.h:198
vehicles ignoring classes
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
Distribution_Points parseTimeLine(const std::vector< std::string > &def, bool timelineDayInHours)
split the given timeline
Definition: ODMatrix.cpp:523
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.
static SUMOReal getTravelTimeStatic(const ROEdge *const edge, const ROVehicle *const veh, SUMOReal time)
Returns the travel time for the given edge.
Definition: ROEdge.h:362
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.