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