Eclipse SUMO - Simulation of Urban MObility
dfrouter_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 /****************************************************************************/
19 // Main for the DFROUTER
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #ifdef HAVE_VERSION_H
29 #include <version.h>
30 #endif
31 
32 #include <iostream>
33 #include <string>
34 #include <limits.h>
35 #include <ctime>
36 #include <xercesc/sax/SAXException.hpp>
37 #include <xercesc/sax/SAXParseException.hpp>
42 #include <utils/common/ToString.h>
44 #include <utils/options/Option.h>
48 #include <utils/xml/XMLSubSys.h>
49 #include <router/ROLoader.h>
50 #include <router/RONet.h>
51 #include "RODFEdgeBuilder.h"
52 #include "RODFFrame.h"
53 #include "RODFNet.h"
54 #include "RODFEdge.h"
55 #include "RODFDetector.h"
56 #include "RODFDetectorHandler.h"
57 #include "RODFRouteCont.h"
58 #include "RODFDetectorFlow.h"
59 #include "RODFDetFlowLoader.h"
60 
61 
62 // ===========================================================================
63 // functions
64 // ===========================================================================
65 /* -------------------------------------------------------------------------
66  * data processing methods
67  * ----------------------------------------------------------------------- */
68 void
69 readDetectors(RODFDetectorCon& detectors, OptionsCont& oc, RODFNet* optNet) {
70  if (!oc.isSet("detector-files")) {
71  throw ProcessError("No detector file given (use --detector-files <FILE>).");
72  }
73  // read definitions stored in XML-format
74  std::vector<std::string> files = oc.getStringVector("detector-files");
75  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
76  if (!FileHelpers::isReadable(*fileIt)) {
77  throw ProcessError("Could not open detector file '" + *fileIt + "'");
78  }
79  PROGRESS_BEGIN_MESSAGE("Loading detector definitions from '" + *fileIt + "'");
80  RODFDetectorHandler handler(optNet, oc.getBool("ignore-invalid-detectors"), detectors, *fileIt);
81  if (XMLSubSys::runParser(handler, *fileIt)) {
83  } else {
85  throw ProcessError();
86  }
87  }
88  if (detectors.getDetectors().empty()) {
89  throw ProcessError("No detectors found.");
90  }
91 }
92 
93 
94 void
96  if (!oc.isSet("measure-files")) {
97  // ok, not given, return an empty container
98  return;
99  }
100  // check whether the file exists
101  std::vector<std::string> files = oc.getStringVector("measure-files");
102  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
103  if (!FileHelpers::isReadable(*fileIt)) {
104  throw ProcessError("The measure-file '" + *fileIt + "' can not be opened.");
105  }
106  // parse
107  PROGRESS_BEGIN_MESSAGE("Loading flows from '" + *fileIt + "'");
108  RODFDetFlowLoader dfl(dc, flows, string2time(oc.getString("begin")), string2time(oc.getString("end")),
109  string2time(oc.getString("time-offset")), string2time(oc.getString("time-factor")));
110  dfl.read(*fileIt);
112  }
113 }
114 
115 
116 void
118  if (oc.getBool("print-absolute-flows")) {
119  flows.printAbsolute();
120  }
121 
122  // if a network was loaded... (mode1)
123  if (optNet != nullptr) {
124  if (oc.getBool("remove-empty-detectors")) {
125  PROGRESS_BEGIN_MESSAGE("Removing empty detectors");
126  optNet->removeEmptyDetectors(detectors, flows);
128  } else if (oc.getBool("report-empty-detectors")) {
129  PROGRESS_BEGIN_MESSAGE("Scanning for empty detectors");
130  optNet->reportEmptyDetectors(detectors, flows);
132  }
133  // compute the detector types (optionally)
134  if (!detectors.detectorsHaveCompleteTypes() || oc.getBool("revalidate-detectors")) {
135  optNet->computeTypes(detectors, oc.getBool("strict-sources"));
136  }
137  std::vector<RODFDetector*>::const_iterator i = detectors.getDetectors().begin();
138  for (; i != detectors.getDetectors().end(); ++i) {
139  if ((*i)->getType() == SOURCE_DETECTOR) {
140  break;
141  }
142  }
143  if (i == detectors.getDetectors().end() && !oc.getBool("routes-for-all")) {
144  throw ProcessError("No source detectors found.");
145  }
146  // compute routes between the detectors (optionally)
147  if (!detectors.detectorsHaveRoutes() || oc.getBool("revalidate-routes") || oc.getBool("guess-empty-flows")) {
148  PROGRESS_BEGIN_MESSAGE("Computing routes");
149  optNet->buildRoutes(detectors,
150  oc.getBool("keep-unfinished-routes"), oc.getBool("routes-for-all"),
151  !oc.getBool("keep-longer-routes"), oc.getInt("max-search-depth"));
153  }
154  }
155 
156  // check
157  // whether the detectors are valid
158  if (!detectors.detectorsHaveCompleteTypes()) {
159  throw ProcessError("The detector types are not defined; use in combination with a network");
160  }
161  // whether the detectors have routes
162  if (!detectors.detectorsHaveRoutes()) {
163  throw ProcessError("The emitters have no routes; use in combination with a network");
164  }
165 
166  // save the detectors if wished
167  if (oc.isSet("detector-output")) {
168  detectors.save(oc.getString("detector-output"));
169  }
170  // save their positions as POIs if wished
171  if (oc.isSet("detectors-poi-output")) {
172  detectors.saveAsPOIs(oc.getString("detectors-poi-output"));
173  }
174 
175  // save the routes file if it was changed or it's wished
176  if (detectors.detectorsHaveRoutes() && oc.isSet("routes-output")) {
177  detectors.saveRoutes(oc.getString("routes-output"));
178  }
179 
180  // guess flows if wished
181  if (oc.getBool("guess-empty-flows")) {
182  optNet->buildDetectorDependencies(detectors);
183  detectors.guessEmptyFlows(flows);
184  }
185 
186  const SUMOTime begin = string2time(oc.getString("begin"));
187  const SUMOTime end = string2time(oc.getString("end"));
188  const SUMOTime step = string2time(oc.getString("time-step"));
189 
190  // save emitters if wished
191  if (oc.isSet("emitters-output") || oc.isSet("emitters-poi-output")) {
192  optNet->buildEdgeFlowMap(flows, detectors, begin, end, step); // !!!
193  if (oc.getBool("revalidate-flows")) {
194  PROGRESS_BEGIN_MESSAGE("Rechecking loaded flows");
195  optNet->revalidateFlows(detectors, flows, begin, end, step);
197  }
198  if (oc.isSet("emitters-output")) {
199  PROGRESS_BEGIN_MESSAGE("Writing emitters");
200  detectors.writeEmitters(oc.getString("emitters-output"), flows,
201  begin, end, step,
202  *optNet,
203  oc.getBool("calibrator-output"),
204  oc.getBool("include-unused-routes"),
205  oc.getFloat("scale"),
206 // oc.getInt("max-search-depth"),
207  oc.getBool("emissions-only"));
209  }
210  if (oc.isSet("emitters-poi-output")) {
211  PROGRESS_BEGIN_MESSAGE("Writing emitter pois");
212  detectors.writeEmitterPOIs(oc.getString("emitters-poi-output"), flows);
214  }
215  }
216  // save end speed trigger if wished
217  if (oc.isSet("variable-speed-sign-output")) {
218  PROGRESS_BEGIN_MESSAGE("Writing speed triggers");
219  detectors.writeSpeedTrigger(optNet, oc.getString("variable-speed-sign-output"), flows,
220  begin, end, step);
222  }
223  // save checking detectors if wished
224  if (oc.isSet("validation-output")) {
225  PROGRESS_BEGIN_MESSAGE("Writing validation detectors");
226  detectors.writeValidationDetectors(oc.getString("validation-output"),
227  oc.getBool("validation-output.add-sources"), true, true); // !!!
229  }
230  // build global rerouter on end if wished
231  if (oc.isSet("end-reroute-output")) {
232  PROGRESS_BEGIN_MESSAGE("Writing highway end rerouter");
233  detectors.writeEndRerouterDetectors(oc.getString("end-reroute-output")); // !!!
235  }
236  /*
237  // save the insertion definitions
238  if(oc.isSet("flow-definitions")) {
239  buildVehicleEmissions(oc.getString("flow-definitions"));
240  }
241  */
242  //
243 }
244 
245 
246 /* -------------------------------------------------------------------------
247  * main
248  * ----------------------------------------------------------------------- */
249 int
250 main(int argc, char** argv) {
252  // give some application descriptions
253  oc.setApplicationDescription("Builds vehicle routes for SUMO using detector values.");
254  oc.setApplicationName("dfrouter", "Eclipse SUMO dfrouter Version " VERSION_STRING);
255  int ret = 0;
256  RODFNet* net = nullptr;
257  RODFDetectorCon* detectors = nullptr;
258  RODFDetectorFlows* flows = nullptr;
259  try {
260  // initialise the application system (messaging, xml, options)
261  XMLSubSys::init();
263  OptionsIO::setArgs(argc, argv);
265  if (oc.processMetaOptions(argc < 2)) {
267  return 0;
268  }
269  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
272  throw ProcessError();
273  }
275  // load data
276  ROLoader loader(oc, false, !oc.getBool("no-step-log"));
277  net = new RODFNet(oc.getBool("highway-mode"));
278  RODFEdgeBuilder builder;
279  loader.loadNet(*net, builder);
280  net->buildApproachList();
281  // load detectors
282  detectors = new RODFDetectorCon();
283  readDetectors(*detectors, oc, net);
284  // load detector values
285  flows = new RODFDetectorFlows(string2time(oc.getString("begin")), string2time(oc.getString("end")),
286  string2time(oc.getString("time-step")));
287  readDetectorFlows(*flows, oc, *detectors);
288  // build routes
289  startComputation(net, *flows, *detectors, oc);
290  } catch (const ProcessError& e) {
291  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
292  WRITE_ERROR(e.what());
293  }
294  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
295  ret = 1;
296 #ifndef _DEBUG
297  } catch (const std::exception& e) {
298  if (std::string(e.what()) != std::string("")) {
299  WRITE_ERROR(e.what());
300  }
301  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
302  ret = 1;
303  } catch (...) {
304  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
305  ret = 1;
306 #endif
307  }
308  delete net;
309  delete flows;
310  delete detectors;
312  if (ret == 0) {
313  std::cout << "Success." << std::endl;
314  }
315  return ret;
316 }
317 
318 
319 
320 /****************************************************************************/
321 
startComputation
void startComputation(RODFNet *optNet, RODFDetectorFlows &flows, RODFDetectorCon &detectors, OptionsCont &oc)
Definition: dfrouter_main.cpp:117
RODFDetectorHandler.h
RODFDetectorFlows
A container for flows.
Definition: RODFDetectorFlow.h:67
OptionsCont::isSet
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
Definition: OptionsCont.cpp:135
ROLoader
The data loader.
Definition: ROLoader.h:55
RODFDetectorCon::detectorsHaveRoutes
bool detectorsHaveRoutes() const
Definition: RODFDetector.cpp:512
OptionsCont::getInt
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
Definition: OptionsCont.cpp:215
OptionsCont::processMetaOptions
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
Definition: OptionsCont.cpp:557
ToString.h
XMLSubSys::runParser
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything's ok.
Definition: XMLSubSys.cpp:112
RODFDetectorHandler
SAX2-Handler for loading DFROUTER-detector definitions.
Definition: RODFDetectorHandler.h:41
SystemFrame::close
static void close()
Closes all of an applications subsystems.
Definition: SystemFrame.cpp:133
MsgHandler::initOutputOptions
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:216
RODFNet::buildApproachList
void buildApproachList()
Definition: RODFNet.cpp:62
RODFFrame::checkOptions
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid for usage within dfrouter.
Definition: RODFFrame.cpp:251
PROGRESS_FAILED_MESSAGE
#define PROGRESS_FAILED_MESSAGE()
Definition: MsgHandler.h:282
OptionsCont.h
RODFDetectorCon::writeSpeedTrigger
void writeSpeedTrigger(const RODFNet *const net, const std::string &file, const RODFDetectorFlows &flows, SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset)
Definition: RODFDetector.cpp:803
RODFDetectorCon::saveRoutes
void saveRoutes(const std::string &file) const
Definition: RODFDetector.cpp:585
RODFDetFlowLoader
A loader for detector flows.
Definition: RODFDetFlowLoader.h:42
RODFDetectorFlows::printAbsolute
void printAbsolute() const
Definition: RODFDetectorFlow.cpp:172
MsgHandler.h
RODFNet::revalidateFlows
void revalidateFlows(const RODFDetectorCon &detectors, RODFDetectorFlows &flows, SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset)
Definition: RODFNet.cpp:570
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:201
FileHelpers.h
RODFNet::buildRoutes
void buildRoutes(RODFDetectorCon &det, bool keepUnfoundEnds, bool includeInBetween, bool keepShortestOnly, int maxFollowingLength) const
Definition: RODFNet.cpp:340
MsgHandler::inform
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:118
RODFEdge.h
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
OptionsCont::getBool
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
Definition: OptionsCont.cpp:222
RODFDetector.h
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:57
readDetectorFlows
void readDetectorFlows(RODFDetectorFlows &flows, OptionsCont &oc, RODFDetectorCon &dc)
Definition: dfrouter_main.cpp:95
RODFDetectorCon
A container for RODFDetectors.
Definition: RODFDetector.h:220
ROLoader.h
RODFDetectorCon::writeEmitters
void writeEmitters(const std::string &file, const RODFDetectorFlows &flows, SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset, const RODFNet &net, bool writeCalibrators, bool includeUnusedRoutes, double scale, bool insertionsOnly)
Definition: RODFDetector.cpp:625
RODFNet::buildDetectorDependencies
void buildDetectorDependencies(RODFDetectorCon &detectors)
Definition: RODFNet.cpp:1025
OptionsCont::getStringVector
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
Definition: OptionsCont.cpp:235
main
int main(int argc, char **argv)
Definition: dfrouter_main.cpp:250
RODFNet::computeTypes
void computeTypes(RODFDetectorCon &dets, bool sourcesStrict) const
Definition: RODFNet.cpp:108
OptionsCont::setApplicationName
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.
Definition: OptionsCont.cpp:481
RODFDetectorCon::writeEmitterPOIs
void writeEmitterPOIs(const std::string &file, const RODFDetectorFlows &flows)
Definition: RODFDetector.cpp:727
SystemFrame.h
XMLSubSys::setValidation
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:58
RONet.h
RODFEdgeBuilder
Interface for building instances of dfrouter-edges.
Definition: RODFEdgeBuilder.h:49
OutputDevice.h
ProcessError
Definition: UtilExceptions.h:39
RODFDetectorCon::detectorsHaveCompleteTypes
bool detectorsHaveCompleteTypes() const
Definition: RODFDetector.cpp:501
UtilExceptions.h
XMLSubSys::init
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:47
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
readDetectors
void readDetectors(RODFDetectorCon &detectors, OptionsCont &oc, RODFNet *optNet)
Definition: dfrouter_main.cpp:69
ROLoader::loadNet
virtual void loadNet(RONet &toFill, ROAbstractEdgeBuilder &eb)
Loads the network.
Definition: ROLoader.cpp:113
string2time
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:44
RODFEdgeBuilder.h
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:77
RODFFrame::fillOptions
static void fillOptions()
Inserts options used by dfrouter into the OptionsCont-singleton.
Definition: RODFFrame.cpp:46
RODFNet::reportEmptyDetectors
void reportEmptyDetectors(RODFDetectorCon &detectors, RODFDetectorFlows &flows)
Definition: RODFNet.cpp:607
OptionsIO::getOptions
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:75
OptionsCont::setApplicationDescription
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
Definition: OptionsCont.cpp:489
OptionsCont::getFloat
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
Definition: OptionsCont.cpp:208
RODFFrame.h
RODFDetectorCon::getDetectors
const std::vector< RODFDetector * > & getDetectors() const
Definition: RODFDetector.cpp:523
StringUtils.h
RODFNet::removeEmptyDetectors
void removeEmptyDetectors(RODFDetectorCon &detectors, RODFDetectorFlows &flows)
Definition: RODFNet.cpp:584
PROGRESS_BEGIN_MESSAGE
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:278
RODFDetectorFlow.h
Option.h
RODFDetFlowLoader.h
PROGRESS_DONE_MESSAGE
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:279
RODFNet
A DFROUTER-network.
Definition: RODFNet.h:44
OptionsIO::setArgs
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:54
RODFDetectorCon::guessEmptyFlows
void guessEmptyFlows(RODFDetectorFlows &flows)
Definition: RODFDetector.cpp:899
SOURCE_DETECTOR
@ SOURCE_DETECTOR
A source detector.
Definition: RODFDetector.h:69
FileHelpers::isReadable
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:49
config.h
RODFDetectorCon::saveAsPOIs
void saveAsPOIs(const std::string &file) const
Definition: RODFDetector.cpp:557
SystemFrame::checkOptions
static bool checkOptions()
checks shared options and sets StdDefs
Definition: SystemFrame.cpp:120
RODFNet.h
MsgHandler::getErrorInstance
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:81
RODFDetectorCon::writeEndRerouterDetectors
void writeEndRerouterDetectors(const std::string &file)
Definition: RODFDetector.cpp:825
VERSION_STRING
#define VERSION_STRING
Definition: config.h:210
RODFDetectorCon::save
void save(const std::string &file) const
Definition: RODFDetector.cpp:529
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:283
RODFNet::buildEdgeFlowMap
void buildEdgeFlowMap(const RODFDetectorFlows &flows, const RODFDetectorCon &detectors, SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset)
Definition: RODFNet.cpp:927
RODFRouteCont.h
OptionsIO.h
RODFDetFlowLoader::read
void read(const std::string &file)
Reads the given file assuming it contains detector values.
Definition: RODFDetFlowLoader.cpp:54
RODFDetectorCon::writeValidationDetectors
void writeValidationDetectors(const std::string &file, bool includeSources, bool singleFile, bool friendly)
Definition: RODFDetector.cpp:841
XMLSubSys.h