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.sourceforge.net/
15 // Copyright (C) 2001-2012 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 }
102 
103 
104 void
106  if (!oc.isSet("measure-files")) {
107  // ok, not given, return an empty container
108  return;
109  }
110  // check whether the file exists
111  std::vector<std::string> files = oc.getStringVector("measure-files");
112  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
113  if (!FileHelpers::exists(*fileIt)) {
114  throw ProcessError("The measure-file '" + *fileIt + "' can not be opened.");
115  }
116  // parse
117  PROGRESS_BEGIN_MESSAGE("Loading flows from '" + *fileIt + "'");
118  RODFDetFlowLoader dfl(dc, flows, string2time(oc.getString("begin")), string2time(oc.getString("end")),
119  string2time(oc.getString("time-offset")), string2time(oc.getString("time-factor")));
120  dfl.read(*fileIt);
122  }
123 }
124 
125 
126 void
128  if (oc.getBool("print-absolute-flows")) {
129  flows.printAbsolute();
130  }
131 
132  // if a network was loaded... (mode1)
133  if (optNet != 0) {
134  if (oc.getBool("remove-empty-detectors")) {
135  PROGRESS_BEGIN_MESSAGE("Removing empty detectors");
136  optNet->removeEmptyDetectors(detectors, flows);
138  } else if (oc.getBool("report-empty-detectors")) {
139  PROGRESS_BEGIN_MESSAGE("Scanning for empty detectors");
140  optNet->reportEmptyDetectors(detectors, flows);
142  }
143  // compute the detector types (optionally)
144  if (!detectors.detectorsHaveCompleteTypes() || oc.getBool("revalidate-detectors")) {
145  optNet->computeTypes(detectors, oc.getBool("strict-sources"));
146  }
147  // compute routes between the detectors (optionally)
148  if (!detectors.detectorsHaveRoutes() || oc.getBool("revalidate-routes") || oc.getBool("guess-empty-flows")) {
149  PROGRESS_BEGIN_MESSAGE("Computing routes");
150  optNet->buildRoutes(detectors,
151  oc.getBool("all-end-follower"), oc.getBool("keep-unfinished-routes"),
152  oc.getBool("routes-for-all"), !oc.getBool("keep-longer-routes"),
153  oc.getInt("max-search-depth"));
155  }
156  }
157 
158  // check
159  // whether the detectors are valid
160  if (!detectors.detectorsHaveCompleteTypes()) {
161  throw ProcessError("The detector types are not defined; use in combination with a network");
162  }
163  // whether the detectors have routes
164  if (!detectors.detectorsHaveRoutes()) {
165  throw ProcessError("The emitters have no routes; use in combination with a network");
166  }
167 
168  // save the detectors if wished
169  if (oc.isSet("detector-output")) {
170  detectors.save(oc.getString("detector-output"));
171  }
172  // save their positions as POIs if wished
173  if (oc.isSet("detectors-poi-output")) {
174  detectors.saveAsPOIs(oc.getString("detectors-poi-output"));
175  }
176 
177  // save the routes file if it was changed or it's wished
178  if (detectors.detectorsHaveRoutes() && oc.isSet("routes-output")) {
179  detectors.saveRoutes(oc.getString("routes-output"));
180  }
181 
182  // guess flows if wished
183  if (oc.getBool("guess-empty-flows")) {
184  optNet->buildDetectorDependencies(detectors);
185  detectors.guessEmptyFlows(flows);
186  }
187 
188  const SUMOTime begin = string2time(oc.getString("begin"));
189  const SUMOTime end = string2time(oc.getString("end"));
190  const SUMOTime step = string2time(oc.getString("time-step"));
191 
192  // save emitters if wished
193  if (oc.isSet("emitters-output") || oc.isSet("emitters-poi-output")) {
194  optNet->buildEdgeFlowMap(flows, detectors, begin, end, step); // !!!
195  if (oc.getBool("revalidate-flows")) {
196  PROGRESS_BEGIN_MESSAGE("Rechecking loaded flows");
197  optNet->revalidateFlows(detectors, flows, begin, end, step);
199  }
200  if (oc.isSet("emitters-output")) {
201  PROGRESS_BEGIN_MESSAGE("Writing emitters");
202  detectors.writeEmitters(oc.getString("emitters-output"), flows,
203  begin, end, step,
204  *optNet,
205  oc.getBool("calibrator-output"),
206  oc.getBool("include-unused-routes"),
207  oc.getFloat("scale"),
208 // oc.getInt("max-search-depth"),
209  oc.getBool("emissions-only"));
211  }
212  if (oc.isSet("emitters-poi-output")) {
213  PROGRESS_BEGIN_MESSAGE("Writing emitter pois");
214  detectors.writeEmitterPOIs(oc.getString("emitters-poi-output"), flows);
216  }
217  }
218  // save end speed trigger if wished
219  if (oc.isSet("variable-speed-sign-output")) {
220  PROGRESS_BEGIN_MESSAGE("Writing speed triggers");
221  detectors.writeSpeedTrigger(optNet, oc.getString("variable-speed-sign-output"), flows,
222  begin, end, step);
224  }
225  // save checking detectors if wished
226  if (oc.isSet("validation-output")) {
227  PROGRESS_BEGIN_MESSAGE("Writing validation detectors");
228  detectors.writeValidationDetectors(oc.getString("validation-output"),
229  oc.getBool("validation-output.add-sources"), true, true); // !!!
231  }
232  // build global rerouter on end if wished
233  if (oc.isSet("end-reroute-output")) {
234  PROGRESS_BEGIN_MESSAGE("Writing highway end rerouter");
235  detectors.writeEndRerouterDetectors(oc.getString("end-reroute-output")); // !!!
237  }
238  /*
239  // save the insertion definitions
240  if(oc.isSet("flow-definitions")) {
241  buildVehicleEmissions(oc.getString("flow-definitions"));
242  }
243  */
244  //
245 }
246 
247 
248 /* -------------------------------------------------------------------------
249  * main
250  * ----------------------------------------------------------------------- */
251 int
252 main(int argc, char** argv) {
254  // give some application descriptions
255  oc.setApplicationDescription("Builds vehicle routes for SUMO using detector values.");
256  oc.setApplicationName("dfrouter", "SUMO dfrouter Version " + (std::string)VERSION_STRING);
257  int ret = 0;
258  RODFNet* net = 0;
259  RODFDetectorCon* detectors = 0;
260  RODFDetectorFlows* flows = 0;
261  try {
262  // initialise the application system (messaging, xml, options)
263  XMLSubSys::init();
265  OptionsIO::getOptions(true, argc, argv);
266  if (oc.processMetaOptions(argc < 2)) {
268  return 0;
269  }
270  XMLSubSys::setValidation(oc.getBool("xml-validation"));
272  if (!RODFFrame::checkOptions()) {
273  throw ProcessError();
274  }
276  // load data
277  ROLoader loader(oc, false);
278  net = new RODFNet(oc.getBool("highway-mode"));
279  RODFEdgeBuilder builder;
280  loader.loadNet(*net, builder);
281  net->buildApproachList();
282  // load detectors
283  detectors = new RODFDetectorCon();
284  readDetectors(*detectors, oc, net);
285  // load detector values
286  flows = new RODFDetectorFlows(string2time(oc.getString("begin")), string2time(oc.getString("end")),
287  string2time(oc.getString("time-step")));
288  readDetectorFlows(*flows, oc, *detectors);
289  // build routes
290  startComputation(net, *flows, *detectors, oc);
291  } catch (const ProcessError& e) {
292  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
293  WRITE_ERROR(e.what());
294  }
295  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
296  ret = 1;
297 #ifndef _DEBUG
298  } catch (const std::exception& e) {
299  if (std::string(e.what()) != std::string("")) {
300  WRITE_ERROR(e.what());
301  }
302  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
303  ret = 1;
304  } catch (...) {
305  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
306  ret = 1;
307 #endif
308  }
309  delete net;
310  delete flows;
311  delete detectors;
313  if (ret == 0) {
314  std::cout << "Success." << std::endl;
315  }
316  return ret;
317 }
318 
319 
320 
321 /****************************************************************************/
322