SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  this->startQuery();
178  const SUMOVehicleClass vClass = vehicle == 0 ? SVC_IGNORING : vehicle->getVClass();
179  const SUMOReal time = STEPS2TIME(msTime);
180  init();
181  // add begin node
182  EdgeInfo* const fromInfo = &(myEdgeInfos[from->getNumericalID()]);
183  fromInfo->traveltime = 0;
184  fromInfo->prev = 0;
185  myFrontierList.push_back(fromInfo);
186  // loop
187  int num_visited = 0;
188  while (!myFrontierList.empty()) {
189  num_visited += 1;
190  // use the node with the minimal length
191  EdgeInfo* const minimumInfo = myFrontierList.front();
192  const E* const minEdge = minimumInfo->edge;
193  pop_heap(myFrontierList.begin(), myFrontierList.end(), myComparator);
194  myFrontierList.pop_back();
195  myFound.push_back(minimumInfo);
196  // check whether the destination node was already reached
197  if (minEdge == to) {
198  buildPathFrom(minimumInfo, into);
199  this->endQuery(num_visited);
200  // DEBUG
201  //std::cout << "visited " + toString(num_visited) + " edges (final path length: " + toString(into.size()) + ")\n";
202  return;
203  }
204  minimumInfo->visited = true;
205  const SUMOReal traveltime = minimumInfo->traveltime + this->getEffort(minEdge, vehicle, time + minimumInfo->traveltime);
206  // admissible A* heuristic: straight line distance at maximum speed
207  const SUMOReal heuristic_remaining = myLookupTable == 0 ? minEdge->getDistanceTo(to) / vehicle->getMaxSpeed() : (*myLookupTable)[minEdge->getNumericalID()][to->getNumericalID()] / vehicle->getChosenSpeedFactor();
208  // check all ways from the node with the minimal length
209  const std::vector<E*>& successors = minEdge->getSuccessors(vClass);
210  for (typename std::vector<E*>::const_iterator it = successors.begin(); it != successors.end(); ++it) {
211  const E* const follower = *it;
212  EdgeInfo* const followerInfo = &(myEdgeInfos[follower->getNumericalID()]);
213  // check whether it can be used
214  if (PF::operator()(follower, vehicle)) {
215  continue;
216  }
217  const SUMOReal oldEffort = followerInfo->traveltime;
218  if (!followerInfo->visited && traveltime < oldEffort) {
219  followerInfo->traveltime = traveltime;
220  followerInfo->heuristicTime = traveltime + heuristic_remaining;
221  /* the code below results in fewer edges being looked up but is more costly due to the effort
222  calculations. Overall it resulted in a slowdown in the Berlin tests but could be made configurable someday.
223  followerInfo->heuristicTime = traveltime;
224  if (follower != to) {
225  if (myLookupTable == 0) {
226  // admissible A* heuristic: straight line distance at maximum speed
227  followerInfo->heuristicTime += this->getEffort(follower, vehicle, time + traveltime) + follower->getDistanceTo(to) / vehicle->getMaxSpeed();
228  } else {
229  followerInfo->heuristicTime += this->getEffort(follower, vehicle, time + traveltime) + (*myLookupTable)[follower->getNumericalID()][to->getNumericalID()] / vehicle->getChosenSpeedFactor();
230  }
231  }*/
232  followerInfo->prev = minimumInfo;
233  if (oldEffort == std::numeric_limits<SUMOReal>::max()) {
234  myFrontierList.push_back(followerInfo);
235  push_heap(myFrontierList.begin(), myFrontierList.end(), myComparator);
236  } else {
237  push_heap(myFrontierList.begin(),
238  find(myFrontierList.begin(), myFrontierList.end(), followerInfo) + 1,
239  myComparator);
240  }
241  }
242  }
243  }
244  this->endQuery(num_visited);
245  myErrorMsgHandler->inform("No connection between '" + from->getID() + "' and '" + to->getID() + "' found.");
246  }
247 
248 
249  SUMOReal recomputeCosts(const std::vector<const E*>& edges, const V* const v, SUMOTime msTime) const {
250  const SUMOReal time = STEPS2TIME(msTime);
251  SUMOReal costs = 0;
252  for (typename std::vector<const E*>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
253  if (PF::operator()(*i, v)) {
254  return -1;
255  }
256  costs += this->getEffort(*i, v, time + costs);
257  }
258  return costs;
259  }
260 
261 public:
263  void buildPathFrom(EdgeInfo* rbegin, std::vector<const E*>& edges) {
264  std::deque<const E*> tmp;
265  while (rbegin != 0) {
266  tmp.push_front((E*) rbegin->edge); // !!!
267  rbegin = rbegin->prev;
268  }
269  std::copy(tmp.begin(), tmp.end(), std::back_inserter(edges));
270  }
271 
272 protected:
274  std::vector<EdgeInfo> myEdgeInfos;
275 
277  std::vector<EdgeInfo*> myFrontierList;
279  std::vector<EdgeInfo*> myFound;
280 
281  EdgeInfoComparator myComparator;
282 
285 
287  const LookupTable* const myLookupTable;
288 
289 };
290 
291 
292 #endif
293 
294 /****************************************************************************/
295 
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
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:274
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types...
std::vector< EdgeInfo * > myFrontierList
A container for reusage of the min edge heap.
Definition: AStarRouter.h:277
EdgeInfoComparator myComparator
Definition: AStarRouter.h:281
std::vector< std::vector< SUMOReal > > LookupTable
Definition: AStarRouter.h:75
MsgHandler *const myErrorMsgHandler
the handler for routing errors
Definition: AStarRouter.h:284
Computes the shortest path through a network using the Dijkstra algorithm.
Definition: AStarRouter.h:71
void init()
Definition: AStarRouter.h:160
SUMOReal(* Operation)(const E *const, const V *const, SUMOReal)
Definition: AStarRouter.h:74
AStarRouter(size_t noE, bool unbuildIsWarning, Operation operation, const LookupTable *const lookup=0)
Constructor.
Definition: AStarRouter.h:77
void buildPathFrom(EdgeInfo *rbegin, std::vector< const E * > &edges)
Builds the path from marked edges.
Definition: AStarRouter.h:263
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
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
Operation myOperation
The object'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
int SUMOTime
Definition: SUMOTime.h:43
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:218
void endQuery(int visits)
const LookupTable *const myLookupTable
the lookup table for travel time heuristics
Definition: AStarRouter.h:287
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:249
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:279
Encapsulates binary reading operations on a file.
vehicles ignoring classes
const E * edge
The current edge.
Definition: AStarRouter.h:123