multi.cpp 6.16 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/limbo.hpp>
Konstantinos Chatzilygeroudis's avatar
Konstantinos Chatzilygeroudis committed
46
47
48
#include <limbo/experimental/bayes_opt/parego.hpp>
#include <limbo/experimental/bayes_opt/nsbo.hpp>
#include <limbo/experimental/bayes_opt/ehvi.hpp>
49
#include <limbo/experimental/stat/pareto_benchmark.hpp>
Jean-Baptiste Mouret's avatar
Jean-Baptiste Mouret committed
50
51
52

using namespace limbo;

53
struct Params {
54
    struct bayes_opt_boptimizer : public defaults::bayes_opt_boptimizer {
55
56
        BO_PARAM(double, noise, 0.01);
    };
57

58
59
    struct init_randomsampling {
        BO_PARAM(int, samples, 10);
60
    };
61

62
    struct kernel_exp : public defaults::kernel_exp {
63
64
    };

65
66
    struct bayes_opt_bobase {
        BO_PARAM(bool, stats_enabled, true);
67
    };
68

69
70
    struct stop_maxiterations {
        BO_PARAM(int, iterations, 30);
71
    };
72

73
    struct acqui_ucb : public defaults::acqui_ucb {
74
    };
75

76
    struct acqui_gpucb : public defaults::acqui_gpucb {
77
    };
78

79
#ifdef USE_LIBCMAES
80
    struct opt_cmaes : public defaults::opt_cmaes {
81
    };
82
83
84
85
86
87
88
#elif defined(USE_NLOPT)
    struct opt_nloptnograd : public defaults::opt_nloptnograd {
    };
#else
    struct opt_gridsearch : public defaults::opt_gridsearch {
    };
#endif
89

90
    struct mean_constant : public defaults::mean_constant {
91
    };
92

93
    struct bayes_opt_ehvi {
94
95
96
        BO_PARAM(double, x_ref, -11);
        BO_PARAM(double, y_ref, -11);
    };
Jean-Baptiste Mouret's avatar
Jean-Baptiste Mouret committed
97
98
};

99
100
#ifdef DIM6
#define ZDT_DIM 6
101
#elif defined(DIM2)
Jean-Baptiste Mouret's avatar
misc    
Jean-Baptiste Mouret committed
102
#define ZDT_DIM 2
103
104
105
106
#else
#define ZDT_DIM 30
#endif

107
struct zdt1 {
108
    static constexpr size_t dim_in = ZDT_DIM;
109
110
111
112
113
114
115
116
117
118
119
120
121
    Eigen::VectorXd operator()(const Eigen::VectorXd& x) const
    {
        Eigen::VectorXd res(2);
        double f1 = x(0);
        double g = 1.0;
        for (int i = 1; i < x.size(); ++i)
            g += 9.0 / (x.size() - 1) * x(i);
        double h = 1.0f - sqrtf(f1 / g);
        double f2 = g * h;
        res(0) = 1.0 - f1;
        res(1) = 1.0 - f2;
        return res;
    }
122
123
};

124
struct zdt2 {
125
    static constexpr size_t dim_in = ZDT_DIM;
126
127
128
129
130
131
132
133
134
135
136
137
138
    Eigen::VectorXd operator()(const Eigen::VectorXd& x) const
    {
        Eigen::VectorXd res(2);
        double f1 = x(0);
        double g = 1.0;
        for (int i = 1; i < x.size(); ++i)
            g += 9.0 / (x.size() - 1) * x(i);
        double h = 1.0f - pow((f1 / g), 2.0);
        double f2 = g * h;
        res(0) = 1.0 - f1;
        res(1) = 1.0 - f2;
        return res;
    }
Jean-Baptiste Mouret's avatar
Jean-Baptiste Mouret committed
139
140
};

141
struct zdt3 {
142
    static constexpr size_t dim_in = ZDT_DIM;
143
144
145
146
147
148
149
150
151
152
153
154
155
    Eigen::VectorXd operator()(const Eigen::VectorXd& x) const
    {
        Eigen::VectorXd res(2);
        double f1 = x(0);
        double g = 1.0;
        for (int i = 1; i < x.size(); ++i)
            g += 9.0 / (x.size() - 1) * x(i);
        double h = 1.0f - sqrtf(f1 / g) - f1 / g * sin(10 * M_PI * f1);
        double f2 = g * h;
        res(0) = 1.0 - f1;
        res(1) = 1.0 - f2;
        return res;
    }
156
157
};

158
struct mop2 {
159
160
161
    static constexpr size_t dim_in = 2;
    static constexpr size_t dim_out = 2;

162
163
164
165
166
167
168
169
    Eigen::VectorXd operator()(const Eigen::VectorXd& x) const
    {
        Eigen::VectorXd res(2);
        // scale to [-2, 2]
        Eigen::VectorXd xx = (x * 4.0).array() - 2.0;
        // f1, f2
        Eigen::VectorXd v1 = (xx.array() - 1.0 / sqrt(xx.size())).array().square();
        Eigen::VectorXd v2 = (xx.array() + 1.0 / sqrt(xx.size())).array().square();
170
171
        double f1 = 1.0 - exp(-v1.sum());
        double f2 = 1.0 - exp(-v2.sum());
172
        // we _maximize in [0:1]
173
174
        res(0) = 1 - f1;
        res(1) = 1 - f2;
175
176
        return res;
    }
Jean-Baptiste Mouret's avatar
Jean-Baptiste Mouret committed
177
178
};

179
180
int main()
{
181
    tools::par::init();
Jean-Baptiste Mouret's avatar
Jean-Baptiste Mouret committed
182

183
#ifdef ZDT1
184
    typedef zdt1 func_t;
185
#elif defined ZDT2
186
    typedef zdt2 func_t;
Jean-Baptiste Mouret's avatar
Jean-Baptiste Mouret committed
187
#elif defined ZDT3
188
    typedef zdt3 func_t;
Jean-Baptiste Mouret's avatar
Jean-Baptiste Mouret committed
189
#else
190
    typedef mop2 func_t;
Jean-Baptiste Mouret's avatar
Jean-Baptiste Mouret committed
191
192
#endif

193
194
    using stat_t = boost::fusion::vector<experimental::stat::ParetoBenchmark<func_t>>;

195
#ifdef PAREGO
196
    Parego<Params, statsfun<stat_t>> opt;
197
#elif defined(NSBO)
198
    Nsbo<Params, statsfun<stat_t>> opt;
Jean-Baptiste Mouret's avatar
Jean-Baptiste Mouret committed
199
#else
200
    experimental::bayes_opt::Ehvi<Params, statsfun<stat_t>> opt;
201
#endif
202
    opt.optimize(func_t());
203

204
    return 0;
Jean-Baptiste Mouret's avatar
Jean-Baptiste Mouret committed
205
}