SUMO - Simulation of Urban MObility
SUMOAbstractRouter.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2006-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // An abstract router base class
18 /****************************************************************************/
19 #ifndef SUMOAbstractRouter_h
20 #define SUMOAbstractRouter_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <string>
29 #include <vector>
30 #include <algorithm>
31 #include <assert.h>
32 #include <utils/common/SysUtils.h>
34 #include <utils/common/SUMOTime.h>
35 #include <utils/common/ToString.h>
36 
37 
38 // ===========================================================================
39 // class definitions
40 // ===========================================================================
45 template<class E, class V>
47 public:
53  class EdgeInfo {
54  public:
56  EdgeInfo(const E* const e)
57  : edge(e), effort(std::numeric_limits<double>::max()),
58  heuristicEffort(std::numeric_limits<double>::max()),
59  leaveTime(0.), prev(nullptr), via(nullptr), visited(false) {}
60 
62  const E* const edge;
63 
65  double effort;
66 
68  // only used by A*
70 
72  double leaveTime;
73 
75  const EdgeInfo* prev;
76 
78  const E* via;
79 
81  bool visited;
82 
83  inline void reset() {
84  effort = std::numeric_limits<double>::max();
85  via = nullptr;
86  visited = false;
87  }
88 
89  private:
91  EdgeInfo& operator=(const EdgeInfo& s) = delete;
92 
93  };
94 
96  typedef double(* Operation)(const E* const, const V* const, double);
97 
99  SUMOAbstractRouter(const std::string& type, Operation operation = nullptr, Operation ttOperation = nullptr) :
100  myOperation(operation), myTTOperation(ttOperation),
101  myBulkMode(false),
102  myType(type),
103  myQueryVisits(0),
104  myNumQueries(0),
105  myQueryStartTime(0),
106  myQueryTimeSum(0) {
107  }
108 
111  if (myNumQueries > 0) {
112  WRITE_MESSAGE(myType + " answered " + toString(myNumQueries) + " queries and explored " + toString(double(myQueryVisits) / myNumQueries) + " edges on average.");
113  WRITE_MESSAGE(myType + " spent " + toString(myQueryTimeSum) + "ms answering queries (" + toString(double(myQueryTimeSum) / myNumQueries) + "ms on average).");
114  }
115  }
116 
117  virtual SUMOAbstractRouter* clone() = 0;
118 
121  virtual bool compute(const E* from, const E* to, const V* const vehicle,
122  SUMOTime msTime, std::vector<const E*>& into) = 0;
123 
124  virtual bool isProhibited(const E* const /* edge */, const V* const /* vehicle */) const {
125  return false;
126  }
127 
128  inline double getTravelTime(const E* const e, const V* const v, const double t, const double effort) const {
129  return myTTOperation == nullptr ? effort : (*myTTOperation)(e, v, t);
130  }
131 
132  inline void updateViaCost(const E* const prev, const E* const e, const V* const v, double& time, double& effort, double& length) const {
133  if (prev != nullptr) {
134  for (const std::pair<const E*, const E*>& follower : prev->getViaSuccessors()) {
135  if (follower.first == e) {
136  const E* viaEdge = follower.second;
137  while (viaEdge != nullptr && viaEdge->isInternal()) {
138  const double viaEffortDelta = this->getEffort(viaEdge, v, time);
139  time += getTravelTime(viaEdge, v, time, viaEffortDelta);
140  effort += viaEffortDelta;
141  length += viaEdge->getLength();
142  viaEdge = viaEdge->getViaSuccessors().front().first;
143  }
144  break;
145  }
146  }
147  }
148  const double effortDelta = this->getEffort(e, v, time);
149  effort += effortDelta;
150  time += getTravelTime(e, v, time, effortDelta);
151  length += e->getLength();
152  }
153 
154 
155  inline double recomputeCosts(const std::vector<const E*>& edges, const V* const v, SUMOTime msTime) const {
156  double time = STEPS2TIME(msTime);
157  double effort = 0.;
158  double length = 0.;
159  const E* prev = nullptr;
160  for (const E* const e : edges) {
161  if (isProhibited(e, v)) {
162  return -1;
163  }
164  updateViaCost(prev, e, v, time, effort, length);
165  prev = e;
166  }
167  return effort;
168  }
169 
170 
171  inline double getEffort(const E* const e, const V* const v, double t) const {
172  return (*myOperation)(e, v, t);
173  }
174 
175  inline void startQuery() {
176  myNumQueries++;
178  }
179 
180  inline void endQuery(int visits) {
181  myQueryVisits += visits;
183  }
184 
185  void setBulkMode(const bool mode) {
186  myBulkMode = mode;
187  }
188 
189 protected:
192 
195 
198 
199 private:
201  const std::string myType;
202 
204  long long int myQueryVisits;
205  long long int myNumQueries;
207  long long int myQueryStartTime;
208  long long int myQueryTimeSum;
209 private:
212 };
213 
214 
215 template<class E, class V>
217 public:
219  SUMOAbstractRouterPermissions(const std::string& type, typename SUMOAbstractRouter<E, V>::Operation operation = nullptr, typename SUMOAbstractRouter<E, V>::Operation ttOperation = nullptr) :
220  SUMOAbstractRouter<E, V>(type, operation, ttOperation) {
221  }
222 
225  }
226 
227  bool isProhibited(const E* const edge, const V* const vehicle) const {
228  if (std::find(myProhibited.begin(), myProhibited.end(), edge) != myProhibited.end()) {
229  return true;
230  }
231  return edge->prohibits(vehicle);
232  }
233 
234  void prohibit(const std::vector<E*>& toProhibit) {
235  myProhibited = toProhibit;
236  }
237 
238 protected:
239  std::vector<E*> myProhibited;
240 
241 };
242 
243 
244 #endif
245 
246 /****************************************************************************/
double getEffort(const E *const e, const V *const v, double t) const
long long int SUMOTime
Definition: SUMOTime.h:36
double(* Operation)(const E *const, const V *const, double)
Type of the function that is used to retrieve the edge effort.
double getTravelTime(const E *const e, const V *const v, const double t, const double effort) const
long long int myQueryTimeSum
Operation myTTOperation
The object&#39;s operation to perform for travel times.
EdgeInfo & operator=(const EdgeInfo &s)=delete
Invalidated assignment operator.
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...
bool visited
The previous edge.
bool isProhibited(const E *const edge, const V *const vehicle) const
double leaveTime
The time the vehicle leaves the edge.
void prohibit(const std::vector< E *> &toProhibit)
void updateViaCost(const E *const prev, const E *const e, const V *const v, double &time, double &effort, double &length) const
virtual ~SUMOAbstractRouterPermissions()
Destructor.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:49
SUMOAbstractRouter(const std::string &type, Operation operation=nullptr, Operation ttOperation=nullptr)
Constructor.
SUMOAbstractRouterPermissions(const std::string &type, typename SUMOAbstractRouter< E, V >::Operation operation=nullptr, typename SUMOAbstractRouter< E, V >::Operation ttOperation=nullptr)
Constructor.
const E * via
The optional internal edge corresponding to prev.
bool myBulkMode
whether we are currently operating several route queries in a bulk
#define STEPS2TIME(x)
Definition: SUMOTime.h:58
Operation myOperation
The object&#39;s operation to perform.
long long int myQueryStartTime
the time spent querying in milliseconds
double effort
Effort to reach the edge.
virtual ~SUMOAbstractRouter()
Destructor.
virtual bool isProhibited(const E *const, const V *const) const
long long int myNumQueries
long long int myQueryVisits
counters for performance logging
EdgeInfo(const E *const e)
Constructor.
void endQuery(int visits)
double recomputeCosts(const std::vector< const E *> &edges, const V *const v, SUMOTime msTime) const
double heuristicEffort
Estimated effort to reach the edge (effort + lower bound on remaining effort)
virtual SUMOAbstractRouter * clone()=0
static long getCurrentMillis()
Returns the current time in milliseconds.
Definition: SysUtils.cpp:39
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:242
const EdgeInfo * prev
The previous edge.
const std::string myType
the type of this router
void setBulkMode(const bool mode)
const E *const edge
The current edge.