eci.hpp 5.64 KB
Newer Older
1
2
3
4
5
6
7
8
//| 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)
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
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
45
//|
//| 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.
//|
46
47
#ifndef LIMBO_ACQUI_ECI_HPP
#define LIMBO_ACQUI_ECI_HPP
48

49
#include <Eigen/Core>
50
51
52
53
54
55
56
#include <cmath>
#include <vector>

#include <limbo/tools/macros.hpp>

namespace limbo {
    namespace defaults {
57
        struct acqui_eci {
58
59
60
61
62
63
64
            /// @ingroup acqui_defaults
            BO_PARAM(double, jitter, 0.0);
        };
    }

    namespace experimental {
        namespace acqui {
65
            template <typename Params, typename Model, typename ConstraintModel>
66
            class ECI {
67
            public:
68
                ECI(const Model& model, const ConstraintModel& constraint_model, int iteration = 0)
69
                    : _model(model), _constraint_model(constraint_model), _nb_samples(-1) {}
70

71
                size_t dim_in() const { return _model.dim_in(); }
72

73
                size_t dim_out() const { return _model.dim_out(); }
74
75

                template <typename AggregatorFunction>
76
                opt::eval_t operator()(const Eigen::VectorXd& v, const AggregatorFunction& afun, bool gradient)
77
                {
78
79
                    assert(!gradient);

80
81
                    Eigen::VectorXd mu;
                    double sigma_sq;
82
                    std::tie(mu, sigma_sq) = _model.query(v);
83
84
85
                    double sigma = std::sqrt(sigma_sq);

                    // If \sigma(x) = 0 or we do not have any observation yet we return 0
86
                    if (sigma < 1e-10 || _model.samples().size() < 1)
87
                        return opt::no_grad(0.0);
88

89
                    // Compute expected constrained improvement
90
91
92
93
94
95
96
97
98
99
                    // First find the best (predicted) observation so far -- if needed
                    if (_nb_samples != _model.nb_samples()) {
                        std::vector<double> rewards;
                        for (auto s : _model.samples()) {
                            rewards.push_back(afun(_model.mu(s)));
                        }

                        _nb_samples = _model.nb_samples();
                        _f_max = *std::max_element(rewards.begin(), rewards.end());
                    }
100
                    // Calculate Z and \Phi(Z) and \phi(Z)
101
                    double X = afun(mu) - _f_max - Params::acqui_eci::jitter();
102
103
                    double Z = X / sigma;
                    double phi = std::exp(-0.5 * std::pow(Z, 2.0)) / std::sqrt(2.0 * M_PI);
104
                    double Phi = 0.5 * std::erfc(-Z / std::sqrt(2));
105

106
                    return opt::no_grad(_pf(v, afun) * (X * Phi + sigma * phi));
107
108
109
                }

            protected:
110
111
                const Model& _model;
                const ConstraintModel& _constraint_model;
112
113
                int _nb_samples;
                double _f_max;
114
115
116
117

                template <typename AggregatorFunction>
                double _pf(const Eigen::VectorXd& v, const AggregatorFunction& afun) const
                {
118
119
120
121
                    Eigen::VectorXd mu;
                    double sigma_sq;
                    std::tie(mu, sigma_sq) = _constraint_model.query(v);
                    double sigma = std::sqrt(sigma_sq);
122

123
124
                    if (sigma < 1e-10 || _constraint_model.samples().size() < 1)
                        return 1.0;
125

126
127
                    double Z = (afun(mu) - 1.0) / sigma;
                    double Phi = 0.5 * std::erfc(-Z / std::sqrt(2));
128

129
                    return Phi;
130
131
132
133
134
135
136
                }
            };
        }
    }
}

#endif