Debian Codegraph @

unittests/unit/math/test_Random.cpp

raw
1 /*
2 * Copyright (c) 2011-2022, The DART development contributors
3 * All rights reserved.
4 *
5 * The list of contributors can be found at:
6 * https://github.com/dartsim/dart/blob/master/LICENSE
7 *
8 * This file is provided under the following "BSD-style" License:
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
11 * conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <dart/math/Random.hpp>
34 #include <gtest/gtest.h>
35
36 #include "TestHelpers.hpp"
37
38 using namespace dart;
39 using namespace dart::math;
40
41 //==============================================================================
42 TEST(Random, CheckSyntaxValidity)
43 {
44 const int rows = 5;
45 const int cols = 5;
46 const int size = 5;
47
48 short mins = -2;
49 short maxs = 3;
50 int mini = -1;
51 int maxi = 10;
52 long minl = -1l;
53 long maxl = 10l;
54 long long minll = -1ll;
55 long long maxll = 10ll;
56 unsigned short minus = 2;
57 unsigned short maxus = 3;
58 unsigned int minui = 1u;
59 unsigned int maxui = 10u;
60 unsigned long minul = 1ul;
61 unsigned long maxul = 10ul;
62 unsigned long long minull = 1ull;
63 unsigned long long maxull = 10ull;
64 float minf = -3.0f;
65 float maxf = 4.0f;
66 double mind = -5.0;
67 double maxd = 10.0;
68 long double minld = -5.0l;
69 long double maxld = 10.0l;
70
71 Eigen::VectorXi minVecXi = Eigen::VectorXi::Constant(size, mini);
72 Eigen::VectorXi maxVecXi = Eigen::VectorXi::Constant(size, maxi);
73 Eigen::VectorXf minVecXf = Eigen::VectorXf::Constant(size, minf);
74 Eigen::VectorXf maxVecXf = Eigen::VectorXf::Constant(size, maxf);
75 Eigen::VectorXd minVecXd = Eigen::VectorXd::Constant(size, mind);
76 Eigen::VectorXd maxVecXd = Eigen::VectorXd::Constant(size, maxd);
77
78 Eigen::Vector3i minVec3i = Eigen::Vector3i::Constant(mini);
79 Eigen::Vector3i maxVec3i = Eigen::Vector3i::Constant(maxi);
80 Eigen::Vector3f minVec3f = Eigen::Vector3f::Constant(minf);
81 Eigen::Vector3f maxVec3f = Eigen::Vector3f::Constant(maxf);
82 Eigen::Vector3d minVec3d = Eigen::Vector3d::Constant(mind);
83 Eigen::Vector3d maxVec3d = Eigen::Vector3d::Constant(maxd);
84
85 Eigen::MatrixXi minMatXi = Eigen::MatrixXi::Constant(rows, cols, mini);
86 Eigen::MatrixXi maxMatXi = Eigen::MatrixXi::Constant(rows, cols, maxi);
87 Eigen::MatrixXf minMatXf = Eigen::MatrixXf::Constant(rows, cols, minf);
88 Eigen::MatrixXf maxMatXf = Eigen::MatrixXf::Constant(rows, cols, maxf);
89 Eigen::MatrixXd minMatXd = Eigen::MatrixXd::Constant(rows, cols, mind);
90 Eigen::MatrixXd maxMatXd = Eigen::MatrixXd::Constant(rows, cols, maxd);
91
92 Eigen::Matrix3i minMat3i = Eigen::Matrix3i::Constant(mini);
93 Eigen::Matrix3i maxMat3i = Eigen::Matrix3i::Constant(maxi);
94 Eigen::Matrix3f minMat3f = Eigen::Matrix3f::Constant(minf);
95 Eigen::Matrix3f maxMat3f = Eigen::Matrix3f::Constant(maxf);
96 Eigen::Matrix3d minMat3d = Eigen::Matrix3d::Constant(mind);
97 Eigen::Matrix3d maxMat3d = Eigen::Matrix3d::Constant(maxd);
98
99 // -- Create random vectors without template parameters.
100 //
101 // The output type will be inferred from the arguments.
102
103 // Create random scalars given scalar bounds
104 Random::uniform(mins, maxs);
105 Random::uniform(mini, maxi);
106 Random::uniform(minl, maxl);
107 Random::uniform(minll, maxll);
108 Random::uniform(minus, maxus);
109 Random::uniform(minui, maxui);
110 Random::uniform(minul, maxul);
111 Random::uniform(minull, maxull);
112 Random::uniform(minf, maxf);
113 Random::uniform(mind, maxd);
114 Random::uniform(minld, maxld);
115
116 // Create random vectors given dynamic size vector bounds
117 Random::uniform(minVecXi, maxVecXi);
118 Random::uniform(minVecXf, maxVecXf);
119 Random::uniform(minVecXd, maxVecXd);
120
121 // Create random vectors given fixed-size vector bounds
122 Random::uniform(minVec3i, maxVec3i);
123 Random::uniform(minVec3f, maxVec3f);
124 Random::uniform(minVec3d, maxVec3d);
125
126 // Create random matrices given dynamic size matrix bounds
127 Random::uniform(minMatXi, maxMatXi);
128 Random::uniform(minMatXf, maxMatXf);
129 Random::uniform(minMatXd, maxMatXd);
130
131 // Create random matrices given fixed-size matrix bounds
132 Random::uniform(minMat3i, maxMat3i);
133 Random::uniform(minMat3f, maxMat3f);
134 Random::uniform(minMat3d, maxMat3d);
135
136 // -- Create random vectors explicitly given template parameters
137
138 // Create random scalars given scalar bounds
139 Random::uniform<short>(mins, maxs);
140 Random::uniform<int>(mini, maxi);
141 Random::uniform<long>(minl, maxl);
142 Random::uniform<long long>(minll, maxll);
143 Random::uniform<unsigned short>(minus, maxus);
144 Random::uniform<unsigned int>(minui, maxui);
145 Random::uniform<unsigned long>(minul, maxul);
146 Random::uniform<unsigned long long>(minull, maxull);
147 Random::uniform<float>(minf, maxf);
148 Random::uniform<double>(mind, maxd);
149 Random::uniform<long double>(minld, maxld);
150
151 // Create random vectors given scalar bounds
152 Random::uniform<Eigen::VectorXi>(size, mini, maxi);
153 Random::uniform<Eigen::VectorXf>(size, minf, maxf);
154 Random::uniform<Eigen::VectorXd>(size, mind, maxd);
155
156 // Create random vectors given dynamic size vector bounds
157 Random::uniform<Eigen::VectorXi>(minVecXi, maxVecXi);
158 Random::uniform<Eigen::VectorXf>(minVecXf, maxVecXf);
159 Random::uniform<Eigen::VectorXd>(minVecXd, maxVecXd);
160
161 // Create random vectors given fixed-size vector bounds
162 Random::uniform<Eigen::Vector3i>(minVec3i, maxVec3i);
163 Random::uniform<Eigen::Vector3f>(minVec3f, maxVec3f);
164 Random::uniform<Eigen::Vector3d>(minVec3d, maxVec3d);
165
166 // Create random vectors given scalar bounds
167 Random::uniform<Eigen::MatrixXi>(rows, cols, mini, maxi);
168 Random::uniform<Eigen::MatrixXf>(rows, cols, minf, maxf);
169 Random::uniform<Eigen::MatrixXd>(rows, cols, mind, maxd);
170
171 // Create random matrices given dynamic size matrix bounds
172 Random::uniform<Eigen::MatrixXi>(minMatXi, maxMatXi);
173 Random::uniform<Eigen::MatrixXf>(minMatXf, maxMatXf);
174 Random::uniform<Eigen::MatrixXd>(minMatXd, maxMatXd);
175
176 // Create random matrices given fixed-size matrix bounds
177 Random::uniform<Eigen::Matrix3i>(minMat3i, maxMat3i);
178 Random::uniform<Eigen::Matrix3f>(minMat3f, maxMat3f);
179 Random::uniform<Eigen::Matrix3d>(minMat3d, maxMat3d);
180 }
181
182 //==============================================================================
183 TEST(Random, UniformScalar)
184 {
185 double mind = -5.0;
186 double maxd = 10.0;
187
188 float minf = -3.0f;
189 float maxf = 4.0f;
190
191 int mini = -5.0;
192 int maxi = 10.0;
193
194 double uniformd = math::Random::uniform(mind, maxd);
195 EXPECT_GE(uniformd, mind);
196 EXPECT_LE(uniformd, maxd);
197
198 float uniformf = math::Random::uniform(minf, maxf);
199 EXPECT_GE(uniformf, minf);
200 EXPECT_LE(uniformf, maxf);
201
202 int uniformi = math::Random::uniform(mini, maxi);
203 EXPECT_GE(uniformi, mini);
204 EXPECT_LE(uniformi, maxi);
205 }
206
207 //==============================================================================
208 template <typename I>
209 bool testClosedEnds(int maxTry, I min = 0, I max = 2)
210 {
211 bool foundMin = false;
212 bool foundMax = false;
213
214 for (int i = 0; i < maxTry; ++i)
215 {
216 const I val = math::Random::uniform<I>(min, max);
217
218 if (val == min)
219 foundMin = true;
220
221 if (val == max)
222 foundMax = true;
223
224 if (foundMin && foundMax)
225 return true;
226 }
227
228 return false;
229 }
230
231 //==============================================================================
232 TEST(Random, UniformClosedEnd)
233 {
234 const int maxTry = 100000;
235
236 EXPECT_TRUE(testClosedEnds<short>(maxTry));
237 EXPECT_TRUE(testClosedEnds<int>(maxTry));
238 EXPECT_TRUE(testClosedEnds<long>(maxTry));
239 EXPECT_TRUE(testClosedEnds<long long>(maxTry));
240
241 EXPECT_TRUE(testClosedEnds<unsigned short>(maxTry));
242 EXPECT_TRUE(testClosedEnds<unsigned int>(maxTry));
243 EXPECT_TRUE(testClosedEnds<unsigned long>(maxTry));
244 EXPECT_TRUE(testClosedEnds<unsigned long long>(maxTry));
245
246 EXPECT_TRUE(testClosedEnds<std::size_t>(maxTry));
247 }
248
249 //==============================================================================
250 TEST(Random, NormalScalar)
251 {
252 double meand = 1.0;
253 double sigmad = 10.0;
254
255 float meanf = -3.0f;
256 float sigmaf = 4.0f;
257
258 int meani = 1;
259 int sigmai = 10;
260
261 // TODO(JS): Not sure what to test because the image of the random values
262 // is infinite in theory.
263 math::Random::normal(meand, sigmad);
264 math::Random::normal(meanf, sigmaf);
265 math::Random::normal(meani, sigmai);
266 }
267
268 //==============================================================================
269 TEST(Random, UniformVector)
270 {
271 const int vectorSize = 5;
272
273 Eigen::VectorXd mind = Eigen::VectorXd::Constant(vectorSize, -5.0);
274 Eigen::VectorXd maxd = Eigen::VectorXd::Constant(vectorSize, 10.0);
275
276 Eigen::VectorXf minf = Eigen::VectorXf::Constant(vectorSize, -3.0f);
277 Eigen::VectorXf maxf = Eigen::VectorXf::Constant(vectorSize, 4.0f);
278
279 Eigen::VectorXi mini = Eigen::VectorXi::Constant(vectorSize, -5);
280 Eigen::VectorXi maxi = Eigen::VectorXi::Constant(vectorSize, 10);
281
282 Eigen::VectorXd uniformd = math::Random::uniform(mind, maxd);
283 EXPECT_TRUE((uniformd.array() >= mind.array()).all());
284 EXPECT_TRUE((uniformd.array() <= maxd.array()).all());
285
286 Eigen::VectorXf uniformf = math::Random::uniform(minf, maxf);
287 EXPECT_TRUE((uniformf.array() >= minf.array()).all());
288 EXPECT_TRUE((uniformf.array() <= maxf.array()).all());
289
290 Eigen::VectorXi uniformi = math::Random::uniform(mini, maxi);
291 EXPECT_TRUE((uniformi.array() >= mini.array()).all());
292 EXPECT_TRUE((uniformi.array() <= maxi.array()).all());
293 }
294
295 //==============================================================================
296 TEST(Random, SetSeed)
297 {
298 unsigned int N = 10;
299
300 int min = -10;
301 int max = 10;
302
303 std::vector<int> first;
304 std::vector<int> second;
305 std::vector<int> third;
306
307 for (unsigned int i = 0; i < N; ++i)
308 {
309 math::Random::setSeed(i);
310 first.push_back(math::Random::uniform(min, max));
311 second.push_back(math::Random::uniform(min, max));
312 third.push_back(math::Random::uniform(min, max));
313 }
314
315 for (unsigned int i = 0; i < N; ++i)
316 {
317 math::Random::setSeed(i);
318 EXPECT_EQ(math::Random::getSeed(), static_cast<unsigned int>(i));
319 EXPECT_EQ(first[i], math::Random::uniform(min, max));
320 EXPECT_EQ(second[i], math::Random::uniform(min, max));
321 EXPECT_EQ(third[i], math::Random::uniform(min, max));
322 }
323 }