testfunctions.hpp 9.39 KB
Newer Older
1
2
3
4
//| Copyright Inria May 2015
//| This project has received funding from the European Research Council (ERC) under
//| the European Union's Horizon 2020 research and innovation programme (grant
//| agreement No 637972) - see http://www.resibots.eu
5
//|
6
7
8
9
10
11
//| Contributor(s):
//|   - Jean-Baptiste Mouret (jean-baptiste.mouret@inria.fr)
//|   - Antoine Cully (antoinecully@gmail.com)
//|   - Kontantinos Chatzilygeroudis (konstantinos.chatzilygeroudis@inria.fr)
//|   - Federico Allocati (fede.allocati@gmail.com)
//|   - Vaios Papaspyros (b.papaspyros@gmail.com)
Konstantinos Chatzilygeroudis's avatar
Konstantinos Chatzilygeroudis committed
12
//|   - Roberto Rama (bertoski@gmail.com)
13
//|
14
15
16
17
18
//| This software is a computer library whose purpose is to optimize continuous,
//| black-box functions. It mainly implements Gaussian processes and Bayesian
//| optimization.
//| Main repository: http://github.com/resibots/limbo
//| Documentation: http://www.resibots.eu/limbo
19
//|
20
21
22
23
24
//| This software is governed by the CeCILL-C license under French law and
//| abiding by the rules of distribution of free software.  You can  use,
//| modify and/ or redistribute the software under the terms of the CeCILL-C
//| license as circulated by CEA, CNRS and INRIA at the following URL
//| "http://www.cecill.info".
25
//|
26
27
28
29
30
//| As a counterpart to the access to the source code and  rights to copy,
//| modify and redistribute granted by the license, users are provided only
//| with a limited warranty  and the software's author,  the holder of the
//| economic rights,  and the successive licensors  have only  limited
//| liability.
31
//|
32
33
34
35
36
37
38
39
40
41
//| In this respect, the user's attention is drawn to the risks associated
//| with loading,  using,  modifying and/or developing or reproducing the
//| software by the user in light of its specific status of free software,
//| that may mean  that it is complicated to manipulate,  and  that  also
//| therefore means  that it is reserved for developers  and  experienced
//| professionals having in-depth computer knowledge. Users are therefore
//| encouraged to load and test the software's suitability as regards their
//| requirements in conditions enabling the security of their systems and/or
//| data to be ensured and,  more generally, to use and operate it in the
//| same conditions as regards security.
42
//|
43
44
//| The fact that you are presently reading this means that you have had
//| knowledge of the CeCILL-C license and that you accept its terms.
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
#define _USE_MATH_DEFINES
#include <Eigen/Core>

// support functions
inline double sign(double x)
{
    if (x < 0)
        return -1;
    if (x > 0)
        return 1;
    return 0;
}

inline double sqr(double x)
{
    return x * x;
};

inline double hat(double x)
{
    if (x != 0)
        return log(fabs(x));
    return 0;
}

inline double c1(double x)
{
    if (x > 0)
        return 10;
    return 5.5;
}

inline double c2(double x)
{
    if (x > 0)
        return 7.9;
    return 3.1;
}

inline Eigen::VectorXd t_osz(const Eigen::VectorXd& x)
{
    Eigen::VectorXd r = x;
    for (int i = 0; i < x.size(); i++)
        r(i) = sign(x(i)) * exp(hat(x(i)) + 0.049 * sin(c1(x(i)) * hat(x(i))) + sin(c2(x(i)) * hat(x(i))));
    return r;
}

struct Sphere {
94
95
    BO_PARAM(size_t, dim_in, 2);
    BO_PARAM(size_t, dim_out, 1);
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

    double operator()(const Eigen::VectorXd& x) const
    {
        Eigen::VectorXd opt(2);
        opt << 0.5, 0.5;

        return (x - opt).squaredNorm();
    }

    Eigen::MatrixXd solutions() const
    {
        Eigen::MatrixXd sols(1, 2);
        sols << 0.5, 0.5;
        return sols;
    }
};

struct Ellipsoid {
114
115
    BO_PARAM(size_t, dim_in, 2);
    BO_PARAM(size_t, dim_out, 1);
116
117
118
119
120
121
122

    double operator()(const Eigen::VectorXd& x) const
    {
        Eigen::VectorXd opt(2);
        opt << 0.5, 0.5;
        Eigen::VectorXd z = t_osz(x - opt);
        double r = 0;
123
124
        for (size_t i = 0; i < dim_in(); ++i)
            r += std::pow(10, ((double)i) / (dim_in() - 1.0)) * z(i) * z(i) + 1;
125
126
127
128
129
130
131
132
133
134
135
136
        return r;
    }

    Eigen::MatrixXd solutions() const
    {
        Eigen::MatrixXd sols(1, 2);
        sols << 0.5, 0.5;
        return sols;
    }
};

struct Rastrigin {
137
138
    BO_PARAM(size_t, dim_in, 4);
    BO_PARAM(size_t, dim_out, 1);
139
140
141

    double operator()(const Eigen::VectorXd& x) const
    {
142
143
144
        double f = 10 * dim_in();
        for (size_t i = 0; i < dim_in(); ++i)
            f += x(i) * x(i) - 10 * std::cos(2 * M_PI * x(i));
145
        return f;
146
147
148
149
    }

    Eigen::MatrixXd solutions() const
    {
150
151
152
153
        Eigen::MatrixXd sols(1, 4);
        for (size_t i = 0; i < 4; ++i)
            sols(0, i) = 0;
        return sols;
154
155
156
157
    }
};

// see : http://www.sfu.ca/~ssurjano/hart3.html
158
struct Hartmann3 {
159
160
    BO_PARAM(size_t, dim_in, 3);
    BO_PARAM(size_t, dim_out, 1);
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

    double operator()(const Eigen::VectorXd& x) const
    {
        Eigen::MatrixXd a(4, 3);
        Eigen::MatrixXd p(4, 3);
        a << 3.0, 10, 30, 0.1, 10, 35, 3.0, 10, 30, 0.1, 10, 36;
        p << 0.3689, 0.1170, 0.2673, 0.4699, 0.4387, 0.7470, 0.1091, 0.8732, 0.5547,
            0.0382, 0.5743, 0.8828;
        Eigen::VectorXd alpha(4);
        alpha << 1.0, 1.2, 3.0, 3.2;

        double res = 0;
        for (int i = 0; i < 4; i++) {
            double s = 0.0f;
            for (size_t j = 0; j < 3; j++) {
                s += a(i, j) * (x(j) - p(i, j)) * (x(j) - p(i, j));
            }
            res += alpha(i) * exp(-s);
        }
        return -res;
    }

    Eigen::MatrixXd solutions() const
    {
        Eigen::MatrixXd sols(1, 3);
        sols << 0.114614, 0.555649, 0.852547;
        return sols;
    }
};

// see : http://www.sfu.ca/~ssurjano/hart6.html
192
struct Hartmann6 {
193
194
    BO_PARAM(size_t, dim_in, 6);
    BO_PARAM(size_t, dim_out, 1);
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

    double operator()(const Eigen::VectorXd& x) const
    {
        Eigen::MatrixXd a(4, 6);
        Eigen::MatrixXd p(4, 6);
        a << 10, 3, 17, 3.5, 1.7, 8, 0.05, 10, 17, 0.1, 8, 14, 3, 3.5, 1.7, 10, 17,
            8, 17, 8, 0.05, 10, 0.1, 14;
        p << 0.1312, 0.1696, 0.5569, 0.0124, 0.8283, 0.5886, 0.2329, 0.4135, 0.8307,
            0.3736, 0.1004, 0.9991, 0.2348, 0.1451, 0.3522, 0.2883, 0.3047, 0.665,
            0.4047, 0.8828, 0.8732, 0.5743, 0.1091, 0.0381;

        Eigen::VectorXd alpha(4);
        alpha << 1.0, 1.2, 3.0, 3.2;

        double res = 0;
        for (int i = 0; i < 4; i++) {
            double s = 0.0f;
            for (size_t j = 0; j < 6; j++) {
                s += a(i, j) * sqr(x(j) - p(i, j));
            }
            res += alpha(i) * exp(-s);
        }
        return -res;
    }

    Eigen::MatrixXd solutions() const
    {
        Eigen::MatrixXd sols(1, 6);
        sols << 0.20169, 0.150011, 0.476874, 0.275332, 0.311652, 0.6573;
        return sols;
    }
};

// see : http://www.sfu.ca/~ssurjano/goldpr.html
// (with ln, as suggested in Jones et al.)
230
struct GoldsteinPrice {
231
232
    BO_PARAM(size_t, dim_in, 2);
    BO_PARAM(size_t, dim_out, 1);
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252

    double operator()(const Eigen::VectorXd& xx) const
    {
        Eigen::VectorXd x = (4.0 * xx);
        x(0) -= 2.0;
        x(1) -= 2.0;
        double r = (1 + (x(0) + x(1) + 1) * (x(0) + x(1) + 1) * (19 - 14 * x(0) + 3 * x(0) * x(0) - 14 * x(1) + 6 * x(0) * x(1) + 3 * x(1) * x(1))) * (30 + (2 * x(0) - 3 * x(1)) * (2 * x(0) - 3 * x(1)) * (18 - 32 * x(0) + 12 * x(0) * x(0) + 48 * x(1) - 36 * x(0) * x(1) + 27 * x(1) * x(1)));

        return log(r) - 5;
    }

    Eigen::MatrixXd solutions() const
    {
        Eigen::MatrixXd sols(1, 2);
        sols << 0.5, 0.25;
        return sols;
    }
};

struct BraninNormalized {
253
254
    BO_PARAM(size_t, dim_in, 2);
    BO_PARAM(size_t, dim_out, 1);
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273

    double operator()(const Eigen::VectorXd& x) const
    {
        double a = x(0) * 15 - 5;
        double b = x(1) * 15;
        return sqr(b - (5.1 / (4 * sqr(M_PI))) * sqr(a) + 5 * a / M_PI - 6) + 10 * (1 - 1 / (8 * M_PI)) * cos(a) + 10;
    }

    Eigen::MatrixXd solutions() const
    {
        Eigen::MatrixXd sols(3, 2);
        sols << 0.1238938, 0.818333,
            0.5427728, 0.151667,
            0.961652, 0.1650;
        return sols;
    }
};

struct SixHumpCamel {
274
275
276
    BO_PARAM(size_t, dim_in, 2);
    BO_PARAM(size_t, dim_out, 1);

277
278
    double operator()(const Eigen::VectorXd& x) const
    {
279
280
281
282
        double x1 = -3 + 6 * x(0);
        double x2 = -2 + 4 * x(1);
        double x1_2 = x1 * x1;
        double x2_2 = x2 * x2;
283
284

        double tmp1 = (4 - 2.1 * x1_2 + (x1_2 * x1_2) / 3) * x1_2;
285
        double tmp2 = x1 * x2;
286
287
288
289
290
291
292
293
294
        double tmp3 = (-4 + 4 * x2_2) * x2_2;
        return tmp1 + tmp2 + tmp3;
    }

    Eigen::MatrixXd solutions() const
    {
        Eigen::MatrixXd sols(2, 2);
        sols << 0.0898, -0.7126,
            -0.0898, 0.7126;
295
296
297
298
        sols(0, 0) = (sols(0, 0) + 3.0) / 6.0;
        sols(1, 0) = (sols(1, 0) + 3.0) / 6.0;
        sols(0, 1) = (sols(0, 1) + 2.0) / 4.0;
        sols(1, 1) = (sols(1, 1) + 2.0) / 4.0;
299
300
301
302
303
304
305
        return sols;
    }
};

template <typename Function>
class Benchmark {
public:
306
307
    BO_PARAM(size_t, dim_in, Function::dim_in());
    BO_PARAM(size_t, dim_out, Function::dim_out());
308
309
310
311
312
313
314
315

    Eigen::VectorXd operator()(const Eigen::VectorXd& x) const
    {
        Eigen::VectorXd res(1);
        res(0) = -f(x);
        return res;
    }

316
    double accuracy(Eigen::VectorXd obs)
317
    {
318
        double x = obs[0];
319
320
321
322
        Eigen::MatrixXd sols = f.solutions();
        double diff = std::abs(x + f(sols.row(0)));
        double min_diff = diff;

323
        for (int i = 1; i < sols.rows(); i++) {
324
325
326
327
328
329
330
331
332
333
            diff = std::abs(x + f(sols.row(i)));
            if (diff < min_diff)
                min_diff = diff;
        }

        return min_diff;
    }

    Function f;
};