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 RouteCostCalculator.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 : #include <memory>
28 : #include <cmath>
29 : #include <utils/common/StdDefs.h>
30 : #include <utils/common/SUMOTime.h>
31 : #include <utils/common/RandHelper.h>
32 : #include <utils/options/OptionsCont.h>
33 :
34 :
35 : // ===========================================================================
36 : // class definitions
37 : // ===========================================================================
38 : /**
39 : * @class RouteCostCalculator
40 : * @brief Abstract base class providing static factory method.
41 : */
42 : template<class R, class E, class V>
43 : class RouteCostCalculator {
44 : public:
45 : static RouteCostCalculator<R, E, V>& getCalculator();
46 :
47 : static void cleanup() {
48 2909 : delete myInstance;
49 2909 : myInstance = 0;
50 : }
51 :
52 : virtual void setCosts(std::shared_ptr<R> route, const double costs, const bool isActive = false) const = 0;
53 :
54 : /** @brief calculate the probabilities in the logit model */
55 : virtual void calculateProbabilities(const std::vector<std::shared_ptr<R> >& alternatives, const V* const veh, const SUMOTime time) = 0;
56 :
57 : int getMaxRouteNumber() const {
58 94389 : return myMaxRouteNumber;
59 : }
60 :
61 : bool keepAllRoutes() const {
62 94389 : return myKeepRoutes;
63 : }
64 :
65 : bool skipRouteCalculation() const {
66 186124 : return mySkipNewRoutes;
67 : }
68 :
69 : bool keepRoute() const {
70 94389 : if (myKeepRouteProb == 1) {
71 : return true;
72 94371 : } else if (myKeepRouteProb == 0) {
73 : return false;
74 : } else {
75 10060 : return RandHelper::rand() < myKeepRouteProb;
76 : }
77 : }
78 :
79 : protected:
80 : /// @brief Constructor
81 989 : RouteCostCalculator() {
82 989 : OptionsCont& oc = OptionsCont::getOptions();
83 989 : myMaxRouteNumber = oc.getInt("max-alternatives");
84 989 : myKeepRoutes = oc.getBool("keep-all-routes");
85 989 : mySkipNewRoutes = oc.getBool("skip-new-routes");
86 1974 : myKeepRouteProb = oc.exists("keep-route-probability") ? oc.getFloat("keep-route-probability") : 0;
87 989 : }
88 :
89 : /// @brief Destructor
90 : virtual ~RouteCostCalculator() {}
91 :
92 : private:
93 : static RouteCostCalculator* myInstance;
94 :
95 : /// @brief The maximum route alternatives number
96 : int myMaxRouteNumber;
97 :
98 : /// @brief Information whether all routes should be saved
99 : bool myKeepRoutes;
100 :
101 : /// @brief Information whether new routes shall be computed
102 : bool mySkipNewRoutes;
103 :
104 : /// @brief Information whether the old route shall be kept
105 : double myKeepRouteProb;
106 :
107 : };
108 :
109 :
110 : // ===========================================================================
111 : // static member definitions
112 : // ===========================================================================
113 : template<class R, class E, class V>
114 : RouteCostCalculator<R, E, V>* RouteCostCalculator<R, E, V>::myInstance = 0;
115 :
116 :
117 : #include "GawronCalculator.h"
118 : #include "LogitCalculator.h"
119 :
120 : template<class R, class E, class V>
121 815552 : RouteCostCalculator<R, E, V>& RouteCostCalculator<R, E, V>::getCalculator() {
122 815552 : if (myInstance == 0) {
123 989 : OptionsCont& oc = OptionsCont::getOptions();
124 1978 : if (oc.getString("route-choice-method") == "logit") {
125 98 : myInstance = new LogitCalculator<R, E, V>(oc.getFloat("logit.beta"), oc.getFloat("logit.gamma"), oc.getFloat("logit.theta"));
126 1880 : } else if (oc.getString("route-choice-method") == "gawron") {
127 1880 : myInstance = new GawronCalculator<R, E, V>(oc.getFloat("gawron.beta"), oc.getFloat("gawron.a"));
128 : }
129 : }
130 815552 : return *myInstance;
131 : }
|