Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/router/GawronCalculator.h
169678 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2002-2025 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
/****************************************************************************/
14
/// @file GawronCalculator.h
15
/// @author Daniel Krajzewicz
16
/// @author Michael Behrisch
17
/// @author Jakob Erdmann
18
/// @date Sept 2002
19
///
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
// ===========================================================================
32
/**
33
* @class GawronCalculator
34
* @brief Cost calculation with Gawron's method.
35
*/
36
template<class R, class E, class V>
37
class GawronCalculator : public RouteCostCalculator<R, E, V> {
38
public:
39
/// Constructor
40
GawronCalculator(const double beta, const double a) : myBeta(beta), myA(a) {}
41
42
/// Destructor
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
53
/** @brief calculate the probabilities */
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:
81
/** @brief Performs the gawron - f() function
82
From "Dynamic User Equilibria..." */
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
91
/** @brief Performs the gawron - g() function
92
From "Dynamic User Equilibria..." */
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:
101
/// @brief gawron beta - value
102
const double myBeta;
103
104
/// @brief gawron a - value
105
const double myA;
106
107
private:
108
/** @brief invalidated assignment operator */
109
GawronCalculator& operator=(const GawronCalculator& s);
110
111
};
112
113