SUMO - Simulation of Urban MObility
AStarRouter.h
Go to the documentation of this file.
1 /****************************************************************************/
9 // A* Algorithm using euclidean distance heuristic.
10 // Based on DijkstraRouterTT. For routing by effort a novel heuristic would be needed.
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2012-2015 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 #ifndef AStarRouter_h
24 #define AStarRouter_h
25 
26 
27 // ===========================================================================
28 // included modules
29 // ===========================================================================
30 #ifdef _MSC_VER
31 #include <windows_config.h>
32 #else
33 #include <config.h>
34 #endif
35 
36 #include <cassert>
37 #include <string>
38 #include <functional>
39 #include <vector>
40 #include <set>
41 #include <limits>
42 #include <algorithm>
43 #include <iterator>
44 #include <map>
46 #include <utils/common/StdDefs.h>
47 #include <utils/common/ToString.h>
49 #include "SUMOAbstractRouter.h"
50 
51 
52 // ===========================================================================
53 // class definitions
54 // ===========================================================================
70 template<class E, class V, class PF>
71 class AStarRouter : public SUMOAbstractRouter<E, V>, public PF {
72 
73 public:
74  typedef SUMOReal(* Operation)(const E* const, const V* const, SUMOReal);
75  typedef std::vector<std::vector<SUMOReal> > LookupTable;
77  AStarRouter(size_t noE, bool unbuildIsWarning, Operation operation, const LookupTable* const lookup = 0):
78  SUMOAbstractRouter<E, V>(operation, "AStarRouter"),
79  myErrorMsgHandler(unbuildIsWarning ? MsgHandler::getWarningInstance() : MsgHandler::getErrorInstance()),
80  myLookupTable(lookup) {
81  for (size_t i = 0; i < noE; i++) {
82  myEdgeInfos.push_back(EdgeInfo(i));
83  }
84  }
85 
87  virtual ~AStarRouter() {}
88 
89  virtual SUMOAbstractRouter<E, V>* clone() const {
91  }
92 
93  static LookupTable* createLookupTable(const std::string& filename, const int size) {
94  LookupTable* const result = new LookupTable();
95  BinaryInputDevice dev(filename);
96  for (int i = 0; i < size; i++) {
97  for (int j = 0; j < size; j++) {
98  SUMOReal val;
99  dev >> val;
100  (*result)[i].push_back(val);
101  }
102  }
103  return result;
104  }
105 
111  class EdgeInfo {
112  public:
114  EdgeInfo(size_t id) :
115  edge(E::dictionary(id)),
116  traveltime(std::numeric_limits<SUMOReal>::max()),
117  heuristicTime(std::numeric_limits<SUMOReal>::max()),
118  prev(0),
119  visited(false)
120  {}
121 
123  const E* edge;
124 
127 
130 
133 
135  bool visited;
136 
137  inline void reset() {
138  // heuristicTime is set before adding to the frontier, thus no reset is needed
139  traveltime = std::numeric_limits<SUMOReal>::max();
140  visited = false;
141  }
142 
143  };
144 
150  public:
152  bool operator()(const EdgeInfo* nod1, const EdgeInfo* nod2) const {
153  if (nod1->heuristicTime == nod2->heuristicTime) {
154  return nod1->edge->getNumericalID() > nod2->edge->getNumericalID();
155  }
156  return nod1->heuristicTime > nod2->heuristicTime;
157  }
158  };
159 
160  void init() {
161  // all EdgeInfos touched in the previous query are either in myFrontierList or myFound: clean those up
162  for (typename std::vector<EdgeInfo*>::iterator i = myFrontierList.begin(); i != myFrontierList.end(); i++) {
163  (*i)->reset();
164  }
165  myFrontierList.clear();
166  for (typename std::vector<EdgeInfo*>::iterator i = myFound.begin(); i != myFound.end(); i++) {
167  (*i)->reset();
168  }
169  myFound.clear();
170  }
171 
172 
174  virtual void compute(const E* from, const E* to, const V* const vehicle,
175  SUMOTime msTime, std::vector<const E*>& into) {
176  assert(from != 0 && to != 0);
177  // check whether from and to can be used
178  if (PF::operator()(from, vehicle)) {
179  myErrorMsgHandler->inform("Vehicle '" + vehicle->getID() + "' is not allowed on from edge '" + from->getID() + "'.");
180  return;
181  }
182  if (PF::operator()(to, vehicle)) {
183  myErrorMsgHandler->inform("Vehicle '" + vehicle->getID() + "' is not allowed on to edge '" + to->getID() + "'.");
184  return;
185  }
186  this->startQuery();
187  const SUMOVehicleClass vClass = vehicle == 0 ? SVC_IGNORING : vehicle->getVClass();
188  const SUMOReal time = STEPS2TIME(msTime);
189  if (this->myBulkMode) {
190  const EdgeInfo& toInfo = myEdgeInfos[to->getNumericalID()];
191  if (toInfo.visited) {
192  buildPathFrom(&toInfo, into);
193  this->endQuery(1);
194  return;
195  }
196  } else {
197  init();
198  // add begin node
199  EdgeInfo* const fromInfo = &(myEdgeInfos[from->getNumericalID()]);
200  fromInfo->traveltime = 0;
201  fromInfo->prev = 0;
202  myFrontierList.push_back(fromInfo);
203  }
204  // loop
205  int num_visited = 0;
206  while (!myFrontierList.empty()) {
207  num_visited += 1;
208  // use the node with the minimal length
209  EdgeInfo* const minimumInfo = myFrontierList.front();
210  const E* const minEdge = minimumInfo->edge;
211  // check whether the destination node was already reached
212  if (minEdge == to) {
213  buildPathFrom(minimumInfo, into);
214  this->endQuery(num_visited);
215  return;
216  }
217  pop_heap(myFrontierList.begin(), myFrontierList.end(), myComparator);
218  myFrontierList.pop_back();
219  myFound.push_back(minimumInfo);
220  minimumInfo->visited = true;
221  const SUMOReal traveltime = minimumInfo->traveltime + this->getEffort(minEdge, vehicle, time + minimumInfo->traveltime);
222  // admissible A* heuristic: straight line distance at maximum speed
223  const SUMOReal heuristic_remaining = myLookupTable == 0 ? minEdge->getDistanceTo(to) / vehicle->getMaxSpeed() : (*myLookupTable)[minEdge->getNumericalID()][to->getNumericalID()] / vehicle->getChosenSpeedFactor();
224  // check all ways from the node with the minimal length
225  const std::vector<E*>& successors = minEdge->getSuccessors(vClass);
226  for (typename std::vector<E*>::const_iterator it = successors.begin(); it != successors.end(); ++it) {
227  const E* const follower = *it;
228  EdgeInfo* const followerInfo = &(myEdgeInfos[follower->getNumericalID()]);
229  // check whether it can be used
230  if (PF::operator()(follower, vehicle)) {
231  continue;
232  }
233  const SUMOReal oldEffort = followerInfo->traveltime;
234  if (!followerInfo->visited && traveltime < oldEffort) {
235  followerInfo->traveltime = traveltime;
236  followerInfo->heuristicTime = traveltime + heuristic_remaining;
237  /* the code below results in fewer edges being looked up but is more costly due to the effort
238  calculations. Overall it resulted in a slowdown in the Berlin tests but could be made configurable someday.
239  followerInfo->heuristicTime = traveltime;
240  if (follower != to) {
241  if (myLookupTable == 0) {
242  // admissible A* heuristic: straight line distance at maximum speed
243  followerInfo->heuristicTime += this->getEffort(follower, vehicle, time + traveltime) + follower->getDistanceTo(to) / vehicle->getMaxSpeed();
244  } else {
245  followerInfo->heuristicTime += this->getEffort(follower, vehicle, time + traveltime) + (*myLookupTable)[follower->getNumericalID()][to->getNumericalID()] / vehicle->getChosenSpeedFactor();
246  }
247  }*/
248  followerInfo->prev = minimumInfo;
249  if (oldEffort == std::numeric_limits<SUMOReal>::max()) {
250  myFrontierList.push_back(followerInfo);
251  push_heap(myFrontierList.begin(), myFrontierList.end(), myComparator);
252  } else {
253  push_heap(myFrontierList.begin(),
254  find(myFrontierList.begin(), myFrontierList.end(), followerInfo) + 1,
255  myComparator);
256  }
257  }
258  }
259  }
260  this->endQuery(num_visited);
261  myErrorMsgHandler->inform("No connection between edge '" + from->getID() + "' and edge '" + to->getID() + "' found.");
262  }
263 
264 
265  SUMOReal recomputeCosts(const std::vector<const E*>& edges, const V* const v, SUMOTime msTime) const {
266  const SUMOReal time = STEPS2TIME(msTime);
267  SUMOReal costs = 0;
268  for (typename std::vector<const E*>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
269  if (PF::operator()(*i, v)) {
270  return -1;
271  }
272  costs += this->getEffort(*i, v, time + costs);
273  }
274  return costs;
275  }
276 
277 public:
279  void buildPathFrom(const EdgeInfo* rbegin, std::vector<const E*>& edges) {
280  std::vector<const E*> tmp;
281  while (rbegin != 0) {
282  tmp.push_back(rbegin->edge);
283  rbegin = rbegin->prev;
284  }
285  std::copy(tmp.rbegin(), tmp.rend(), std::back_inserter(edges));
286  }
287 
288 protected:
290  std::vector<EdgeInfo> myEdgeInfos;
291 
293  std::vector<EdgeInfo*> myFrontierList;
295  std::vector<EdgeInfo*> myFound;
296 
297  EdgeInfoComparator myComparator;
298 
301 
303  const LookupTable* const myLookupTable;
304 };
305 
306 
307 #endif
308 
309 /****************************************************************************/
310 
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:71
static LookupTable * createLookupTable(const std::string &filename, const int size)
Definition: AStarRouter.h:93
long long int SUMOTime
Definition: SUMOTime.h:43
virtual SUMOAbstractRouter< E, V > * clone() const
Definition: AStarRouter.h:89
bool visited
The previous edge.
Definition: AStarRouter.h:135
std::vector< EdgeInfo > myEdgeInfos
The container of edge information.
Definition: AStarRouter.h:290
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types...
void buildPathFrom(const EdgeInfo *rbegin, std::vector< const E * > &edges)
Builds the path from marked edges.
Definition: AStarRouter.h:279
std::vector< EdgeInfo * > myFrontierList
A container for reusage of the min edge heap.
Definition: AStarRouter.h:293
EdgeInfoComparator myComparator
Definition: AStarRouter.h:297
std::vector< std::vector< SUMOReal > > LookupTable
Definition: AStarRouter.h:75
MsgHandler *const myErrorMsgHandler
the handler for routing errors
Definition: AStarRouter.h:300
Computes the shortest path through a network using the A* algorithm.
Definition: AStarRouter.h:71
void init()
Definition: AStarRouter.h:160
AStarRouter(size_t noE, bool unbuildIsWarning, Operation operation, const LookupTable *const lookup=0)
Constructor.
Definition: AStarRouter.h:77
EdgeInfo * prev
The previous edge.
Definition: AStarRouter.h:132
bool operator()(const EdgeInfo *nod1, const EdgeInfo *nod2) const
Comparing method.
Definition: AStarRouter.h:152
#define max(a, b)
Definition: polyfonts.c:65
virtual ~AStarRouter()
Destructor.
Definition: AStarRouter.h:87
bool myBulkMode
whether we are currently operating several route queries in a bulk
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
Operation myOperation
The object&#39;s operation to perform.
SUMOReal heuristicTime
Estimated time to reach the edge (traveltime + lower bound on remaining time)
Definition: AStarRouter.h:129
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:89
EdgeInfo(size_t id)
Constructor.
Definition: AStarRouter.h:114
virtual void compute(const E *from, const E *to, const V *const vehicle, SUMOTime msTime, std::vector< const E * > &into)
Builds the route between the given edges using the minimum travel time.
Definition: AStarRouter.h:174
#define SUMOReal
Definition: config.h:214
void endQuery(int visits)
const LookupTable *const myLookupTable
the lookup table for travel time heuristics
Definition: AStarRouter.h:303
SUMOReal traveltime
Effort to reach the edge.
Definition: AStarRouter.h:126
SUMOReal recomputeCosts(const std::vector< const E * > &edges, const V *const v, SUMOTime msTime) const
Definition: AStarRouter.h:265
SUMOReal getEffort(const E *const e, const V *const v, SUMOReal t) const
std::vector< EdgeInfo * > myFound
list of visited Edges (for resetting)
Definition: AStarRouter.h:295
Encapsulates binary reading operations on a file.
vehicles ignoring classes
SUMOReal(* Operation)(const E *const, const V *const, SUMOReal)
Definition: AStarRouter.h:74
const E * edge
The current edge.
Definition: AStarRouter.h:123