SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DijkstraRouterEffort.h
Go to the documentation of this file.
1 /****************************************************************************/
9 // Dijkstra shortest path algorithm using other values
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2015 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 #ifndef DijkstraRouterEffort_h
23 #define DijkstraRouterEffort_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <cassert>
36 #include <string>
37 #include <functional>
38 #include <vector>
39 #include <set>
40 #include <limits>
41 #include <algorithm>
43 #include <utils/common/StdDefs.h>
44 #include "SUMOAbstractRouter.h"
45 
46 
47 // ===========================================================================
48 // class definitions
49 // ===========================================================================
65 template<class E, class V, class PF>
66 class DijkstraRouterEffort : public SUMOAbstractRouter<E, V>, public PF {
67 
68 public:
69  typedef SUMOReal(* Operation)(const E* const, const V* const, SUMOReal);
71  DijkstraRouterEffort(size_t noE, bool unbuildIsWarning, Operation effortOperation, Operation ttOperation) :
72  SUMOAbstractRouter<E, V>(effortOperation, "DijkstraRouterEffort"), myTTOperation(ttOperation),
73  myErrorMsgHandler(unbuildIsWarning ? MsgHandler::getWarningInstance() : MsgHandler::getErrorInstance()) {
74  for (size_t i = 0; i < noE; i++) {
75  myEdgeInfos.push_back(EdgeInfo(i));
76  }
77  }
78 
80  virtual ~DijkstraRouterEffort() { }
81 
82  virtual SUMOAbstractRouter<E, V>* clone() const {
84  }
85 
91  class EdgeInfo {
92  public:
94  EdgeInfo(size_t id)
95  : edge(E::dictionary(id)), effort(std::numeric_limits<SUMOReal>::max()), leaveTime(0), prev(0), visited(false) {}
96 
98  const E* edge;
99 
102 
105 
108 
110  bool visited;
111 
112  inline void reset() {
114  visited = false;
115  }
116  };
117 
123  public:
125  bool operator()(EdgeInfo* nod1, EdgeInfo* nod2) const {
126  if (nod1->effort == nod2->effort) {
127  return nod1->edge->getNumericalID() > nod2->edge->getNumericalID();
128  }
129  return nod1->effort > nod2->effort;
130  }
131  };
132 
133  inline SUMOReal getTravelTime(const E* const e, const V* const v, SUMOReal t) const {
134  return (*myTTOperation)(e, v, t);
135  }
136 
137  void init() {
138  // all EdgeInfos touched in the previous query are either in myFrontierList or myFound: clean those up
139  for (typename std::vector<EdgeInfo*>::iterator i = myFrontierList.begin(); i != myFrontierList.end(); i++) {
140  (*i)->reset();
141  }
142  myFrontierList.clear();
143  for (typename std::vector<EdgeInfo*>::iterator i = myFound.begin(); i != myFound.end(); i++) {
144  (*i)->reset();
145  }
146  myFound.clear();
147  }
148 
149 
152  virtual void compute(const E* from, const E* to, const V* const vehicle,
153  SUMOTime msTime, std::vector<const E*>& into) {
154  assert(from != 0 && to != 0);
155  this->startQuery();
156  const SUMOVehicleClass vClass = vehicle == 0 ? SVC_IGNORING : vehicle->getVClass();
157  if (this->myBulkMode) {
158  const EdgeInfo& toInfo = myEdgeInfos[to->getNumericalID()];
159  if (toInfo.visited) {
160  buildPathFrom(&toInfo, into);
161  this->endQuery(1);
162  return;
163  }
164  } else {
165  init();
166  // add begin node
167  EdgeInfo* const fromInfo = &(myEdgeInfos[from->getNumericalID()]);
168  fromInfo->effort = 0;
169  fromInfo->prev = 0;
170  fromInfo->leaveTime = STEPS2TIME(msTime);
171  myFrontierList.push_back(fromInfo);
172  }
173  // loop
174  int num_visited = 0;
175  while (!myFrontierList.empty()) {
176  num_visited += 1;
177  // use the node with the minimal length
178  EdgeInfo* const minimumInfo = myFrontierList.front();
179  const E* const minEdge = minimumInfo->edge;
180  pop_heap(myFrontierList.begin(), myFrontierList.end(), myComparator);
181  myFrontierList.pop_back();
182  myFound.push_back(minimumInfo);
183  // check whether the destination node was already reached
184  if (minEdge == to) {
185  buildPathFrom(minimumInfo, into);
186  this->endQuery(num_visited);
187  return;
188  }
189  minimumInfo->visited = true;
190  const SUMOReal effort = minimumInfo->effort + this->getEffort(minEdge, vehicle, minimumInfo->leaveTime);
191  const SUMOReal leaveTime = minimumInfo->leaveTime + getTravelTime(minEdge, vehicle, minimumInfo->leaveTime);
192  // check all ways from the node with the minimal length
193  const std::vector<E*>& successors = minEdge->getSuccessors(vClass);
194  for (typename std::vector<E*>::const_iterator it = successors.begin(); it != successors.end(); ++it) {
195  const E* const follower = *it;
196  EdgeInfo* const followerInfo = &(myEdgeInfos[follower->getNumericalID()]);
197  // check whether it can be used
198  if (PF::operator()(follower, vehicle)) {
199  continue;
200  }
201  const SUMOReal oldEffort = followerInfo->effort;
202  if (!followerInfo->visited && effort < oldEffort) {
203  followerInfo->effort = effort;
204  followerInfo->leaveTime = leaveTime;
205  followerInfo->prev = minimumInfo;
206  if (oldEffort == std::numeric_limits<SUMOReal>::max()) {
207  myFrontierList.push_back(followerInfo);
208  push_heap(myFrontierList.begin(), myFrontierList.end(), myComparator);
209  } else {
210  push_heap(myFrontierList.begin(),
211  find(myFrontierList.begin(), myFrontierList.end(), followerInfo) + 1,
212  myComparator);
213  }
214  }
215  }
216  }
217  this->endQuery(num_visited);
218  myErrorMsgHandler->inform("No connection between '" + from->getID() + "' and '" + to->getID() + "' found.");
219  }
220 
221 
222  SUMOReal recomputeCosts(const std::vector<const E*>& edges, const V* const v, SUMOTime msTime) const {
223  SUMOReal costs = 0;
224  SUMOReal t = STEPS2TIME(msTime);
225  for (typename std::vector<const E*>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
226  if (PF::operator()(*i, v)) {
227  return -1;
228  }
229  costs += this->getEffort(*i, v, t);
230  t += getTravelTime(*i, v, t);
231  }
232  return costs;
233  }
234 
235 public:
237  void buildPathFrom(const EdgeInfo* rbegin, std::vector<const E*>& edges) {
238  std::vector<const E*> tmp;
239  while (rbegin != 0) {
240  tmp.push_back(rbegin->edge);
241  rbegin = rbegin->prev;
242  }
243  std::copy(tmp.rbegin(), tmp.rend(), std::back_inserter(edges));
244  }
245 
246 private:
249 
251  std::vector<EdgeInfo> myEdgeInfos;
252 
254  std::vector<EdgeInfo*> myFrontierList;
256  std::vector<EdgeInfo*> myFound;
257 
259 
262 
263 };
264 
265 
266 #endif
267 
268 /****************************************************************************/
269 
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:71
Computes the shortest path through a network using the Dijkstra algorithm.
SUMOReal getTravelTime(const E *const e, const V *const v, SUMOReal t) const
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types...
bool operator()(EdgeInfo *nod1, EdgeInfo *nod2) const
Comparing method.
SUMOReal recomputeCosts(const std::vector< const E * > &edges, const V *const v, SUMOTime msTime) const
std::vector< EdgeInfo > myEdgeInfos
The container of edge information.
MsgHandler *const myErrorMsgHandler
the handler for routing errors
EdgeInfo(size_t id)
Constructor.
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 effort at the given time The definition of...
#define max(a, b)
Definition: polyfonts.c:65
DijkstraRouterEffort(size_t noE, bool unbuildIsWarning, Operation effortOperation, Operation ttOperation)
Constructor.
const E * edge
The current edge.
bool myBulkMode
whether we are currently operating several route queries in a bulk
SUMOReal(* Operation)(const E *const, const V *const, SUMOReal)
std::vector< EdgeInfo * > myFound
list of visited Edges (for resetting)
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
SUMOReal leaveTime
The time the vehicle leaves the edge.
EdgeInfoByEffortComparator myComparator
void buildPathFrom(const EdgeInfo *rbegin, std::vector< const E * > &edges)
Builds the path from marked edges.
Operation myOperation
The object&#39;s operation to perform.
SUMOReal effort
Effort to reach the edge.
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:89
#define SUMOReal
Definition: config.h:215
void endQuery(int visits)
Operation myTTOperation
The object&#39;s operation to perform for travel times.
std::vector< EdgeInfo * > myFrontierList
A container for reusage of the min edge heap.
virtual ~DijkstraRouterEffort()
Destructor.
SUMOReal getEffort(const E *const e, const V *const v, SUMOReal t) const
EdgeInfo * prev
The previous edge.
virtual SUMOAbstractRouter< E, V > * clone() const
vehicles ignoring classes