SUMO - Simulation of Urban MObility
CHRouterWrapper.h
Go to the documentation of this file.
1 /****************************************************************************/
9 // Wraps multiple CHRouters for different vehicle types
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2017 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 CHRouterWrapper_h
23 #define CHRouterWrapper_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 <string>
36 #include <functional>
37 #include <vector>
38 #include <set>
39 #include <limits>
40 #include <algorithm>
41 #include <iterator>
42 #include <utils/common/SysUtils.h>
44 #include <utils/common/StdDefs.h>
47 #include "CHRouter.h"
48 
49 #ifdef HAVE_FOX
51 #endif
52 
53 
54 // ===========================================================================
55 // class definitions
56 // ===========================================================================
71 template<class E, class V, class PF>
72 class CHRouterWrapper: public SUMOAbstractRouter<E, V>, public PF {
73 
74 public:
76  typedef double(* Operation)(const E* const, const V* const, double);
77 
80  CHRouterWrapper(const std::vector<E*>& edges, const bool ignoreErrors, Operation operation,
81  const SUMOTime begin, const SUMOTime end, const SUMOTime weightPeriod, const int numThreads) :
82  SUMOAbstractRouter<E, V>(operation, "CHRouterWrapper"),
83  myEdges(edges),
84  myIgnoreErrors(ignoreErrors),
85  myBegin(begin),
86  myEnd(end),
87  myWeightPeriod(weightPeriod),
88  myMaxNumInstances(numThreads) {
89  }
90 
92  for (typename RouterMap::iterator i = myRouters.begin(); i != myRouters.end(); ++i) {
93  for (typename std::vector<CHRouterType*>::iterator j = i->second.begin(); j != i->second.end(); ++j) {
94  delete *j;
95  }
96  }
97  }
98 
99 
102  for (typename RouterMap::iterator i = myRouters.begin(); i != myRouters.end(); ++i) {
103  for (typename std::vector<CHRouterType*>::iterator j = i->second.begin(); j != i->second.end(); ++j) {
104  clone->myRouters[i->first].push_back(static_cast<CHRouterType*>((*j)->clone()));
105  }
106  }
107  return clone;
108  }
109 
110 
111  bool compute(const E* from, const E* to, const V* const vehicle,
112  SUMOTime msTime, std::vector<const E*>& into) {
113  const std::pair<const SUMOVehicleClass, const double> svc = std::make_pair(vehicle->getVClass(), vehicle->getMaxSpeed());
114  int index = 0;
115  int numIntervals = 1;
116 #ifdef HAVE_FOX
118  index = (int)((msTime - myBegin) / myWeightPeriod);
119  numIntervals = (int)((myEnd - myBegin) / myWeightPeriod);
120  if (numIntervals > 0) {
121  while ((int)myThreadPool.size() < myMaxNumInstances) {
122  new FXWorkerThread(myThreadPool);
123  }
124  } else {
125  // this covers the cases of negative (unset) end time and unset weight period (no weight file)
126  numIntervals = 1;
127  }
128  }
129 #endif
130  if (myRouters.count(svc) == 0) {
131  // create new router for the given permissions and maximum speed
132  // XXX a new router may also be needed if vehicles differ in speed factor
133  for (int i = 0; i < numIntervals; i++) {
134  myRouters[svc].push_back(new CHRouterType(
135  myEdges, myIgnoreErrors, &E::getTravelTimeStatic, svc.first, myWeightPeriod, false));
136 #ifdef HAVE_FOX
137  if (myThreadPool.size() > 0) {
138  myThreadPool.add(new ComputeHierarchyTask(myRouters[svc].back(), vehicle, myBegin + i * myWeightPeriod));
139  }
140 #endif
141  }
142 #ifdef HAVE_FOX
143  if (myThreadPool.size() > 0) {
144  myThreadPool.waitAll();
145  }
146 #endif
147  }
148  return myRouters[svc][index]->compute(from, to, vehicle, msTime, into);
149  }
150 
151 
152  double recomputeCosts(const std::vector<const E*>& edges,
153  const V* const v, SUMOTime msTime) const {
154  const double time = STEPS2TIME(msTime);
155  double costs = 0;
156  for (typename std::vector<const E*>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
157  if (PF::operator()(*i, v)) {
158  WRITE_WARNING("Vehicle '" + v->getID() + "' is restricted from using its assigned route.");
159  return -1;
160  }
161  costs += this->getEffort(*i, v, time + costs);
162  }
163  return costs;
164  }
165 
166 
167 private:
169 
170 #ifdef HAVE_FOX
171 private:
172  class ComputeHierarchyTask : public FXWorkerThread::Task {
173  public:
174  ComputeHierarchyTask(CHRouterType* router, const V* const vehicle, const SUMOTime msTime)
175  : myRouter(router), myVehicle(vehicle), myStartTime(msTime) {}
176  void run(FXWorkerThread* /* context */) {
177  myRouter->buildContractionHierarchy(myStartTime, myVehicle);
178  }
179  private:
180  CHRouterType* myRouter;
181  const V* const myVehicle;
182  const SUMOTime myStartTime;
183  private:
185  ComputeHierarchyTask& operator=(const ComputeHierarchyTask&);
186  };
187 
188 
189 private:
191  FXWorkerThread::Pool myThreadPool;
192 #endif
193 
194 private:
195  typedef std::map<std::pair<const SUMOVehicleClass, const double>, std::vector<CHRouterType*> > RouterMap;
196 
197  RouterMap myRouters;
198 
200  const std::vector<E*>& myEdges;
201 
202  const bool myIgnoreErrors;
203 
207  const int myMaxNumInstances;
208 };
209 
210 
211 #endif
212 
213 /****************************************************************************/
214 
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:200
#define max(a, b)
Definition: polyfonts.c:65
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:65
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:52
std::map< std::pair< const SUMOVehicleClass, const double >, std::vector< CHRouterType * > > RouterMap
Computes the shortest path through a contracted network.