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-sim.org/
14 // Copyright (C) 2001-2013 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>
52 #include "RONet.h"
53 #include "RONetHandler.h"
54 #include "ROLoader.h"
55 #include "ROEdge.h"
56 #include "RORouteHandler.h"
57 
58 #ifdef HAVE_INTERNAL // catchall for internal stuff
59 #include <internal/RouteAggregator.h>
60 #endif // have HAVE_INTERNAL
61 
62 #ifdef CHECK_MEMORY_LEAKS
63 #include <foreign/nvwa/debug_new.h>
64 #endif // CHECK_MEMORY_LEAKS
65 
66 
67 // ===========================================================================
68 // method definitions
69 // ===========================================================================
70 // ---------------------------------------------------------------------------
71 // ROLoader::EdgeFloatTimeLineRetriever_EdgeTravelTime - methods
72 // ---------------------------------------------------------------------------
73 void
75  SUMOReal val, SUMOReal beg, SUMOReal end) const {
76  ROEdge* e = myNet.getEdge(id);
77  if (e != 0) {
78  e->addTravelTime(val, beg, end);
79  } else {
80  if (id[0] != ':') {
81  if (OptionsCont::getOptions().getBool("ignore-errors")) {
82  WRITE_WARNING("Trying to set a weight for the unknown edge '" + id + "'.");
83  } else {
84  WRITE_ERROR("Trying to set a weight for the unknown edge '" + id + "'.");
85  }
86  }
87  }
88 }
89 
90 
91 // ---------------------------------------------------------------------------
92 // ROLoader::EdgeFloatTimeLineRetriever_EdgeWeight - methods
93 // ---------------------------------------------------------------------------
94 void
96  SUMOReal val, SUMOReal beg, SUMOReal end) const {
97  ROEdge* e = myNet.getEdge(id);
98  if (e != 0) {
99  e->addEffort(val, beg, end);
100  } else {
101  if (id[0] != ':') {
102  if (OptionsCont::getOptions().getBool("ignore-errors")) {
103  WRITE_WARNING("Trying to set a weight for the unknown edge '" + id + "'.");
104  } else {
105  WRITE_ERROR("Trying to set a weight for the unknown edge '" + id + "'.");
106  }
107  }
108  }
109 }
110 
111 
112 // ---------------------------------------------------------------------------
113 // ROLoader - methods
114 // ---------------------------------------------------------------------------
115 ROLoader::ROLoader(OptionsCont& oc, const bool emptyDestinationsAllowed, const bool logSteps) :
116  myOptions(oc),
117  myEmptyDestinationsAllowed(emptyDestinationsAllowed),
118  myLogSteps(logSteps),
119  myLoaders(oc.exists("unsorted-input") && oc.getBool("unsorted-input") ? 0 : DELTA_T)
120 {}
121 
122 
124 }
125 
126 
127 void
129  std::string file = myOptions.getString("net-file");
130  if (file == "") {
131  throw ProcessError("Missing definition of network to load!");
132  }
133  if (!FileHelpers::exists(file)) {
134  throw ProcessError("The network file '" + file + "' could not be found.");
135  }
136  PROGRESS_BEGIN_MESSAGE("Loading net");
137  RONetHandler handler(toFill, eb);
138  handler.setFileName(file);
139  if (!XMLSubSys::runParser(handler, file)) {
141  throw ProcessError();
142  } else {
144  }
145  if (myOptions.isSet("taz-files", false)) { // dfrouter does not register this option
146  file = myOptions.getString("taz-files");
147  if (!FileHelpers::exists(file)) {
148  throw ProcessError("The districts file '" + file + "' could not be found.");
149  }
150  PROGRESS_BEGIN_MESSAGE("Loading districts");
151  handler.setFileName(file);
152  if (!XMLSubSys::runParser(handler, file)) {
154  throw ProcessError();
155  } else {
157  }
158  }
159 }
160 
161 
162 void
164  // build loader
165  // load sumo-routes when wished
166  bool ok = openTypedRoutes("route-files", net);
167  // load the XML-trip definitions when wished
168  ok &= openTypedRoutes("trip-files", net);
169  // load the sumo-alternative file when wished
170  ok &= openTypedRoutes("alternative-files", net);
171  // load the amount definitions if wished
172  ok &= openTypedRoutes("flow-files", net);
173  // check
174  if (ok) {
177  throw ProcessError("No route input specified.");
178  }
179  // skip routes prior to the begin time
180  if (!myOptions.getBool("unsorted-input")) {
181  WRITE_MESSAGE("Skipped until: " + time2string(myLoaders.getFirstLoadTime()));
182  }
183  }
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  const SUMOTime firstStep = myLoaders.getFirstLoadTime();
196  SUMOTime lastStep = firstStep;
197  for (SUMOTime time = firstStep; time < end && !errorOccured && !endReached; time += DELTA_T) {
198  writeStats(time, start, absNo);
199  myLoaders.loadNext(time);
200  net.saveAndRemoveRoutesUntil(myOptions, router, time);
201  endReached = !net.furtherStored();
202  lastStep = time;
203  errorOccured = MsgHandler::getErrorInstance()->wasInformed() && !myOptions.getBool("ignore-errors");
204  }
205  if (myLogSteps) {
206  WRITE_MESSAGE("Routes found between time steps " + time2string(firstStep) + " and " + time2string(lastStep) + ".");
207  }
208 }
209 
210 
211 #ifdef HAVE_INTERNAL // catchall for internal stuff
212 void
213 ROLoader::processAllRoutesWithBulkRouter(SUMOTime start, SUMOTime end,
216  RouteAggregator::processAllRoutes(net, router);
217  net.saveAndRemoveRoutesUntil(myOptions, router, end);
218 }
219 #endif
220 
221 
222 bool
223 ROLoader::openTypedRoutes(const std::string& optionName,
224  RONet& net) {
225  // check whether the current loader is known
226  // (not all routers import all route formats)
227  if (!myOptions.exists(optionName)) {
228  return true;
229  }
230  // check whether the current loader is wished
231  // and the file(s) can be used
232  if (!myOptions.isUsableFileList(optionName)) {
233  return !myOptions.isSet(optionName);
234  }
235  bool ok = true;
236  std::vector<std::string> files = myOptions.getStringVector(optionName);
237  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
238  // build the instance when everything's all right
239  try {
240  myLoaders.add(new SUMORouteLoader(new RORouteHandler(net, *fileIt, myOptions.getBool("repair"), myEmptyDestinationsAllowed, myOptions.getBool("ignore-errors"))));
241  } catch (ProcessError& e) {
242  std::string msg = "The loader for " + optionName + " from file '" + *fileIt + "' could not be initialised;";
243  std::string reason = e.what();
244  if (reason != "Process Error" && reason != "") {
245  msg = msg + "\n Reason: " + reason + ".";
246  } else {
247  msg = msg + "\n (unknown reason).";
248  }
249  WRITE_ERROR(msg);
250  ok = false;
251  }
252  }
253  return ok;
254 }
255 
256 
257 bool
258 ROLoader::loadWeights(RONet& net, const std::string& optionName,
259  const std::string& measure, bool useLanes) {
260  // check whether the file exists
261  if (!myOptions.isUsableFileList(optionName)) {
262  return false;
263  }
264  // build and prepare the weights handler
265  std::vector<SAXWeightsHandler::ToRetrieveDefinition*> retrieverDefs;
266  // travel time, first (always used)
268  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition("traveltime", !useLanes, ttRetriever));
269  // the measure to use, then
271  if (measure != "traveltime") {
272  std::string umeasure = measure;
273  if (measure == "CO" || measure == "CO2" || measure == "HC" || measure == "PMx" || measure == "NOx" || measure == "fuel") {
274  umeasure = measure + "_perVeh";
275  }
276  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition(umeasure, !useLanes, eRetriever));
277  }
278  // set up handler
279  SAXWeightsHandler handler(retrieverDefs, "");
280  // go through files
281  std::vector<std::string> files = myOptions.getStringVector(optionName);
282  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
283  PROGRESS_BEGIN_MESSAGE("Loading precomputed net weights from '" + *fileIt + "'");
284  if (XMLSubSys::runParser(handler, *fileIt)) {
286  } else {
287  WRITE_MESSAGE("failed.");
288  return false;
289  }
290  }
291  // build edge-internal time lines
292  const std::map<std::string, ROEdge*>& edges = net.getEdgeMap();
293  for (std::map<std::string, ROEdge*>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
294  (*i).second->buildTimeLines(measure);
295  }
296  return true;
297 }
298 
299 
300 void
301 ROLoader::writeStats(SUMOTime time, SUMOTime start, int absNo) {
302  if (myLogSteps) {
303  const SUMOReal perc = (SUMOReal)(time - start) / (SUMOReal) absNo;
304  std::cout << "Reading time step: " + time2string(time) + " (" + time2string(time - start) + "/" + time2string(absNo) + " = " + toString(perc * 100) + "% done) \r";
305  }
306 }
307 
308 
309 /****************************************************************************/
310