obs_multi.cpp 4.81 KB
Newer Older
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
//| 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
//| 
//| 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)
//| 
//| 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
//| 
//| 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".
//| 
//| 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.
//| 
//| 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.
//| 
//| 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
#include <limbo/tools/macros.hpp>
46
#include <limbo/kernel/matern_five_halves.hpp>
47
48
49
50
#include <limbo/mean/data.hpp>
#include <limbo/model/gp.hpp>
#include <limbo/acqui/gp_ucb.hpp>
#include <limbo/bayes_opt/boptimizer.hpp>
51
52
53

using namespace limbo;

54
struct Params {
55
    struct acqui_gpucb : public defaults::acqui_gpucb {
56
57
    };

58
#ifdef USE_LIBCMAES
Konstantinos Chatzilygeroudis's avatar
Konstantinos Chatzilygeroudis committed
59
60
    struct opt_cmaes : public defaults::opt_cmaes {
    };
61
#elif defined(USE_NLOPT)
Konstantinos Chatzilygeroudis's avatar
Konstantinos Chatzilygeroudis committed
62
63
    struct opt_nloptnograd : public defaults::opt_nloptnograd {
    };
64
#else
Konstantinos Chatzilygeroudis's avatar
Konstantinos Chatzilygeroudis committed
65
66
    struct opt_gridsearch : public defaults::opt_gridsearch {
    };
67
#endif
68
    struct kernel_maternfivehalves {
69
        BO_PARAM(double, sigma_sq, 1);
70
        BO_PARAM(double, l, 0.2);
71
72
    };

73
    struct bayes_opt_bobase {
74
        BO_PARAM(bool, stats_enabled, true);
75
76
    };

77
    struct bayes_opt_boptimizer : public defaults::bayes_opt_boptimizer {
78
79
80
81
82
        BO_PARAM(double, noise, 0.001);
    };

    struct init_randomsampling {
        BO_PARAM(int, samples, 5);
83
84
    };

85
86
    struct stop_maxiterations {
        BO_PARAM(int, iterations, 20);
87
    };
88
89
};

90
struct StateEval {
91
92
93
94
95
96
    static constexpr size_t dim_in = 2;
    static constexpr size_t dim_out = 2;

    Eigen::VectorXd operator()(const Eigen::VectorXd& x) const
    {
        Eigen::VectorXd res(2);
97
98
        res(0) = 3 * x(0) + 5;
        res(1) = -5 * x(1) + 2;
99
100
        return res;
    }
101
102
};

103
104
105
106
struct Average {
    typedef double result_type;
    double operator()(const Eigen::VectorXd& x) const
    {
107
        return x.sum() / x.size();
108
109
110
111
112
113
114
115
116
117
118
    }
};

struct SecondElem {
    typedef double result_type;
    double operator()(const Eigen::VectorXd& x) const
    {
        return x(1);
    }
};

119
120
int main()
{
Antoine Cully's avatar
Antoine Cully committed
121
    typedef kernel::MaternFiveHalves<Params> Kernel_t;
122
123
124
    typedef mean::Data<Params> Mean_t;
    typedef model::GP<Params, Kernel_t, Mean_t> GP_t;
    typedef acqui::GP_UCB<Params, GP_t> Acqui_t;
125

Konstantinos Chatzilygeroudis's avatar
Konstantinos Chatzilygeroudis committed
126
    bayes_opt::BOptimizer<Params, modelfun<GP_t>, acquifun<Acqui_t>> opt;
127
128
129
130
131
132

    std::cout << "Optimize using  Average aggregator" << std::endl;
    opt.optimize(StateEval(), Average());
    std::cout << "best obs based on Average aggregator: " << opt.best_observation(Average()) << " res  " << opt.best_sample(Average()).transpose() << std::endl;
    std::cout << "best obs based on FirstElem aggregator: " << opt.best_observation(FirstElem()) << " res  " << opt.best_sample(FirstElem()).transpose() << std::endl;
    std::cout << "best obs based on SecondElem aggregator: " << opt.best_observation(SecondElem()) << " res  " << opt.best_sample(SecondElem()).transpose() << std::endl;
133
    return 0;
134
}