Eclipse SUMO - Simulation of Urban MObility
duarouter_main.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // Main for DUAROUTER
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #ifdef HAVE_VERSION_H
27 #include <version.h>
28 #endif
29 
30 #include <iostream>
31 #include <string>
32 #include <limits.h>
33 #include <ctime>
34 #include <memory>
35 #include <xercesc/sax/SAXException.hpp>
36 #include <xercesc/sax/SAXParseException.hpp>
42 #include <utils/common/ToString.h>
44 #include <utils/options/Option.h>
49 #include <utils/router/CHRouter.h>
51 #include <utils/xml/XMLSubSys.h>
52 #include <router/ROFrame.h>
53 #include <router/ROLoader.h>
54 #include <router/RONet.h>
55 #include <router/ROEdge.h>
56 #include "RODUAEdgeBuilder.h"
57 #include "RODUAFrame.h"
58 
59 
60 // ===========================================================================
61 // functions
62 // ===========================================================================
63 /* -------------------------------------------------------------------------
64  * data processing methods
65  * ----------------------------------------------------------------------- */
71 void
72 initNet(RONet& net, ROLoader& loader, OptionsCont& oc) {
73  // load the net
74  RODUAEdgeBuilder builder;
75  ROEdge::setGlobalOptions(oc.getBool("weights.interpolate"));
76  loader.loadNet(net, builder);
77  // load the weights when wished/available
78  if (oc.isSet("weight-files")) {
79  loader.loadWeights(net, "weight-files", oc.getString("weight-attribute"), false, oc.getBool("weights.expand"));
80  }
81  if (oc.isSet("lane-weight-files")) {
82  loader.loadWeights(net, "lane-weight-files", oc.getString("weight-attribute"), true, oc.getBool("weights.expand"));
83  }
84 }
85 
86 
87 
91 void
92 computeRoutes(RONet& net, ROLoader& loader, OptionsCont& oc) {
93  // initialise the loader
94  loader.openRoutes(net);
95  // build the router
98  const std::string measure = oc.getString("weight-attribute");
99  const std::string routingAlgorithm = oc.getString("routing-algorithm");
100  const SUMOTime begin = string2time(oc.getString("begin"));
101  const SUMOTime end = string2time(oc.getString("end"));
102  if (measure == "traveltime") {
103  if (routingAlgorithm == "dijkstra") {
104  if (net.hasPermissions()) {
106  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction);
107  } else {
109  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction);
110  }
111  } else if (routingAlgorithm == "astar") {
112  if (net.hasPermissions()) {
114  std::shared_ptr<const AStar::LookupTable> lookup;
115  if (oc.isSet("astar.all-distances")) {
116  lookup = std::make_shared<const AStar::FLT>(oc.getString("astar.all-distances"), (int)ROEdge::getAllEdges().size());
117  } else if (oc.isSet("astar.landmark-distances")) {
120  begin, end, std::numeric_limits<int>::max(), 1);
121  ROVehicle defaultVehicle(SUMOVehicleParameter(), nullptr, net.getVehicleTypeSecure(DEFAULT_VTYPE_ID), &net);
122  lookup = std::make_shared<const AStar::LMLT>(oc.getString("astar.landmark-distances"), ROEdge::getAllEdges(), &router, &defaultVehicle,
123  oc.isSet("astar.save-landmark-distances") ? oc.getString("astar.save-landmark-distances") : "", oc.getInt("routing-threads"));
124  }
125  router = new AStar(ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction, lookup);
126  } else {
128  std::shared_ptr<const AStar::LookupTable> lookup;
129  if (oc.isSet("astar.all-distances")) {
130  lookup = std::make_shared<const AStar::FLT>(oc.getString("astar.all-distances"), (int)ROEdge::getAllEdges().size());
131  } else if (oc.isSet("astar.landmark-distances")) {
134  begin, end, std::numeric_limits<int>::max(), 1);
135  ROVehicle defaultVehicle(SUMOVehicleParameter(), nullptr, net.getVehicleTypeSecure(DEFAULT_VTYPE_ID), &net);
136  lookup = std::make_shared<const AStar::LMLT>(oc.getString("astar.landmark-distances"), ROEdge::getAllEdges(), &router, &defaultVehicle,
137  oc.isSet("astar.save-landmark-distances") ? oc.getString("astar.save-landmark-distances") : "", oc.getInt("routing-threads"));
138  }
139  router = new AStar(ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction, lookup);
140  }
141  } else if (routingAlgorithm == "CH") {
142  const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
143  string2time(oc.getString("weight-period")) :
144  std::numeric_limits<int>::max());
145  if (net.hasPermissions()) {
147  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), &ROEdge::getTravelTimeStatic, SVC_IGNORING, weightPeriod, true);
148  } else {
150  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), &ROEdge::getTravelTimeStatic, SVC_IGNORING, weightPeriod, false);
151  }
152  } else if (routingAlgorithm == "CHWrapper") {
153  const SUMOTime weightPeriod = (oc.isSet("weight-files") ?
154  string2time(oc.getString("weight-period")) :
155  std::numeric_limits<int>::max());
158  begin, end, weightPeriod, oc.getInt("routing-threads"));
159  } else {
160  throw ProcessError("Unknown routing Algorithm '" + routingAlgorithm + "'!");
161  }
162  } else {
164  if (measure == "CO") {
165  op = &ROEdge::getEmissionEffort<PollutantsInterface::CO>;
166  } else if (measure == "CO2") {
167  op = &ROEdge::getEmissionEffort<PollutantsInterface::CO2>;
168  } else if (measure == "PMx") {
169  op = &ROEdge::getEmissionEffort<PollutantsInterface::PM_X>;
170  } else if (measure == "HC") {
171  op = &ROEdge::getEmissionEffort<PollutantsInterface::HC>;
172  } else if (measure == "NOx") {
173  op = &ROEdge::getEmissionEffort<PollutantsInterface::NO_X>;
174  } else if (measure == "fuel") {
175  op = &ROEdge::getEmissionEffort<PollutantsInterface::FUEL>;
176  } else if (measure == "electricity") {
177  op = &ROEdge::getEmissionEffort<PollutantsInterface::ELEC>;
178  } else if (measure == "noise") {
180  } else {
181  throw ProcessError("Unknown measure (weight attribute '" + measure + "')!");
182  }
183  if (net.hasPermissions()) {
185  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op, ttFunction);
186  } else {
188  ROEdge::getAllEdges(), oc.getBool("ignore-errors"), op, ttFunction);
189  }
190  }
191  int carWalk = 0;
192  for (const std::string& opt : oc.getStringVector("persontrip.transfer.car-walk")) {
193  if (opt == "parkingAreas") {
195  } else if (opt == "ptStops") {
197  } else if (opt == "allJunctions") {
199  }
200  }
202  new ROIntermodalRouter(RONet::adaptIntermodalRouter, carWalk, routingAlgorithm));
203  // process route definitions
204  try {
205  net.openOutput(oc);
206  loader.processRoutes(begin, end, string2time(oc.getString("route-steps")), net, provider);
207  net.writeIntermodal(oc, provider.getIntermodalRouter());
208  // end the processing
209  net.cleanup();
210  } catch (ProcessError&) {
211  net.cleanup();
212  throw;
213  }
214 }
215 
216 
217 /* -------------------------------------------------------------------------
218  * main
219  * ----------------------------------------------------------------------- */
220 int
221 main(int argc, char** argv) {
223  // give some application descriptions
224  oc.setApplicationDescription("Shortest path router and DUE computer for the microscopic, multi-modal traffic simulation SUMO.");
225  oc.setApplicationName("duarouter", "Eclipse SUMO duarouter Version " VERSION_STRING);
226  int ret = 0;
227  RONet* net = nullptr;
228  try {
229  XMLSubSys::init();
231  OptionsIO::setArgs(argc, argv);
233  if (oc.processMetaOptions(argc < 2)) {
235  return 0;
236  }
237  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
240  throw ProcessError();
241  }
243  // load data
244  ROLoader loader(oc, false, !oc.getBool("no-step-log"));
245  net = new RONet();
246  initNet(*net, loader, oc);
247  // build routes
248  try {
249  computeRoutes(*net, loader, oc);
250  } catch (XERCES_CPP_NAMESPACE::SAXParseException& e) {
251  WRITE_ERROR(toString(e.getLineNumber()));
252  ret = 1;
253  } catch (XERCES_CPP_NAMESPACE::SAXException& e) {
254  WRITE_ERROR(StringUtils::transcode(e.getMessage()));
255  ret = 1;
256  }
257  if (MsgHandler::getErrorInstance()->wasInformed() || ret != 0) {
258  throw ProcessError();
259  }
260  } catch (const ProcessError& e) {
261  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
262  WRITE_ERROR(e.what());
263  }
264  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
265  ret = 1;
266 #ifndef _DEBUG
267  } catch (const std::exception& e) {
268  if (std::string(e.what()) != std::string("")) {
269  WRITE_ERROR(e.what());
270  }
271  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
272  ret = 1;
273  } catch (...) {
274  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
275  ret = 1;
276 #endif
277  }
278  delete net;
280  if (ret == 0) {
281  std::cout << "Success." << std::endl;
282  }
283  return ret;
284 }
285 
286 
287 
288 /****************************************************************************/
289 
OptionsCont::isSet
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
Definition: OptionsCont.cpp:136
ROLoader
The data loader.
Definition: ROLoader.h:56
ROEdge::getAllEdges
static const ROEdgeVector & getAllEdges()
Returns all ROEdges.
Definition: ROEdge.cpp:336
OptionsCont::getInt
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
Definition: OptionsCont.cpp:216
OptionsCont::processMetaOptions
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
Definition: OptionsCont.cpp:561
ToString.h
RONet::openOutput
void openOutput(const OptionsCont &options)
Opens the output for computed routes.
Definition: RONet.cpp:215
PedestrianRouter
Definition: MSNet.h:82
SystemFrame::close
static void close()
Closes all of an applications subsystems.
Definition: SystemFrame.cpp:131
MsgHandler::initOutputOptions
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:208
ROLoader::openRoutes
void openRoutes(RONet &net)
Builds and opens all route loaders.
Definition: ROLoader.cpp:156
IntermodalNetwork::ALL_JUNCTIONS
junctions with edges allowing the additional mode
Definition: IntermodalNetwork.h:94
OptionsCont.h
MsgHandler.h
ROEdge::getTravelTimeStatic
static double getTravelTimeStatic(const ROEdge *const edge, const ROVehicle *const veh, double time)
Returns the travel time for the given edge.
Definition: ROEdge.h:396
RONet::hasPermissions
bool hasPermissions() const
Definition: RONet.cpp:693
OptionsCont::getString
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
Definition: OptionsCont.cpp:202
MsgHandler::inform
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:118
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:35
IntermodalNetwork::PARKING_AREAS
parking areas
Definition: IntermodalNetwork.h:90
OptionsCont::getBool
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
Definition: OptionsCont.cpp:223
RONet::writeIntermodal
void writeIntermodal(const OptionsCont &options, ROIntermodalRouter &router) const
Writes the intermodal network and weights if requested.
Definition: RONet.cpp:236
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
RONet::adaptIntermodalRouter
static void adaptIntermodalRouter(ROIntermodalRouter &router)
Definition: RONet.cpp:661
RONet
The router's network representation.
Definition: RONet.h:64
ROVehicle
A vehicle as used by router.
Definition: ROVehicle.h:53
ROFrame.h
SUMOVehicleParameter
Structure representing possible vehicle parameter.
Definition: SUMOVehicleParameter.h:291
ROLoader.h
ROIntermodalRouter
IntermodalRouter< ROEdge, ROLane, RONode, ROVehicle > ROIntermodalRouter
Definition: RORoutable.h:44
CHRouterWrapper.h
ROEdge::getNoiseEffort
static double getNoiseEffort(const ROEdge *const edge, const ROVehicle *const veh, double time)
Definition: ROEdge.cpp:209
DijkstraRouter
Computes the shortest path through a network using the Dijkstra algorithm.
Definition: DijkstraRouter.h:63
ROEdge::setGlobalOptions
static void setGlobalOptions(const bool interpolate)
Definition: ROEdge.h:450
gWeightsRandomFactor
double gWeightsRandomFactor
Definition: StdDefs.cpp:31
CHRouter.h
main
int main(int argc, char **argv)
Definition: duarouter_main.cpp:221
OptionsCont::setApplicationName
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.
Definition: OptionsCont.cpp:485
ROEdge::getTravelTimeStaticRandomized
static double getTravelTimeStaticRandomized(const ROEdge *const edge, const ROVehicle *const veh, double time)
Definition: ROEdge.h:400
SystemFrame.h
XMLSubSys::setValidation
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:59
RONet.h
AStarRouter.h
DEFAULT_VTYPE_ID
const std::string DEFAULT_VTYPE_ID
CHRouterWrapper
Computes the shortest path through a contracted network.
Definition: CHRouterWrapper.h:65
IntermodalNetwork::PT_STOPS
public transport stops and access
Definition: IntermodalNetwork.h:92
OutputDevice.h
ProcessError
Definition: UtilExceptions.h:40
UtilExceptions.h
XMLSubSys::init
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:48
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:90
ROLoader::loadNet
virtual void loadNet(RONet &toFill, ROAbstractEdgeBuilder &eb)
Loads the network.
Definition: ROLoader.cpp:114
RODUAFrame.h
RouterProvider
Definition: RouterProvider.h:38
initNet
void initNet(RONet &net, ROLoader &loader, OptionsCont &oc)
Definition: duarouter_main.cpp:72
string2time
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:42
RODUAFrame::checkOptions
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid for usage within duarouter.
Definition: RODUAFrame.cpp:178
RandHelper::initRandGlobal
static void initRandGlobal(std::mt19937 *which=0)
Reads the given random number options and initialises the random number generator in accordance.
Definition: RandHelper.cpp:72
SUMOAbstractRouter< ROEdge, ROVehicle >
RODUAEdgeBuilder.h
OptionsIO::getOptions
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:76
OptionsCont::setApplicationDescription
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
Definition: OptionsCont.cpp:493
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
StringUtils.h
RONet::cleanup
void cleanup()
closes the file output for computed routes and deletes associated threads if necessary
Definition: RONet.cpp:255
StringUtils::transcode
static std::string transcode(const XMLCh *const data)
converts a 0-terminated XMLCh* array (usually UTF-16, stemming from Xerces) into std::string in UTF-8
Definition: StringUtils.h:132
Option.h
RODUAEdgeBuilder
Interface for building instances of duarouter-edges.
Definition: RODUAEdgeBuilder.h:51
CHRouter
Computes the shortest path through a contracted network.
Definition: CHRouter.h:63
RouterProvider::getIntermodalRouter
IntermodalRouter< E, L, N, V > & getIntermodalRouter() const
Definition: RouterProvider.h:58
AStarRouter
Computes the shortest path through a network using the A* algorithm.
Definition: AStarRouter.h:78
OptionsIO::setArgs
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:55
config.h
ROLoader::processRoutes
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:186
RandHelper.h
DijkstraRouter.h
SystemFrame::checkOptions
static bool checkOptions()
checks shared options and sets StdDefs
Definition: SystemFrame.cpp:118
computeRoutes
void computeRoutes(RONet &net, ROLoader &loader, OptionsCont &oc)
Definition: duarouter_main.cpp:92
MsgHandler::getErrorInstance
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:81
SVC_IGNORING
vehicles ignoring classes
Definition: SUMOVehicleClass.h:136
RONet::getVehicleTypeSecure
SUMOVTypeParameter * getVehicleTypeSecure(const std::string &id)
Retrieves the named vehicle type.
Definition: RONet.cpp:279
VERSION_STRING
#define VERSION_STRING
Definition: config.h:207
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
ROEdge.h
OptionsCont::getStringVector
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)
Definition: OptionsCont.cpp:921
RODUAFrame::fillOptions
static void fillOptions()
Inserts options used by duarouter into the OptionsCont-singleton.
Definition: RODUAFrame.cpp:45
OptionsIO.h
XMLSubSys.h
ROLoader::loadWeights
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:247