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