obs_multi_auto_mean.cpp 4.46 KB
Newer Older
1
2
3
#include <limbo/tools/macros.hpp>
#include <limbo/kernel/squared_exp_ard.hpp>
#include <limbo/mean/function_ard.hpp>
4
#include <limbo/model/gp.hpp>
5
#include <limbo/model/gp/kernel_mean_lf_opt.hpp>
6
#include <limbo/bayes_opt/boptimizer.hpp>
Aneoshun's avatar
Aneoshun committed
7
8
9

using namespace limbo;

Federico Allocati's avatar
Federico Allocati committed
10
struct Params {
11
    struct gp_auto_mean {
12
13
14
15
        BO_PARAM(int, n_rprop, 100);
        BO_PARAM(int, rprop_restart, 10);
    };

16
    struct cmaes : public defaults::cmaes {
17
18
    };

19
20
21
    struct rprop : public defaults::rprop {
    };

22
    struct kf_maternfivehalfs {
23
24
25
26
        BO_PARAM(float, sigma, 1);
        BO_PARAM(float, l, 0.2);
    };

27
    struct boptimizer {
28
29
30
31
        BO_PARAM(double, noise, 0.001);
        BO_PARAM(int, dump_period, 1);
    };

32
    struct init {
33
34
35
        BO_PARAM(int, nb_samples, 5);
    };

36
    struct maxiterations {
37
38
        BO_PARAM(int, n_iterations, 100);
    };
39
40
41

    struct parallel_repeater : defaults::parallel_repeater {
    };
Aneoshun's avatar
Aneoshun committed
42
43
};

44
template <typename Params, typename Model>
45
class UCB_multi {
Aneoshun's avatar
Aneoshun committed
46
public:
47
    UCB_multi(const Model& model, int iteration = 0) : _model(model) {}
Aneoshun's avatar
Aneoshun committed
48

49
    size_t dim_in() const { return _model.dim_in(); }
Aneoshun's avatar
Aneoshun committed
50

51
    size_t dim_out() const { return _model.dim_out(); }
52

53
54
    template <typename AggregatorFunction>
    double operator()(const Eigen::VectorXd& v, const AggregatorFunction& afun) const
55
56
57
58
    {
        // double mu, sigma;
        // std::tie(mu, sigma) = _model.query(v);
        // return (mu + Params::ucb::alpha() * sqrt(sigma));
Aneoshun's avatar
Aneoshun committed
59

60
61
        return (sqrt(_model.sigma(v)));
    }
Aneoshun's avatar
Aneoshun committed
62

63
64
65
protected:
    const Model& _model;
};
Aneoshun's avatar
Aneoshun committed
66

67
template <typename Params>
68
struct MeanOffset {
69
70
71
    MeanOffset(size_t dim_out = 1) {}

    template <typename GP>
72
    Eigen::VectorXd operator()(const Eigen::VectorXd& x, const GP& gp) const
73
74
75
76
77
    {
        Eigen::VectorXd res(2);
        res(0) = 2; // constant overestimation
        res(1) = 2; // constant overestimation

78
        for (int i = 0; i < x.size(); i++) {
79
80
81
82
83
84
            res(0) += 1 - (x[i] - 0.3) * (x[i] - 0.3) + sin(10 * x[i]) * 0.2;
            res(1) += 1 - (x[i] - 0.3) * (x[i] - 0.3) * 0.4;
        }
        return res;
    }
};
Aneoshun's avatar
Aneoshun committed
85

86
template <typename Params>
87
struct MeanRotation {
88
89
90
    MeanRotation(size_t dim_out = 1) {}

    template <typename GP>
91
    Eigen::VectorXd operator()(const Eigen::VectorXd& x, const GP& gp) const
92
93
94
95
    {
        Eigen::VectorXd res(2);
        res(0) = 0; // constant overestimation
        res(1) = 0; // constant overestimation
96
        for (int i = 0; i < x.size(); i++) {
97
98
99
100
101
102
103
104
105
106
107
108
            res(0) += 1 - (x[i] - 0.3) * (x[i] - 0.3) + sin(10 * x[i]) * 0.2;
            res(1) += 1 - (x[i] - 0.3) * (x[i] - 0.3) * 0.4;
        }
        double theta = M_PI / 2;
        Eigen::Matrix2d rot;
        rot(0, 0) = cos(theta);
        rot(0, 1) = -sin(theta);
        rot(1, 0) = sin(theta);
        rot(1, 1) = cos(theta);
        return rot * res;
    }
};
Aneoshun's avatar
Aneoshun committed
109

110
template <typename Params>
111
struct MeanComplet {
112
113
114
    MeanComplet(size_t dim_out = 1) {}

    template <typename GP>
115
    Eigen::VectorXd operator()(const Eigen::VectorXd& x, const GP& gp) const
116
117
118
119
    {
        Eigen::VectorXd res(2);
        res(0) = 2; // constant overestimation
        res(1) = 2; // constant overestimation
120
        for (int i = 0; i < x.size(); i++) {
121
122
123
124
125
126
127
128
129
130
131
            res(0) += 1 - (x[i] - 0.3) * (x[i] - 0.3) + sin(10 * x[i]) * 0.2;
            res(1) += 1 - (x[i] - 0.3) * (x[i] - 0.3) * 0.4;
        }
        double theta = M_PI / 2;
        Eigen::Matrix2d rot;
        rot(0, 0) = cos(theta);
        rot(0, 1) = -sin(theta);
        rot(1, 0) = sin(theta);
        rot(1, 1) = cos(theta);
        return rot * res;
    }
Aneoshun's avatar
Aneoshun committed
132
133
};

134
struct fit_eval {
135
136
137
138
139
140
141
142
    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(dim_out);
        res(0) = 0;
        res(1) = 0;
143
        for (int i = 0; i < x.size(); i++) {
144
145
146
147
148
149
            res(0) += 1 - (x[i] - 0.3) * (x[i] - 0.3) + sin(10 * x[i]) * 0.2;
            res(1) += 1 - (x[i] - 0.3) * (x[i] - 0.3) * 0.4;
        }
        return res;
    }
};
Aneoshun's avatar
Aneoshun committed
150

151
152
int main()
{
Aneoshun's avatar
Aneoshun committed
153

154
155
    typedef kernel::SquaredExpARD<Params> Kernel_t;
    typedef mean::FunctionARD<Params, MeanComplet<Params>> Mean_t;
156
    typedef model::GP<Params, Kernel_t, Mean_t> GP_t;
157
    typedef UCB_multi<Params, GP_t> Acqui_t;
158
159
    
    bayes_opt::BOptimizer<Params, modelfun<GP_t>, acquifun<Acqui_t>, model::gp::KernelMeanLFOpt<Params>> opt;
160
    opt.optimize(fit_eval());
Aneoshun's avatar
Aneoshun committed
161

162
    std::cout << opt.best_observation() << " res  "
163
164
              << opt.best_sample().transpose() << std::endl;
    return 0;
Aneoshun's avatar
Aneoshun committed
165
}