SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ROLoader.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // Loader for networks and route imports
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
14 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
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 #include <iostream>
36 #include <string>
37 #include <iomanip>
38 #include <xercesc/parsers/SAXParser.hpp>
39 #include <xercesc/util/PlatformUtils.hpp>
40 #include <xercesc/util/TransService.hpp>
41 #include <xercesc/sax2/SAX2XMLReader.hpp>
43 #include <utils/common/ToString.h>
48 #include <utils/xml/XMLSubSys.h>
50 #include "RONet.h"
51 #include "RONetHandler.h"
52 #include "ROLoader.h"
53 #include "ROEdge.h"
54 #include "RORDLoader_TripDefs.h"
55 #include "RORDLoader_SUMOBase.h"
57 #include "ROTypedXMLRoutesLoader.h"
58 
59 #ifdef HAVE_MESOSIM // catchall for internal stuff
60 #include <internal/RouteAggregator.h>
61 #endif // have HAVE_MESOSIM
62 
63 #ifdef CHECK_MEMORY_LEAKS
64 #include <foreign/nvwa/debug_new.h>
65 #endif // CHECK_MEMORY_LEAKS
66 
67 
68 // ===========================================================================
69 // method definitions
70 // ===========================================================================
71 // ---------------------------------------------------------------------------
72 // ROLoader::EdgeFloatTimeLineRetriever_EdgeTravelTime - methods
73 // ---------------------------------------------------------------------------
74 void
76  SUMOReal val, SUMOReal beg, SUMOReal end) const {
77  ROEdge* e = myNet.getEdge(id);
78  if (e != 0) {
79  e->addTravelTime(val, beg, end);
80  } else {
81  if (id[0] != ':') {
82  WRITE_ERROR("Trying to set a weight for the unknown edge '" + id + "'.");
83  }
84  }
85 }
86 
87 
88 // ---------------------------------------------------------------------------
89 // ROLoader::EdgeFloatTimeLineRetriever_EdgeWeight - methods
90 // ---------------------------------------------------------------------------
91 void
93  SUMOReal val, SUMOReal beg, SUMOReal end) const {
94  ROEdge* e = myNet.getEdge(id);
95  if (e != 0) {
96  e->addEffort(val, beg, end);
97  } else {
98  if (id[0] != ':') {
99  WRITE_ERROR("Trying to set a weight for the unknown edge '" + id + "'.");
100  }
101  }
102 }
103 
104 
105 // ---------------------------------------------------------------------------
106 // ROLoader - methods
107 // ---------------------------------------------------------------------------
108 ROLoader::ROLoader(OptionsCont& oc, bool emptyDestinationsAllowed) :
109  myOptions(oc),
110  myEmptyDestinationsAllowed(emptyDestinationsAllowed),
111  myLogSteps(!oc.getBool("no-step-log"))
112 {}
113 
114 
116  destroyHandlers();
117 }
118 
119 
120 void
122  std::string file = myOptions.getString("net-file");
123  if (file == "") {
124  throw ProcessError("Missing definition of network to load!");
125  }
126  if (!FileHelpers::exists(file)) {
127  throw ProcessError("The network file '" + file + "' could not be found.");
128  }
129  PROGRESS_BEGIN_MESSAGE("Loading net");
130  RONetHandler handler(toFill, eb);
131  handler.setFileName(file);
132  if (!XMLSubSys::runParser(handler, file)) {
134  throw ProcessError();
135  } else {
137  }
138  if (myOptions.isSet("taz-files", false)) { // dfrouter does not register this option
139  file = myOptions.getString("taz-files");
140  if (!FileHelpers::exists(file)) {
141  throw ProcessError("The districts file '" + file + "' could not be found.");
142  }
143  PROGRESS_BEGIN_MESSAGE("Loading districts");
144  handler.setFileName(file);
145  if (!XMLSubSys::runParser(handler, file)) {
147  throw ProcessError();
148  } else {
150  }
151  }
152 }
153 
154 
155 unsigned int
157  // build loader
158  // load sumo-routes when wished
159  bool ok = openTypedRoutes("route-files", net);
160  // load the XML-trip definitions when wished
161  ok &= openTypedRoutes("trip-files", net);
162  // load the sumo-alternative file when wished
163  ok &= openTypedRoutes("alternative-files", net);
164  // load the amount definitions if wished
165  ok &= openTypedRoutes("flow-files", net);
166  // check
167  if (ok && myHandler.size() == 0) {
168  throw ProcessError("No route input specified.");
169  }
170  // skip routes prior to the begin time
171  if (ok && !myOptions.getBool("unsorted-input")) {
172  WRITE_MESSAGE("Skipping...");
173  for (RouteLoaderCont::iterator i = myHandler.begin(); ok && i != myHandler.end(); i++) {
174  ok &= (*i)->readRoutesAtLeastUntil(string2time(myOptions.getString("begin")));
175  }
176  WRITE_MESSAGE("Skipped until: " + time2string(getMinTimeStep()));
177  }
178  // check whether everything's ok
179  if (!ok) {
180  destroyHandlers();
181  throw ProcessError();
182  }
183  return (unsigned int) myHandler.size();
184 }
185 
186 
187 void
190  SUMOTime absNo = end - start;
191  // skip routes that begin before the simulation's begin
192  // loop till the end
193  bool endReached = false;
194  bool errorOccured = false;
195  SUMOTime time = myHandler.size() != 0 ? getMinTimeStep() : start;
196  SUMOTime firstStep = time;
197  SUMOTime lastStep = time;
198  for (; time < end && !errorOccured && !endReached; time += DELTA_T) {
199  writeStats(time, start, absNo);
200  makeSingleStep(time, net, router);
201  // check whether further data exist
202  endReached = !net.furtherStored();
203  lastStep = time;
204  for (RouteLoaderCont::iterator i = myHandler.begin(); endReached && i != myHandler.end(); i++) {
205  if (!(*i)->ended()) {
206  endReached = false;
207  }
208  }
209  errorOccured = MsgHandler::getErrorInstance()->wasInformed() && !myOptions.getBool("ignore-errors");
210  }
211  if (myLogSteps) {
212  WRITE_MESSAGE("Routes found between time steps " + time2string(firstStep) + " and " + time2string(lastStep) + ".");
213  }
214 }
215 
216 
217 bool
219  RouteLoaderCont::iterator i;
220  // go through all handlers
221  if (myHandler.size() != 0) {
222  for (i = myHandler.begin(); i != myHandler.end(); i++) {
223  // load routes until the time point is reached
224  if ((*i)->readRoutesAtLeastUntil(end)) {
225  // save the routes
226  net.saveAndRemoveRoutesUntil(myOptions, router, end);
227  } else {
228  return false;
229  }
230  }
232  } else {
233  return false;
234  }
235 }
236 
237 
238 SUMOTime
240  RouteLoaderCont::const_iterator i = myHandler.begin();
241  SUMOTime ret = (*i)->getLastReadTimeStep();
242  ++i;
243  for (; i != myHandler.end(); i++) {
244  SUMOTime akt = (*i)->getLastReadTimeStep();
245  if (akt < ret) {
246  ret = akt;
247  }
248  }
249  return ret;
250 }
251 
252 
253 void
256  long absNo = end - start;
257  bool ok = true;
258  for (RouteLoaderCont::iterator i = myHandler.begin(); ok && i != myHandler.end(); i++) {
259  ok &= (*i)->readRoutesAtLeastUntil(SUMOTime_MAX);
260  }
261  // save the routes
262  SUMOTime time = start;
263  for (; time < end;) {
264  writeStats(time, start, absNo);
265  time = net.saveAndRemoveRoutesUntil(myOptions, router, time);
266  if (time < 0) {
267  time = end;
268  }
269  }
270 }
271 
272 
273 void
276 #ifndef HAVE_MESOSIM // catchall for internal stuff
277  assert(false);
278 #else
279  bool ok = true;
280  for (RouteLoaderCont::iterator i = myHandler.begin(); ok && i != myHandler.end(); i++) {
281  ok &= (*i)->readRoutesAtLeastUntil(SUMOTime_MAX);
282  }
284  net.saveAndRemoveRoutesUntil(myOptions, router, end);
285 #endif
286 }
287 
288 
289 bool
290 ROLoader::openTypedRoutes(const std::string& optionName,
291  RONet& net) {
292  // check whether the current loader is known
293  // (not all routers import all route formats)
294  if (!myOptions.exists(optionName)) {
295  return true;
296  }
297  // check whether the current loader is wished
298  // and the file(s) can be used
299  if (!myOptions.isUsableFileList(optionName)) {
300  return !myOptions.isSet(optionName);
301  }
302  bool ok = true;
303  std::vector<std::string> files = myOptions.getStringVector(optionName);
304  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
305  // build the instance when everything's all right
306  try {
307  ROTypedXMLRoutesLoader* instance = buildNamedHandler(optionName, *fileIt, net);
308  myHandler.push_back(instance);
309  } catch (ProcessError& e) {
310  std::string msg = "The loader for " + optionName + " from file '" + *fileIt + "' could not be initialised;";
311  std::string reason = e.what();
312  if (reason != "Process Error" && reason != "") {
313  msg = msg + "\n Reason: " + reason + ".";
314  } else {
315  msg = msg + "\n (unknown reason).";
316  }
317  WRITE_ERROR(msg);
318  ok = false;
319  }
320  }
321  return ok;
322 }
323 
324 
326 ROLoader::buildNamedHandler(const std::string& optionName,
327  const std::string& file,
328  RONet& net) {
329  if (optionName == "route-files" || optionName == "alternative-files") {
330  return new RORDLoader_SUMOBase(net,
332  myOptions.getInt("max-alternatives"), myOptions.getBool("repair"),
333  myOptions.getBool("with-taz"), myOptions.getBool("keep-all-routes"),
334  myOptions.getBool("skip-new-routes"), file);
335  }
336  if (optionName == "trip-files") {
337  return new RORDLoader_TripDefs(net,
339  myEmptyDestinationsAllowed, myOptions.getBool("with-taz"), file);
340  }
341  if (optionName == "flow-files") {
342  return new RORDGenerator_ODAmounts(net,
344  myEmptyDestinationsAllowed, myOptions.getBool("randomize-flows"), file);
345  }
346  return 0;
347 }
348 
349 
350 bool
351 ROLoader::loadWeights(RONet& net, const std::string& optionName,
352  const std::string& measure, bool useLanes) {
353  // check whether the file exists
354  if (!myOptions.isUsableFileList(optionName)) {
355  return false;
356  }
357  // build and prepare the weights handler
358  std::vector<SAXWeightsHandler::ToRetrieveDefinition*> retrieverDefs;
359  // travel time, first (always used)
361  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition("traveltime", !useLanes, ttRetriever));
362  // the measure to use, then
364  if (measure != "traveltime") {
365  std::string umeasure = measure;
366  if (measure == "CO" || measure == "CO2" || measure == "HC" || measure == "PMx" || measure == "NOx" || measure == "fuel") {
367  umeasure = measure + "_perVeh";
368  }
369  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition(umeasure, !useLanes, eRetriever));
370  }
371  // set up handler
372  SAXWeightsHandler handler(retrieverDefs, "");
373  // go through files
374  std::vector<std::string> files = myOptions.getStringVector(optionName);
375  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
376  PROGRESS_BEGIN_MESSAGE("Loading precomputed net weights from '" + *fileIt + "'");
377  if (XMLSubSys::runParser(handler, *fileIt)) {
379  } else {
380  WRITE_MESSAGE("failed.");
381  return false;
382  }
383  }
384  // build edge-internal time lines
385  const std::map<std::string, ROEdge*> &edges = net.getEdgeMap();
386  for (std::map<std::string, ROEdge*>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
387  (*i).second->buildTimeLines(measure);
388  }
389  return true;
390 }
391 
392 
393 void
394 ROLoader::writeStats(SUMOTime time, SUMOTime start, int absNo) {
395  if (myLogSteps) {
396  const SUMOReal perc = (SUMOReal)(time - start) / (SUMOReal) absNo;
397  std::cout << "Reading time step: " + time2string(time) + " (" + time2string(time - start) + "/" + time2string(absNo) + " = " + toString(perc * 100) + "% done) \r";
398  }
399 }
400 
401 
402 void
404  for (RouteLoaderCont::const_iterator i = myHandler.begin(); i != myHandler.end(); ++i) {
405  delete *i;
406  }
407  myHandler.clear();
408 }
409 
410 
411 /****************************************************************************/
412