Commit 94f79319 authored by Federico Allocati's avatar Federico Allocati
Browse files

Implementation and test of Chained Optimizer

parent d4efc50b
#ifndef LIMBO_OPT_CHAINED_HPP
#define LIMBO_OPT_CHAINED_HPP
#include <algorithm>
#include <Eigen/Core>
#include <limbo/opt/optimizer.hpp>
namespace limbo {
namespace opt {
// Needed for the variadic data structure
template <typename Params, typename... Optimizers> struct Chained {};
// Base case: just 1 optimizer to call
template <typename Params, typename Optimizer>
struct Chained<Params, Optimizer> {
template <typename F>
Eigen::VectorXd operator()(const F& f, const Eigen::VectorXd& init, bool bounded) const
{
return Optimizer()(f, init, bounded);
};
};
// Recursive case: call current optimizer, and pass result as init value for the next one
template <typename Params, typename Optimizer, typename... Optimizers>
struct Chained<Params, Optimizer, Optimizers...> : Chained<Params, Optimizers...> {
template <typename F>
Eigen::VectorXd operator()(const F& f, const Eigen::VectorXd& init, bool bounded) const
{
return Chained<Params, Optimizers...>::operator ()(f, Optimizer()(f, init, bounded), bounded);
};
};
}
}
#endif
......@@ -7,6 +7,7 @@
#include <limbo/opt/cmaes.hpp>
#include <limbo/opt/grid_search.hpp>
#include <limbo/opt/random_point.hpp>
#include <limbo/opt/chained.hpp>
using namespace limbo;
......@@ -104,3 +105,22 @@ BOOST_AUTO_TEST_CASE(test_grid_search_bi_dim)
// TO-DO: Maybe alter a little grid search so not to call more times the utility function
BOOST_CHECK_EQUAL(bidim_calls, (Params::opt_gridsearch::bins() + 1) * (Params::opt_gridsearch::bins() + 1) + 21);
}
BOOST_AUTO_TEST_CASE(test_chained)
{
using namespace limbo;
typedef opt::GridSearch<Params> opt_1_t;
typedef opt::RandomPoint<Params> opt_2_t;
typedef opt::GridSearch<Params> opt_3_t;
typedef opt::GridSearch<Params> opt_4_t;
opt::Chained<Params, opt_1_t, opt_2_t, opt_3_t, opt_4_t> optimizer;
monodim_calls = 0;
Eigen::VectorXd best_point = optimizer(acqui_mono, Eigen::VectorXd::Constant(1, 0.5), true);
BOOST_CHECK_EQUAL(best_point.size(), 1);
BOOST_CHECK(best_point(0) > 0 || std::abs(best_point(0)) < 1e-7);
BOOST_CHECK(best_point(0) < 1 || std::abs(best_point(0) - 1) < 1e-7);
BOOST_CHECK_EQUAL(monodim_calls, (Params::opt_gridsearch::bins() + 1) * 3);
}
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