SUMO - Simulation of Urban MObility
CHRouterWrapper.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Wraps multiple CHRouters for different vehicle types
20 /****************************************************************************/
21 #ifndef CHRouterWrapper_h
22 #define CHRouterWrapper_h
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <string>
35 #include <functional>
36 #include <vector>
37 #include <set>
38 #include <limits>
39 #include <algorithm>
40 #include <iterator>
41 #include <utils/common/SysUtils.h>
43 #include <utils/common/StdDefs.h>
46 #include "CHRouter.h"
47 
48 #ifdef HAVE_FOX
50 #endif
51 
52 
53 // ===========================================================================
54 // class definitions
55 // ===========================================================================
70 template<class E, class V, class PF>
71 class CHRouterWrapper: public SUMOAbstractRouter<E, V>, public PF {
72 
73 public:
75  typedef double(* Operation)(const E* const, const V* const, double);
76 
79  CHRouterWrapper(const std::vector<E*>& edges, const bool ignoreErrors, Operation operation,
80  const SUMOTime begin, const SUMOTime end, const SUMOTime weightPeriod, const int numThreads) :
81  SUMOAbstractRouter<E, V>(operation, "CHRouterWrapper"),
82  myEdges(edges),
83  myIgnoreErrors(ignoreErrors),
84  myBegin(begin),
85  myEnd(end),
86  myWeightPeriod(weightPeriod),
87  myMaxNumInstances(numThreads) {
88  }
89 
91  for (typename RouterMap::iterator i = myRouters.begin(); i != myRouters.end(); ++i) {
92  for (typename std::vector<CHRouterType*>::iterator j = i->second.begin(); j != i->second.end(); ++j) {
93  delete *j;
94  }
95  }
96  }
97 
98 
101  for (typename RouterMap::iterator i = myRouters.begin(); i != myRouters.end(); ++i) {
102  for (typename std::vector<CHRouterType*>::iterator j = i->second.begin(); j != i->second.end(); ++j) {
103  clone->myRouters[i->first].push_back(static_cast<CHRouterType*>((*j)->clone()));
104  }
105  }
106  return clone;
107  }
108 
109 
110  bool compute(const E* from, const E* to, const V* const vehicle,
111  SUMOTime msTime, std::vector<const E*>& into) {
112  const std::pair<const SUMOVehicleClass, const double> svc = std::make_pair(vehicle->getVClass(), vehicle->getMaxSpeed());
113  int index = 0;
114  int numIntervals = 1;
115 #ifdef HAVE_FOX
116  if (myMaxNumInstances >= 2 && myEnd < std::numeric_limits<int>::max()) {
117  index = (int)((msTime - myBegin) / myWeightPeriod);
118  numIntervals = (int)((myEnd - myBegin) / myWeightPeriod);
119  if (numIntervals > 0) {
120  while ((int)myThreadPool.size() < myMaxNumInstances) {
121  new FXWorkerThread(myThreadPool);
122  }
123  } else {
124  // this covers the cases of negative (unset) end time and unset weight period (no weight file)
125  numIntervals = 1;
126  }
127  }
128 #endif
129  if (myRouters.count(svc) == 0) {
130  // create new router for the given permissions and maximum speed
131  // XXX a new router may also be needed if vehicles differ in speed factor
132  for (int i = 0; i < numIntervals; i++) {
133  myRouters[svc].push_back(new CHRouterType(
134  myEdges, myIgnoreErrors, &E::getTravelTimeStatic, svc.first, myWeightPeriod, false));
135 #ifdef HAVE_FOX
136  if (myThreadPool.size() > 0) {
137  myThreadPool.add(new ComputeHierarchyTask(myRouters[svc].back(), vehicle, myBegin + i * myWeightPeriod));
138  }
139 #endif
140  }
141 #ifdef HAVE_FOX
142  if (myThreadPool.size() > 0) {
143  myThreadPool.waitAll();
144  }
145 #endif
146  }
147  return myRouters[svc][index]->compute(from, to, vehicle, msTime, into);
148  }
149 
150 
151  double recomputeCosts(const std::vector<const E*>& edges,
152  const V* const v, SUMOTime msTime) const {
153  const double time = STEPS2TIME(msTime);
154  double costs = 0;
155  for (typename std::vector<const E*>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
156  if (PF::operator()(*i, v)) {
157  WRITE_WARNING("Vehicle '" + v->getID() + "' is restricted from using its assigned route.");
158  return -1;
159  }
160  costs += this->getEffort(*i, v, time + costs);
161  }
162  return costs;
163  }
164 
165 
166 private:
168 
169 #ifdef HAVE_FOX
170 private:
171  class ComputeHierarchyTask : public FXWorkerThread::Task {
172  public:
173  ComputeHierarchyTask(CHRouterType* router, const V* const vehicle, const SUMOTime msTime)
174  : myRouter(router), myVehicle(vehicle), myStartTime(msTime) {}
175  void run(FXWorkerThread* /* context */) {
176  myRouter->buildContractionHierarchy(myStartTime, myVehicle);
177  }
178  private:
179  CHRouterType* myRouter;
180  const V* const myVehicle;
181  const SUMOTime myStartTime;
182  private:
184  ComputeHierarchyTask& operator=(const ComputeHierarchyTask&);
185  };
186 
187 
188 private:
190  FXWorkerThread::Pool myThreadPool;
191 #endif
192 
193 private:
194  typedef std::map<std::pair<const SUMOVehicleClass, const double>, std::vector<CHRouterType*> > RouterMap;
195 
196  RouterMap myRouters;
197 
199  const std::vector<E*>& myEdges;
200 
201  const bool myIgnoreErrors;
202 
206  const int myMaxNumInstances;
207 };
208 
209 
210 #endif
211 
212 /****************************************************************************/
213 
Computes the shortest path through a contracted network.
Definition: CHRouter.h:69
const SUMOTime myEnd
double getEffort(const E *const e, const V *const v, double t) const
CHRouterWrapper(const std::vector< E *> &edges, const bool ignoreErrors, Operation operation, const SUMOTime begin, const SUMOTime end, const SUMOTime weightPeriod, const int numThreads)
Constructor.
bool 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 WRITE_WARNING(msg)
Definition: MsgHandler.h:199
const SUMOTime myWeightPeriod
double(* Operation)(const E *const, const V *const, double)
Type of the function that is used to retrieve the edge effort.
#define STEPS2TIME(x)
Definition: SUMOTime.h:64
Operation myOperation
The object&#39;s operation to perform.
SUMOAbstractRouter & operator=(const SUMOAbstractRouter &s)
Invalidated assignment operator.
const int myMaxNumInstances
const bool myIgnoreErrors
A pool of worker threads which distributes the tasks and collects the results.
CHRouter< E, V, noProhibitions< E, V > > CHRouterType
const SUMOTime myBegin
double recomputeCosts(const std::vector< const E *> &edges, const V *const v, SUMOTime msTime) const
const std::vector< E * > & myEdges
all edges with numerical ids
Abstract superclass of a task to be run with an index to keep track of pending tasks.
virtual SUMOAbstractRouter< E, V > * clone()
A thread repeatingly calculating incoming tasks.
long long int SUMOTime
Definition: TraCIDefs.h:51
std::map< std::pair< const SUMOVehicleClass, const double >, std::vector< CHRouterType * > > RouterMap
Computes the shortest path through a contracted network.