Line data Source code
1 : /*
2 : * File: TestDistanceToBall2.cpp
3 : * Author: chung
4 : *
5 : * Created on Jan 14, 2016, 4:32:13 PM
6 : */
7 :
8 : #include "TestDistanceToBall2.h"
9 :
10 :
11 1 : CPPUNIT_TEST_SUITE_REGISTRATION(TestDistanceToBall2);
12 :
13 7 : TestDistanceToBall2::TestDistanceToBall2() {
14 7 : }
15 :
16 14 : TestDistanceToBall2::~TestDistanceToBall2() {
17 14 : }
18 :
19 7 : void TestDistanceToBall2::setUp() {
20 7 : }
21 :
22 7 : void TestDistanceToBall2::tearDown() {
23 7 : }
24 :
25 1 : void TestDistanceToBall2::testCall() {
26 1 : Function * d2b = new DistanceToBall2();
27 1 : Matrix x(2, 1);
28 1 : x[0] = 1.0;
29 1 : x[1] = 1.0;
30 :
31 1 : double b = std::pow(M_SQRT2 - 1, 2) / 2.0;
32 : double f;
33 1 : int status = d2b -> call(x, f);
34 1 : _ASSERT(ForBESUtils::is_status_ok(status));
35 1 : _ASSERT_NUM_EQ(b, f, 1e-10);
36 :
37 : double y_values[10] = {
38 : 0.706046088019609,
39 : 0.031832846377421,
40 : 0.276922984960890,
41 : 0.046171390631154,
42 : 0.097131781235848,
43 : 0.823457828327293,
44 : 0.694828622975817,
45 : 0.317099480060861,
46 : 0.950222048838355,
47 : 0.034446080502909
48 1 : };
49 2 : Matrix y(10, 1, y_values);
50 1 : b = 0.217342378005960;
51 1 : status = d2b -> call(y, f);
52 1 : _ASSERT(ForBESUtils::is_status_ok(status));
53 1 : _ASSERT_NUM_EQ(b, f, 1e-8);
54 :
55 2 : delete d2b;
56 1 : }
57 :
58 1 : void TestDistanceToBall2::testCall2() {
59 1 : Function * d2b = new DistanceToBall2(5.4312);
60 : double x_values[10] = {
61 : 0.438744359656398,
62 : 0.381558457093008,
63 : 0.765516788149002,
64 : 0.795199901137063,
65 : 0.186872604554379,
66 : 0.489764395788231,
67 : 0.445586200710899,
68 : 0.646313010111265,
69 : 0.709364830858073,
70 : 0.754686681982361
71 1 : };
72 1 : Matrix x(10, 1, x_values);
73 1 : double expected = 2.084995711340361;
74 : double f;
75 1 : int status = d2b->call(x, f);
76 1 : _ASSERT(ForBESUtils::is_status_ok(status));
77 1 : _ASSERT_NUM_EQ(expected, f, 1e-8);
78 1 : delete d2b;
79 1 : }
80 :
81 1 : void TestDistanceToBall2::testCall3() {
82 1 : Function * d2b = new DistanceToBall2(0.998, 2.54);
83 : double x_values[10] = {
84 : 0.877488719312796,
85 : 0.763116914186016,
86 : 1.531033576298004,
87 : 1.590399802274126,
88 : 0.373745209108758,
89 : 0.979528791576462,
90 : 0.891172401421798,
91 : 1.292626020222530,
92 : 1.418729661716146,
93 : 1.509373363964722
94 1 : };
95 1 : Matrix x(10, 1, x_values);
96 1 : double expected = 0.733567206618426;
97 : double f;
98 1 : int status = d2b->call(x, f);
99 1 : _ASSERT(ForBESUtils::is_status_ok(status));
100 1 : _ASSERT_NUM_EQ(expected, f, 1e-8);
101 1 : delete d2b;
102 1 : }
103 :
104 1 : void TestDistanceToBall2::testCall4() {
105 : double x_values[10] = {
106 : 0.877488719312796,
107 : 0.763116914186016,
108 : 1.531033576298004,
109 : 1.590399802274126,
110 : 0.373745209108758,
111 : 0.979528791576462,
112 : 0.891172401421798,
113 : 1.292626020222530,
114 : 1.418729661716146,
115 : 1.509373363964722
116 1 : };
117 1 : Matrix x(10, 1, x_values);
118 :
119 : double c_values[10] = {
120 : -0.863652821988714,
121 : 0.077359091130425,
122 : -1.214117043615409,
123 : -1.113500741486764,
124 : -0.006849328103348,
125 : 1.532630308284750,
126 : -0.769665913753682,
127 : 0.371378812760058,
128 : -0.225584402271252,
129 : 1.117356138814467
130 1 : };
131 :
132 :
133 2 : Matrix c_shallow = MatrixFactory::ShallowVector(c_values, 10, 0);
134 1 : Function * d2b = new DistanceToBall2(0.998, 2.54, c_shallow);
135 :
136 1 : double expected = 3.084754993825916;
137 : double f;
138 1 : int status = d2b->call(x, f);
139 1 : _ASSERT(ForBESUtils::is_status_ok(status));
140 1 : _ASSERT_NUM_EQ(expected, f, 1e-8);
141 2 : delete d2b;
142 1 : }
143 :
144 1 : void TestDistanceToBall2::testCall5() {
145 : double x_values[10] = {
146 : 0.877488719312796,
147 : 0.763116914186016,
148 : 1.531033576298004,
149 : 1.590399802274126,
150 : 0.373745209108758,
151 : 0.979528791576462,
152 : 0.891172401421798,
153 : 1.292626020222530,
154 : 1.418729661716146,
155 : 1.509373363964722
156 1 : };
157 1 : Matrix x(10, 1, x_values);
158 2 : Matrix c(x);
159 1 : double rho = 3.23;
160 1 : double w = 0.887;
161 11 : for (size_t i = 0; i < 10; i++) {
162 10 : c[i] += rho / 11.0;
163 : }
164 :
165 1 : Function * d2b = new DistanceToBall2(w, rho, c);
166 1 : double expected = 0.0;
167 : double f;
168 1 : int status = d2b->call(x, f);
169 1 : _ASSERT(ForBESUtils::is_status_ok(status));
170 1 : _ASSERT_NUM_EQ(expected, f, 1e-8);
171 2 : delete d2b;
172 1 : }
173 :
174 1 : void TestDistanceToBall2::testGradient() {
175 1 : Function * d2b = new DistanceToBall2(5.4312);
176 : double x_values[10] = {
177 : 0.438744359656398,
178 : 0.381558457093008,
179 : 0.765516788149002,
180 : 0.795199901137063,
181 : 0.186872604554379,
182 : 0.489764395788231,
183 : 0.445586200710899,
184 : 0.646313010111265,
185 : 0.709364830858073,
186 : 0.754686681982361
187 1 : };
188 1 : Matrix x(10, 1, x_values);
189 2 : Matrix grad(10, 1);
190 :
191 : double grad_expected_values[10] = {
192 : 1.112859194860052,
193 : 0.967809221946716,
194 : 1.941705637375089,
195 : 2.016995779558804,
196 : 0.473995600555761,
197 : 1.242269670645696,
198 : 1.130213277162638,
199 : 1.639349567076561,
200 : 1.799278229238064,
201 : 1.914235464907696
202 :
203 1 : };
204 2 : Matrix grad_expected(10, 1, grad_expected_values);
205 1 : double expected = 2.084995711340361;
206 : double f;
207 1 : int status = d2b->call(x, f, grad);
208 1 : _ASSERT(ForBESUtils::is_status_ok(status));
209 1 : _ASSERT_NUM_EQ(expected, f, 1e-8);
210 1 : _ASSERT_EQ(grad_expected, grad);
211 :
212 2 : delete d2b;
213 1 : }
214 :
215 1 : void TestDistanceToBall2::testGradient2() {
216 : double x_values[10] = {
217 : 0.438744359656398,
218 : 0.381558457093008,
219 : 0.765516788149002,
220 : 0.795199901137063,
221 : 0.186872604554379,
222 : 0.489764395788231,
223 : 0.445586200710899,
224 : 0.646313010111265,
225 : 0.709364830858073,
226 : 0.754686681982361
227 1 : };
228 1 : Matrix x(10, 1, x_values);
229 :
230 : double c_values[10] = {
231 : -1.089064295052236,
232 : 0.032557464164973,
233 : 0.552527021112224,
234 : 1.100610217880866,
235 : 1.544211895503951,
236 : 0.085931133175425,
237 : -1.491590310637609,
238 : -0.742301837259857,
239 : -1.061581733319986,
240 : 2.350457224002042
241 1 : };
242 2 : Matrix c_shallow = MatrixFactory::ShallowVector(c_values, 10, 0);
243 :
244 : double grad_expected_values[10] = {
245 : 0.555435626832365,
246 : 0.126879491534933,
247 : 0.077432539996649,
248 : -0.111032078656484,
249 : -0.493461401445295,
250 : 0.146813791546350,
251 : 0.704261522900583,
252 : 0.504831646162784,
253 : 0.643828539607569,
254 : -0.580143206124434
255 :
256 1 : };
257 2 : Matrix grad_expected(10, 1, grad_expected_values);
258 :
259 1 : Function * d2b = new DistanceToBall2(0.998, 2.54, c_shallow);
260 2 : Matrix grad(10, 1);
261 1 : double expected = 1.057069197096058;
262 : double f;
263 1 : int status = d2b->call(x, f, grad);
264 1 : _ASSERT(ForBESUtils::is_status_ok(status));
265 1 : _ASSERT_NUM_EQ(expected, f, 1e-8);
266 1 : _ASSERT_EQ(grad_expected, grad);
267 2 : delete d2b;
268 4 : }
|