test_gp.cpp 29.8 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
//| Contributor(s):
//|   - Jean-Baptiste Mouret (jean-baptiste.mouret@inria.fr)
//|   - Antoine Cully (antoinecully@gmail.com)
9
//|   - Konstantinos Chatzilygeroudis (konstantinos.chatzilygeroudis@inria.fr)
10
11
//|   - 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
#define BOOST_TEST_DYN_LINK
47
#define BOOST_TEST_MODULE test_gp
48
#define protected public
49
50
51

#include <boost/test/unit_test.hpp>

52
#include <limbo/acqui/ucb.hpp>
53
#include <limbo/kernel/exp.hpp>
54
55
#include <limbo/kernel/matern_five_halves.hpp>
#include <limbo/kernel/matern_three_halves.hpp>
56
#include <limbo/kernel/squared_exp_ard.hpp>
57
#include <limbo/mean/constant.hpp>
58
#include <limbo/mean/function_ard.hpp>
59
#include <limbo/model/gp.hpp>
60
#include <limbo/model/gp/kernel_lf_opt.hpp>
61
#include <limbo/model/gp/kernel_loo_opt.hpp>
62
63
#include <limbo/model/gp/kernel_mean_lf_opt.hpp>
#include <limbo/model/gp/mean_lf_opt.hpp>
64
#include <limbo/model/sparsified_gp.hpp>
65
#include <limbo/opt/grid_search.hpp>
66
#include <limbo/tools/macros.hpp>
67
68
69

using namespace limbo;

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Check gradient via finite differences method
template <typename F>
std::tuple<double, Eigen::VectorXd, Eigen::VectorXd> check_grad(const F& f, const Eigen::VectorXd& x, double e = 1e-4)
{
    Eigen::VectorXd analytic_result, finite_diff_result;

    opt::eval_t res = f(x, true);
    analytic_result = opt::grad(res);

    finite_diff_result = Eigen::VectorXd::Zero(x.size());
    for (int j = 0; j < x.size(); j++) {
        Eigen::VectorXd test1 = x, test2 = x;
        test1[j] -= e;
        test2[j] += e;
        double res1 = opt::fun(f(test1, false));
        double res2 = opt::fun(f(test2, false));
        finite_diff_result[j] = (res2 - res1) / (2.0 * e);
    }

    return std::make_tuple((analytic_result - finite_diff_result).norm(), analytic_result, finite_diff_result);
}

92
93
Eigen::VectorXd make_v1(double x)
{
94
    return tools::make_vector(x);
95
96
}

97
98
Eigen::VectorXd make_v2(double x1, double x2)
{
99
100
101
    Eigen::VectorXd v2(2);
    v2 << x1, x2;
    return v2;
102
103
}

104
struct Params {
105
106
107
    struct kernel : public defaults::kernel {
    };

108
    struct kernel_squared_exp_ard : public defaults::kernel_squared_exp_ard {
109
    };
110

111
    struct kernel_maternfivehalves {
112
        BO_PARAM(double, sigma_sq, 1);
113
114
115
        BO_PARAM(double, l, 0.25);
    };

116
    struct mean_constant : public defaults::mean_constant {
117
    };
118

119
    struct opt_rprop : public defaults::opt_rprop {
120
121
    };

122
    struct opt_parallelrepeater : public defaults::opt_parallelrepeater {
123
    };
124
125
126
127
128
129

    struct acqui_ucb : public defaults::acqui_ucb {
    };

    struct opt_gridsearch : public defaults::opt_gridsearch {
    };
130
};
131

