max_predicted_value.hpp 3.75 KB
Newer Older
1
2
#ifndef LIMBO_STOP_MAX_PREDICTED_VALUE_HPP
#define LIMBO_STOP_MAX_PREDICTED_VALUE_HPP
3
4

#include <iostream>
5

6
7
#include <boost/parameter/aux_/void.hpp>

8
9
10
#include <Eigen/Core>

#include <limbo/tools/macros.hpp>
11
#include <limbo/tools/random_generator.hpp>
12

13
14
namespace limbo {
    namespace defaults {
15
        struct stop_maxpredictedvalue {
Jean-Baptiste Mouret's avatar
Jean-Baptiste Mouret committed
16
            ///@ingroup stop_defaults
17
            BO_PARAM(double, ratio, 0.9);
18
19
        };
    }
20
    namespace stop {
Jean-Baptiste Mouret's avatar
Jean-Baptiste Mouret committed
21
        ///@ingroup stop
Jean-Baptiste Mouret's avatar
Jean-Baptiste Mouret committed
22
        ///Stop once the value for the best sample is above : ratio * (best value predicted by the model)
Jean-Baptiste Mouret's avatar
Jean-Baptiste Mouret committed
23
        ///
Jean-Baptiste Mouret's avatar
Jean-Baptiste Mouret committed
24
        ///Parameter: double ratio
25
        template <typename Params, typename Optimizer = boost::parameter::void_>
26
        struct MaxPredictedValue {
27
28
29

            MaxPredictedValue() {}

30
            template <typename BO, typename AggregatorFunction>
31
            bool operator()(const BO& bo, const AggregatorFunction& afun) const
32
            {
Jean-Baptiste Mouret's avatar
Jean-Baptiste Mouret committed
33
                // Prevent instantiation of GPMean if there are no observed samples
34
                if (bo.observations().size() == 0)
35
                    return false;
36

37
                auto optimizer = _get_optimizer(typename BO::acqui_optimizer_t(), Optimizer());
38
                Eigen::VectorXd starting_point = tools::random_vector(bo.model().dim_in());
39
                auto model_optimization =
40
                    [&](const Eigen::VectorXd& x, bool g) { return opt::no_grad(afun(bo.model().mu(x))); };
41
42
                auto x = optimizer(model_optimization, starting_point, true);
                double val = afun(bo.model().mu(x));
43

44
                if (bo.observations().size() == 0 || afun(bo.best_observation(afun)) <= Params::stop_maxpredictedvalue::ratio() * val)
45
                    return false;
46
                else {
47
                    std::cout << "stop caused by Max predicted value reached. Threshold: "
48
                              << Params::stop_maxpredictedvalue::ratio() * val
49
                              << " max observations: " << afun(bo.best_observation(afun)) << std::endl;
50
                    return true;
51
52
53
54
                }
            }

        protected:
55
56
57
58
            template <typename Model, typename AggregatorFunction>
            struct ModelMeanOptimization {
            public:
                ModelMeanOptimization(const Model& model, const AggregatorFunction& afun, const Eigen::VectorXd& init) : _model(model), _afun(afun), _init(init) {}
59

60
                double utility(const Eigen::VectorXd& v) const
61
62
63
                {
                    return _afun(_model.mu(v));
                }
64

65
                size_t param_size() const
66
                {
67
68
                    return _model.dim_in();
                }
69

70
71
72
73
74
                const Eigen::VectorXd& init() const
                {
                    return _init;
                }

75
            protected:
76
77
78
                const Model& _model;
                const AggregatorFunction& _afun;
                const Eigen::VectorXd _init;
79
            };
80
81
82
83
84
85
86

            template <typename Model, typename AggregatorFunction>
            inline ModelMeanOptimization<Model, AggregatorFunction> _make_model_mean_optimization(const Model& model, const AggregatorFunction& afun, const Eigen::VectorXd& init) const
            {
                return ModelMeanOptimization<Model, AggregatorFunction>(model, afun, init);
            }

87
88
            template <typename BoAcquiOpt>
            inline BoAcquiOpt _get_optimizer(const BoAcquiOpt& bo_acqui_opt, boost::parameter::void_) const
89
90
91
92
            {
                return bo_acqui_opt;
            }

93
            template <typename BoAcquiOpt, typename CurrentOpt>
94
95
96
97
            inline CurrentOpt _get_optimizer(const BoAcquiOpt&, const CurrentOpt& current_opt) const
            {
                return current_opt;
            }
98
99
        };
    }
100
}
101

102
#endif