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:
49  typedef double(* Operation)(const E* const, const V* const, double);
50 
52  SUMOAbstractRouter(const std::string& type, Operation operation = nullptr, Operation ttOperation = nullptr) :
53  myOperation(operation), myTTOperation(ttOperation),
54  myBulkMode(false),
55  myType(type),
56  myQueryVisits(0),
57  myNumQueries(0),
59  myQueryTimeSum(0) {
60  }
61 
63  virtual ~SUMOAbstractRouter() {
64  if (myNumQueries > 0) {
65  WRITE_MESSAGE(myType + " answered " + toString(myNumQueries) + " queries and explored " + toString(double(myQueryVisits) / myNumQueries) + " edges on average.");
66  WRITE_MESSAGE(myType + " spent " + toString(myQueryTimeSum) + "ms answering queries (" + toString(double(myQueryTimeSum) / myNumQueries) + "ms on average).");
67  }
68  }
69 
70  virtual SUMOAbstractRouter* clone() = 0;
71 
74  virtual bool compute(const E* from, const E* to, const V* const vehicle,
75  SUMOTime msTime, std::vector<const E*>& into) = 0;
76 
77  virtual bool isProhibited(const E* const /* edge */, const V* const /* vehicle */) const {
78  return false;
79  }
80 
81  inline double getTravelTime(const E* const e, const V* const v, const double t, const double effort) const {
82  return myTTOperation == nullptr ? effort : (*myTTOperation)(e, v, t);
83  }
84 
85  inline void updateViaCost(const E* const prev, const E* const e, const V* const v, double& time, double& effort) const {
86  if (prev != nullptr) {
87  for (const std::pair<const E*, const E*>& follower : prev->getViaSuccessors()) {
88  if (follower.first == e) {
89  const E* viaEdge = follower.second;
90  while (viaEdge != nullptr && viaEdge != e) {
91  const double viaEffortDelta = this->getEffort(viaEdge, v, time);
92  time += getTravelTime(viaEdge, v, time, viaEffortDelta);
93  effort += viaEffortDelta;
94  viaEdge = viaEdge->getViaSuccessors().front().first;
95  }
96  break;
97  }
98  }
99  }
100  const double effortDelta = this->getEffort(e, v, time);
101  effort += effortDelta;
102  time += getTravelTime(e, v, time, effortDelta);
103  }
104 
105 
106  inline double recomputeCosts(const std::vector<const E*>& edges, const V* const v, SUMOTime msTime) const {
107  double effort = 0.;
108  double time = STEPS2TIME(msTime);
109  const E* prev = nullptr;
110  for (const E* const e : edges) {
111  if (isProhibited(e, v)) {
112  return -1;
113  }
114  updateViaCost(prev, e, v, time, effort);
115  prev = e;
116  }
117  return effort;
118  }
119 
120 
121  inline double getEffort(const E* const e, const V* const v, double t) const {
122  return (*myOperation)(e, v, t);
123  }
124 
125  inline void startQuery() {
126  myNumQueries++;
128  }
129 
130  inline void endQuery(int visits) {
131  myQueryVisits += visits;
133  }
134 
135  void setBulkMode(const bool mode) {
136  myBulkMode = mode;
137  }
138 
139 protected:
142 
145 
148 
149 private:
151  const std::string myType;
152 
154  long long int myQueryVisits;
155  long long int myNumQueries;
157  long long int myQueryStartTime;
158  long long int myQueryTimeSum;
159 private:
162 };
163 
164 
165 template<class E, class V>
167 public:
169  SUMOAbstractRouterPermissions(const std::string& type, typename SUMOAbstractRouter<E, V>::Operation operation = nullptr, typename SUMOAbstractRouter<E, V>::Operation ttOperation = nullptr) :
170  SUMOAbstractRouter<E, V>(type, operation, ttOperation) {
171  }
172 
175  }
176 
177  bool isProhibited(const E* const edge, const V* const vehicle) const {
178  if (std::find(myProhibited.begin(), myProhibited.end(), edge) != myProhibited.end()) {
179  return true;
180  }
181  return edge->prohibits(vehicle);
182  }
183 
184  void prohibit(const std::vector<E*>& toProhibit) {
185  myProhibited = toProhibit;
186  }
187 
188 protected:
189  std::vector<E*> myProhibited;
190 
191 };
192 
193 
194 #endif
195 
196 /****************************************************************************/
197 
void updateViaCost(const E *const prev, const E *const e, const V *const v, double &time, double &effort) const
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.
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 isProhibited(const E *const edge, const V *const vehicle) const
void prohibit(const std::vector< E *> &toProhibit)
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.
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
SUMOAbstractRouter & operator=(const SUMOAbstractRouter &s)
Invalidated assignment operator.
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
void endQuery(int visits)
double recomputeCosts(const std::vector< const E *> &edges, const V *const v, SUMOTime msTime) const
virtual SUMOAbstractRouter * clone()=0
static long getCurrentMillis()
Returns the current time in milliseconds.
Definition: SysUtils.cpp:39
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:243
const std::string myType
the type of this router
void setBulkMode(const bool mode)