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