SUMO - Simulation of Urban MObility
RORouteDef.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Base class for a vehicle's route definition
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2002-2016 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <string>
34 #include <iterator>
35 #include <algorithm>
37 #include <utils/common/ToString.h>
38 #include <utils/common/Named.h>
44 #include "ROEdge.h"
45 #include "RORoute.h"
48 #include "RORouteDef.h"
49 #include "ROVehicle.h"
50 
51 #ifdef CHECK_MEMORY_LEAKS
52 #include <foreign/nvwa/debug_new.h>
53 #endif // CHECK_MEMORY_LEAKS
54 
55 // ===========================================================================
56 // static members
57 // ===========================================================================
58 bool RORouteDef::myUsingJTRR(false);
59 
60 // ===========================================================================
61 // method definitions
62 // ===========================================================================
63 RORouteDef::RORouteDef(const std::string& id, const unsigned int lastUsed,
64  const bool tryRepair, const bool mayBeDisconnected) :
65  Named(StringUtils::convertUmlaute(id)),
66  myPrecomputed(0), myLastUsed(lastUsed), myTryRepair(tryRepair), myMayBeDisconnected(mayBeDisconnected) {
67 }
68 
69 
71  for (std::vector<RORoute*>::iterator i = myAlternatives.begin(); i != myAlternatives.end(); i++) {
72  if (myRouteRefs.count(*i) == 0) {
73  delete *i;
74  }
75  }
76 }
77 
78 
79 void
81  myAlternatives.push_back(alt);
82 }
83 
84 
85 void
87  std::copy(alt->myAlternatives.begin(), alt->myAlternatives.end(),
88  back_inserter(myAlternatives));
89  std::copy(alt->myAlternatives.begin(), alt->myAlternatives.end(),
90  std::inserter(myRouteRefs, myRouteRefs.end()));
91 }
92 
93 
94 RORoute*
96  SUMOTime begin, const ROVehicle& veh) const {
97  if (myPrecomputed == 0) {
98  preComputeCurrentRoute(router, begin, veh);
99  }
100  return myPrecomputed;
101 }
102 
103 
104 void
106  SUMOTime begin, const ROVehicle& veh) const {
107  myNewRoute = false;
108  const OptionsCont& oc = OptionsCont::getOptions();
109  assert(myAlternatives[0]->getEdgeVector().size() > 0);
110  MsgHandler* mh = (OptionsCont::getOptions().getBool("ignore-errors") ?
112  if (myAlternatives[0]->getFirst()->prohibits(&veh) && (!oc.getBool("repair.from")
113  // do not try to reassign starting edge for trip input
114  || myMayBeDisconnected || myAlternatives[0]->getEdgeVector().size() < 2)) {
115  mh->inform("Vehicle '" + veh.getID() + "' is not allowed to depart on edge '" +
116  myAlternatives[0]->getFirst()->getID() + "'.");
117  return;
118  } else if (myAlternatives[0]->getLast()->prohibits(&veh) && (!oc.getBool("repair.to")
119  // do not try to reassign destination edge for trip input
120  || myMayBeDisconnected || myAlternatives[0]->getEdgeVector().size() < 2)) {
121  // this check is not strictly necessary unless myTryRepair is set.
122  // However, the error message is more helpful than "no connection found"
123  mh->inform("Vehicle '" + veh.getID() + "' is not allowed to arrive on edge '" +
124  myAlternatives[0]->getLast()->getID() + "'.");
125  return;
126  }
127  if (myTryRepair) {
128  ConstROEdgeVector newEdges;
129  if (repairCurrentRoute(router, begin, veh, myAlternatives[0]->getEdgeVector(), newEdges)) {
130  if (myAlternatives[0]->getEdgeVector() != newEdges) {
131  if (!myMayBeDisconnected) {
132  WRITE_WARNING("Repaired route of vehicle '" + veh.getID() + "'.");
133  }
134  myNewRoute = true;
135  RGBColor* col = myAlternatives[0]->getColor() != 0 ? new RGBColor(*myAlternatives[0]->getColor()) : 0;
136  myPrecomputed = new RORoute(myID, 0, myAlternatives[0]->getProbability(), newEdges, col, myAlternatives[0]->getStops());
137  } else {
139  }
140  }
141  return;
142  }
144  || OptionsCont::getOptions().getBool("remove-loops")) {
146  } else {
147  // build a new route to test whether it is better
148  ConstROEdgeVector oldEdges;
149  oldEdges.push_back(myAlternatives[0]->getFirst());
150  oldEdges.push_back(myAlternatives[0]->getLast());
151  ConstROEdgeVector edges;
152  repairCurrentRoute(router, begin, veh, oldEdges, edges);
153  // check whether the same route was already used
154  int cheapest = -1;
155  for (unsigned int i = 0; i < myAlternatives.size(); i++) {
156  if (edges == myAlternatives[i]->getEdgeVector()) {
157  cheapest = i;
158  break;
159  }
160  }
161  if (cheapest >= 0) {
162  myPrecomputed = myAlternatives[cheapest];
163  } else {
164  RGBColor* col = myAlternatives[0]->getColor() != 0 ? new RGBColor(*myAlternatives[0]->getColor()) : 0;
165  myPrecomputed = new RORoute(myID, 0, 1, edges, col, myAlternatives[0]->getStops());
166  myNewRoute = true;
167  }
168  }
169 }
170 
171 
172 bool
174  SUMOTime begin, const ROVehicle& veh,
175  ConstROEdgeVector oldEdges, ConstROEdgeVector& newEdges) const {
176  MsgHandler* mh = (OptionsCont::getOptions().getBool("ignore-errors") ?
178  ConstROEdgeVector mandatory;
179  const size_t initialSize = oldEdges.size();
180  if (initialSize == 1) {
181  if (myUsingJTRR) {
183  router.compute(oldEdges.front(), 0, &veh, begin, newEdges);
184  } else {
185  newEdges = oldEdges;
186  }
187  } else {
188  // prepare mandatory edges
189  if (oldEdges.front()->prohibits(&veh)) {
190  // option repair.from is in effect
191  const std::string& frontID = oldEdges.front()->getID();
192  for (ConstROEdgeVector::iterator i = oldEdges.begin(); i != oldEdges.end();) {
193  if ((*i)->prohibits(&veh)) {
194  i = oldEdges.erase(i);
195  } else {
196  WRITE_MESSAGE("Changing invalid starting edge '" + frontID
197  + "' to '" + (*i)->getID() + "' for vehicle '" + veh.getID() + "'.");
198  break;
199  }
200  }
201  }
202  if (oldEdges.size() == 0) {
203  mh->inform("Could not find new starting edge for vehicle '" + veh.getID() + "'.");
204  return false;
205  }
206  mandatory.push_back(oldEdges.front());
207  ConstROEdgeVector stops = veh.getStopEdges();
208  for (ConstROEdgeVector::const_iterator i = stops.begin(); i != stops.end(); ++i) {
209  if (*i != mandatory.back()) {
210  mandatory.push_back(*i);
211  }
212  }
213  if (oldEdges.back()->prohibits(&veh)) {
214  // option repair.to is in effect
215  const std::string& backID = oldEdges.back()->getID();
216  for (ConstROEdgeVector::reverse_iterator i = oldEdges.rbegin(); i != oldEdges.rend();) {
217  if ((*i)->prohibits(&veh)) {
218  ++i;
219  oldEdges.erase(i.base());
220  } else {
221  WRITE_MESSAGE("Changing invalid destination edge '" + backID
222  + "' to edge '" + (*i)->getID() + "' for vehicle '" + veh.getID() + "'.");
223  break;
224  }
225  }
226  }
227  if (mandatory.size() < 2 || oldEdges.back() != mandatory.back()) {
228  mandatory.push_back(oldEdges.back());
229  }
230  assert(mandatory.size() >= 2);
231  // removed prohibited
232  for (ConstROEdgeVector::iterator i = oldEdges.begin(); i != oldEdges.end();) {
233  if ((*i)->prohibits(&veh)) {
234  // no need to check the mandatories here, this was done before
235  i = oldEdges.erase(i);
236  } else {
237  ++i;
238  }
239  }
240  // reconnect remaining edges
241  if (mandatory.size() > oldEdges.size() && initialSize > 2) {
242  WRITE_MESSAGE("There are stop edges which were not part of the original route for vehicle '" + veh.getID() + "'.");
243  }
244  const ConstROEdgeVector& targets = mandatory.size() > oldEdges.size() ? mandatory : oldEdges;
245  newEdges.push_back(*(targets.begin()));
246  ConstROEdgeVector::iterator nextMandatory = mandatory.begin() + 1;
247  size_t lastMandatory = 0;
248  for (ConstROEdgeVector::const_iterator i = targets.begin() + 1;
249  i != targets.end() && nextMandatory != mandatory.end(); ++i) {
250  if ((*(i - 1))->isConnectedTo(*i, &veh)) {
251  newEdges.push_back(*i);
252  } else {
253  if (initialSize > 2) {
254  // only inform if the input is (probably) not a trip
255  WRITE_MESSAGE("Edge '" + (*(i - 1))->getID() + "' not connected to edge '" + (*i)->getID() + "' for vehicle '" + veh.getID() + "'.");
256  }
257  const ROEdge* const last = newEdges.back();
258  newEdges.pop_back();
259  if (!router.compute(last, *i, &veh, begin, newEdges)) {
260  // backtrack: try to route from last mandatory edge to next mandatory edge
261  // XXX add option for backtracking in smaller increments
262  // (i.e. previous edge to edge after *i)
263  // we would then need to decide whether we have found a good
264  // tradeoff between faithfulness to the input data and detour-length
265  ConstROEdgeVector edges;
266  if (last == newEdges[lastMandatory] || !router.compute(newEdges[lastMandatory], *nextMandatory, &veh, begin, edges)) {
267  mh->inform("Mandatory edge '" + (*i)->getID() + "' not reachable by vehicle '" + veh.getID() + "'.");
268  return false;
269  }
270  while (*i != *nextMandatory) {
271  ++i;
272  }
273  newEdges.erase(newEdges.begin() + lastMandatory + 1, newEdges.end());
274  std::copy(edges.begin() + 1, edges.end(), back_inserter(newEdges));
275  }
276  }
277  if (*i == *nextMandatory) {
278  nextMandatory++;
279  lastMandatory = newEdges.size() - 1;
280  }
281  }
282  }
283  return true;
284 }
285 
286 
287 void
289  const ROVehicle* const veh, RORoute* current, SUMOTime begin) {
290  if (myTryRepair) {
291  if (myNewRoute) {
292  delete myAlternatives[0];
293  myAlternatives[0] = current;
294  }
295  const SUMOReal costs = router.recomputeCosts(current->getEdgeVector(), veh, begin);
296  if (costs < 0) {
297  throw ProcessError("Route '" + getID() + "' (vehicle '" + veh->getID() + "') is not valid.");
298  }
299  current->setCosts(costs);
300  return;
301  }
302  // add the route when it's new
303  if (myNewRoute) {
304  myAlternatives.push_back(current);
305  }
306  // recompute the costs and (when a new route was added) scale the probabilities
307  const SUMOReal scale = SUMOReal(myAlternatives.size() - 1) / SUMOReal(myAlternatives.size());
308  for (std::vector<RORoute*>::iterator i = myAlternatives.begin(); i != myAlternatives.end(); i++) {
309  RORoute* alt = *i;
310  // recompute the costs for all routes
311  const SUMOReal newCosts = router.recomputeCosts(alt->getEdgeVector(), veh, begin);
312  if (newCosts < 0.) {
313  throw ProcessError("Route '" + current->getID() + "' (vehicle '" + veh->getID() + "') is not valid.");
314  }
315  assert(myAlternatives.size() != 0);
316  if (myNewRoute) {
317  if (*i == current) {
318  // set initial probability and costs
319  alt->setProbability((SUMOReal)(1.0 / (SUMOReal) myAlternatives.size()));
320  alt->setCosts(newCosts);
321  } else {
322  // rescale probs for all others
323  alt->setProbability(alt->getProbability() * scale);
324  }
325  }
327  }
328  assert(myAlternatives.size() != 0);
331  // remove with probability of 0 (not mentioned in Gawron)
332  for (std::vector<RORoute*>::iterator i = myAlternatives.begin(); i != myAlternatives.end();) {
333  if ((*i)->getProbability() == 0) {
334  delete *i;
335  i = myAlternatives.erase(i);
336  } else {
337  i++;
338  }
339  }
340  }
342  // only keep the routes with highest probability
343  sort(myAlternatives.begin(), myAlternatives.end(), ComparatorProbability());
344  for (std::vector<RORoute*>::iterator i = myAlternatives.begin() + RouteCostCalculator<RORoute, ROEdge, ROVehicle>::getCalculator().getMaxRouteNumber(); i != myAlternatives.end(); i++) {
345  delete *i;
346  }
348  // rescale probabilities
349  SUMOReal newSum = 0;
350  for (std::vector<RORoute*>::iterator i = myAlternatives.begin(); i != myAlternatives.end(); i++) {
351  newSum += (*i)->getProbability();
352  }
353  assert(newSum > 0);
354  // @note newSum may be larger than 1 for numerical reasons
355  for (std::vector<RORoute*>::iterator i = myAlternatives.begin(); i != myAlternatives.end(); i++) {
356  (*i)->setProbability((*i)->getProbability() / newSum);
357  }
358  }
359 
360  // find the route to use
361  SUMOReal chosen = RandHelper::rand();
362  int pos = 0;
363  for (std::vector<RORoute*>::iterator i = myAlternatives.begin(); i != myAlternatives.end() - 1; i++, pos++) {
364  chosen -= (*i)->getProbability();
365  if (chosen <= 0) {
366  myLastUsed = pos;
367  return;
368  }
369  }
370  myLastUsed = pos;
371 }
372 
373 
374 const ROEdge*
376  return myAlternatives[0]->getLast();
377 }
378 
379 
382  bool asAlternatives, bool withExitTimes) const {
383  if (asAlternatives) {
385  for (size_t i = 0; i != myAlternatives.size(); i++) {
386  myAlternatives[i]->writeXMLDefinition(dev, veh, true, withExitTimes);
387  }
388  dev.closeTag();
389  return dev;
390  } else {
391  return myAlternatives[myLastUsed]->writeXMLDefinition(dev, veh, false, withExitTimes);
392  }
393 }
394 
395 
396 RORouteDef*
397 RORouteDef::copyOrigDest(const std::string& id) const {
398  RORouteDef* result = new RORouteDef(id, 0, true, true);
399  RORoute* route = myAlternatives[0];
400  RGBColor* col = route->getColor() != 0 ? new RGBColor(*route->getColor()) : 0;
401  ConstROEdgeVector edges;
402  edges.push_back(route->getFirst());
403  edges.push_back(route->getLast());
404  result->addLoadedAlternative(new RORoute(id, 0, 1, edges, col, route->getStops()));
405  return result;
406 }
407 
408 
409 RORouteDef*
410 RORouteDef::copy(const std::string& id, const SUMOTime stopOffset) const {
411  RORouteDef* result = new RORouteDef(id, 0, myTryRepair, myMayBeDisconnected);
412  for (std::vector<RORoute*>::const_iterator i = myAlternatives.begin(); i != myAlternatives.end(); i++) {
413  RORoute* route = *i;
414  RGBColor* col = route->getColor() != 0 ? new RGBColor(*route->getColor()) : 0;
415  RORoute* newRoute = new RORoute(id, 0, 1, route->getEdgeVector(), col, route->getStops());
416  newRoute->addStopOffset(stopOffset);
417  result->addLoadedAlternative(newRoute);
418  }
419  return result;
420 }
421 
422 
423 SUMOReal
425  SUMOReal sum = 0.;
426  for (std::vector<RORoute*>::const_iterator i = myAlternatives.begin(); i != myAlternatives.end(); i++) {
427  sum += (*i)->getProbability();
428  }
429  return sum;
430 }
431 
432 
433 /****************************************************************************/
SUMOTime getDepartureTime() const
Returns the time the vehicle starts at, 0 for triggered vehicles.
Definition: ROVehicle.h:111
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:71
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
bool myNewRoute
Information whether a new route was generated.
Definition: RORouteDef.h:168
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:397
long long int SUMOTime
Definition: SUMOTime.h:43
const bool myTryRepair
Definition: RORouteDef.h:170
bool repairCurrentRoute(SUMOAbstractRouter< ROEdge, ROVehicle > &router, SUMOTime begin, const ROVehicle &veh, ConstROEdgeVector oldEdges, ConstROEdgeVector &newEdges) const
Builds the complete route (or chooses her from the list of alternatives, when existing) ...
Definition: RORouteDef.cpp:173
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:288
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:95
void addAlternativeDef(const RORouteDef *alternative)
Adds an alternative loaded from the file.
Definition: RORouteDef.cpp:86
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:62
const ConstROEdgeVector & getEdgeVector() const
Returns the list of edges this route consists of.
Definition: RORoute.h:162
void setProbability(SUMOReal prob)
Sets the probability of the route.
Definition: RORoute.cpp:83
virtual bool 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...
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:105
void addStopOffset(const SUMOTime offset)
Adapts the until time of all stops by the given offset.
Definition: RORoute.h:197
std::vector< const ROEdge * > ConstROEdgeVector
Definition: ROEdge.h:62
static bool myUsingJTRR
Definition: RORouteDef.h:173
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
RORoute * myPrecomputed
precomputed route for out-of-order computation
Definition: RORouteDef.h:156
const std::string & getID() const
Returns the id.
Definition: Named.h:65
const bool myMayBeDisconnected
Definition: RORouteDef.h:171
A vehicle as used by router.
Definition: ROVehicle.h:60
const ROEdge * getLast() const
Returns the last edge in the route.
Definition: RORoute.h:110
std::set< RORoute * > myRouteRefs
Routes which are deleted someplace else.
Definition: RORouteDef.h:165
OutputDevice & writeXMLDefinition(OutputDevice &dev, const ROVehicle *const veh, bool asAlternatives, bool withExitTimes) const
Saves the built route / route alternatives.
Definition: RORouteDef.cpp:381
const RGBColor * getColor() const
Returns this route&#39;s color.
Definition: RORoute.h:170
SUMOReal getProbability() const
Returns the probability the driver will take this route with.
Definition: RORoute.h:130
void setCosts(SUMOReal costs)
Sets the costs of the route.
Definition: RORoute.cpp:77
Abstract base class providing static factory method.
const ROEdge * getDestination() const
Definition: RORouteDef.cpp:375
const ROEdge * getFirst() const
Returns the first edge in the route.
Definition: RORoute.h:101
A basic edge for routing applications.
Definition: ROEdge.h:77
Base class for objects which have an id.
Definition: Named.h:45
virtual ~RORouteDef()
Destructor.
Definition: RORouteDef.cpp:70
unsigned int myLastUsed
Index of the route used within the last step.
Definition: RORouteDef.h:159
std::string myID
The name of the object.
Definition: Named.h:133
const ConstROEdgeVector & getStopEdges() const
Definition: ROVehicle.h:116
RORouteDef * copy(const std::string &id, const SUMOTime stopOffset) const
Returns a deep copy of the route definition.
Definition: RORouteDef.cpp:410
const std::string & getID() const
Returns the id of the vehicle.
Definition: RORoutable.h:92
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:89
A storage for options typed value containers)
Definition: OptionsCont.h:108
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:80
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
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:213
static RouteCostCalculator< R, E, V > & getCalculator()
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
RORouteDef(const std::string &id, const unsigned int lastUsed, const bool tryRepair, const bool mayBeDisconnected)
Constructor.
Definition: RORouteDef.cpp:63
std::vector< RORoute * > myAlternatives
The alternatives.
Definition: RORouteDef.h:162
SUMOReal getOverallProb() const
Returns the sum of the probablities of the contained routes.
Definition: RORouteDef.cpp:424
const std::vector< SUMOVehicleParameter::Stop > & getStops() const
Returns the list of stops this route contains.
Definition: RORoute.h:191
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
A complete router&#39;s route.
Definition: RORoute.h:62