Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2002-2026 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 : #include "RouteCostCalculator.h"
29 :
30 :
31 : // ===========================================================================
32 : // class definitions
33 : // ===========================================================================
34 : /**
35 : * @class GawronCalculator
36 : * @brief Cost calculation with Gawron's method.
37 : */
38 : template<class R, class E, class V>
39 : class GawronCalculator : public RouteCostCalculator<R, E, V> {
40 : public:
41 : /// Constructor
42 940 : GawronCalculator(const double beta, const double a) : myBeta(beta), myA(a) {}
43 :
44 : /// Destructor
45 940 : virtual ~GawronCalculator() {}
46 :
47 223538 : void setCosts(std::shared_ptr<R> route, const double costs, const bool isActive = false) const {
48 223538 : if (isActive) {
49 82677 : route->setCosts(costs);
50 : } else {
51 140861 : route->setCosts(myBeta * costs + ((double) 1.0 - myBeta) * route->getCosts());
52 : }
53 223538 : }
54 :
55 : /** @brief calculate the probabilities */
56 82677 : void calculateProbabilities(const std::vector<std::shared_ptr<R> >& alternatives, const V* const /* veh */, const SUMOTime /* time */) {
57 223538 : for (typename std::vector<std::shared_ptr<R> >::const_iterator i = alternatives.begin(); i != alternatives.end() - 1; i++) {
58 : std::shared_ptr<R> pR = *i;
59 345638 : for (typename std::vector<std::shared_ptr<R> >::const_iterator j = i + 1; j != alternatives.end(); j++) {
60 : std::shared_ptr<R> pS = *j;
61 : // see [Gawron, 1998] (4.2)
62 204777 : const double delta =
63 204777 : (pS->getCosts() - pR->getCosts()) /
64 204777 : (pS->getCosts() + pR->getCosts());
65 : // see [Gawron, 1998] (4.3a, 4.3b)
66 204777 : double newPR = gawronF(pR->getProbability(), pS->getProbability(), delta);
67 204777 : double newPS = pR->getProbability() + pS->getProbability() - newPR;
68 204777 : if (std::isnan(newPR) || std::isnan(newPS)) {
69 : newPR = pS->getCosts() > pR->getCosts()
70 0 : ? (double) 1. : 0;
71 : newPS = pS->getCosts() > pR->getCosts()
72 0 : ? 0 : (double) 1.;
73 : }
74 : newPR = MIN2((double) MAX2(newPR, (double) 0), (double) 1);
75 : newPS = MIN2((double) MAX2(newPS, (double) 0), (double) 1);
76 204777 : pR->setProbability(newPR);
77 204777 : pS->setProbability(newPS);
78 : }
79 : }
80 82677 : }
81 :
82 : private:
83 : /** @brief Performs the gawron - f() function
84 : From "Dynamic User Equilibria..." */
85 204777 : double gawronF(const double pdr, const double pds, const double x) const {
86 204777 : if (pdr * gawronG(myA, x) + pds == 0) {
87 : return std::numeric_limits<double>::max();
88 : }
89 409554 : return (pdr * (pdr + pds) * gawronG(myA, x)) /
90 204777 : (pdr * gawronG(myA, x) + pds);
91 : }
92 :
93 : /** @brief Performs the gawron - g() function
94 : From "Dynamic User Equilibria..." */
95 : double gawronG(const double a, const double x) const {
96 614331 : if (((1.0 - (x * x)) == 0)) {
97 : return std::numeric_limits<double>::max();
98 : }
99 614331 : return (double) exp((a * x) / (1.0 - (x * x)));
100 : }
101 :
102 : private:
103 : /// @brief gawron beta - value
104 : const double myBeta;
105 :
106 : /// @brief gawron a - value
107 : const double myA;
108 :
109 : private:
110 : /** @brief invalidated assignment operator */
111 : GawronCalculator& operator=(const GawronCalculator& s);
112 :
113 : };
|