132
133
134
135
BOOST_AUTO_TEST_CASE(test_gp_check_lf_grad)
{
    using namespace limbo;

136
137
138
    using KF_t = kernel::SquaredExpARD<Params>;
    using Mean_t = mean::FunctionARD<Params, mean::Constant<Params>>;
    using GP_t = model::GP<Params, KF_t, Mean_t>;
139
140
141
142
143
144
145

    GP_t gp(4, 2);

    std::vector<Eigen::VectorXd> observations, samples, test_samples, test_samples_mean, test_samples_kernel_mean;
    double e = 1e-4;

    // Random samples and test samples
146
    int N = 40, M = 100;
147

148
    for (int i = 0; i < N; i++) {
149
        samples.push_back(tools::random_vector(4));
150
151
152
        Eigen::VectorXd ob(2);
        ob << std::cos(samples[i](0)), std::sin(samples[i](1));
        observations.push_back(ob);
153
154
    }

155
    for (int i = 0; i < M; i++) {
156
157
158
        test_samples.push_back(tools::random_vector(gp.kernel_function().h_params_size()));
        test_samples_mean.push_back(tools::random_vector(gp.mean_function().h_params_size()));
        test_samples_kernel_mean.push_back(tools::random_vector(gp.kernel_function().h_params_size() + gp.mean_function().h_params_size()));
159
160
    }

161
    gp.compute(samples, observations);
162
163
164
165
166

    model::gp::KernelLFOpt<Params>::KernelLFOptimization<GP_t> kernel_optimization(gp);

    Eigen::VectorXd results(M);

167
    for (int i = 0; i < M; i++) {
168
169
170
171
172
173
174
175
176
        auto res = check_grad(kernel_optimization, test_samples[i], 1e-4);
        results(i) = std::get<0>(res);
        // std::cout << std::get<1>(res).transpose() << " vs " << std::get<2>(res).transpose() << " --> " << results(i) << std::endl;
    }

    BOOST_CHECK(results.array().sum() < M * e);

    model::gp::KernelMeanLFOpt<Params>::KernelMeanLFOptimization<GP_t> kernel_mean_optimization(gp);

177
    for (int i = 0; i < M; i++) {
178
179
180
181
182
183
184
185
186
        auto res = check_grad(kernel_mean_optimization, test_samples_kernel_mean[i], 1e-4);
        results(i) = std::get<0>(res);
        // std::cout << std::get<1>(res).transpose() << " vs " << std::get<2>(res).transpose() << " --> " << results(i) << std::endl;
    }

    BOOST_CHECK(results.array().sum() < M * e);

    model::gp::MeanLFOpt<Params>::MeanLFOptimization<GP_t> mean_optimization(gp);

187
    for (int i = 0; i < M; i++) {
188
189
        auto res = check_grad(mean_optimization, test_samples_mean[i], 1e-4);
        results(i) = std::get<0>(res);
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
        // std::cout << std::get<1>(res).transpose() << " vs " << std::get<2>(res).transpose() << " --> " << results(i) << std::endl;
    }

    BOOST_CHECK(results.array().sum() < M * e);
}

BOOST_AUTO_TEST_CASE(test_gp_check_lf_grad_noise)
{
    using namespace limbo;
    struct Parameters {
        struct kernel : public defaults::kernel {
            BO_PARAM(bool, optimize_noise, true);
        };

        struct kernel_squared_exp_ard : public defaults::kernel_squared_exp_ard {
        };

        struct kernel_maternfivehalves {
            BO_PARAM(double, sigma_sq, 1);
            BO_PARAM(double, l, 0.25);
        };

        struct mean_constant : public defaults::mean_constant {
        };

        struct opt_rprop : public defaults::opt_rprop {
        };

        struct opt_parallelrepeater : public defaults::opt_parallelrepeater {
        };

        struct acqui_ucb : public defaults::acqui_ucb {
        };

        struct opt_gridsearch : public defaults::opt_gridsearch {
        };
    };

    using KF_t = kernel::SquaredExpARD<Parameters>;
    using Mean_t = mean::FunctionARD<Params, mean::Constant<Parameters>>;
    using GP_t = model::GP<Parameters, KF_t, Mean_t>;

    GP_t gp(4, 2);

    std::vector<Eigen::VectorXd> observations, samples, test_samples, test_samples_kernel_mean;
    double e = 1e-4;

    // Random samples and test samples
238
    int N = 40, M = 100;
239
240
241

    for (int i = 0; i < N; i++) {
        samples.push_back(tools::random_vector(4));
242
243
244
        Eigen::VectorXd ob(2);
        ob << std::cos(samples[i](0)), std::sin(samples[i](1));
        observations.push_back(ob);
245
246
247
    }

    for (int i = 0; i < M; i++) {
248
249
        test_samples.push_back(tools::random_vector(gp.kernel_function().h_params_size()));
        test_samples_kernel_mean.push_back(tools::random_vector(gp.kernel_function().h_params_size() + gp.mean_function().h_params_size()));
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
    }

    gp.compute(samples, observations);

    model::gp::KernelLFOpt<Parameters>::KernelLFOptimization<GP_t> kernel_optimization(gp);

    Eigen::VectorXd results(M);

    for (int i = 0; i < M; i++) {
        auto res = check_grad(kernel_optimization, test_samples[i], 1e-4);
        results(i) = std::get<0>(res);
        // std::cout << std::get<1>(res).transpose() << " vs " << std::get<2>(res).transpose() << " --> " << results(i) << std::endl;
    }

    BOOST_CHECK(results.array().sum() < M * e);

    model::gp::KernelMeanLFOpt<Parameters>::KernelMeanLFOptimization<GP_t> kernel_mean_optimization(gp);

    for (int i = 0; i < M; i++) {
        auto res = check_grad(kernel_mean_optimization, test_samples_kernel_mean[i], 1e-4);
        results(i) = std::get<0>(res);
271
272
273
274
275
276
        // std::cout << std::get<1>(res).transpose() << " vs " << std::get<2>(res).transpose() << " --> " << results(i) << std::endl;
    }

    BOOST_CHECK(results.array().sum() < M * e);
}

