Commit 80dbe59d authored by Konstantinos Chatzilygeroudis's avatar Konstantinos Chatzilygeroudis
Browse files

Transfered functor to GP from BOptimizer

parent c2d79603
#ifndef LIMBO_BAYES_OPT_BO_BASE_HPP
#define LIMBO_BAYES_OPT_BO_BASE_HPP
#define BOOST_PARAMETER_MAX_ARITY 7
#include <vector>
#include <iostream>
#include <limits>
......@@ -24,7 +24,6 @@
#include <limbo/mean/data.hpp>
#include <limbo/opt/cmaes.hpp>
#include <limbo/model/gp.hpp>
#include <limbo/opt/impl/model_no_opt.hpp>
#include <limbo/init/random_sampling.hpp>
namespace limbo {
......@@ -59,7 +58,6 @@ namespace limbo {
BOOST_PARAMETER_TEMPLATE_KEYWORD(modelfun)
BOOST_PARAMETER_TEMPLATE_KEYWORD(statsfun)
BOOST_PARAMETER_TEMPLATE_KEYWORD(stopcrit)
BOOST_PARAMETER_TEMPLATE_KEYWORD(optfun)
template <typename T, typename std::enable_if<std::is_arithmetic<T>::value, int>::type = 0>
inline bool is_nan_or_inf(T v)
......@@ -83,13 +81,12 @@ namespace limbo {
boost::parameter::optional<tag::initfun>,
boost::parameter::optional<tag::acquifun>,
boost::parameter::optional<tag::stopcrit>,
boost::parameter::optional<tag::modelfun>,
boost::parameter::optional<tag::optfun>> class_signature;
boost::parameter::optional<tag::modelfun>> class_signature;
template <class Params, class A1 = boost::parameter::void_,
class A2 = boost::parameter::void_, class A3 = boost::parameter::void_,
class A4 = boost::parameter::void_, class A5 = boost::parameter::void_,
class A6 = boost::parameter::void_, class A7 = boost::parameter::void_>
class A6 = boost::parameter::void_>
class BoBase {
public:
typedef Params params_t;
......@@ -105,7 +102,6 @@ namespace limbo {
typedef acqui::GP_UCB<Params, model_t> acqui_t; // 4
typedef stat::Acquisitions<Params> stat_t; // 5
typedef boost::fusion::vector<stop::MaxIterations<Params>> stop_t; // 6
typedef opt::impl::ModelNoOpt<Params> opt_t; // 7
};
// extract the types
......@@ -116,7 +112,6 @@ namespace limbo {
typedef typename boost::parameter::binding<args, tag::modelfun, typename defaults::model_t>::type model_t;
typedef typename boost::parameter::binding<args, tag::statsfun, typename defaults::stat_t>::type Stat;
typedef typename boost::parameter::binding<args, tag::stopcrit, typename defaults::stop_t>::type StoppingCriteria;
typedef typename boost::parameter::binding<args, tag::optfun, typename defaults::opt_t>::type opt_t;
typedef typename boost::mpl::if_<boost::fusion::traits::is_sequence<StoppingCriteria>, StoppingCriteria, boost::fusion::vector<StoppingCriteria>>::type stopping_criteria_t;
typedef typename boost::mpl::if_<boost::fusion::traits::is_sequence<Stat>, Stat, boost::fusion::vector<Stat>>::type stat_t;
......
......@@ -18,14 +18,13 @@ namespace limbo {
template <class Params, class A1 = boost::parameter::void_,
class A2 = boost::parameter::void_, class A3 = boost::parameter::void_,
class A4 = boost::parameter::void_, class A5 = boost::parameter::void_,
class A6 = boost::parameter::void_, class A7 = boost::parameter::void_>
class BOptimizer : public BoBase<Params, A1, A2, A3, A4, A5, A6, A7> {
class A6 = boost::parameter::void_>
class BOptimizer : public BoBase<Params, A1, A2, A3, A4, A5, A6> {
public:
typedef BoBase<Params, A1, A2, A3, A4, A5, A6, A7> base_t;
typedef BoBase<Params, A1, A2, A3, A4, A5, A6> base_t;
typedef typename base_t::model_t model_t;
typedef typename base_t::acquisition_function_t acquisition_function_t;
typedef typename base_t::acqui_optimizer_t acqui_optimizer_t;
typedef typename base_t::opt_t opt_t;
template <typename AcquisitionFunction, typename AggregatorFunction>
struct AcquiOptimization {
......@@ -80,7 +79,7 @@ namespace limbo {
blacklisted = true;
}
opt_t()(_model, this->_samples, this->_observations,
_model.compute(this->_samples, this->_observations,
Params::boptimizer::noise(), this->_bl_samples);
this->_update_stats(*this, blacklisted);
......
......@@ -6,15 +6,33 @@
#include <limits>
#include <vector>
#include <boost/parameter.hpp>
#include <Eigen/Core>
#include <Eigen/LU>
#include <Eigen/Cholesky>
#include <limbo/opt/impl/model_no_opt.hpp>
namespace limbo {
BOOST_PARAMETER_TEMPLATE_KEYWORD(optfun)
namespace model {
template <typename Params, typename KernelFunction, typename MeanFunction>
typedef boost::parameter::parameters<boost::parameter::optional<tag::optfun>> gp_signature;
template <typename Params, typename KernelFunction, typename MeanFunction, class OptFun = boost::parameter::void_>
class GP {
public:
// defaults
struct defaults {
typedef opt::impl::ModelNoOpt<Params> opt_t; // 1
};
typedef typename gp_signature::bind<OptFun>::type args;
typedef typename boost::parameter::binding<args, tag::optfun, typename defaults::opt_t>::type opt_t;
GP() : _dim_in(-1), _dim_out(-1) {}
// useful because the model might be created before having samples
GP(int dim_in, int dim_out)
......@@ -48,6 +66,8 @@ namespace limbo {
_compute_obs_mean();
_compute_kernel();
opt_t()(*this);
}
// return mu, sigma (unormaliz)
......
......@@ -102,10 +102,8 @@ namespace limbo {
template <typename Params>
struct GPKernelLFOpt {
template <typename Opt>
void operator()(Opt& opt, const std::vector<Eigen::VectorXd>& samples, const std::vector<Eigen::VectorXd>& observations, double noise,
const std::vector<Eigen::VectorXd>& bl_samples = std::vector<Eigen::VectorXd>())
void operator()(Opt& opt)
{
opt.compute(samples, observations, noise, bl_samples);
GPKernelLFOptStruct<Params, Opt> util(opt);
ParallelRepeater<Params, Rprop<Params>> par_rprop;
auto params = par_rprop(util);
......
......@@ -109,10 +109,8 @@ namespace limbo {
template <typename Params>
struct GPKernelMeanLFOpt {
template <typename Opt>
void operator()(Opt& opt, const std::vector<Eigen::VectorXd>& samples, const std::vector<Eigen::VectorXd>& observations, double noise,
const std::vector<Eigen::VectorXd>& bl_samples = std::vector<Eigen::VectorXd>())
void operator()(Opt& opt)
{
opt.compute(samples, observations, noise, bl_samples);
GPKernelMeanLFOptStruct<Params, Opt> util(opt);
ParallelRepeater<Params, Rprop<Params>> par_rprop;
auto params = par_rprop(util);
......
......@@ -93,10 +93,8 @@ namespace limbo {
template <typename Params>
struct GPMeanLFOpt {
template <typename Opt>
void operator()(Opt& opt, const std::vector<Eigen::VectorXd>& samples, const std::vector<Eigen::VectorXd>& observations, double noise,
const std::vector<Eigen::VectorXd>& bl_samples = std::vector<Eigen::VectorXd>())
void operator()(Opt& opt)
{
opt.compute(samples, observations, noise, bl_samples);
GPMeanLFOptStruct<Params, Opt> util(opt);
ParallelRepeater<Params, Rprop<Params>> par_rprop;
auto params = par_rprop(util);
......
......@@ -11,11 +11,7 @@ namespace limbo {
template <typename Params>
struct ModelNoOpt {
template <typename Opt>
void operator()(Opt& opt, const std::vector<Eigen::VectorXd>& samples, const std::vector<Eigen::VectorXd>& observations, double noise,
const std::vector<Eigen::VectorXd>& bl_samples = std::vector<Eigen::VectorXd>())
{
opt.compute(samples, observations, noise, bl_samples);
}
void operator()(Opt& opt) {}
};
}
}
......
......@@ -9,10 +9,9 @@ using namespace limbo;
struct Params {
struct rprop {
BO_PARAM(int, n_rprop, 300);
BO_PARAM(int, rprop_restart, 10);
struct rprop : public defaults::rprop {
};
struct grid_search {
BO_PARAM(int, nb_pts, 10);
};
......@@ -92,11 +91,11 @@ BOOST_AUTO_TEST_CASE(test_bo_gp_auto)
typedef mean::Data<Params> Mean_t;
typedef boost::fusion::vector<stat::Acquisitions<Params>> Stat_t;
typedef init::NoInit<Params> Init_t;
typedef model::GP<Params, Kernel_t, Mean_t> GP_t;
typedef acqui::UCB<Params, GP_t> Acqui_t;
typedef opt::impl::GPKernelLFOpt<Params> opt_t;
typedef model::GP<Params, Kernel_t, Mean_t, optfun<opt_t>> GP_t;
typedef acqui::UCB<Params, GP_t> Acqui_t;
bayes_opt::BOptimizer<Params, modelfun<GP_t>, initfun<Init_t>, acquifun<Acqui_t>, acquiopt<AcquiOpt_t>, statsfun<Stat_t>, stopcrit<Stop_t>, optfun<opt_t>> opt;
bayes_opt::BOptimizer<Params, modelfun<GP_t>, initfun<Init_t>, acquifun<Acqui_t>, acquiopt<AcquiOpt_t>, statsfun<Stat_t>, stopcrit<Stop_t>> opt;
opt.optimize(fit_eval_map<Params>());
BOOST_CHECK_CLOSE(opt.best_sample()(0), 0.1, 0.000001);
......@@ -114,11 +113,11 @@ BOOST_AUTO_TEST_CASE(test_bo_gp_auto_mean)
typedef mean::FunctionARD<Params, mean::Data<Params>> Mean_t;
typedef boost::fusion::vector<stat::Acquisitions<Params>> Stat_t;
typedef init::NoInit<Params> Init_t;
typedef model::GP<Params, Kernel_t, Mean_t> GP_t;
typedef acqui::UCB<Params, GP_t> Acqui_t;
typedef opt::impl::GPKernelMeanLFOpt<Params> opt_t;
typedef model::GP<Params, Kernel_t, Mean_t, optfun<opt_t>> GP_t;
typedef acqui::UCB<Params, GP_t> Acqui_t;
bayes_opt::BOptimizer<Params, modelfun<GP_t>, initfun<Init_t>, acquifun<Acqui_t>, acquiopt<AcquiOpt_t>, statsfun<Stat_t>, stopcrit<Stop_t>, optfun<opt_t>> opt;
bayes_opt::BOptimizer<Params, modelfun<GP_t>, initfun<Init_t>, acquifun<Acqui_t>, acquiopt<AcquiOpt_t>, statsfun<Stat_t>, stopcrit<Stop_t>> opt;
opt.optimize(fit_eval_map<Params>());
BOOST_CHECK_CLOSE(opt.best_sample()(0), 0.1, 0.000001);
......@@ -136,11 +135,11 @@ BOOST_AUTO_TEST_CASE(test_bo_gp_mean)
typedef mean::FunctionARD<Params, mean::Data<Params>> Mean_t;
typedef boost::fusion::vector<stat::Acquisitions<Params>> Stat_t;
typedef init::NoInit<Params> Init_t;
typedef model::GP<Params, Kernel_t, Mean_t> GP_t;
typedef acqui::UCB<Params, GP_t> Acqui_t;
typedef opt::impl::GPMeanLFOpt<Params> opt_t;
typedef model::GP<Params, Kernel_t, Mean_t, optfun<opt_t>> GP_t;
typedef acqui::UCB<Params, GP_t> Acqui_t;
bayes_opt::BOptimizer<Params, modelfun<GP_t>, initfun<Init_t>, acquifun<Acqui_t>, acquiopt<AcquiOpt_t>, statsfun<Stat_t>, stopcrit<Stop_t>, optfun<opt_t>> opt;
bayes_opt::BOptimizer<Params, modelfun<GP_t>, initfun<Init_t>, acquifun<Acqui_t>, acquiopt<AcquiOpt_t>, statsfun<Stat_t>, stopcrit<Stop_t>> opt;
opt.optimize(fit_eval_map<Params>());
BOOST_CHECK_CLOSE(opt.best_sample()(0), 0.1, 0.000001);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment