PX4 Firmware
PX4 Autopilot Software http://px4.io
test_search_min.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2012-2019 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF 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 
34 /**
35  * @file test_search_min.c
36  * Tests arithmetic search algorithms.
37  */
38 
39 #include <unit_test.h>
40 #include <float.h>
41 #include <math.h>
42 
44 
45 // linear function
46 float _linear_function(float x)
47 {
48  float slope = 2.0f;
49  return slope * x - 1.4f;
50 
51 }
52 
53 //linear function without slope
54 float _linear_function_flat(float x)
55 {
56  return 1.4f;
57 }
58 
59 // quadratic function with min at 2
60 float _quadratic_function(float x)
61 {
62  return ((x - 2.0f) * (x - 2.0f) + 3.0f);
63 }
64 
65 class SearchMinTest : public UnitTest
66 {
67 public:
68  virtual bool run_tests();
69 
70 private:
71  bool _init_inputs();
72  bool _init_inputs_flipped();
73  bool _init_inputs_negative();
76  bool _no_extremum();
77 
78 };
79 
80 
82 {
89 
90  return (_tests_failed == 0);
91 }
92 
94 {
95  float a = 1.0f;
96  float b = 4.0f;
97  float tol = 0.001f;
98  float (*fun)(float);
99  float (*fun2)(float);
100 
101  fun = &_linear_function;
102  fun2 = &_quadratic_function;
103 
104  float opt = math::goldensection(a, b, fun, tol);
105  float opt2 = math::goldensection(a, b, fun2, tol);
106  ut_assert("linear function opt not equal min ", fabsf(opt - a) <= (tol * 2.0f));
107  ut_assert("quad function opt not equal min ", fabsf(opt2 - 2.0f) <= (tol * 2.0f));
108 
109  return true;
110 }
111 
113 {
114  float a = 4.0f;
115  float b = 1.0f;
116  float tol = 0.001f;
117  float (*fun)(float);
118  float (*fun2)(float);
119 
120  fun = &_linear_function;
121  fun2 = &_quadratic_function;
122 
123  float opt = math::goldensection(a, b, fun, tol);
124  float opt2 = math::goldensection(a, b, fun2, tol);
125 
126  ut_assert("linear function opt not equal min", fabsf(opt - b) <= (tol * 2.0f));
127  ut_assert("quad function opt not equal min ", fabsf(opt2 - 2.0f) <= (tol * 2.0f));
128 
129  return true;
130 }
131 
133 {
134  float a = -4.0f;
135  float b = -2.0f;
136  float tol = 0.001f;
137  float (*fun)(float);
138  float (*fun2)(float);
139 
140  fun = &_linear_function;
141  fun2 = &_quadratic_function;
142 
143  float opt = math::goldensection(a, b, fun, tol);
144  float opt2 = math::goldensection(a, b, fun2, tol);
145 
146  ut_assert("linear function opt not equal min", fabsf(opt - a) <= (tol * 2.0f));
147  ut_assert("quad function opt not equal min ", fabsf(opt2 - b) <= (tol * 2.0f));
148 
149  return true;
150 }
151 
153 {
154  float a = 1.0f;
155  float b = 4.0f;
156  float tol = 6.0f;
157  float (*fun)(float);
158  float (*fun2)(float);
159 
160  fun = &_linear_function;
161  fun2 = &_quadratic_function;
162 
163  float opt = math::goldensection(a, b, fun, tol);
164  float opt2 = math::goldensection(a, b, fun2, tol);
165 
166  ut_assert("linear function opt not equal min", fabsf(opt - (b + a) / 2.0f) <= (0.001f * 2.0f));
167  ut_assert("quad function opt not equal min ", fabsf(opt2 - (b + a) / 2.0f) <= (0.001f * 2.0f));
168 
169  return true;
170 }
171 
173 {
174  float a = 4.0f;
175  float b = 1.0f;
176  float tol = 6.0f;
177  float (*fun)(float);
178  float (*fun2)(float);
179 
180  fun = &_linear_function;
181  fun2 = &_quadratic_function;
182 
183  float opt = math::goldensection(a, b, fun, tol);
184  float opt2 = math::goldensection(a, b, fun2, tol);
185 
186  ut_assert("linear function opt not equal min", fabsf(opt - (b + a) / 2.0f) <= (0.001f * 2.0f));
187  ut_assert("quad function opt not equal min ", fabsf(opt2 - (b + a) / 2.0f) <= (0.001f * 2.0f));
188 
189  return true;
190 }
191 
193 {
194  float a = 1.f;
195  float b = 4.0f;
196  float tol = 0.001f;
197  float (*fun)(float);
198  fun = &_linear_function_flat;
199 
200  float opt = math::goldensection(a, b, fun, tol);
201  ut_assert("linear function function opt not equal min", fabsf(fun(opt) - fun(b)) <= (tol));
202 
203  return true;
204 }
205 
bool _init_inputs_negative()
bool _init_tol_larger_than_range_flipped()
#define ut_declare_test_c(test_function, test_class)
Definition: unit_test.h:40
float _linear_function(float x)
Base class to be used for unit tests.
Definition: unit_test.h:54
bool _init_inputs_flipped()
float _quadratic_function(float x)
const _Tp goldensection(const _Tp &arg1, const _Tp &arg2, _Tp(*fun)(_Tp), const _Tp &tol)
Definition: SearchMin.hpp:56
int _tests_failed
The number of unit tests which failed.
Definition: unit_test.h:206
bool _init_tol_larger_than_range()
#define ut_assert(message, test)
Used to assert a value within a unit test.
Definition: unit_test.h:113
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
float _linear_function_flat(float x)
virtual bool run_tests()
Override to run your unit tests.
#define ut_run_test(test)
Runs a single unit test.
Definition: unit_test.h:96
int test_search_min(int argc, char *argv[])