277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
BOOST_AUTO_TEST_CASE(test_gp_check_loo_grad)
{
    using namespace limbo;

    using KF_t = kernel::SquaredExpARD<Params>;
    using Mean_t = mean::Constant<Params>;
    using GP_t = model::GP<Params, KF_t, Mean_t>;

    GP_t gp(4, 2);

    std::vector<Eigen::VectorXd> observations, samples, test_samples;
    double e = 1e-4;

    // Random samples and test samples
    int N = 40, M = 100;

    for (int i = 0; i < N; i++) {
        samples.push_back(tools::random_vector(4));
        Eigen::VectorXd ob(2);
        ob << std::cos(samples[i](0)), std::sin(samples[i](1));
        observations.push_back(ob);
    }

    for (int i = 0; i < M; i++) {
301
        test_samples.push_back(tools::random_vector(gp.kernel_function().h_params_size()));
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
    }

    gp.compute(samples, observations);

    model::gp::KernelLooOpt<Params>::KernelLooOptimization<GP_t> kernel_optimization(gp);

    Eigen::VectorXd results(M);

    for (int i = 0; i < M; i++) {
        auto res = check_grad(kernel_optimization, test_samples[i], 1e-4);
        results(i) = std::get<0>(res);
        // std::cout << std::get<1>(res).transpose() << " vs " << std::get<2>(res).transpose() << " --> " << results(i) << std::endl;
    }

    BOOST_CHECK(results.array().sum() < M * e);
}

BOOST_AUTO_TEST_CASE(test_gp_check_loo_grad_noise)
{
    using namespace limbo;
    struct Parameters {
        struct kernel : public defaults::kernel {
            BO_PARAM(bool, optimize_noise, true);
        };

        struct kernel_squared_exp_ard : public defaults::kernel_squared_exp_ard {
        };

        struct kernel_maternfivehalves {
            BO_PARAM(double, sigma_sq, 1);
            BO_PARAM(double, l, 0.25);
        };

        struct mean_constant : public defaults::mean_constant {
        };

        struct opt_rprop : public defaults::opt_rprop {
        };

        struct opt_parallelrepeater : public defaults::opt_parallelrepeater {
        };

        struct acqui_ucb : public defaults::acqui_ucb {
        };

        struct opt_gridsearch : public defaults::opt_gridsearch {
        };
    };

    using KF_t = kernel::SquaredExpARD<Parameters>;
    using Mean_t = mean::Constant<Parameters>;
    using GP_t = model::GP<Parameters, KF_t, Mean_t>;

    GP_t gp(4, 2);

    std::vector<Eigen::VectorXd> observations, samples, test_samples;
    double e = 1e-4;

    // Random samples and test samples
    int N = 40, M = 100;

    for (int i = 0; i < N; i++) {
        samples.push_back(tools::random_vector(4));
        Eigen::VectorXd ob(2);
        ob << std::cos(samples[i](0)), std::sin(samples[i](1));
        observations.push_back(ob);
    }

    for (int i = 0; i < M; i++) {
371
        test_samples.push_back(tools::random_vector(gp.kernel_function().h_params_size()));
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
    }

    gp.compute(samples, observations);

    model::gp::KernelLooOpt<Parameters>::KernelLooOptimization<GP_t> kernel_optimization(gp);

    Eigen::VectorXd results(M);

    for (int i = 0; i < M; i++) {
        auto res = check_grad(kernel_optimization, test_samples[i], 1e-4);
        results(i) = std::get<0>(res);
        // std::cout << std::get<1>(res).transpose() << " vs " << std::get<2>(res).transpose() << " --> " << results(i) << std::endl;
    }

    BOOST_CHECK(results.array().sum() < M * e);
}

