Commit 7ad606ee authored by Jean-Baptiste Mouret's avatar Jean-Baptiste Mouret
Browse files

minor improvements, before attacking boptimizer

parent 0cba6e9c
......@@ -18,14 +18,14 @@ namespace limbo {
template <typename Params, typename Optimizer>
struct ParallelRepeater {
template <typename F>
Eigen::VectorXd operator()(const F& f, bool bounded) const
Eigen::VectorXd operator()(const F& f, const Eigen::VectorXd& init, bool bounded) const
{
tools::par::init();
typedef std::pair<Eigen::VectorXd, double> pair_t;
auto body = [&](int i) {
// clang-format off
Eigen::VectorXd v = Optimizer()(f, bounded);
Eigen::VectorXd r_init = tools::rand_vec(init.size());
Eigen::VectorXd v = Optimizer()(f, init, bounded);
double lik = f.utility(v);
return std::make_pair(v, lik);
// clang-format on
......@@ -37,8 +37,8 @@ namespace limbo {
// clang-format on
};
pair_t init = std::make_pair(f.init(), -std::numeric_limits<float>::max());
auto m = tools::par::max(init, Params::opt_parallelrepeater::repeats(), body, comp);
pair_t init_v = std::make_pair(init, -std::numeric_limits<float>::max());
auto m = tools::par::max(init_v, Params::opt_parallelrepeater::repeats(), body, comp);
return m.first;
};
......
......@@ -26,10 +26,10 @@ namespace limbo {
template <typename Params>
struct Rprop {
template <typename F>
Eigen::VectorXd operator()(const F& f, bool bounded) const
Eigen::VectorXd operator()(const F& f, const Eigen::VectorXd& init, bool bounded) const
{
// params
size_t param_dim = f.param_size();
size_t param_dim = init.size();
double delta0 = 0.1;
double deltamin = 1e-6;
double deltamax = 50;
......@@ -39,7 +39,7 @@ namespace limbo {
Eigen::VectorXd delta = Eigen::VectorXd::Ones(param_dim) * delta0;
Eigen::VectorXd grad_old = Eigen::VectorXd::Zero(param_dim);
Eigen::VectorXd params = f.init();
Eigen::VectorXd params = init;
if (bounded) {
for (int j = 0; j < params.size(); j++) {
......@@ -55,12 +55,12 @@ namespace limbo {
for (int i = 0; i < Params::opt_rprop::iterations(); ++i) {
auto perf = f.utility_and_grad(params);
double lik = std::get<0>(perf);
double lik = opt::fun(perf);
if (lik > best) {
best = lik;
best_params = params;
}
Eigen::VectorXd grad = -std::get<1>(perf);
Eigen::VectorXd grad = -opt::grad(perf);
grad_old = grad_old.cwiseProduct(grad);
for (int j = 0; j < grad_old.size(); ++j) {
......
......@@ -48,6 +48,13 @@
// someday we will have a real thread-safe random number generator...
namespace limbo {
namespace tools {
// return a random vector in [0, 1]
inline Eigen::VectorXd rand_vec(size_t size)
{
return (Eigen::VectorXd::Random(size).array() + 1) / 2.0;
}
// NOT Thread-safe !
template <typename T>
inline T rand(T max = 1.0)
......
......@@ -7,6 +7,7 @@
#include <limbo/opt/nlopt_grad.hpp>
#include <limbo/opt/nlopt_no_grad.hpp>
using namespace limbo;
struct Params {
struct opt_nloptgrad {
......@@ -18,11 +19,11 @@ struct Params {
};
};
limbo::opt::eval_t my_function(const Eigen::VectorXd& params, bool eval_grad)
opt::eval_t my_function(const Eigen::VectorXd& params, bool eval_grad)
{
double v = -params(0) * params(0) - params(1) * params(1);
if (!eval_grad)
return limbo::opt::no_grad(v);
return opt::no_grad(v);
Eigen::VectorXd grad(2);
grad(0) = -2 * params(0);
grad(1) = -2 * params(1);
......@@ -31,9 +32,8 @@ limbo::opt::eval_t my_function(const Eigen::VectorXd& params, bool eval_grad)
BOOST_AUTO_TEST_CASE(test_nlopt_grad_simple)
{
limbo::opt::NLOptGrad<Params, nlopt::LD_MMA> optimizer;
Eigen::VectorXd init = (Eigen::VectorXd::Random(2).array() + 1) / 2.0;
Eigen::VectorXd g = optimizer(my_function, init, false);
opt::NLOptGrad<Params, nlopt::LD_MMA> optimizer;
Eigen::VectorXd g = optimizer(my_function, tools::rand_vec(2), false);
BOOST_CHECK_SMALL(g(0), 0.00000001);
BOOST_CHECK_SMALL(g(1), 0.00000001);
......@@ -41,10 +41,8 @@ BOOST_AUTO_TEST_CASE(test_nlopt_grad_simple)
BOOST_AUTO_TEST_CASE(test_nlopt_no_grad_simple)
{
limbo::opt::NLOptGrad<Params, nlopt::LN_COBYLA> optimizer;
Eigen::VectorXd init = (Eigen::VectorXd::Random(2).array() + 1) / 2.0;
Eigen::VectorXd g = optimizer(my_function, init, false);
opt::NLOptGrad<Params, nlopt::LN_COBYLA> optimizer;
Eigen::VectorXd g = optimizer(my_function, tools::rand_vec(2), false);
BOOST_CHECK_SMALL(g(0), 0.00000001);
BOOST_CHECK_SMALL(g(1), 0.00000001);
......
......@@ -117,6 +117,9 @@ BOOST_AUTO_TEST_CASE(test_grid_search_bi_dim)
BOOST_CHECK_EQUAL(bidim_calls, (Params::opt_gridsearch::bins() + 1) * (Params::opt_gridsearch::bins() + 1) + 21);
}
// TODO test parallel repeater
// TODO test rprop
/*
BOOST_AUTO_TEST_CASE(test_cmaes_mono_dim)
{
......
Markdown is supported
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