-
Notifications
You must be signed in to change notification settings - Fork 8
/
common.hh
331 lines (304 loc) · 11.2 KB
/
common.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright (C) 2013 by Thomas Moulard, AIST, CNRS.
//
// This file is part of the roboptim.
//
// roboptim is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// roboptim is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with roboptim. If not, see <http://www.gnu.org/licenses/>.
#ifndef ROBOPTIM_SHARED_TESTS_COMMON_HH
# define ROBOPTIM_SHARED_TESTS_COMMON_HH
# ifdef WIN32
# define _USE_MATH_DEFINES
# endif //WIN32
# include <cmath>
# include <iostream>
# include <boost/ref.hpp>
# include <boost/make_shared.hpp>
# include <boost/mpl/vector.hpp>
# include <boost/mpl/push_back.hpp>
# include <boost/make_shared.hpp>
# include <boost/shared_ptr.hpp>
# include <boost/test/test_case_template.hpp>
# include <boost/test/unit_test.hpp>
# include <boost/test/floating_point_comparison.hpp>
# include <roboptim/core/twice-differentiable-function.hh>
# include <roboptim/core/io.hh>
# include <roboptim/core/numeric-linear-function.hh>
# include <roboptim/core/linear-function.hh>
# include <roboptim/core/optimization-logger.hh>
# include <roboptim/core/solver.hh>
# include <roboptim/core/solver-factory.hh>
# include "fixture.hh"
# ifndef SOLVER_NAME
# error "please define solver name"
# endif //! PROBLEM_TYPE
# ifndef PLUGIN_PATH
# error "please define plug-in path"
# endif //! PROBLEM_TYPE
# ifndef FUNCTION_TYPE
# error "please define function type"
# endif //! PROBLEM_TYPE
# ifndef TESTS_DATA_DIR
# error "please define TESTS_DATA_DIR"
# endif //! PROBLEM_TYPE
# ifdef LOG_FILENAME
# define SET_LOG_FILE(solver) \
solver.parameters()["ipopt.output_file"].value = std::string(LOG_FILENAME); \
solver.parameters()["cmaes.output_file"].value = std::string(LOG_FILENAME); \
solver.parameters()["pagmo.output_file"].value = std::string(LOG_FILENAME); \
solver.parameters()["knitro.outdir"].value = std::string(LOG_DIR)
# else //! LOG_FILENAME
# define SET_LOG_FILE(solver)
# endif //! LOG_FILENAME
typedef FUNCTION_TYPE functionType_t;
// Define solver type.
typedef ::roboptim::Solver<functionType_t> solver_t;
typedef ::roboptim::OptimizationLogger<solver_t> logger_t;
namespace roboptim
{
boost::shared_ptr<logger_t> logger;
} // end of namespace roboptim
#define SET_OPTIMIZATION_LOGGER(SOLVER,FILENAME) \
logger = boost::make_shared<logger_t> \
(boost::ref<solver_t> (SOLVER), \
"/tmp/roboptim-shared-tests/" SOLVER_NAME \
"/" FILENAME);
#define RELEASE_OPTIMIZATION_LOGGER() \
if (logger) \
{ \
logger.reset (); \
}
// Note: tolerances here are in percent, since this is what
// Boost is expecting.
// See: http://stackoverflow.com/a/20050381/1043187
#define BOOST_CHECK_SMALL_OR_CLOSE(EXP, OBS, TOL) \
if (std::fabs (EXP) < TOL) { \
BOOST_CHECK_SMALL(OBS, TOL); \
} else { \
BOOST_CHECK_CLOSE(EXP, OBS, TOL); \
}
// Run BOOST_SMALL_OR_CLOSE and get result
#define BOOST_SMALL_OR_CLOSE_RES(EXP, OBS, TOL, RES) \
if (std::fabs (EXP) < TOL) { \
BOOST_CHECK_SMALL (OBS, TOL); \
RES = boost::test_tools::check_is_small (OBS, TOL); \
} else { \
BOOST_CHECK_CLOSE(EXP, OBS, TOL); \
RES = (std::fabs (EXP - OBS) < TOL/100. * std::fabs (EXP) \
&& std::fabs (EXP - OBS) < TOL/100. * std::fabs (OBS)); \
}
// Run BOOST_CHECK and get result
#define BOOST_CHECK_RES(COND, RES) \
BOOST_CHECK(COND); \
RES = (COND);
// Check the result of the optimization process for an unconstrained problem
#define CHECK_RESULT_UNCONSTRAINED(RESULT_TYPE) \
/* Get the result. */ \
RESULT_TYPE& result = boost::get<RESULT_TYPE> (res); \
/* Check final value. */ \
bool correct_fx = true; \
BOOST_SMALL_OR_CLOSE_RES (result.value[0], expectedResult.fx, f_tol, correct_fx); \
/* Check final bounds on x. */ \
bool correct_bounds = true; \
for (GenericFunction<functionType_t>::size_type i = 0; i < result.x.size (); ++i) { \
bool res = true; \
std::size_t ii = static_cast<std::size_t> (i); \
/* Check lower bound. */ \
BOOST_CHECK_RES (result.x[i] \
- problem.argumentBounds ()[ii].first > -x_tol, res); \
correct_bounds &= res; \
/* Check upper bound. */ \
BOOST_CHECK_RES (result.x[i] \
- problem.argumentBounds ()[ii].second < x_tol, res); \
correct_bounds &= res; \
} \
/* Only check x is we have not found an optimal result. */ \
if (!(correct_fx && correct_bounds)) { \
/* Check final x. */ \
for (GenericFunction<functionType_t>::size_type i = 0; i < result.x.size (); ++i) \
BOOST_CHECK_SMALL_OR_CLOSE (result.x[i], expectedResult.x[i], x_tol); \
} \
/* Display the result. */ \
std::cout << "A solution has been found: " << std::endl \
<< result << std::endl;
// Check the result of the optimization process
#define CHECK_RESULT(RESULT_TYPE) \
/* Get the result. */ \
RESULT_TYPE& result = boost::get<RESULT_TYPE> (res); \
/* Check final value. */ \
bool success = false; \
bool correct_fx = true; \
BOOST_SMALL_OR_CLOSE_RES (result.value[0], expectedResult.fx, f_tol, correct_fx); \
/* Check final bounds on x. */ \
bool correct_bounds = true; \
for (F<functionType_t>::size_type i = 0; i < result.x.size (); ++i) { \
bool res = true; \
std::size_t ii = static_cast<std::size_t> (i); \
/* Check lower bound. */ \
BOOST_CHECK_RES (result.x[i] \
- problem.argumentBounds ()[ii].first > -x_tol, res); \
correct_bounds &= res; \
/* Check upper bound. */ \
BOOST_CHECK_RES (result.x[i] \
- problem.argumentBounds ()[ii].second < x_tol, res); \
correct_bounds &= res; \
} \
/* Check final constraints. */ \
bool correct_g = true; \
/* Check that final constraint values have been copied to Result. */ \
F<functionType_t>::size_type n_cstr = 0; \
for (size_t i = 0; i < problem.boundsVector ().size (); ++i) { \
n_cstr += static_cast<F<functionType_t>::size_type> \
(problem.boundsVector ()[i].size ()); \
} \
BOOST_CHECK(n_cstr == static_cast<F<functionType_t>::size_type> \
(result.constraints.size ())); \
/* For each multidimensional constraint. */ \
Function::vector_t::Index cstr_i = 0; \
for (size_t i = 0; i < problem.boundsVector ().size (); ++i) { \
/* For each dimension of the constraint. */ \
for (size_t j = 0; j < problem.boundsVector ()[i].size (); ++j) { \
bool res = true; \
/* Check lower bound. */ \
BOOST_CHECK_RES (result.constraints[cstr_i] \
- problem.boundsVector ()[i][j].first > -f_tol, res); \
correct_g &= res; \
/* Check upper bound. */ \
BOOST_CHECK_RES (result.constraints[cstr_i] \
- problem.boundsVector ()[i][j].second < f_tol, res); \
correct_g &= res; \
++cstr_i; \
} \
} \
/* Only check x is we have not found an optimal result. */ \
if (!(correct_fx && correct_g && correct_bounds)) { \
success = true; \
/* Check final x. */ \
for (F<functionType_t>::size_type i = 0; i < result.x.size (); ++i) \
{ \
bool res = false; \
BOOST_SMALL_OR_CLOSE_RES (result.x[i], expectedResult.x[i], \
x_tol, res); \
success &= res; \
} \
} \
else success = true; \
if (logger) \
{ \
if (success) \
(*logger) << log_result_true; \
else \
(*logger) << log_result_false; \
(*logger) << solver; \
} \
/* Display the result. */ \
std::cout << "A solution has been found: " << std::endl \
<< result << std::endl;
// Process the result for a constrained problem
#define PROCESS_RESULT() \
std::string log_result_true = "Optimal solution found: true"; \
std::string log_result_false = "Optimal solution found: false"; \
/* Process the result */ \
switch (res.which ()) \
{ \
case solver_t::SOLVER_VALUE: \
{ \
CHECK_RESULT (Result); \
break; \
} \
case solver_t::SOLVER_NO_SOLUTION: \
{ \
std::cout << "A solution should have been found. Failing..." \
<< std::endl \
<< "No solution was found." \
<< std::endl; \
BOOST_CHECK_EQUAL (res.which (), solver_t::SOLVER_VALUE); \
if (logger) \
{ \
(*logger) << log_result_false \
<< solver; \
logger.reset (); \
} \
return; \
} \
case solver_t::SOLVER_ERROR: \
{ \
std::cout << "A solution should have been found. Failing..." \
<< std::endl \
<< boost::get<SolverError> (res).what () \
<< std::endl; \
BOOST_CHECK_EQUAL (res.which (), solver_t::SOLVER_VALUE); \
if (logger) \
{ \
(*logger) << log_result_false \
<< solver; \
logger.reset (); \
} \
return; \
} \
} \
RELEASE_OPTIMIZATION_LOGGER ();
// Process the result for an unconstrained problem
#define PROCESS_RESULT_UNCONSTRAINED() \
std::string log_result_true = "Optimal solution found: true"; \
std::string log_result_false = "Optimal solution found: false"; \
/* Process the result */ \
switch (res.which ()) \
{ \
case solver_t::SOLVER_VALUE: \
{ \
CHECK_RESULT_UNCONSTRAINED (Result); \
break; \
} \
case solver_t::SOLVER_NO_SOLUTION: \
{ \
std::cout << "A solution should have been found. Failing..." \
<< std::endl \
<< "No solution was found." \
<< std::endl; \
BOOST_CHECK_EQUAL (res.which (), solver_t::SOLVER_VALUE); \
if (logger) \
{ \
(*logger) << log_result_false \
<< solver; \
logger.reset (); \
} \
return; \
} \
case solver_t::SOLVER_ERROR: \
{ \
std::cout << "A solution should have been found. Failing..." \
<< std::endl \
<< boost::get<SolverError> (res).what () \
<< std::endl; \
BOOST_CHECK_EQUAL (res.which (), solver_t::SOLVER_VALUE); \
if (logger) \
{ \
(*logger) << log_result_false \
<< solver; \
logger.reset (); \
} \
return; \
} \
} \
RELEASE_OPTIMIZATION_LOGGER ();
namespace roboptim
{
struct ExpectedResult
{
typedef solver_t::problem_t::function_t::argument_t argument_t;
double f0;
argument_t x;
double fx;
};
} // end of namespace roboptim
#endif //! ROBOPTIM_SHARED_TESTS_COMMON_HH