389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
BOOST_AUTO_TEST_CASE(test_gp_check_inv_kernel_computation)
{
    using namespace limbo;

    using KF_t = kernel::SquaredExpARD<Params>;
    using Mean_t = mean::FunctionARD<Params, mean::Constant<Params>>;
    using GP_t = model::GP<Params, KF_t, Mean_t>;

    GP_t gp(4, 2);

    std::vector<Eigen::VectorXd> observations, samples;

    // Random samples
    int N = 10;

    for (int i = 0; i < N; i++) {
        samples.push_back(tools::random_vector(4));
        Eigen::VectorXd ob(2);
        ob << std::cos(samples[i](0)), std::sin(samples[i](1));
        observations.push_back(ob);
    }

    gp.compute(samples, observations);

    // Check that variable is properly initialized
    BOOST_CHECK(!gp.inv_kernel_computed());

    // Check that kernel is computed
    gp.compute_inv_kernel();
    BOOST_CHECK(gp.inv_kernel_computed());

    // Check recompute alternatives
    gp.recompute(false, false);
    BOOST_CHECK(gp.inv_kernel_computed());

    gp.recompute(true, false);
    BOOST_CHECK(gp.inv_kernel_computed());

    gp.recompute(true, true);
    BOOST_CHECK(!gp.inv_kernel_computed());

    // Check add_sample
    gp.compute_inv_kernel();
    gp.add_sample(samples.back(), observations.back());
    BOOST_CHECK(!gp.inv_kernel_computed());

    // Check different implicit computations of inverse kernel
    gp.compute_kernel_grad_log_lik();
    BOOST_CHECK(gp.inv_kernel_computed());

    gp.recompute();
    BOOST_CHECK(!gp.inv_kernel_computed());
    gp.compute_mean_grad_log_lik();
    BOOST_CHECK(gp.inv_kernel_computed());

    gp.recompute();
    BOOST_CHECK(!gp.inv_kernel_computed());
    gp.compute_log_loo_cv();
    BOOST_CHECK(gp.inv_kernel_computed());

    gp.recompute();
    BOOST_CHECK(!gp.inv_kernel_computed());
    gp.compute_kernel_grad_log_loo_cv();
    BOOST_CHECK(gp.inv_kernel_computed());
}

455
456
BOOST_AUTO_TEST_CASE(test_gp_dim)
{
457
458
    using namespace limbo;

459
460
461
    using KF_t = kernel::MaternFiveHalves<Params>;
    using Mean_t = mean::Constant<Params>;
    using GP_t = model::GP<Params, KF_t, Mean_t>;
462
463
464
465
466
467
468

    GP_t gp; // no init with dim

    std::vector<Eigen::VectorXd> observations = {make_v2(5, 5), make_v2(10, 10),
        make_v2(5, 5)};
    std::vector<Eigen::VectorXd> samples = {make_v2(1, 1), make_v2(2, 2), make_v2(3, 3)};

469
    gp.compute(samples, observations);
470
471
472
473
474
475
476

    Eigen::VectorXd mu;
    double sigma;
    std::tie(mu, sigma) = gp.query(make_v2(1, 1));
    BOOST_CHECK(std::abs((mu(0) - 5)) < 1);
    BOOST_CHECK(std::abs((mu(1) - 5)) < 1);

477
    BOOST_CHECK(sigma <= 2. * (Params::kernel::noise() + 1e-8));
478
479
}

480
481
482
BOOST_AUTO_TEST_CASE(test_gp)
{
    using namespace limbo;
483

484
485
486
    using KF_t = kernel::MaternFiveHalves<Params>;
    using Mean_t = mean::Constant<Params>;
    using GP_t = model::GP<Params, KF_t, Mean_t>;
487

488
489
490
491
    GP_t gp;
    std::vector<Eigen::VectorXd> observations = {make_v1(5), make_v1(10),
        make_v1(5)};
    std::vector<Eigen::VectorXd> samples = {make_v1(1), make_v1(2), make_v1(3)};
492

493
    gp.compute(samples, observations);
494

495
496
    Eigen::VectorXd mu;
    double sigma;
497
    std::tie(mu, sigma) = gp.query(make_v1(1));
498
    BOOST_CHECK(std::abs((mu(0) - 5)) < 1);
499
    BOOST_CHECK(sigma <= 2. * (Params::kernel::noise() + 1e-8));
500
501

    std::tie(mu, sigma) = gp.query(make_v1(2));
502
    BOOST_CHECK(std::abs((mu(0) - 10)) < 1);
503
    BOOST_CHECK(sigma <= 2. * (Params::kernel::noise() + 1e-8));
504
505

    std::tie(mu, sigma) = gp.query(make_v1(3));
506
    BOOST_CHECK(std::abs((mu(0) - 5)) < 1);
507
    BOOST_CHECK(sigma <= 2. * (Params::kernel::noise() + 1e-8));
508

509
    for (double x = 0; x < 4; x += 0.05) {
510
511
512
513
514
        Eigen::VectorXd mu;
        double sigma;
        std::tie(mu, sigma) = gp.query(make_v1(x));
        BOOST_CHECK(gp.mu(make_v1(x)) == mu);
        BOOST_CHECK(gp.sigma(make_v1(x)) == sigma);
515
516
        // std::cout << x << " " << mu << " " << mu.array() - sigma << " "
        //           << mu.array() + sigma << std::endl;
517
    }
518
}
519

520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
BOOST_AUTO_TEST_CASE(test_gp_identical_samples)
{
    using namespace limbo;

    using KF_t = kernel::MaternFiveHalves<Params>;
    using Mean_t = mean::Constant<Params>;
    using GP_t = model::GP<Params, KF_t, Mean_t>;

    GP_t gp;
    std::vector<Eigen::VectorXd> observations;
    std::vector<Eigen::VectorXd> samples;
    for (int i = 0; i < 10; i++) {
        samples.push_back(make_v1(1));
        observations.push_back(make_v1(std::cos(1)));
    }

    gp.compute(samples, observations);

538
539
540
541
542
    GP_t gp2;
    for (int i = 0; i < 10; i++) {
        gp2.add_sample(make_v1(1), make_v1(std::cos(1)));
    }

543
544
545
546
547
548
549
550
551
552
553
554
555
    // Compute kernel matrix
    Eigen::MatrixXd kernel;
    size_t n = samples.size();
    kernel.resize(n, n);

    for (size_t i = 0; i < n; i++)
        for (size_t j = 0; j <= i; ++j)
            kernel(i, j) = gp.kernel_function()(samples[i], samples[j], i, j);

    for (size_t i = 0; i < n; i++)
        for (size_t j = 0; j < i; ++j)
            kernel(j, i) = kernel(i, j);

556
557
558
    // Reconstruct kernels from cholesky decomposition
    Eigen::MatrixXd reconstructed_kernel = gp.matrixL() * gp.matrixL().transpose();
    Eigen::MatrixXd reconstructed_kernel2 = gp2.matrixL() * gp2.matrixL().transpose();
559

560
561
562
    // Check if the reconstructed kernels match the actual one
    BOOST_CHECK(kernel.isApprox(reconstructed_kernel, 1e-5));
    BOOST_CHECK(kernel.isApprox(reconstructed_kernel2, 1e-5));
563
564
565
566
567
568
569
570
571
572

    // Check if incremental cholesky produces the same result
    Eigen::VectorXd mu1, mu2;
    double s1, s2;

    std::tie(mu1, s1) = gp.query(make_v1(1));
    std::tie(mu2, s2) = gp2.query(make_v1(1));

    BOOST_CHECK((mu1 - mu2).norm() < 1e-4);
    BOOST_CHECK(std::sqrt((s1 - s2) * (s1 - s2)) < 1e-4);
573
574
}

