Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-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 Distribution_Parameterized.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Michael Behrisch
17 : /// @date Sept 2002
18 : ///
19 : // A distribution described by parameters such as the mean value and std-dev
20 : /****************************************************************************/
21 : #include <config.h>
22 :
23 : #include <cassert>
24 : #include <utils/common/RandHelper.h>
25 : #include <utils/common/StringTokenizer.h>
26 : #include <utils/common/ToString.h>
27 : #include <utils/common/StringUtils.h>
28 : #include <utils/common/MsgHandler.h>
29 :
30 : #include "Distribution_Parameterized.h"
31 :
32 :
33 : // ===========================================================================
34 : // method definitions
35 : // ===========================================================================
36 :
37 415332 : Distribution_Parameterized::Distribution_Parameterized() :
38 830664 : Distribution("") {
39 415332 : }
40 :
41 : /// @brief Constructor for any temporary distribution parsed directly from the description
42 3105109 : Distribution_Parameterized::Distribution_Parameterized(const std::string& description) :
43 6210218 : Distribution("") {
44 3105109 : myParameter = {0., 0.};
45 3105109 : parse(description, true);
46 3105109 : }
47 :
48 :
49 269 : Distribution_Parameterized::Distribution_Parameterized(const std::string& id, double mean, double deviation) :
50 269 : Distribution(id) {
51 269 : myParameter.push_back(mean);
52 269 : myParameter.push_back(deviation);
53 269 : }
54 :
55 :
56 745869 : Distribution_Parameterized::Distribution_Parameterized(const std::string& id, double mean, double deviation, double min, double max) :
57 745869 : Distribution(id) {
58 745869 : myParameter.push_back(mean);
59 745869 : myParameter.push_back(deviation);
60 745869 : myParameter.push_back(min);
61 745869 : myParameter.push_back(max);
62 745869 : }
63 :
64 :
65 5566477 : Distribution_Parameterized::~Distribution_Parameterized() {}
66 :
67 :
68 : void
69 3107629 : Distribution_Parameterized::parse(const std::string& description, const bool hardFail) {
70 : try {
71 3107629 : const std::string distName = description.substr(0, description.find('('));
72 3107629 : if (distName == "norm" || distName == "normc") {
73 562 : const std::vector<std::string> params = StringTokenizer(description.substr(distName.size() + 1, description.size() - distName.size() - 2), ',').getVector();
74 562 : myParameter.resize(params.size());
75 : std::transform(params.begin(), params.end(), myParameter.begin(), StringUtils::toDoubleAllowTime);
76 562 : setID(distName);
77 562 : } else {
78 3107067 : myParameter[0] = StringUtils::toDoubleAllowTime(description);
79 : }
80 3107629 : if (myParameter.size() == 1) {
81 5 : myParameter.push_back(0.);
82 : }
83 0 : } catch (...) {
84 : // set default distribution parameterized
85 0 : myParameter = {0., 0.};
86 0 : if (hardFail) {
87 0 : throw ProcessError(TL("Invalid format of distribution parameterized"));
88 : } else {
89 0 : WRITE_ERROR(TL("Invalid format of distribution parameterized"));
90 : }
91 0 : }
92 3107629 : }
93 :
94 :
95 : bool
96 23372 : Distribution_Parameterized::isValidDescription(const std::string& description, std::string& error) {
97 : try {
98 23372 : Distribution_Parameterized dummy(description);
99 23372 : error = dummy.isValid();
100 23372 : if (error == "") {
101 : return true;
102 : }
103 23372 : } catch (...) {
104 0 : error = TL("Invalid format of distribution parameterized");
105 0 : }
106 : return false;
107 : }
108 :
109 :
110 : double
111 8958672 : Distribution_Parameterized::sample(SumoRNG* which) const {
112 8958672 : if (myParameter.empty()) {
113 0 : throw ProcessError(TL("Attempted to sample from dummy distribution"));
114 : }
115 8958672 : if (myParameter[1] <= 0.) {
116 5716371 : return myParameter[0];
117 : }
118 3242301 : double val = RandHelper::randNorm(myParameter[0], myParameter[1], which);
119 3242301 : if (myParameter.size() > 2) {
120 3242227 : const double min = myParameter[2];
121 3242227 : const double max = getMax();
122 3274079 : while (val < min || val > max) {
123 31852 : val = RandHelper::randNorm(myParameter[0], myParameter[1], which);
124 : }
125 : }
126 : return val;
127 : }
128 :
129 :
130 : double
131 3325263 : Distribution_Parameterized::getMax() const {
132 3325263 : if (myParameter.empty()) {
133 : return std::numeric_limits<double>::infinity();
134 : }
135 3325263 : if (myParameter[1] <= 0.) {
136 62272 : return myParameter[0];
137 : }
138 3262991 : return myParameter.size() > 3 ? myParameter[3] : std::numeric_limits<double>::infinity();
139 : }
140 :
141 :
142 : double
143 83036 : Distribution_Parameterized::getMin() const {
144 83036 : if (myParameter.empty()) {
145 : return -std::numeric_limits<double>::infinity();
146 : }
147 83036 : if (myParameter[1] <= 0.) {
148 62272 : return myParameter[0];
149 : }
150 20764 : return myParameter.size() > 2 ? myParameter[2] : -std::numeric_limits<double>::infinity();
151 : }
152 :
153 :
154 : void
155 543893 : Distribution_Parameterized::setParameter(const int index, const double value) {
156 543893 : if (index >= (int)myParameter.size()) {
157 0 : throw ProcessError(TLF("Attempted to set parameter index % of distribut '%' with % parameters", index, myID, myParameter.size()));
158 : }
159 543893 : myParameter[index] = value;
160 543893 : }
161 :
162 :
163 : std::string
164 484 : Distribution_Parameterized::toStr(std::streamsize accuracy) const {
165 484 : if (myParameter.empty()) {
166 0 : return "";
167 484 : } else if (myParameter[1] < 0) {
168 : // only write simple speedFactor
169 106 : return toString(myParameter[0]);
170 : } else {
171 : return (myParameter[1] == 0.
172 876 : ? myID + "(" + toString(myParameter[0], accuracy) + "," + toString(myParameter[1], accuracy) + ")"
173 1810 : : myID + "(" + joinToString(myParameter, ",", accuracy) + ")");
174 : }
175 : }
176 :
177 :
178 : const std::string
179 3147161 : Distribution_Parameterized::isValid() const {
180 3147161 : if (myParameter.empty()) {
181 0 : return "Dummy Distribution (invalid)";
182 3147161 : } else if (myParameter.size() >= 2 && myParameter[1] > 0.) {
183 6413 : if (getMin() > getMax()) {
184 0 : return TLF("minimum value % larger than maximum %", getMin(), getMax());
185 : }
186 6413 : if (getMin() > myParameter[0] + 3 * myParameter[1]) {
187 0 : return TLF("minimum value % too large for distribution with mean % and deviation %", myParameter[2], myParameter[0], myParameter[1]);
188 : }
189 6413 : if (getMax() < myParameter[0] - 3 * myParameter[1]) {
190 26 : return TLF("maximum value % too small for distribution with mean % and deviation %", myParameter[3], myParameter[0], myParameter[1]);
191 : }
192 6400 : if (myParameter.size() > 3 && myParameter[3] - myParameter[2] < NUMERICAL_EPS * myParameter[1]) {
193 12 : return TLF("maximum value % and minimum value % too close for distribution with mean % and deviation %", myParameter[3], myParameter[2], myParameter[0], myParameter[1]);
194 : }
195 : }
196 3147142 : return "";
197 : }
198 :
199 :
200 : /****************************************************************************/
|