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-2014 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 " + (std::string)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::getOptions(true, argc, argv);
277  if (oc.processMetaOptions(argc < 2)) {
279  return 0;
280  }
281  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
283  if (!RODFFrame::checkOptions()) {
284  throw ProcessError();
285  }
287  // load data
288  ROLoader loader(oc, false, !oc.getBool("no-step-log"));
289  net = new RODFNet(oc.getBool("highway-mode"));
290  RODFEdgeBuilder builder;
291  loader.loadNet(*net, builder);
292  net->buildApproachList();
293  // load detectors
294  detectors = new RODFDetectorCon();
295  readDetectors(*detectors, oc, net);
296  // load detector values
297  flows = new RODFDetectorFlows(string2time(oc.getString("begin")), string2time(oc.getString("end")),
298  string2time(oc.getString("time-step")));
299  readDetectorFlows(*flows, oc, *detectors);
300  // build routes
301  startComputation(net, *flows, *detectors, oc);
302  } catch (const ProcessError& e) {
303  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
304  WRITE_ERROR(e.what());
305  }
306  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
307  ret = 1;
308 #ifndef _DEBUG
309  } catch (const std::exception& e) {
310  if (std::string(e.what()) != std::string("")) {
311  WRITE_ERROR(e.what());
312  }
313  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
314  ret = 1;
315  } catch (...) {
316  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
317  ret = 1;
318 #endif
319  }
320  delete net;
321  delete flows;
322  delete detectors;
324  if (ret == 0) {
325  std::cout << "Success." << std::endl;
326  }
327  return ret;
328 }
329 
330 
331 
332 /****************************************************************************/
333