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_INTERNAL // catchall for internal stuff
60 #include <internal/RouteAggregator.h>
61 #endif // have HAVE_INTERNAL
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  if (OptionsCont::getOptions().getBool("ignore-errors")) {
83  WRITE_WARNING("Trying to set a weight for the unknown edge '" + id + "'.");
84  } else {
85  WRITE_ERROR("Trying to set a weight for the unknown edge '" + id + "'.");
86  }
87  }
88  }
89 }
90 
91 
92 // ---------------------------------------------------------------------------
93 // ROLoader::EdgeFloatTimeLineRetriever_EdgeWeight - methods
94 // ---------------------------------------------------------------------------
95 void
97  SUMOReal val, SUMOReal beg, SUMOReal end) const {
98  ROEdge* e = myNet.getEdge(id);
99  if (e != 0) {
100  e->addEffort(val, beg, end);
101  } else {
102  if (id[0] != ':') {
103  if (OptionsCont::getOptions().getBool("ignore-errors")) {
104  WRITE_WARNING("Trying to set a weight for the unknown edge '" + id + "'.");
105  } else {
106  WRITE_ERROR("Trying to set a weight for the unknown edge '" + id + "'.");
107  }
108  }
109  }
110 }
111 
112 
113 // ---------------------------------------------------------------------------
114 // ROLoader - methods
115 // ---------------------------------------------------------------------------
116 ROLoader::ROLoader(OptionsCont& oc, bool emptyDestinationsAllowed) :
117  myOptions(oc),
118  myEmptyDestinationsAllowed(emptyDestinationsAllowed),
119  myLogSteps(!oc.getBool("no-step-log"))
120 {}
121 
122 
124  destroyHandlers();
125 }
126 
127 
128 void
130  std::string file = myOptions.getString("net-file");
131  if (file == "") {
132  throw ProcessError("Missing definition of network to load!");
133  }
134  if (!FileHelpers::exists(file)) {
135  throw ProcessError("The network file '" + file + "' could not be found.");
136  }
137  PROGRESS_BEGIN_MESSAGE("Loading net");
138  RONetHandler handler(toFill, eb);
139  handler.setFileName(file);
140  if (!XMLSubSys::runParser(handler, file)) {
142  throw ProcessError();
143  } else {
145  }
146  if (myOptions.isSet("taz-files", false)) { // dfrouter does not register this option
147  file = myOptions.getString("taz-files");
148  if (!FileHelpers::exists(file)) {
149  throw ProcessError("The districts file '" + file + "' could not be found.");
150  }
151  PROGRESS_BEGIN_MESSAGE("Loading districts");
152  handler.setFileName(file);
153  if (!XMLSubSys::runParser(handler, file)) {
155  throw ProcessError();
156  } else {
158  }
159  }
160 }
161 
162 
163 unsigned int
165  // build loader
166  // load sumo-routes when wished
167  bool ok = openTypedRoutes("route-files", net);
168  // load the XML-trip definitions when wished
169  ok &= openTypedRoutes("trip-files", net);
170  // load the sumo-alternative file when wished
171  ok &= openTypedRoutes("alternative-files", net);
172  // load the amount definitions if wished
173  ok &= openTypedRoutes("flow-files", net);
174  // check
175  if (ok && myHandler.size() == 0) {
176  throw ProcessError("No route input specified.");
177  }
178  // skip routes prior to the begin time
179  if (ok && !myOptions.getBool("unsorted-input")) {
180  WRITE_MESSAGE("Skipping...");
181  for (RouteLoaderCont::iterator i = myHandler.begin(); ok && i != myHandler.end(); i++) {
182  ok &= (*i)->readRoutesAtLeastUntil(string2time(myOptions.getString("begin")));
183  }
184  WRITE_MESSAGE("Skipped until: " + time2string(getMinTimeStep()));
185  }
186  // check whether everything's ok
187  if (!ok) {
188  destroyHandlers();
189  throw ProcessError();
190  }
191  return (unsigned int) myHandler.size();
192 }
193 
194 
195 void
198  SUMOTime absNo = end - start;
199  // skip routes that begin before the simulation's begin
200  // loop till the end
201  bool endReached = false;
202  bool errorOccured = false;
203  SUMOTime time = myHandler.size() != 0 ? getMinTimeStep() : start;
204  SUMOTime firstStep = time;
205  SUMOTime lastStep = time;
206  for (; time < end && !errorOccured && !endReached; time += DELTA_T) {
207  writeStats(time, start, absNo);
208  makeSingleStep(time, net, router);
209  // check whether further data exist
210  endReached = !net.furtherStored();
211  lastStep = time;
212  for (RouteLoaderCont::iterator i = myHandler.begin(); endReached && i != myHandler.end(); i++) {
213  if (!(*i)->ended()) {
214  endReached = false;
215  }
216  }
217  errorOccured = MsgHandler::getErrorInstance()->wasInformed() && !myOptions.getBool("ignore-errors");
218  }
219  if (myLogSteps) {
220  WRITE_MESSAGE("Routes found between time steps " + time2string(firstStep) + " and " + time2string(lastStep) + ".");
221  }
222 }
223 
224 
225 bool
227  RouteLoaderCont::iterator i;
228  // go through all handlers
229  if (myHandler.size() != 0) {
230  for (i = myHandler.begin(); i != myHandler.end(); i++) {
231  // load routes until the time point is reached
232  if ((*i)->readRoutesAtLeastUntil(end)) {
233  // save the routes
234  net.saveAndRemoveRoutesUntil(myOptions, router, end);
235  } else {
236  return false;
237  }
238  }
240  } else {
241  return false;
242  }
243 }
244 
245 
246 SUMOTime
248  RouteLoaderCont::const_iterator i = myHandler.begin();
249  SUMOTime ret = (*i)->getLastReadTimeStep();
250  ++i;
251  for (; i != myHandler.end(); i++) {
252  SUMOTime akt = (*i)->getLastReadTimeStep();
253  if (akt < ret) {
254  ret = akt;
255  }
256  }
257  return ret;
258 }
259 
260 
261 void
264  long absNo = end - start;
265  bool ok = true;
266  for (RouteLoaderCont::iterator i = myHandler.begin(); ok && i != myHandler.end(); i++) {
267  ok &= (*i)->readRoutesAtLeastUntil(SUMOTime_MAX);
268  }
269  // save the routes
270  SUMOTime time = start;
271  for (; time < end;) {
272  writeStats(time, start, absNo);
273  time = net.saveAndRemoveRoutesUntil(myOptions, router, time);
274  if (time < 0) {
275  time = end;
276  }
277  }
278 }
279 
280 
281 #ifdef HAVE_INTERNAL // catchall for internal stuff
282 void
283 ROLoader::processAllRoutesWithBulkRouter(SUMOTime start, SUMOTime end,
285  bool ok = true;
286  for (RouteLoaderCont::iterator i = myHandler.begin(); ok && i != myHandler.end(); i++) {
287  ok &= (*i)->readRoutesAtLeastUntil(SUMOTime_MAX);
288  }
289  RouteAggregator::processAllRoutes(net, router);
290  net.saveAndRemoveRoutesUntil(myOptions, router, end);
291 }
292 #endif
293 
294 
295 bool
296 ROLoader::openTypedRoutes(const std::string& optionName,
297  RONet& net) {
298  // check whether the current loader is known
299  // (not all routers import all route formats)
300  if (!myOptions.exists(optionName)) {
301  return true;
302  }
303  // check whether the current loader is wished
304  // and the file(s) can be used
305  if (!myOptions.isUsableFileList(optionName)) {
306  return !myOptions.isSet(optionName);
307  }
308  bool ok = true;
309  std::vector<std::string> files = myOptions.getStringVector(optionName);
310  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
311  // build the instance when everything's all right
312  try {
313  ROTypedXMLRoutesLoader* instance = buildNamedHandler(optionName, *fileIt, net);
314  myHandler.push_back(instance);
315  } catch (ProcessError& e) {
316  std::string msg = "The loader for " + optionName + " from file '" + *fileIt + "' could not be initialised;";
317  std::string reason = e.what();
318  if (reason != "Process Error" && reason != "") {
319  msg = msg + "\n Reason: " + reason + ".";
320  } else {
321  msg = msg + "\n (unknown reason).";
322  }
323  WRITE_ERROR(msg);
324  ok = false;
325  }
326  }
327  return ok;
328 }
329 
330 
332 ROLoader::buildNamedHandler(const std::string& optionName,
333  const std::string& file,
334  RONet& net) {
335  if (optionName == "route-files" || optionName == "alternative-files") {
336  return new RORDLoader_SUMOBase(net,
338  myOptions.getBool("repair"), myOptions.getBool("with-taz"), file);
339  }
340  if (optionName == "trip-files") {
341  return new RORDLoader_TripDefs(net,
343  myEmptyDestinationsAllowed, myOptions.getBool("with-taz"), file);
344  }
345  if (optionName == "flow-files") {
346  return new RORDGenerator_ODAmounts(net,
348  myEmptyDestinationsAllowed, myOptions.getBool("randomize-flows"), file);
349  }
350  return 0;
351 }
352 
353 
354 bool
355 ROLoader::loadWeights(RONet& net, const std::string& optionName,
356  const std::string& measure, bool useLanes) {
357  // check whether the file exists
358  if (!myOptions.isUsableFileList(optionName)) {
359  return false;
360  }
361  // build and prepare the weights handler
362  std::vector<SAXWeightsHandler::ToRetrieveDefinition*> retrieverDefs;
363  // travel time, first (always used)
365  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition("traveltime", !useLanes, ttRetriever));
366  // the measure to use, then
368  if (measure != "traveltime") {
369  std::string umeasure = measure;
370  if (measure == "CO" || measure == "CO2" || measure == "HC" || measure == "PMx" || measure == "NOx" || measure == "fuel") {
371  umeasure = measure + "_perVeh";
372  }
373  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition(umeasure, !useLanes, eRetriever));
374  }
375  // set up handler
376  SAXWeightsHandler handler(retrieverDefs, "");
377  // go through files
378  std::vector<std::string> files = myOptions.getStringVector(optionName);
379  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
380  PROGRESS_BEGIN_MESSAGE("Loading precomputed net weights from '" + *fileIt + "'");
381  if (XMLSubSys::runParser(handler, *fileIt)) {
383  } else {
384  WRITE_MESSAGE("failed.");
385  return false;
386  }
387  }
388  // build edge-internal time lines
389  const std::map<std::string, ROEdge*>& edges = net.getEdgeMap();
390  for (std::map<std::string, ROEdge*>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
391  (*i).second->buildTimeLines(measure);
392  }
393  return true;
394 }
395 
396 
397 void
398 ROLoader::writeStats(SUMOTime time, SUMOTime start, int absNo) {
399  if (myLogSteps) {
400  const SUMOReal perc = (SUMOReal)(time - start) / (SUMOReal) absNo;
401  std::cout << "Reading time step: " + time2string(time) + " (" + time2string(time - start) + "/" + time2string(absNo) + " = " + toString(perc * 100) + "% done) \r";
402  }
403 }
404 
405 
406 void
408  for (RouteLoaderCont::const_iterator i = myHandler.begin(); i != myHandler.end(); ++i) {
409  delete *i;
410  }
411  myHandler.clear();
412 }
413 
414 
415 /****************************************************************************/
416