Eclipse 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-2019 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 /****************************************************************************/
16 // Calculators for route costs and probabilities
17 /****************************************************************************/
18 #ifndef LogitCalculator_h
19 #define LogitCalculator_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <vector>
28 #include <map>
29 
30 
31 // ===========================================================================
32 // class definitions
33 // ===========================================================================
38 template<class R, class E, class V>
39 class LogitCalculator : public RouteCostCalculator<R, E, V> {
40 public:
42  LogitCalculator(const double beta, const double gamma,
43  const double theta) : myBeta(beta), myGamma(gamma), myTheta(theta) {}
44 
46  virtual ~LogitCalculator() {}
47 
48  void setCosts(R* route, const double costs, const bool /* isActive */) const {
49  route->setCosts(costs);
50  }
51 
53  void calculateProbabilities(std::vector<R*> alternatives, const V* const veh, const SUMOTime time) {
54  const double theta = myTheta >= 0 ? myTheta : getThetaForCLogit(alternatives);
55  const double beta = myBeta >= 0 ? myBeta : getBetaForCLogit(alternatives);
56  if (beta > 0) {
57  // calculate commonalities
58  for (typename std::vector<R*>::const_iterator i = alternatives.begin(); i != alternatives.end(); i++) {
59  const R* pR = *i;
60  double lengthR = 0;
61  const std::vector<const E*>& edgesR = pR->getEdgeVector();
62  for (typename std::vector<const E*>::const_iterator edge = edgesR.begin(); edge != edgesR.end(); ++edge) {
63  //@todo we should use costs here
64  lengthR += (*edge)->getTravelTime(veh, STEPS2TIME(time));
65  }
66  double overlapSum = 0;
67  for (typename std::vector<R*>::const_iterator j = alternatives.begin(); j != alternatives.end(); j++) {
68  const R* pS = *j;
69  double overlapLength = 0.;
70  double lengthS = 0;
71  const std::vector<const E*>& edgesS = pS->getEdgeVector();
72  for (typename std::vector<const E*>::const_iterator edge = edgesS.begin(); edge != edgesS.end(); ++edge) {
73  lengthS += (*edge)->getTravelTime(veh, STEPS2TIME(time));
74  if (std::find(edgesR.begin(), edgesR.end(), *edge) != edgesR.end()) {
75  overlapLength += (*edge)->getTravelTime(veh, STEPS2TIME(time));
76  }
77  }
78  overlapSum += pow(overlapLength / sqrt(lengthR * lengthS), myGamma);
79  }
80  myCommonalities[pR] = beta * log(overlapSum);
81  }
82  }
83  for (typename std::vector<R*>::iterator i = alternatives.begin(); i != alternatives.end(); i++) {
84  R* pR = *i;
85  double weightedSum = 0;
86  for (typename std::vector<R*>::iterator j = alternatives.begin(); j != alternatives.end(); j++) {
87  R* pS = *j;
88  weightedSum += exp(theta * (pR->getCosts() - pS->getCosts() + myCommonalities[pR] - myCommonalities[pS]));
89  }
90  pR->setProbability(1. / weightedSum);
91  }
92  }
93 
94 
95 private:
97  double getBetaForCLogit(const std::vector<R*> alternatives) const {
98  double min = std::numeric_limits<double>::max();
99  for (typename std::vector<R*>::const_iterator i = alternatives.begin(); i != alternatives.end(); i++) {
100  const double cost = (*i)->getCosts() / 3600.;
101  if (cost < min) {
102  min = cost;
103  }
104  }
105  return min;
106  }
107 
109  double getThetaForCLogit(const std::vector<R*> alternatives) const {
110  // @todo this calculation works for travel times only
111  double sum = 0.;
112  double diff = 0.;
113  double min = std::numeric_limits<double>::max();
114  for (typename std::vector<R*>::const_iterator i = alternatives.begin(); i != alternatives.end(); i++) {
115  const double cost = (*i)->getCosts() / 3600.;
116  sum += cost;
117  if (cost < min) {
118  min = cost;
119  }
120  }
121  const double meanCost = sum / double(alternatives.size());
122  for (typename std::vector<R*>::const_iterator i = alternatives.begin(); i != alternatives.end(); i++) {
123  diff += pow((*i)->getCosts() / 3600. - meanCost, 2);
124  }
125  const double cvCost = sqrt(diff / double(alternatives.size())) / meanCost;
126  // @todo re-evaluate function
127  // if (cvCost > 0.04) { // Magic numbers from Lohse book
128  return 3.1415926535897932384626433832795 / (sqrt(6.) * cvCost * (min + 1.1)) / 3600.;
129  // }
130  // return 1./3600.;
131  }
132 
133 
134 private:
136  const double myBeta;
137 
139  const double myGamma;
140 
142  const double myTheta;
143 
145  std::map<const R*, double> myCommonalities;
146 
147 private:
150 
151 };
152 
153 
154 #endif
155 
156 /****************************************************************************/
157 
LogitCalculator::operator=
LogitCalculator & operator=(const LogitCalculator &s)
invalidated assignment operator
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
LogitCalculator::getBetaForCLogit
double getBetaForCLogit(const std::vector< R * > alternatives) const
calculate the scaling factor in the logit model
Definition: LogitCalculator.h:97
LogitCalculator
Cost calculation with c-logit or logit method.
Definition: LogitCalculator.h:39
LogitCalculator::setCosts
void setCosts(R *route, const double costs, const bool) const
Definition: LogitCalculator.h:48
RouteCostCalculator
Abstract base class providing static factory method.
Definition: RouteCostCalculator.h:43
LogitCalculator::getThetaForCLogit
double getThetaForCLogit(const std::vector< R * > alternatives) const
calculate the scaling factor in the logit model
Definition: LogitCalculator.h:109
STEPS2TIME
#define STEPS2TIME(x)
Definition: SUMOTime.h:56
LogitCalculator::myGamma
const double myGamma
logit gamma - value
Definition: LogitCalculator.h:139
LogitCalculator::LogitCalculator
LogitCalculator(const double beta, const double gamma, const double theta)
Constructor.
Definition: LogitCalculator.h:42
LogitCalculator::calculateProbabilities
void calculateProbabilities(std::vector< R * > alternatives, const V *const veh, const SUMOTime time)
calculate the probabilities in the logit model
Definition: LogitCalculator.h:53
LogitCalculator::myCommonalities
std::map< const R *, double > myCommonalities
The route commonality factors for c-logit.
Definition: LogitCalculator.h:145
LogitCalculator::myTheta
const double myTheta
logit theta - value
Definition: LogitCalculator.h:142
config.h
LogitCalculator::myBeta
const double myBeta
logit beta - value
Definition: LogitCalculator.h:136
LogitCalculator::~LogitCalculator
virtual ~LogitCalculator()
Destructor.
Definition: LogitCalculator.h:46