Eclipse SUMO - Simulation of Urban MObility
GawronCalculator.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2002-2024 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // Calculators for route costs and probabilities
21 /****************************************************************************/
22 #pragma once
23 #include <config.h>
24 
25 #include <vector>
26 #include <map>
27 
28 
29 // ===========================================================================
30 // class definitions
31 // ===========================================================================
36 template<class R, class E, class V>
37 class GawronCalculator : public RouteCostCalculator<R, E, V> {
38 public:
40  GawronCalculator(const double beta, const double a) : myBeta(beta), myA(a) {}
41 
43  virtual ~GawronCalculator() {}
44 
45  void setCosts(R* route, const double costs, const bool isActive = false) const {
46  if (isActive) {
47  route->setCosts(costs);
48  } else {
49  route->setCosts(myBeta * costs + ((double) 1.0 - myBeta) * route->getCosts());
50  }
51  }
52 
54  void calculateProbabilities(std::vector<R*> alternatives, const V* const /* veh */, const SUMOTime /* time */) {
55  for (typename std::vector<R*>::iterator i = alternatives.begin(); i != alternatives.end() - 1; i++) {
56  R* pR = *i;
57  for (typename std::vector<R*>::iterator j = i + 1; j != alternatives.end(); j++) {
58  R* pS = *j;
59  // see [Gawron, 1998] (4.2)
60  const double delta =
61  (pS->getCosts() - pR->getCosts()) /
62  (pS->getCosts() + pR->getCosts());
63  // see [Gawron, 1998] (4.3a, 4.3b)
64  double newPR = gawronF(pR->getProbability(), pS->getProbability(), delta);
65  double newPS = pR->getProbability() + pS->getProbability() - newPR;
66  if (std::isnan(newPR) || std::isnan(newPS)) {
67  newPR = pS->getCosts() > pR->getCosts()
68  ? (double) 1. : 0;
69  newPS = pS->getCosts() > pR->getCosts()
70  ? 0 : (double) 1.;
71  }
72  newPR = MIN2((double) MAX2(newPR, (double) 0), (double) 1);
73  newPS = MIN2((double) MAX2(newPS, (double) 0), (double) 1);
74  pR->setProbability(newPR);
75  pS->setProbability(newPS);
76  }
77  }
78  }
79 
80 private:
83  double gawronF(const double pdr, const double pds, const double x) const {
84  if (pdr * gawronG(myA, x) + pds == 0) {
85  return std::numeric_limits<double>::max();
86  }
87  return (pdr * (pdr + pds) * gawronG(myA, x)) /
88  (pdr * gawronG(myA, x) + pds);
89  }
90 
93  double gawronG(const double a, const double x) const {
94  if (((1.0 - (x * x)) == 0)) {
95  return std::numeric_limits<double>::max();
96  }
97  return (double) exp((a * x) / (1.0 - (x * x)));
98  }
99 
100 private:
102  const double myBeta;
103 
105  const double myA;
106 
107 private:
110 
111 };
long long int SUMOTime
Definition: GUI.h:35
T MIN2(T a, T b)
Definition: StdDefs.h:76
T MAX2(T a, T b)
Definition: StdDefs.h:82
Cost calculation with Gawron's method.
GawronCalculator & operator=(const GawronCalculator &s)
invalidated assignment operator
virtual ~GawronCalculator()
Destructor.
GawronCalculator(const double beta, const double a)
Constructor.
const double myA
gawron a - value
void calculateProbabilities(std::vector< R * > alternatives, const V *const, const SUMOTime)
calculate the probabilities
void setCosts(R *route, const double costs, const bool isActive=false) const
double gawronF(const double pdr, const double pds, const double x) const
Performs the gawron - f() function From "Dynamic User Equilibria...".
double gawronG(const double a, const double x) const
Performs the gawron - g() function From "Dynamic User Equilibria...".
const double myBeta
gawron beta - value
Abstract base class providing static factory method.