Eclipse SUMO - Simulation of Urban MObility
ROLoader.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
19 // Loader for networks and route imports
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <iostream>
29 #include <string>
30 #include <iomanip>
31 #include <xercesc/parsers/SAXParser.hpp>
32 #include <xercesc/util/PlatformUtils.hpp>
33 #include <xercesc/util/TransService.hpp>
34 #include <xercesc/sax2/SAX2XMLReader.hpp>
36 #include <utils/common/ToString.h>
41 #include <utils/xml/XMLSubSys.h>
45 #include "RONet.h"
46 #include "RONetHandler.h"
47 #include "ROLoader.h"
48 #include "ROLane.h"
49 #include "ROEdge.h"
50 #include "RORouteHandler.h"
51 
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
56 // ---------------------------------------------------------------------------
57 // ROLoader::EdgeFloatTimeLineRetriever_EdgeTravelTime - methods
58 // ---------------------------------------------------------------------------
59 void
61  double val, double beg, double end) const {
62  ROEdge* e = myNet.getEdge(id);
63  if (e != nullptr) {
64  e->addTravelTime(val, beg, end);
65  } else {
66  if (id[0] != ':') {
67  if (OptionsCont::getOptions().getBool("ignore-errors")) {
68  WRITE_WARNING("Trying to set a weight for the unknown edge '" + id + "'.");
69  } else {
70  WRITE_ERROR("Trying to set a weight for the unknown edge '" + id + "'.");
71  }
72  }
73  }
74 }
75 
76 
77 // ---------------------------------------------------------------------------
78 // ROLoader::EdgeFloatTimeLineRetriever_EdgeWeight - methods
79 // ---------------------------------------------------------------------------
80 void
82  double val, double beg, double end) const {
83  ROEdge* e = myNet.getEdge(id);
84  if (e != nullptr) {
85  e->addEffort(val, beg, end);
86  } else {
87  if (id[0] != ':') {
88  if (OptionsCont::getOptions().getBool("ignore-errors")) {
89  WRITE_WARNING("Trying to set a weight for the unknown edge '" + id + "'.");
90  } else {
91  WRITE_ERROR("Trying to set a weight for the unknown edge '" + id + "'.");
92  }
93  }
94  }
95 }
96 
97 
98 // ---------------------------------------------------------------------------
99 // ROLoader - methods
100 // ---------------------------------------------------------------------------
101 ROLoader::ROLoader(OptionsCont& oc, const bool emptyDestinationsAllowed, const bool logSteps) :
102  myOptions(oc),
103  myEmptyDestinationsAllowed(emptyDestinationsAllowed),
104  myLogSteps(logSteps),
105  myLoaders(oc.exists("unsorted-input") && oc.getBool("unsorted-input") ? 0 : DELTA_T) {
106 }
107 
108 
110 }
111 
112 
113 void
115  std::string file = myOptions.getString("net-file");
116  if (file == "") {
117  throw ProcessError("Missing definition of network to load!");
118  }
119  if (!FileHelpers::isReadable(file)) {
120  throw ProcessError("The network file '" + file + "' is not accessible.");
121  }
122  PROGRESS_BEGIN_MESSAGE("Loading net");
123  RONetHandler handler(toFill, eb, !myOptions.exists("no-internal-links") || myOptions.getBool("no-internal-links"),
124  myOptions.exists("weights.minor-penalty") ? myOptions.getFloat("weights.minor-penalty") : 0);
125  handler.setFileName(file);
126  if (!XMLSubSys::runParser(handler, file, true)) {
128  throw ProcessError();
129  } else {
131  }
132  if (!deprecatedVehicleClassesSeen.empty()) {
133  WRITE_WARNING("Deprecated vehicle classes '" + toString(deprecatedVehicleClassesSeen) + "' in input network.");
135  }
136  if (myOptions.isSet("additional-files", false)) { // dfrouter does not register this option
137  std::vector<std::string> files = myOptions.getStringVector("additional-files");
138  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
139  if (!FileHelpers::isReadable(*fileIt)) {
140  throw ProcessError("The additional file '" + *fileIt + "' is not accessible.");
141  }
142  PROGRESS_BEGIN_MESSAGE("Loading additional file '" + *fileIt + "' ");
143  handler.setFileName(*fileIt);
144  if (!XMLSubSys::runParser(handler, *fileIt)) {
146  throw ProcessError();
147  } else {
149  }
150  }
151  }
152 }
153 
154 
155 void
157  // build loader
158  // load relevant elements from additional file
159  bool ok = openTypedRoutes("additional-files", net, true);
160  // load sumo routes, trips, and flows
161  ok &= openTypedRoutes("route-files", net);
162  // check
163  if (ok) {
165  if (!net.furtherStored()) {
167  throw ProcessError();
168  } else {
169  const std::string error = "No route input specified or all routes were invalid.";
170  if (myOptions.getBool("ignore-errors")) {
171  WRITE_WARNING(error);
172  } else {
173  throw ProcessError(error);
174  }
175  }
176  }
177  // skip routes prior to the begin time
178  if (!myOptions.getBool("unsorted-input")) {
179  WRITE_MESSAGE("Skipped until: " + time2string(myLoaders.getFirstLoadTime()));
180  }
181  }
182 }
183 
184 
185 void
186 ROLoader::processRoutes(const SUMOTime start, const SUMOTime end, const SUMOTime increment,
187  RONet& net, const RORouterProvider& provider) {
188  const SUMOTime absNo = end - start;
189  const bool endGiven = !OptionsCont::getOptions().isDefault("end");
190  // skip routes that begin before the simulation's begin
191  // loop till the end
192  const SUMOTime firstStep = myLoaders.getFirstLoadTime();
193  SUMOTime lastStep = firstStep;
194  SUMOTime time = MIN2(firstStep, end);
195  while (time <= end) {
196  writeStats(time, start, absNo, endGiven);
197  myLoaders.loadNext(time);
199  break;
200  }
201  lastStep = net.saveAndRemoveRoutesUntil(myOptions, provider, time);
203  break;
204  }
205  if (time < end && time + increment > end) {
206  time = end;
207  } else {
208  time += increment;
209  }
210  }
211  if (myLogSteps) {
212  WRITE_MESSAGE("Routes found between time steps " + time2string(firstStep) + " and " + time2string(lastStep) + ".");
213  }
214 }
215 
216 
217 bool
218 ROLoader::openTypedRoutes(const std::string& optionName,
219  RONet& net, const bool readAll) {
220  // check whether the current loader is wished
221  // and the file(s) can be used
222  if (!myOptions.isUsableFileList(optionName)) {
223  return !myOptions.isSet(optionName);
224  }
225  for (const std::string& fileIt : myOptions.getStringVector(optionName)) {
226  try {
227  RORouteHandler* handler = new RORouteHandler(net, fileIt, myOptions.getBool("repair"), myEmptyDestinationsAllowed, myOptions.getBool("ignore-errors"), !readAll);
228  if (readAll) {
229  if (!XMLSubSys::runParser(*handler, fileIt)) {
230  WRITE_ERROR("Loading of " + fileIt + " failed.");
231  return false;
232  }
233  delete handler;
234  } else {
235  myLoaders.add(new SUMORouteLoader(handler));
236  }
237  } catch (ProcessError& e) {
238  WRITE_ERROR("The loader for " + optionName + " from file '" + fileIt + "' could not be initialised (" + e.what() + ").");
239  return false;
240  }
241  }
242  return true;
243 }
244 
245 
246 bool
247 ROLoader::loadWeights(RONet& net, const std::string& optionName,
248  const std::string& measure, const bool useLanes, const bool boundariesOverride) {
249  // check whether the file exists
250  if (!myOptions.isUsableFileList(optionName)) {
251  return false;
252  }
253  // build and prepare the weights handler
254  std::vector<SAXWeightsHandler::ToRetrieveDefinition*> retrieverDefs;
255  // travel time, first (always used)
257  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition("traveltime", !useLanes, ttRetriever));
258  // the measure to use, then
260  if (measure != "traveltime") {
261  std::string umeasure = measure;
262  if (measure == "CO" || measure == "CO2" || measure == "HC" || measure == "PMx" || measure == "NOx" || measure == "fuel" || measure == "electricity") {
263  umeasure = measure + "_perVeh";
264  }
265  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition(umeasure, !useLanes, eRetriever));
266  }
267  // set up handler
268  SAXWeightsHandler handler(retrieverDefs, "");
269  // go through files
270  std::vector<std::string> files = myOptions.getStringVector(optionName);
271  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
272  PROGRESS_BEGIN_MESSAGE("Loading precomputed net weights from '" + *fileIt + "'");
273  if (XMLSubSys::runParser(handler, *fileIt)) {
275  } else {
276  WRITE_MESSAGE("failed.");
277  return false;
278  }
279  }
280  // build edge-internal time lines
281  for (const auto& i : net.getEdgeMap()) {
282  i.second->buildTimeLines(measure, boundariesOverride);
283  }
284  return true;
285 }
286 
287 
288 void
289 ROLoader::writeStats(const SUMOTime time, const SUMOTime start, const SUMOTime absNo, bool endGiven) {
290  if (myLogSteps) {
291  if (endGiven) {
292  const double perc = (double)(time - start) / (double) absNo;
293  std::cout << "Reading up to time step: " + time2string(time) + " (" + time2string(time - start) + "/" + time2string(absNo) + " = " + toString(perc * 100) + "% done) \r";
294  } else {
295  std::cout << "Reading up to time step: " + time2string(time) + "\r";
296  }
297  }
298 }
299 
300 
301 /****************************************************************************/
302 
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:81
long long int SUMOTime
Definition: SUMOTime.h:35
SUMOTime getFirstLoadTime() const
returns the timestamp of the first loaded vehicle or flow
std::set< std::string > deprecatedVehicleClassesSeen
void addTravelTime(double value, double timeBegin, double timeEnd)
Adds a travel time value.
Definition: ROEdge.cpp:141
void loadNext(SUMOTime step)
loads the next routes up to and including the given time step
RONet & myNet
The network edges shall be obtained from.
Definition: ROLoader.h:166
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:49
void openRoutes(RONet &net)
Builds and opens all route loaders.
Definition: ROLoader.cpp:156
void addEdgeWeight(const std::string &id, double val, double beg, double end) const
Adds a travel time for a given edge and time period.
Definition: ROLoader.cpp:60
An XML-handler for network weights.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:65
void add(SUMORouteLoader *loader)
add another loader
SUMOTime DELTA_T
Definition: SUMOTime.cpp:35
const bool myLogSteps
Information whether the routing steps should be logged.
Definition: ROLoader.h:184
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
void writeStats(const SUMOTime time, const SUMOTime start, const SUMOTime absNo, bool endGiven)
Definition: ROLoader.cpp:289
bool wasInformed() const
Returns the information whether any messages were added.
Definition: MsgHandler.cpp:270
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything&#39;s ok.
Definition: XMLSubSys.cpp:113
Interface for building instances of router-edges.
void addEdgeWeight(const std::string &id, double val, double beg, double end) const
Adds an effort for a given edge and time period.
Definition: ROLoader.cpp:81
bool openTypedRoutes(const std::string &optionName, RONet &net, const bool readAll=false)
Opens route handler of the given type.
Definition: ROLoader.cpp:218
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:239
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
OptionsCont & myOptions
Options to use.
Definition: ROLoader.h:178
virtual ~ROLoader()
Destructor.
Definition: ROLoader.cpp:109
bool haveAllLoaded() const
returns whether loading is completed
#define PROGRESS_FAILED_MESSAGE()
Definition: MsgHandler.h:244
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
void setFileName(const std::string &name)
Sets the current file name.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
bool isUsableFileList(const std::string &name) const
Checks whether the named option is usable as a file list (with at least a single file) ...
Obtains edge weights from a weights handler and stores them within the edges.
Definition: ROLoader.h:114
SUMORouteLoaderControl myLoaders
List of route loaders.
Definition: ROLoader.h:187
void addEffort(double value, double timeBegin, double timeEnd)
Adds a weight value.
Definition: ROEdge.cpp:134
const bool myEmptyDestinationsAllowed
Information whether empty destinations are allowed.
Definition: ROLoader.h:181
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool exists(const std::string &name) const
Returns the information whether the named option is known.
bool furtherStored()
Returns the information whether further vehicles, persons or containers are stored.
Definition: RONet.cpp:643
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:42
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String) ...
T MIN2(T a, T b)
Definition: StdDefs.h:74
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:241
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
virtual void loadNet(RONet &toFill, ROAbstractEdgeBuilder &eb)
Loads the network.
Definition: ROLoader.cpp:114
A basic edge for routing applications.
Definition: ROEdge.h:73
Complete definition about what shall be retrieved and where to store it.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
The router&#39;s network representation.
Definition: RONet.h:64
Obtains edge travel times from a weights handler and stores them within the edges.
Definition: ROLoader.h:145
const NamedObjectCont< ROEdge * > & getEdgeMap() const
Definition: RONet.h:393
void processRoutes(const SUMOTime start, const SUMOTime end, const SUMOTime increment, RONet &net, const RORouterProvider &provider)
Loads routes from all previously build route loaders.
Definition: ROLoader.cpp:186
A storage for options typed value containers)
Definition: OptionsCont.h:90
The handler that parses a SUMO-network for its usage in a router.
Definition: RONetHandler.h:53
Parser and container for routes during their loading.
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:153
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:242
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:240
ROLoader(OptionsCont &oc, const bool emptyDestinationsAllowed, const bool logSteps)
Constructor.
Definition: ROLoader.cpp:101
bool loadWeights(RONet &net, const std::string &optionName, const std::string &measure, const bool useLanes, const bool boundariesOverride)
Loads the net weights.
Definition: ROLoader.cpp:247
SUMOTime saveAndRemoveRoutesUntil(OptionsCont &options, const RORouterProvider &provider, SUMOTime time)
Computes routes described by their definitions and saves them.
Definition: RONet.cpp:535