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