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