SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-sim.org/
15 // Copyright (C) 2001-2013 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::exists(*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::exists(*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("all-end-follower"), oc.getBool("keep-unfinished-routes"),
164  oc.getBool("routes-for-all"), !oc.getBool("keep-longer-routes"),
165  oc.getInt("max-search-depth"));
167  }
168  }
169 
170  // check
171  // whether the detectors are valid
172  if (!detectors.detectorsHaveCompleteTypes()) {
173  throw ProcessError("The detector types are not defined; use in combination with a network");
174  }
175  // whether the detectors have routes
176  if (!detectors.detectorsHaveRoutes()) {
177  throw ProcessError("The emitters have no routes; use in combination with a network");
178  }
179 
180  // save the detectors if wished
181  if (oc.isSet("detector-output")) {
182  detectors.save(oc.getString("detector-output"));
183  }
184  // save their positions as POIs if wished
185  if (oc.isSet("detectors-poi-output")) {
186  detectors.saveAsPOIs(oc.getString("detectors-poi-output"));
187  }
188 
189  // save the routes file if it was changed or it's wished
190  if (detectors.detectorsHaveRoutes() && oc.isSet("routes-output")) {
191  detectors.saveRoutes(oc.getString("routes-output"));
192  }
193 
194  // guess flows if wished
195  if (oc.getBool("guess-empty-flows")) {
196  optNet->buildDetectorDependencies(detectors);
197  detectors.guessEmptyFlows(flows);
198  }
199 
200  const SUMOTime begin = string2time(oc.getString("begin"));
201  const SUMOTime end = string2time(oc.getString("end"));
202  const SUMOTime step = string2time(oc.getString("time-step"));
203 
204  // save emitters if wished
205  if (oc.isSet("emitters-output") || oc.isSet("emitters-poi-output")) {
206  optNet->buildEdgeFlowMap(flows, detectors, begin, end, step); // !!!
207  if (oc.getBool("revalidate-flows")) {
208  PROGRESS_BEGIN_MESSAGE("Rechecking loaded flows");
209  optNet->revalidateFlows(detectors, flows, begin, end, step);
211  }
212  if (oc.isSet("emitters-output")) {
213  PROGRESS_BEGIN_MESSAGE("Writing emitters");
214  detectors.writeEmitters(oc.getString("emitters-output"), flows,
215  begin, end, step,
216  *optNet,
217  oc.getBool("calibrator-output"),
218  oc.getBool("include-unused-routes"),
219  oc.getFloat("scale"),
220 // oc.getInt("max-search-depth"),
221  oc.getBool("emissions-only"));
223  }
224  if (oc.isSet("emitters-poi-output")) {
225  PROGRESS_BEGIN_MESSAGE("Writing emitter pois");
226  detectors.writeEmitterPOIs(oc.getString("emitters-poi-output"), flows);
228  }
229  }
230  // save end speed trigger if wished
231  if (oc.isSet("variable-speed-sign-output")) {
232  PROGRESS_BEGIN_MESSAGE("Writing speed triggers");
233  detectors.writeSpeedTrigger(optNet, oc.getString("variable-speed-sign-output"), flows,
234  begin, end, step);
236  }
237  // save checking detectors if wished
238  if (oc.isSet("validation-output")) {
239  PROGRESS_BEGIN_MESSAGE("Writing validation detectors");
240  detectors.writeValidationDetectors(oc.getString("validation-output"),
241  oc.getBool("validation-output.add-sources"), true, true); // !!!
243  }
244  // build global rerouter on end if wished
245  if (oc.isSet("end-reroute-output")) {
246  PROGRESS_BEGIN_MESSAGE("Writing highway end rerouter");
247  detectors.writeEndRerouterDetectors(oc.getString("end-reroute-output")); // !!!
249  }
250  /*
251  // save the insertion definitions
252  if(oc.isSet("flow-definitions")) {
253  buildVehicleEmissions(oc.getString("flow-definitions"));
254  }
255  */
256  //
257 }
258 
259 
260 /* -------------------------------------------------------------------------
261  * main
262  * ----------------------------------------------------------------------- */
263 int
264 main(int argc, char** argv) {
266  // give some application descriptions
267  oc.setApplicationDescription("Builds vehicle routes for SUMO using detector values.");
268  oc.setApplicationName("dfrouter", "SUMO dfrouter Version " + (std::string)VERSION_STRING);
269  int ret = 0;
270  RODFNet* net = 0;
271  RODFDetectorCon* detectors = 0;
272  RODFDetectorFlows* flows = 0;
273  try {
274  // initialise the application system (messaging, xml, options)
275  XMLSubSys::init();
277  OptionsIO::getOptions(true, argc, argv);
278  if (oc.processMetaOptions(argc < 2)) {
280  return 0;
281  }
282  XMLSubSys::setValidation(oc.getBool("xml-validation"));
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