575
576
577
BOOST_AUTO_TEST_CASE(test_gp_bw_inversion)
{
    using namespace limbo;
578
579
    size_t N = 1000;
    size_t failures = 0;
580

581
582
583
    using KF_t = kernel::MaternFiveHalves<Params>;
    using Mean_t = mean::Constant<Params>;
    using GP_t = model::GP<Params, KF_t, Mean_t>;
584

585
586
587
588
589
590
591
592
593
594
595
596
    for (size_t i = 0; i < N; i++) {

        std::vector<Eigen::VectorXd> observations;
        std::vector<Eigen::VectorXd> samples;
        tools::rgen_double_t rgen(0.0, 10);
        for (size_t i = 0; i < 100; i++) {
            observations.push_back(make_v1(rgen.rand()));
            samples.push_back(make_v1(rgen.rand()));
        }

        GP_t gp;
        auto t1 = std::chrono::steady_clock::now();
597
        gp.compute(samples, observations);
598
        // auto time_init = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - t1).count();
599
600
601
        // std::cout.precision(17);
        // std::cout << "Time running first batch: " << time_init << "us" << std::endl
        //           << std::endl;
602

603
604
        observations.push_back(make_v1(rgen.rand()));
        samples.push_back(make_v1(rgen.rand()));
605
606

        t1 = std::chrono::steady_clock::now();
607
        gp.add_sample(samples.back(), observations.back());
608
        auto time_increment = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - t1).count();
609
610
        // std::cout << "Time running increment: " << time_increment << "us" << std::endl
        //           << std::endl;
611
612
613
614

        t1 = std::chrono::steady_clock::now();
        gp.recompute(true);
        auto time_recompute = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - t1).count();
615
616
        // std::cout << "Time recomputing: " << time_recompute << "us" << std::endl
        //           << std::endl;
617
618
619

        GP_t gp2;
        t1 = std::chrono::steady_clock::now();
620
        gp2.compute(samples, observations);
621
        auto time_full = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - t1).count();
622
623
        // std::cout << "Time running whole batch: " << time_full << "us" << std::endl
        //           << std::endl;
624

625
626
        bool failed = false;

627
628
        Eigen::VectorXd s = make_v1(rgen.rand());
        if ((gp.mu(s) - gp2.mu(s)).norm() >= 1e-5)
629
            failed = true;
630
        if (!gp.matrixL().isApprox(gp2.matrixL(), 1e-5))
631
            failed = true;
632
        if (time_full <= time_increment)
633
            failed = true;
634
        if (time_recompute <= time_increment)
635
636
637
            failed = true;

        if (failed)
638
            failures++;
639
640
    }

641
    BOOST_CHECK(double(failures) / double(N) < 0.1);
642
643
}

644
645
646
647
648
649
650
651
652
653
654
BOOST_AUTO_TEST_CASE(test_gp_no_samples_acqui_opt)
{
    using namespace limbo;

    struct FirstElem {
        double operator()(const Eigen::VectorXd& x) const
        {
            return x(0);
        }
    };

655
    using acquiopt_t = opt::GridSearch<Params>;
656

657
658
659
660
    using KF_t = kernel::SquaredExpARD<Params>;
    using Mean_t = mean::Constant<Params>;
    using GP_t = model::GP<Params, KF_t, Mean_t>;
    using acquisition_function_t = acqui::UCB<Params, GP_t>;
661
662
663
664
665
666
667
668

    GP_t gp(2, 2);

    acquisition_function_t acqui(gp, 0);
    acquiopt_t acqui_optimizer;

    // we do not have gradient in our current acquisition function
    auto acqui_optimization =
669
        [&](const Eigen::VectorXd& x, bool g) { return acqui(x, FirstElem(), g); };
670
671
672
673
674
675
    Eigen::VectorXd starting_point = tools::random_vector(2);
    Eigen::VectorXd test = acqui_optimizer(acqui_optimization, starting_point, true);
    BOOST_CHECK(test(0) < 1e-5);
    BOOST_CHECK(test(1) < 1e-5);
}

676
677
BOOST_AUTO_TEST_CASE(test_gp_auto)
{
678
679
680
    using KF_t = kernel::SquaredExpARD<Params>;
    using Mean_t = mean::Constant<Params>;
    using GP_t = model::GP<Params, KF_t, Mean_t, model::gp::KernelLFOpt<Params>>;
681

682
    GP_t gp;
683
684
685
    std::vector<Eigen::VectorXd> observations = {make_v1(5), make_v1(10), make_v1(5)};
    std::vector<Eigen::VectorXd> samples = {make_v1(1), make_v1(2), make_v1(3)};

686
    gp.compute(samples, observations, false);
687
    gp.optimize_hyperparams();
688
689
690
691

    Eigen::VectorXd mu;
    double sigma;
    std::tie(mu, sigma) = gp.query(make_v1(1));
692
    BOOST_CHECK(std::abs((mu(0) - 5)) < 1);
693
    BOOST_CHECK(sigma <= 2. * (gp.kernel_function().noise() + 1e-8));
694
695

    std::tie(mu, sigma) = gp.query(make_v1(2));
696
    BOOST_CHECK(std::abs((mu(0) - 10)) < 1);
697
    BOOST_CHECK(sigma <= 2. * (gp.kernel_function().noise() + 1e-8));
698
699

    std::tie(mu, sigma) = gp.query(make_v1(3));
700
    BOOST_CHECK(std::abs((mu(0) - 5)) < 1);
701
    BOOST_CHECK(sigma <= 2. * (gp.kernel_function().noise() + 1e-8));
702
}
703
704
705
706
707
708

