SUMO - Simulation of Urban MObility
dfrouter_main.cpp
Go to the documentation of this file.
1 /****************************************************************************/
12 // Main for the DFROUTER
13 /****************************************************************************/
14 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
15 // Copyright (C) 2001-2015 DLR (http://www.dlr.de/) and contributors
16 /****************************************************************************/
17 //
18 // This file is part of SUMO.
19 // SUMO is free software: you can redistribute it and/or modify
20 // it under the terms of the GNU General Public License as published by
21 // the Free Software Foundation, either version 3 of the License, or
22 // (at your option) any later version.
23 //
24 /****************************************************************************/
25 
26 
27 // ===========================================================================
28 // included modules
29 // ===========================================================================
30 #ifdef _MSC_VER
31 #include <windows_config.h>
32 #else
33 #include <config.h>
34 #endif
35 
36 #ifdef HAVE_VERSION_H
37 #include <version.h>
38 #endif
39 
40 #include <xercesc/sax/SAXException.hpp>
41 #include <xercesc/sax/SAXParseException.hpp>
43 #include <iostream>
44 #include <string>
45 #include <limits.h>
46 #include <ctime>
47 #include <router/ROLoader.h>
48 #include <router/RONet.h>
49 #include "RODFEdgeBuilder.h"
50 #include <router/ROFrame.h>
52 #include <utils/options/Option.h>
57 #include <utils/common/ToString.h>
58 #include <utils/xml/XMLSubSys.h>
59 #include "RODFFrame.h"
60 #include "RODFNet.h"
61 #include "RODFEdge.h"
62 #include "RODFDetector.h"
63 #include "RODFDetectorHandler.h"
64 #include "RODFRouteCont.h"
65 #include "RODFDetectorFlow.h"
66 #include "RODFDetFlowLoader.h"
69 
70 #ifdef CHECK_MEMORY_LEAKS
71 #include <foreign/nvwa/debug_new.h>
72 #endif // CHECK_MEMORY_LEAKS
73 
74 
75 // ===========================================================================
76 // functions
77 // ===========================================================================
78 /* -------------------------------------------------------------------------
79  * data processing methods
80  * ----------------------------------------------------------------------- */
81 void
82 readDetectors(RODFDetectorCon& detectors, OptionsCont& oc, RODFNet* optNet) {
83  if (!oc.isSet("detector-files")) {
84  throw ProcessError("No detector file given (use --detector-files <FILE>).");
85  }
86  // read definitions stored in XML-format
87  std::vector<std::string> files = oc.getStringVector("detector-files");
88  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
89  if (!FileHelpers::isReadable(*fileIt)) {
90  throw ProcessError("Could not open detector file '" + *fileIt + "'");
91  }
92  PROGRESS_BEGIN_MESSAGE("Loading detector definitions from '" + *fileIt + "'");
93  RODFDetectorHandler handler(optNet, oc.getBool("ignore-invalid-detectors"), detectors, *fileIt);
94  if (XMLSubSys::runParser(handler, *fileIt)) {
96  } else {
98  throw ProcessError();
99  }
100  }
101  if (detectors.getDetectors().empty()) {
102  throw ProcessError("No detectors found.");
103  }
104 }
105 
106 
107 void
109  if (!oc.isSet("measure-files")) {
110  // ok, not given, return an empty container
111  return;
112  }
113  // check whether the file exists
114  std::vector<std::string> files = oc.getStringVector("measure-files");
115  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
116  if (!FileHelpers::isReadable(*fileIt)) {
117  throw ProcessError("The measure-file '" + *fileIt + "' can not be opened.");
118  }
119  // parse
120  PROGRESS_BEGIN_MESSAGE("Loading flows from '" + *fileIt + "'");
121  RODFDetFlowLoader dfl(dc, flows, string2time(oc.getString("begin")), string2time(oc.getString("end")),
122  string2time(oc.getString("time-offset")), string2time(oc.getString("time-factor")));
123  dfl.read(*fileIt);
125  }
126 }
127 
128 
129 void
131  if (oc.getBool("print-absolute-flows")) {
132  flows.printAbsolute();
133  }
134 
135  // if a network was loaded... (mode1)
136  if (optNet != 0) {
137  if (oc.getBool("remove-empty-detectors")) {
138  PROGRESS_BEGIN_MESSAGE("Removing empty detectors");
139  optNet->removeEmptyDetectors(detectors, flows);
141  } else if (oc.getBool("report-empty-detectors")) {
142  PROGRESS_BEGIN_MESSAGE("Scanning for empty detectors");
143  optNet->reportEmptyDetectors(detectors, flows);
145  }
146  // compute the detector types (optionally)
147  if (!detectors.detectorsHaveCompleteTypes() || oc.getBool("revalidate-detectors")) {
148  optNet->computeTypes(detectors, oc.getBool("strict-sources"));
149  }
150  std::vector<RODFDetector*>::const_iterator i = detectors.getDetectors().begin();
151  for (; i != detectors.getDetectors().end(); ++i) {
152  if ((*i)->getType() == SOURCE_DETECTOR) {
153  break;
154  }
155  }
156  if (i == detectors.getDetectors().end() && !oc.getBool("routes-for-all")) {
157  throw ProcessError("No source detectors found.");
158  }
159  // compute routes between the detectors (optionally)
160  if (!detectors.detectorsHaveRoutes() || oc.getBool("revalidate-routes") || oc.getBool("guess-empty-flows")) {
161  PROGRESS_BEGIN_MESSAGE("Computing routes");
162  optNet->buildRoutes(detectors,
163  oc.getBool("keep-unfinished-routes"), oc.getBool("routes-for-all"),
164  !oc.getBool("keep-longer-routes"), oc.getInt("max-search-depth"));
166  }
167  }
168 
169  // check
170  // whether the detectors are valid
171  if (!detectors.detectorsHaveCompleteTypes()) {
172  throw ProcessError("The detector types are not defined; use in combination with a network");
173  }
174  // whether the detectors have routes
175  if (!detectors.detectorsHaveRoutes()) {
176  throw ProcessError("The emitters have no routes; use in combination with a network");
177  }
178 
179  // save the detectors if wished
180  if (oc.isSet("detector-output")) {
181  detectors.save(oc.getString("detector-output"));
182  }
183  // save their positions as POIs if wished
184  if (oc.isSet("detectors-poi-output")) {
185  detectors.saveAsPOIs(oc.getString("detectors-poi-output"));
186  }
187 
188  // save the routes file if it was changed or it's wished
189  if (detectors.detectorsHaveRoutes() && oc.isSet("routes-output")) {
190  detectors.saveRoutes(oc.getString("routes-output"));
191  }
192 
193  // guess flows if wished
194  if (oc.getBool("guess-empty-flows")) {
195  optNet->buildDetectorDependencies(detectors);
196  detectors.guessEmptyFlows(flows);
197  }
198 
199  const SUMOTime begin = string2time(oc.getString("begin"));
200  const SUMOTime end = string2time(oc.getString("end"));
201  const SUMOTime step = string2time(oc.getString("time-step"));
202 
203  // save emitters if wished
204  if (oc.isSet("emitters-output") || oc.isSet("emitters-poi-output")) {
205  optNet->buildEdgeFlowMap(flows, detectors, begin, end, step); // !!!
206  if (oc.getBool("revalidate-flows")) {
207  PROGRESS_BEGIN_MESSAGE("Rechecking loaded flows");
208  optNet->revalidateFlows(detectors, flows, begin, end, step);
210  }
211  if (oc.isSet("emitters-output")) {
212  PROGRESS_BEGIN_MESSAGE("Writing emitters");
213  detectors.writeEmitters(oc.getString("emitters-output"), flows,
214  begin, end, step,
215  *optNet,
216  oc.getBool("calibrator-output"),
217  oc.getBool("include-unused-routes"),
218  oc.getFloat("scale"),
219 // oc.getInt("max-search-depth"),
220  oc.getBool("emissions-only"));
222  }
223  if (oc.isSet("emitters-poi-output")) {
224  PROGRESS_BEGIN_MESSAGE("Writing emitter pois");
225  detectors.writeEmitterPOIs(oc.getString("emitters-poi-output"), flows);
227  }
228  }
229  // save end speed trigger if wished
230  if (oc.isSet("variable-speed-sign-output")) {
231  PROGRESS_BEGIN_MESSAGE("Writing speed triggers");
232  detectors.writeSpeedTrigger(optNet, oc.getString("variable-speed-sign-output"), flows,
233  begin, end, step);
235  }
236  // save checking detectors if wished
237  if (oc.isSet("validation-output")) {
238  PROGRESS_BEGIN_MESSAGE("Writing validation detectors");
239  detectors.writeValidationDetectors(oc.getString("validation-output"),
240  oc.getBool("validation-output.add-sources"), true, true); // !!!
242  }
243  // build global rerouter on end if wished
244  if (oc.isSet("end-reroute-output")) {
245  PROGRESS_BEGIN_MESSAGE("Writing highway end rerouter");
246  detectors.writeEndRerouterDetectors(oc.getString("end-reroute-output")); // !!!
248  }
249  /*
250  // save the insertion definitions
251  if(oc.isSet("flow-definitions")) {
252  buildVehicleEmissions(oc.getString("flow-definitions"));
253  }
254  */
255  //
256 }
257 
258 
259 /* -------------------------------------------------------------------------
260  * main
261  * ----------------------------------------------------------------------- */
262 int
263 main(int argc, char** argv) {
265  // give some application descriptions
266  oc.setApplicationDescription("Builds vehicle routes for SUMO using detector values.");
267  oc.setApplicationName("dfrouter", "SUMO dfrouter Version " + getBuildName(VERSION_STRING));
268  int ret = 0;
269  RODFNet* net = 0;
270  RODFDetectorCon* detectors = 0;
271  RODFDetectorFlows* flows = 0;
272  try {
273  // initialise the application system (messaging, xml, options)
274  XMLSubSys::init();
276  OptionsIO::setArgs(argc, argv);
278  if (oc.processMetaOptions(argc < 2)) {
280  return 0;
281  }
282  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
284  if (!RODFFrame::checkOptions()) {
285  throw ProcessError();
286  }
288  // load data
289  ROLoader loader(oc, false, !oc.getBool("no-step-log"));
290  net = new RODFNet(oc.getBool("highway-mode"));
291  RODFEdgeBuilder builder;
292  loader.loadNet(*net, builder);
293  net->buildApproachList();
294  // load detectors
295  detectors = new RODFDetectorCon();
296  readDetectors(*detectors, oc, net);
297  // load detector values
298  flows = new RODFDetectorFlows(string2time(oc.getString("begin")), string2time(oc.getString("end")),
299  string2time(oc.getString("time-step")));
300  readDetectorFlows(*flows, oc, *detectors);
301  // build routes
302  startComputation(net, *flows, *detectors, oc);
303  } catch (const ProcessError& e) {
304  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
305  WRITE_ERROR(e.what());
306  }
307  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
308  ret = 1;
309 #ifndef _DEBUG
310  } catch (const std::exception& e) {
311  if (std::string(e.what()) != std::string("")) {
312  WRITE_ERROR(e.what());
313  }
314  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
315  ret = 1;
316  } catch (...) {
317  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
318  ret = 1;
319 #endif
320  }
321  delete net;
322  delete flows;
323  delete detectors;
325  if (ret == 0) {
326  std::cout << "Success." << std::endl;
327  }
328  return ret;
329 }
330 
331 
332 
333 /****************************************************************************/
334 
void revalidateFlows(const RODFDetectorCon &detectors, RODFDetectorFlows &flows, SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset)
Definition: RODFNet.cpp:575
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) ...
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:58
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
long long int SUMOTime
Definition: SUMOTime.h:43
void readDetectors(RODFDetectorCon &detectors, OptionsCont &oc, RODFNet *optNet)
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:58
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:69
A source detector.
Definition: RODFDetector.h:76
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)
void reportEmptyDetectors(RODFDetectorCon &detectors, RODFDetectorFlows &flows)
Definition: RODFNet.cpp:612
void computeTypes(RODFDetectorCon &dets, bool sourcesStrict) const
Definition: RODFNet.cpp:113
const std::vector< RODFDetector * > & getDetectors() const
SUMOReal getFloat(const std::string &name) const
Returns the SUMOReal-value of the named option (only for Option_Float)
void saveRoutes(const std::string &file) const
void writeSpeedTrigger(const RODFNet *const net, const std::string &file, const RODFDetectorFlows &flows, SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset)
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything&#39;s ok.
Definition: XMLSubSys.cpp:114
static void close()
Closes all of an applications subsystems.
A container for flows.
A container for RODFDetectors.
Definition: RODFDetector.h:227
A loader for detector flows.
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:64
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
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 buildRoutes(RODFDetectorCon &det, bool keepUnfoundEnds, bool includeInBetween, bool keepShortestOnly, int maxFollowingLength) const
Definition: RODFNet.cpp:345
void printAbsolute() const
void writeEmitters(const std::string &file, const RODFDetectorFlows &flows, SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset, const RODFNet &net, bool writeCalibrators, bool includeUnusedRoutes, SUMOReal scale, bool insertionsOnly)
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
#define PROGRESS_FAILED_MESSAGE()
Definition: MsgHandler.h:204
void save(const std::string &file) const
void read(const std::string &file)
Reads the given file assuming it contains detector values.
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:91
A DFROUTER-network.
Definition: RODFNet.h:51
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:48
Interface for building instances of dfrouter-edges.
void writeValidationDetectors(const std::string &file, bool includeSources, bool singleFile, bool friendly)
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:202
void readDetectorFlows(RODFDetectorFlows &flows, OptionsCont &oc, RODFDetectorCon &dc)
void saveAsPOIs(const std::string &file) const
#define VERSION_STRING
Definition: config.h:226
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
static void getOptions()
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:71
void startComputation(RODFNet *optNet, RODFDetectorFlows &flows, RODFDetectorCon &detectors, OptionsCont &oc)
SAX2-Handler for loading DFROUTER-detector definitions.
void buildEdgeFlowMap(const RODFDetectorFlows &flows, const RODFDetectorCon &detectors, SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset)
Definition: RODFNet.cpp:933
void guessEmptyFlows(RODFDetectorFlows &flows)
void buildDetectorDependencies(RODFDetectorCon &detectors)
Definition: RODFNet.cpp:1006
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
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid for usage within dfrouter...
Definition: RODFFrame.cpp:261
void writeEmitterPOIs(const std::string &file, const RODFDetectorFlows &flows)
void writeEndRerouterDetectors(const std::string &file)
void removeEmptyDetectors(RODFDetectorCon &detectors, RODFDetectorFlows &flows)
Definition: RODFNet.cpp:589
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:203
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
bool detectorsHaveRoutes() const
static void initOutputOptions()
Definition: MsgHandler.cpp:197
int main(int argc, char **argv)
bool detectorsHaveCompleteTypes() const
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
static void fillOptions()
Inserts options used by dfrouter into the OptionsCont-singleton.
Definition: RODFFrame.cpp:58
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.