SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RORouteDef.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 // Base class for a vehicle's route definition
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
11 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <string>
33 #include <iterator>
34 #include <algorithm>
36 #include <utils/common/ToString.h>
37 #include <utils/common/Named.h>
43 #include "ROEdge.h"
44 #include "RORoute.h"
46 #include "ReferencedItem.h"
47 #include "RORouteDef.h"
48 #include "ROVehicle.h"
49 #include "ROCostCalculator.h"
50 
51 #ifdef CHECK_MEMORY_LEAKS
52 #include <foreign/nvwa/debug_new.h>
53 #endif // CHECK_MEMORY_LEAKS
54 
55 
56 // ===========================================================================
57 // method definitions
58 // ===========================================================================
59 RORouteDef::RORouteDef(const std::string& id, const unsigned int lastUsed,
60  const bool tryRepair) :
61  ReferencedItem(), Named(StringUtils::convertUmlaute(id)),
62  myPrecomputed(0), myLastUsed(lastUsed), myTryRepair(tryRepair)
63 {}
64 
65 
67  for (std::vector<RORoute*>::iterator i = myAlternatives.begin(); i != myAlternatives.end(); i++) {
68  delete *i;
69  }
70 }
71 
72 
73 void
75  myAlternatives.push_back(alt);
76 }
77 
78 
79 void
81  std::copy(alt->myAlternatives.begin(), alt->myAlternatives.end(),
82  back_inserter(myAlternatives));
83 }
84 
85 
86 RORoute*
88  SUMOTime begin, const ROVehicle& veh) const {
89  if (myPrecomputed == 0) {
90  preComputeCurrentRoute(router, begin, veh);
91  }
92  return myPrecomputed;
93 }
94 
95 
96 void
98  SUMOTime begin, const ROVehicle& veh) const {
99  myNewRoute = false;
100  assert(myAlternatives[0]->getEdgeVector().size() > 0);
101  MsgHandler* mh = (OptionsCont::getOptions().getBool("ignore-errors") ?
103  if (myAlternatives[0]->getFirst()->prohibits(&veh)) {
105  mh->inform("Vehicle '" + veh.getID() + "' is not allowed to depart on edge '" +
106  myAlternatives[0]->getFirst()->getID() + "'.");
107  return;
108  } else if (myAlternatives[0]->getLast()->prohibits(&veh)) {
109  // this check is not strictly necessary unless myTryRepair is set.
110  // However, the error message is more helpful than "no connection found"
111  mh->inform("Vehicle '" + veh.getID() + "' is not allowed to arrive on edge '" +
112  myAlternatives[0]->getLast()->getID() + "'.");
113  return;
114  }
115  if (myTryRepair) {
116  repairCurrentRoute(router, begin, veh);
117  return;
118  }
119  if (ROCostCalculator::getCalculator().skipRouteCalculation()) {
121  } else {
122  // build a new route to test whether it is better
123  std::vector<const ROEdge*> edges;
124  router.compute(myAlternatives[0]->getFirst(), myAlternatives[0]->getLast(), &veh, begin, edges);
125  // check whether the same route was already used
126  int cheapest = -1;
127  for (unsigned int i = 0; i < myAlternatives.size(); i++) {
128  if (edges == myAlternatives[i]->getEdgeVector()) {
129  cheapest = i;
130  break;
131  }
132  }
133  if (cheapest >= 0) {
134  myPrecomputed = myAlternatives[cheapest];
135  } else {
136  RGBColor* col = myAlternatives[0]->getColor() != 0 ? new RGBColor(*myAlternatives[0]->getColor()) : 0;
137  myPrecomputed = new RORoute(myID, 0, 1, edges, col, myAlternatives[0]->getStops());
138  myNewRoute = true;
139  }
140  }
141 }
142 
143 
144 void
146  SUMOTime begin, const ROVehicle& veh) const {
147  MsgHandler* mh = (OptionsCont::getOptions().getBool("ignore-errors") ?
149  std::vector<const ROEdge*> oldEdges = myAlternatives[0]->getEdgeVector();
150  std::vector<const ROEdge*> newEdges;
151  std::vector<const ROEdge*> mandatory;
152  if (oldEdges.size() == 1) {
154  router.compute(oldEdges.front(), oldEdges.front(), &veh, begin, newEdges);
155  } else {
156  // prepare mandatory edges
157  mandatory.push_back(oldEdges.front());
158  std::vector<const ROEdge*> stops = veh.getStopEdges();
159  for (std::vector<const ROEdge*>::const_iterator i = stops.begin(); i != stops.end(); ++i) {
160  if (*i != mandatory.back()) {
161  mandatory.push_back(*i);
162  }
163  }
164  if (mandatory.size() < 2 || oldEdges.back() != mandatory.back()) {
165  mandatory.push_back(oldEdges.back());
166  }
167  assert(mandatory.size() >= 2);
168  // removed prohibited
169  for (std::vector<const ROEdge*>::iterator i = oldEdges.begin(); i != oldEdges.end();) {
170  if ((*i)->prohibits(&veh)) {
171  if (std::find(mandatory.begin(), mandatory.end(), *i) != mandatory.end()) {
172  mh->inform("Mandatory edge '" + (*i)->getID() + "' does not allow vehicle '" + veh.getID() + "'.");
173  return;
174  }
175  i = oldEdges.erase(i);
176  } else {
177  ++i;
178  }
179  }
180  // reconnect remaining edges
181  newEdges.push_back(*(oldEdges.begin()));
182  std::vector<const ROEdge*>::iterator nextMandatory = mandatory.begin() + 1;
183  size_t lastMandatory = 0;
184  for (std::vector<const ROEdge*>::iterator i = oldEdges.begin() + 1;
185  i != oldEdges.end() && nextMandatory != mandatory.end(); ++i) {
186  if ((*(i - 1))->isConnectedTo(*i)) {
188  newEdges.push_back(*i);
189  } else {
190  std::vector<const ROEdge*> edges;
191  router.compute(newEdges.back(), *i, &veh, begin, edges);
192  if (edges.size() == 0) {
193  // backtrack: try to route from last mandatory edge to next mandatory edge
194  // XXX add option for backtracking in smaller increments
195  // (i.e. previous edge to edge after *i)
196  // we would then need to decide whether we have found a good
197  // tradeoff between faithfulness to the input data and detour-length
198  router.compute(newEdges[lastMandatory], *nextMandatory, &veh, begin, edges);
199  if (edges.size() == 0) {
200  mh->inform("Mandatory edge '" + (*i)->getID() + "' not reachable by vehicle '" + veh.getID() + "'.");
201  return;
202  } else {
203  while (*i != *nextMandatory) {
204  ++i;
205  }
206  newEdges.erase(newEdges.begin() + lastMandatory + 1, newEdges.end());
207  }
208  }
209  std::copy(edges.begin() + 1, edges.end(), back_inserter(newEdges));
210  }
211  if (*i == *nextMandatory) {
212  nextMandatory++;
213  lastMandatory = newEdges.size() - 1;
214  }
215  }
216  }
217  if (myAlternatives[0]->getEdgeVector() != newEdges) {
218  WRITE_MESSAGE("Repaired route of vehicle '" + veh.getID() + "'.");
219  myNewRoute = true;
220  RGBColor* col = myAlternatives[0]->getColor() != 0 ? new RGBColor(*myAlternatives[0]->getColor()) : 0;
221  myPrecomputed = new RORoute(myID, 0, myAlternatives[0]->getProbability(), newEdges, col, myAlternatives[0]->getStops());
222  } else {
224  }
225 }
226 
227 
228 void
230  const ROVehicle* const veh, RORoute* current, SUMOTime begin) {
231  if (myTryRepair) {
232  if (myNewRoute) {
233  delete myAlternatives[0];
234  myAlternatives.pop_back();
235  myAlternatives.push_back(current);
236  }
237  const SUMOReal costs = router.recomputeCosts(current->getEdgeVector(), veh, begin);
238  if (costs < 0) {
239  throw ProcessError("Route '" + getID() + "' (vehicle '" + veh->getID() + "') is not valid.");
240  }
241  current->setCosts(costs);
242  return;
243  }
244  // add the route when it's new
245  if (myNewRoute) {
246  myAlternatives.push_back(current);
247  }
248  // recompute the costs and (when a new route was added) scale the probabilities
249  const SUMOReal scale = SUMOReal(myAlternatives.size() - 1) / SUMOReal(myAlternatives.size());
250  for (std::vector<RORoute*>::iterator i = myAlternatives.begin(); i != myAlternatives.end(); i++) {
251  RORoute* alt = *i;
252  // recompute the costs for all routes
253  const SUMOReal newCosts = router.recomputeCosts(alt->getEdgeVector(), veh, begin);
254  if (newCosts < 0.) {
255  throw ProcessError("Route '" + current->getID() + "' (vehicle '" + veh->getID() + "') is not valid.");
256  }
257  assert(myAlternatives.size() != 0);
258  if (myNewRoute) {
259  if (*i == current) {
260  // set initial probability and costs
261  alt->setProbability((SUMOReal)(1.0 / (SUMOReal) myAlternatives.size()));
262  alt->setCosts(newCosts);
263  } else {
264  // rescale probs for all others
265  alt->setProbability(alt->getProbability() * scale);
266  }
267  }
269  }
270  assert(myAlternatives.size() != 0);
273  // remove with probability of 0 (not mentioned in Gawron)
274  for (std::vector<RORoute*>::iterator i = myAlternatives.begin(); i != myAlternatives.end();) {
275  if ((*i)->getProbability() == 0) {
276  delete *i;
277  i = myAlternatives.erase(i);
278  } else {
279  i++;
280  }
281  }
282  }
284  // only keep the routes with highest probability
285  sort(myAlternatives.begin(), myAlternatives.end(), ComparatorProbability());
286  for (std::vector<RORoute*>::iterator i = myAlternatives.begin() + ROCostCalculator::getCalculator().getMaxRouteNumber(); i != myAlternatives.end(); i++) {
287  delete *i;
288  }
290  // rescale probabilities
291  SUMOReal newSum = 0;
292  for (std::vector<RORoute*>::iterator i = myAlternatives.begin(); i != myAlternatives.end(); i++) {
293  newSum += (*i)->getProbability();
294  }
295  assert(newSum > 0);
296  // @note newSum may be larger than 1 for numerical reasons
297  for (std::vector<RORoute*>::iterator i = myAlternatives.begin(); i != myAlternatives.end(); i++) {
298  (*i)->setProbability((*i)->getProbability() / newSum);
299  }
300  }
301 
302  // find the route to use
303  SUMOReal chosen = RandHelper::rand();
304  int pos = 0;
305  for (std::vector<RORoute*>::iterator i = myAlternatives.begin(); i != myAlternatives.end() - 1; i++, pos++) {
306  chosen -= (*i)->getProbability();
307  if (chosen <= 0) {
308  myLastUsed = pos;
309  return;
310  }
311  }
312  myLastUsed = pos;
313 }
314 
315 
316 const ROEdge*
318  return myAlternatives[0]->getLast();
319 }
320 
321 
324  bool asAlternatives, bool withExitTimes) const {
325  if (asAlternatives) {
327  for (size_t i = 0; i != myAlternatives.size(); i++) {
328  myAlternatives[i]->writeXMLDefinition(dev, veh, true, withExitTimes);
329  }
330  dev.closeTag();
331  return dev;
332  } else {
333  return myAlternatives[myLastUsed]->writeXMLDefinition(dev, veh, false, withExitTimes);
334  }
335 }
336 
337 
338 RORouteDef*
339 RORouteDef::copyOrigDest(const std::string& id) const {
340  RORouteDef* result = new RORouteDef(id, 0, true);
341  RORoute* route = myAlternatives[0];
342  RGBColor* col = route->getColor() != 0 ? new RGBColor(*route->getColor()) : 0;
343  std::vector<const ROEdge*> edges;
344  edges.push_back(route->getFirst());
345  edges.push_back(route->getLast());
346  result->addLoadedAlternative(new RORoute(id, 0, 1, edges, col, route->getStops()));
347  return result;
348 }
349 
350 
351 RORouteDef*
352 RORouteDef::copy(const std::string& id) const {
353  RORouteDef* result = new RORouteDef(id, 0, myTryRepair);
354  for (std::vector<RORoute*>::const_iterator i = myAlternatives.begin(); i != myAlternatives.end(); i++) {
355  RORoute* route = *i;
356  RGBColor* col = route->getColor() != 0 ? new RGBColor(*route->getColor()) : 0;
357  result->addLoadedAlternative(new RORoute(id, 0, 1, route->getEdgeVector(), col, route->getStops()));
358  }
359  return result;
360 }
361 
362 
363 SUMOReal
365  SUMOReal sum = 0.;
366  for (std::vector<RORoute*>::const_iterator i = myAlternatives.begin(); i != myAlternatives.end(); i++) {
367  sum += (*i)->getProbability();
368  }
369  return sum;
370 }
371 
372 
373 /****************************************************************************/
SUMOTime getDepartureTime() const
Returns the time the vehicle starts at.
Definition: ROVehicle.h:110
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:71
const std::string & getID() const
Returns the id of the vehicle.
Definition: ROVehicle.h:101
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:254
bool myNewRoute
Information whether a new route was generated.
Definition: RORouteDef.h:157
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
RORouteDef * copyOrigDest(const std::string &id) const
Returns a origin-destination copy of the route definition.
Definition: RORouteDef.cpp:339
const bool myTryRepair
Definition: RORouteDef.h:159
Some static methods for string processing.
Definition: StringUtils.h:45
void addAlternative(SUMOAbstractRouter< ROEdge, ROVehicle > &router, const ROVehicle *const, RORoute *current, SUMOTime begin)
Adds an alternative to the list of routes.
Definition: RORouteDef.cpp:229
RORoute * buildCurrentRoute(SUMOAbstractRouter< ROEdge, ROVehicle > &router, SUMOTime begin, const ROVehicle &veh) const
Triggers building of the complete route (via preComputeCurrentRoute) or returns precomputed route...
Definition: RORouteDef.cpp:87
void addAlternativeDef(const RORouteDef *alternative)
Adds an alternative loaded from the file.
Definition: RORouteDef.cpp:80
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
static SUMOReal rand()
Returns a random real number in [0, 1)
Definition: RandHelper.h:61
void setProbability(SUMOReal prob)
Sets the probability of the route.
Definition: RORoute.cpp:79
void preComputeCurrentRoute(SUMOAbstractRouter< ROEdge, ROVehicle > &router, SUMOTime begin, const ROVehicle &veh) const
Builds the complete route (or chooses her from the list of alternatives, when existing) ...
Definition: RORouteDef.cpp:97
void repairCurrentRoute(SUMOAbstractRouter< ROEdge, ROVehicle > &router, SUMOTime begin, const ROVehicle &veh) const
Builds the complete route (or chooses her from the list of alternatives, when existing) ...
Definition: RORouteDef.cpp:145
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
RORoute * myPrecomputed
precomputed route for out-of-order computation
Definition: RORouteDef.h:148
const std::string & getID() const
Returns the id.
Definition: Named.h:60
A vehicle as used by router.
Definition: ROVehicle.h:58
Helper base for things that are referenced and have to be saved only once.
const ROEdge * getLast() const
Returns the last edge in the route.
Definition: RORoute.h:100
OutputDevice & writeXMLDefinition(OutputDevice &dev, const ROVehicle *const veh, bool asAlternatives, bool withExitTimes) const
Saves the built route / route alternatives.
Definition: RORouteDef.cpp:323
const std::vector< const ROEdge * > & getStopEdges() const
Definition: ROVehicle.h:114
virtual void calculateProbabilities(std::vector< RORoute * > alternatives, const ROVehicle *const veh, const SUMOTime time)=0
calculate the probabilities in the logit model
const RGBColor * getColor() const
Returns this route&#39;s color.
Definition: RORoute.h:160
SUMOReal getProbability() const
Returns the probability the driver will take this route with.
Definition: RORoute.h:120
void setCosts(SUMOReal costs)
Sets the costs of the route.
Definition: RORoute.cpp:73
const ROEdge * getDestination() const
Definition: RORouteDef.cpp:317
const ROEdge * getFirst() const
Returns the first edge in the route.
Definition: RORoute.h:91
A basic edge for routing applications.
Definition: ROEdge.h:67
Base class for objects which have an id.
Definition: Named.h:45
virtual ~RORouteDef()
Destructor.
Definition: RORouteDef.cpp:66
unsigned int myLastUsed
Index of the route used within the last step.
Definition: RORouteDef.h:151
RORouteDef(const std::string &id, const unsigned int lastUsed, const bool tryRepair)
Constructor.
Definition: RORouteDef.cpp:59
bool keepRoutes() const
std::string myID
The name of the object.
Definition: Named.h:121
const std::vector< const ROEdge * > & getEdgeVector() const
Returns the list of edges this route consists of.
Definition: RORoute.h:152
unsigned int getMaxRouteNumber() const
RORouteDef * copy(const std::string &id) const
Returns a deep copy of the route definition.
Definition: RORouteDef.cpp:352
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:89
Base class for a vehicle&#39;s route definition.
Definition: RORouteDef.h:63
void addLoadedAlternative(RORoute *alternative)
Adds a single alternative loaded from the file An alternative may also be generated during DUA...
Definition: RORouteDef.cpp:74
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
virtual SUMOReal recomputeCosts(const std::vector< const E * > &edges, const V *const v, SUMOTime msTime) const =0
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:215
virtual void compute(const E *from, const E *to, const V *const vehicle, SUMOTime msTime, std::vector< const E * > &into)=0
Builds the route between the given edges using the minimum effort at the given time The definition of...
static ROCostCalculator & getCalculator()
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:197
std::vector< RORoute * > myAlternatives
The alternatives.
Definition: RORouteDef.h:154
SUMOReal getOverallProb() const
Returns the sum of the probablities of the contained routes.
Definition: RORouteDef.cpp:364
const std::vector< SUMOVehicleParameter::Stop > & getStops() const
Returns the list of stops this route contains.
Definition: RORoute.h:181
virtual void setCosts(RORoute *route, const SUMOReal costs, const bool isActive=false) const =0
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
A complete router&#39;s route.
Definition: RORoute.h:59