BOOST_AUTO_TEST_CASE(test_gp_init_variance)
{
    using namespace limbo;

    struct Parameters {
709
710
711
        struct kernel : public defaults::kernel {
        };

712
713
714
715
716
717
718
719
        struct kernel_squared_exp_ard {
            BO_PARAM(int, k, 0);
            BO_PARAM(double, sigma_sq, 10);
        };
        struct kernel_exp {
            BO_PARAM(double, sigma_sq, 10);
            BO_PARAM(double, l, 1);
        };
720
        struct kernel_maternthreehalves {
721
722
723
            BO_PARAM(double, sigma_sq, 10);
            BO_PARAM(double, l, 0.25);
        };
724
        struct kernel_maternfivehalves {
725
726
727
728
729
            BO_PARAM(double, sigma_sq, 10);
            BO_PARAM(double, l, 0.25);
        };
    };

Antoine Cully's avatar
Antoine Cully committed
730
    // MaternThreeHalves
731
    using GP1_t = model::GP<Params, kernel::MaternThreeHalves<Parameters>, mean::Constant<Params>>;
732
733
734
735
736

    GP1_t gp1(1, 1);

    double sigma = gp1.sigma(tools::random_vector(1));

737
    BOOST_CHECK_CLOSE(sigma, 10.0, 1);
738

Antoine Cully's avatar
Antoine Cully committed
739
    // MaternFiveHalves
740
    using GP2_t = model::GP<Params, kernel::MaternFiveHalves<Parameters>, mean::Constant<Params>>;
741
742
743
744
745

    GP2_t gp2(1, 1);

    sigma = gp2.sigma(tools::random_vector(1));

746
    BOOST_CHECK_CLOSE(sigma, 10.0, 1);
747
748

    // Exponential
749
    using GP3_t = model::GP<Params, kernel::Exp<Parameters>, mean::Constant<Params>>;
750
751
752
753
754

    GP3_t gp3(1, 1);

    sigma = gp3.sigma(tools::random_vector(1));

755
    BOOST_CHECK_CLOSE(sigma, 10.0, 1);
756
757

    // ARD Squared Exponential
758
    using GP4_t = model::GP<Params, kernel::SquaredExpARD<Parameters>, mean::Constant<Params>>;
759
760
761
762
763

    GP4_t gp4(1, 1);

    sigma = gp4.sigma(tools::random_vector(1));

764
    BOOST_CHECK_CLOSE(sigma, 10.0, 1);
765
}
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790

BOOST_AUTO_TEST_CASE(test_sparse_gp)
{
    using namespace limbo;
    constexpr size_t N = 10;
    constexpr size_t M = 100;
    size_t failures = 0;

    struct SparseParams {
        struct mean_constant : public defaults::mean_constant {
        };
        struct kernel : public defaults::kernel {
        };
        struct kernel_squared_exp_ard : public defaults::kernel_squared_exp_ard {
        };
        struct opt_rprop : public defaults::opt_rprop {
        };
        struct model_sparse_gp {
            BO_PARAM(int, max_points, M / 3);
        };
    };

    using KF_t = kernel::SquaredExpARD<SparseParams>;
    using MF_t = mean::Constant<SparseParams>;
    using GP_t = model::GP<SparseParams, KF_t, MF_t, model::gp::KernelLFOpt<SparseParams, opt::Rprop<SparseParams>>>;
791
    using SparsifiedGP_t = model::SparsifiedGP<SparseParams, KF_t, MF_t, model::gp::KernelLFOpt<SparseParams, opt::Rprop<SparseParams>>>;
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808

    for (size_t i = 0; i < N; i++) {

        std::vector<Eigen::VectorXd> observations;
        std::vector<Eigen::VectorXd> samples;
        tools::rgen_double_t rgen(0.0, 10);
        for (size_t i = 0; i < M; i++) {
            observations.push_back(make_v1(rgen.rand()));
            samples.push_back(make_v1(rgen.rand()));
        }

        GP_t gp;
        auto t1 = std::chrono::steady_clock::now();
        gp.compute(samples, observations, false);
        gp.optimize_hyperparams();
        auto time_full = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - t1).count();

809
        SparsifiedGP_t sgp;
810
811
812
813
814
815
816
817
818
819
820
        auto t2 = std::chrono::steady_clock::now();
        sgp.compute(samples, observations, false);
        sgp.optimize_hyperparams();
        auto time_sparse = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - t2).count();

        if (time_full <= time_sparse)
            failures++;
    }

    BOOST_CHECK(double(failures) / double(N) < 0.1);
}
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845

