SUMO - Simulation of Urban MObility
LogitCalculator.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-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 // Calculators for route costs and probabilities
20 /****************************************************************************/
21 #ifndef LogitCalculator_h
22 #define LogitCalculator_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 <vector>
35 #include <map>
36 
37 
38 // ===========================================================================
39 // class definitions
40 // ===========================================================================
45 template<class R, class E, class V>
46 class LogitCalculator : public RouteCostCalculator<R, E, V> {
47 public:
49  LogitCalculator(const double beta, const double gamma,
50  const double theta) : myBeta(beta), myGamma(gamma), myTheta(theta) {}
51 
53  virtual ~LogitCalculator() {}
54 
55  void setCosts(R* route, const double costs, const bool /* isActive */) const {
56  route->setCosts(costs);
57  }
58 
60  void calculateProbabilities(std::vector<R*> alternatives, const V* const veh, const SUMOTime time) {
61  const double theta = myTheta >= 0 ? myTheta : getThetaForCLogit(alternatives);
62  const double beta = myBeta >= 0 ? myBeta : getBetaForCLogit(alternatives);
63  if (beta > 0) {
64  // calculate commonalities
65  for (typename std::vector<R*>::const_iterator i = alternatives.begin(); i != alternatives.end(); i++) {
66  const R* pR = *i;
67  double lengthR = 0;
68  const std::vector<const E*>& edgesR = pR->getEdgeVector();
69  for (typename std::vector<const E*>::const_iterator edge = edgesR.begin(); edge != edgesR.end(); ++edge) {
70  //@todo we should use costs here
71  lengthR += (*edge)->getTravelTime(veh, STEPS2TIME(time));
72  }
73  double overlapSum = 0;
74  for (typename std::vector<R*>::const_iterator j = alternatives.begin(); j != alternatives.end(); j++) {
75  const R* pS = *j;
76  double overlapLength = 0.;
77  double lengthS = 0;
78  const std::vector<const E*>& edgesS = pS->getEdgeVector();
79  for (typename std::vector<const E*>::const_iterator edge = edgesS.begin(); edge != edgesS.end(); ++edge) {
80  lengthS += (*edge)->getTravelTime(veh, STEPS2TIME(time));
81  if (std::find(edgesR.begin(), edgesR.end(), *edge) != edgesR.end()) {
82  overlapLength += (*edge)->getTravelTime(veh, STEPS2TIME(time));
83  }
84  }
85  overlapSum += pow(overlapLength / sqrt(lengthR * lengthS), myGamma);
86  }
87  myCommonalities[pR] = beta * log(overlapSum);
88  }
89  }
90  for (typename std::vector<R*>::iterator i = alternatives.begin(); i != alternatives.end(); i++) {
91  R* pR = *i;
92  double weightedSum = 0;
93  for (typename std::vector<R*>::iterator j = alternatives.begin(); j != alternatives.end(); j++) {
94  R* pS = *j;
95  weightedSum += exp(theta * (pR->getCosts() - pS->getCosts() + myCommonalities[pR] - myCommonalities[pS]));
96  }
97  pR->setProbability(1. / weightedSum);
98  }
99  }
100 
101 
102 private:
104  double getBetaForCLogit(const std::vector<R*> alternatives) const {
105  double min = std::numeric_limits<double>::max();
106  for (typename std::vector<R*>::const_iterator i = alternatives.begin(); i != alternatives.end(); i++) {
107  const double cost = (*i)->getCosts() / 3600.;
108  if (cost < min) {
109  min = cost;
110  }
111  }
112  return min;
113  }
114 
116  double getThetaForCLogit(const std::vector<R*> alternatives) const {
117  // @todo this calculation works for travel times only
118  double sum = 0.;
119  double diff = 0.;
120  double min = std::numeric_limits<double>::max();
121  for (typename std::vector<R*>::const_iterator i = alternatives.begin(); i != alternatives.end(); i++) {
122  const double cost = (*i)->getCosts() / 3600.;
123  sum += cost;
124  if (cost < min) {
125  min = cost;
126  }
127  }
128  const double meanCost = sum / double(alternatives.size());
129  for (typename std::vector<R*>::const_iterator i = alternatives.begin(); i != alternatives.end(); i++) {
130  diff += pow((*i)->getCosts() / 3600. - meanCost, 2);
131  }
132  const double cvCost = sqrt(diff / double(alternatives.size())) / meanCost;
133  // @todo re-evaluate function
134  // if (cvCost > 0.04) { // Magic numbers from Lohse book
135  return 3.1415926535897932384626433832795 / (sqrt(6.) * cvCost * (min + 1.1)) / 3600.;
136  // }
137  // return 1./3600.;
138  }
139 
140 
141 private:
143  const double myBeta;
144 
146  const double myGamma;
147 
149  const double myTheta;
150 
152  std::map<const R*, double> myCommonalities;
153 
154 private:
157 
158 };
159 
160 
161 #endif
162 
163 /****************************************************************************/
164 
Cost calculation with c-logit or logit method.
std::map< const R *, double > myCommonalities
The route commonality factors for c-logit.
void calculateProbabilities(std::vector< R *> alternatives, const V *const veh, const SUMOTime time)
calculate the probabilities in the logit model
double getThetaForCLogit(const std::vector< R *> alternatives) const
calculate the scaling factor in the logit model
LogitCalculator(const double beta, const double gamma, const double theta)
Constructor.
void setCosts(R *route, const double costs, const bool) const
#define STEPS2TIME(x)
Definition: SUMOTime.h:64
Abstract base class providing static factory method.
const double myTheta
logit theta - value
const double myGamma
logit gamma - value
virtual ~LogitCalculator()
Destructor.
double getBetaForCLogit(const std::vector< R *> alternatives) const
calculate the scaling factor in the logit model
long long int SUMOTime
Definition: TraCIDefs.h:51
const double myBeta
logit beta - value
LogitCalculator & operator=(const LogitCalculator &s)
invalidated assignment operator