Line data Source code
1 : /*
2 : * File: TestDistanceToBox.cpp
3 : * Author: Pantelis Sopasakis
4 : *
5 : * Created on Oct 28, 2015, 7:28:27 PM
6 : *
7 : * ForBES is free software: you can redistribute it and/or modify
8 : * it under the terms of the GNU Lesser General Public License as published by
9 : * the Free Software Foundation, either version 3 of the License, or
10 : * (at your option) any later version.
11 : *
12 : * ForBES is distributed in the hope that it will be useful,
13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : * GNU Lesser General Public License for more details.
16 : *
17 : * You should have received a copy of the GNU Lesser General Public License
18 : * along with ForBES. If not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #include "TestDistanceToBox.h"
22 : #include "DistanceToBox.h"
23 :
24 :
25 1 : CPPUNIT_TEST_SUITE_REGISTRATION(TestDistanceToBox);
26 :
27 3 : TestDistanceToBox::TestDistanceToBox() {
28 3 : }
29 :
30 6 : TestDistanceToBox::~TestDistanceToBox() {
31 6 : }
32 :
33 3 : void TestDistanceToBox::setUp() {
34 3 : std::srand(time(NULL));
35 3 : }
36 :
37 3 : void TestDistanceToBox::tearDown() {
38 3 : }
39 :
40 1 : void TestDistanceToBox::testCall() {
41 :
42 1 : Matrix LB(2, 1);
43 2 : Matrix UB(2, 1);
44 :
45 1 : LB[0] = -1.0;
46 1 : UB[0] = 1.0;
47 :
48 1 : LB[1] = -2.0;
49 1 : UB[1] = 5.0;
50 :
51 2 : Matrix weights(2, 1);
52 1 : weights[0] = 0.5;
53 1 : weights[1] = 2.0;
54 :
55 1 : Function * d2b = new DistanceToBox(&LB, &UB, &weights);
56 :
57 2 : Matrix x(2, 1);
58 1 : x[0] = -1.5;
59 1 : x[1] = 6;
60 :
61 : double f;
62 : double f2;
63 2 : Matrix grad(2, 1);
64 1 : _ASSERT(d2b->category().defines_f());
65 1 : _ASSERT(d2b->category().defines_grad());
66 1 : int status = d2b->call(x, f, grad);
67 1 : _ASSERT_EQ(ForBESUtils::STATUS_OK, status);
68 1 : status = d2b->call(x, f2);
69 1 : _ASSERT_EQ(ForBESUtils::STATUS_OK, status);
70 :
71 1 : const double tol = 1e-12;
72 1 : _ASSERT_NUM_EQ(f, f2, tol);
73 1 : _ASSERT_NUM_EQ(1.06250, f, tol);
74 1 : _ASSERT_NUM_EQ(-0.25, grad[0], tol);
75 1 : _ASSERT_NUM_EQ(2.0, grad[1], tol);
76 :
77 2 : delete d2b;
78 1 : }
79 :
80 1 : void TestDistanceToBox::testCall2() {
81 1 : Matrix LB(2, 1);
82 2 : Matrix UB(2, 1);
83 1 : LB[0] = -1.0;
84 1 : UB[0] = 1.0;
85 1 : LB[1] = -2.0;
86 1 : UB[1] = 5.0;
87 :
88 1 : double w = 10.5;
89 2 : Matrix W(2, 1);
90 1 : W[0] = w;
91 1 : W[1] = w;
92 :
93 1 : Function * d2b = new DistanceToBox(&LB, &UB, w);
94 1 : Function * d2b_ = new DistanceToBox(&LB, &UB, &W);
95 :
96 2 : Matrix x(2, 1);
97 1 : x[0] = -1.5;
98 1 : x[1] = 6;
99 :
100 : double f, f2;
101 1 : const double tol = 1e-12;
102 :
103 2 : Matrix grad(2, 1);
104 2 : Matrix grad2(2, 1);
105 1 : _ASSERT(d2b->category().defines_f());
106 1 : _ASSERT(d2b->category().defines_grad());
107 1 : int status = d2b->call(x, f, grad);
108 1 : _ASSERT_EQ(ForBESUtils::STATUS_OK, status);
109 1 : _ASSERT_EQ(ForBESUtils::STATUS_OK, d2b_ ->call(x, f2, grad2));
110 1 : _ASSERT_NUM_EQ(f, f2, tol);
111 1 : _ASSERT_EQ(grad, grad2);
112 :
113 1 : delete d2b;
114 2 : delete d2b_;
115 1 : }
116 :
117 1 : void TestDistanceToBox::testCall3() {
118 1 : double lb = -1.1;
119 1 : double ub = 1.2;
120 1 : double w = 0.985;
121 1 : Function * db = new DistanceToBox(lb, ub, w);
122 :
123 1 : const size_t n = 10;
124 :
125 1 : Matrix lb_vec(n, 1);
126 2 : Matrix ub_vec(n, 1);
127 2 : Matrix w_vec(n, 1);
128 :
129 11 : for (size_t i = 0; i < n; i++) {
130 10 : lb_vec[i] = lb;
131 10 : ub_vec[i] = ub;
132 10 : w_vec[i] = w;
133 : }
134 :
135 1 : Function * db_ = new DistanceToBox(&lb_vec, &ub_vec, &w_vec);
136 :
137 1 : const double tol = 1e-12;
138 :
139 51 : for (size_t r = 0; r < 50; r++) {
140 : double f;
141 : double f_;
142 : int status;
143 50 : Matrix x = MatrixFactory::MakeRandomMatrix(n, 1, -1.0, 2.0);
144 50 : status = db->call(x, f);
145 50 : _ASSERT(ForBESUtils::is_status_ok(status));
146 50 : status = db_->call(x, f_);
147 50 : _ASSERT(ForBESUtils::is_status_ok(status));
148 50 : _ASSERT_NUM_EQ(f, f_, tol);
149 :
150 51 : }
151 :
152 4 : }
153 :
|