BOOST_AUTO_TEST_CASE(test_sparse_gp_accuracy)
{
    using namespace limbo;
    constexpr size_t N = 20;
    constexpr size_t M = 100;
    size_t failures = 0;

    struct SparseParams {
        struct mean_constant : public defaults::mean_constant {
        };
        struct kernel : public defaults::kernel {
        };
        struct kernel_squared_exp_ard : public defaults::kernel_squared_exp_ard {
        };
        struct opt_rprop : public defaults::opt_rprop {
        };
        struct model_sparse_gp {
            BO_PARAM(int, max_points, M / 2);
        };
    };

    using KF_t = kernel::SquaredExpARD<SparseParams>;
    using MF_t = mean::Constant<SparseParams>;
    using GP_t = model::GP<SparseParams, KF_t, MF_t, model::gp::KernelLFOpt<SparseParams, opt::Rprop<SparseParams>>>;
846
    using SparsifiedGP_t = model::SparsifiedGP<SparseParams, KF_t, MF_t, model::gp::KernelLFOpt<SparseParams, opt::Rprop<SparseParams>>>;
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868

    for (size_t i = 0; i < N; i++) {

        std::vector<Eigen::VectorXd> observations;
        std::vector<Eigen::VectorXd> samples;
        tools::rgen_double_t rgen(-2., 2.);
        for (size_t i = 0; i < M; i++) {
            samples.push_back(make_v1(rgen.rand()));
            observations.push_back(make_v1(std::cos(samples.back()[0])));
        }

        std::vector<Eigen::VectorXd> test_observations;
        std::vector<Eigen::VectorXd> test_samples;
        for (size_t i = 0; i < M; i++) {
            test_samples.push_back(make_v1(rgen.rand()));
            test_observations.push_back(make_v1(std::cos(test_samples.back()[0])));
        }

        GP_t gp;
        gp.compute(samples, observations, false);
        gp.optimize_hyperparams();

869
        SparsifiedGP_t sgp;
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
        sgp.compute(samples, observations, false);
        sgp.optimize_hyperparams();

        bool failed = false;

        // check if normal GP and sparse GP produce very similar results
        // in the learned points
        for (size_t i = 0; i < M; i++) {
            Eigen::VectorXd gp_val, sgp_val;
            double gp_sigma, sgp_sigma;
            std::tie(gp_val, gp_sigma) = gp.query(samples[i]);
            std::tie(sgp_val, sgp_sigma) = sgp.query(samples[i]);

            if (std::abs(gp_val[0] - sgp_val[0]) > 1e-2 || std::abs(gp_sigma - sgp_sigma) > 1e-2)
                failed = true;
        }

        // check if normal GP and sparse GP produce very similar results
        // in the test points
        for (size_t i = 0; i < M; i++) {
            Eigen::VectorXd gp_val, sgp_val;
            double gp_sigma, sgp_sigma;
            std::tie(gp_val, gp_sigma) = gp.query(test_samples[i]);
            std::tie(sgp_val, sgp_sigma) = sgp.query(test_samples[i]);

            if (std::abs(gp_val[0] - sgp_val[0]) > 1e-2 || std::abs(gp_sigma - sgp_sigma) > 1e-2)
                failed = true;
        }

        // check if normal GP and sparse GP produce very similar errors
        // in the test points
        for (size_t i = 0; i < M; i++) {
            double gp_val = gp.mu(test_samples[i])[0];
            double sgp_val = sgp.mu(test_samples[i])[0];

            double gp_error_val = std::abs(gp_val - test_observations[i][0]);
            double sgp_error_val = std::abs(sgp_val - test_observations[i][0]);

            if (std::abs(gp_error_val - sgp_error_val) > 1e-2)
                failed = true;
        }

        if (failed)
            failures++;
    }

    BOOST_CHECK(double(failures) / double(N) < 0.1);
}