Commit 908d5733 authored by Konstantinos Chatzilygeroudis's avatar Konstantinos Chatzilygeroudis
Browse files

Ran formatter

parent e7380ae1
......@@ -12,14 +12,14 @@ struct Params {
};
#ifdef USE_LIBCMAES
struct opt_cmaes : public defaults::opt_cmaes {
};
struct opt_cmaes : public defaults::opt_cmaes {
};
#elif defined(USE_NLOPT)
struct opt_nloptnograd : public defaults::opt_nloptnograd {
};
struct opt_nloptnograd : public defaults::opt_nloptnograd {
};
#else
struct opt_gridsearch : public defaults::opt_gridsearch {
};
struct opt_gridsearch : public defaults::opt_gridsearch {
};
#endif
struct kernel_maternfivehalfs {
BO_PARAM(double, sigma, 1);
......
......@@ -15,7 +15,7 @@ struct Params {
struct opt_nloptnograd : public defaults::opt_nloptnograd {
};
#else
struct opt_gridsearch : public defaults::opt_gridsearch {
struct opt_gridsearch : public defaults::opt_gridsearch {
};
#endif
struct opt_rprop : public defaults::opt_rprop {
......
......@@ -63,7 +63,8 @@ namespace limbo {
return x(0);
}
};
class EvaluationError : public std::exception {};
class EvaluationError : public std::exception {
};
// we use optimal named template parameters
// see:
......
......@@ -40,7 +40,7 @@ namespace limbo {
<< this->_models[1].mu(best_v) << ")"
<< " sigma:" << this->_models[0].sigma(best_v) << " "
<< this->_models[1].sigma(best_v) << std::endl;
this->_update_stats(*this, FirstElem(), false);
this->_update_stats(*this, FirstElem(), false);
this->_current_iteration++;
this->_total_iterations++;
}
......
......@@ -29,20 +29,21 @@ namespace limbo {
template <typename F>
Eigen::VectorXd operator()(const F& f, const Eigen::VectorXd& init, double bounded) const
{
size_t dim = init.size();
size_t dim = init.size();
// wrap the function
libcmaes::FitFunc f_cmaes = [&](const double* x, const int n) {
// wrap the function
libcmaes::FitFunc f_cmaes = [&](const double* x, const int n) {
Eigen::Map<const Eigen::VectorXd> m(x, n);
// remember that our optimizers maximize
return -eval(f, m);
};
if (bounded)
return _opt_bounded(f_cmaes, dim, init);
else
return _opt_unbounded(f_cmaes, dim, init);
};
if (bounded)
return _opt_bounded(f_cmaes, dim, init);
else
return _opt_unbounded(f_cmaes, dim, init);
}
private:
// F is a CMA-ES style function, not our function
template <typename F>
......
......@@ -9,35 +9,38 @@
namespace limbo {
namespace opt {
// return type of the function to optimize
typedef std::pair<double, boost::optional<Eigen::VectorXd> > eval_t;
// return with opt::no_grand(your_val) if no gradient is available
eval_t no_grad(double x) { return eval_t{x, boost::optional<Eigen::VectorXd>{} } ;}
// get the gradient from a function evaluation
const Eigen::VectorXd& grad(const eval_t& fg) {
assert(std::get<1>(fg).is_initialized());
return std::get<1>(fg).get();
}
// get the value from a function evaluation
double fun(const eval_t& fg) {
return std::get<0>(fg);
}
// eval f without gradient
template<typename F>
double eval(const F& f, const Eigen::VectorXd& x) {
return std::get<0>(f(x, false));
}
// eval f with gradient
template<typename F>
eval_t eval_grad(const F& f, const Eigen::VectorXd& x) {
return f(x, true);
}
// return type of the function to optimize
typedef std::pair<double, boost::optional<Eigen::VectorXd>> eval_t;
// return with opt::no_grand(your_val) if no gradient is available
eval_t no_grad(double x) { return eval_t{x, boost::optional<Eigen::VectorXd>{}}; }
// get the gradient from a function evaluation
const Eigen::VectorXd& grad(const eval_t& fg)
{
assert(std::get<1>(fg).is_initialized());
return std::get<1>(fg).get();
}
// get the value from a function evaluation
double fun(const eval_t& fg)
{
return std::get<0>(fg);
}
// eval f without gradient
template <typename F>
double eval(const F& f, const Eigen::VectorXd& x)
{
return std::get<0>(f(x, false));
}
// eval f with gradient
template <typename F>
eval_t eval_grad(const F& f, const Eigen::VectorXd& x)
{
return f(x, true);
}
}
}
......
......@@ -17,7 +17,7 @@ namespace limbo {
if (bo.total_iterations() == 0) {
(*this->_log_file) << "#iteration aggregated_observation" << std::endl;
for (size_t i = 0; i < bo.observations().size() -1; i++)
for (size_t i = 0; i < bo.observations().size() - 1; i++)
(*this->_log_file) << "-1 " << afun(bo.observations()[i]) << std::endl;
}
......
......@@ -17,7 +17,7 @@ namespace limbo {
if (bo.total_iterations() == 0) {
(*this->_log_file) << "#iteration bl_sample" << std::endl;
for (size_t i = 0; i < bo.bl_samples().size() -1; i++)
for (size_t i = 0; i < bo.bl_samples().size() - 1; i++)
(*this->_log_file) << "-1 " << bo.bl_samples()[i].transpose() << std::endl;
}
......
......@@ -17,7 +17,7 @@ namespace limbo {
if (bo.total_iterations() == 0) {
(*this->_log_file) << "#iteration observation" << std::endl;
for (size_t i = 0; i < bo.observations().size() -1; i++)
for (size_t i = 0; i < bo.observations().size() - 1; i++)
(*this->_log_file) << "-1 " << bo.observations()[i].transpose() << std::endl;
}
......
......@@ -17,7 +17,7 @@ namespace limbo {
if (bo.total_iterations() == 0) {
(*this->_log_file) << "#iteration sample" << std::endl;
for (size_t i = 0; i < bo.samples().size() -1; i++)
for (size_t i = 0; i < bo.samples().size() - 1; i++)
(*this->_log_file) << "-1 " << bo.samples()[i].transpose() << std::endl;
}
......
......@@ -4,7 +4,7 @@
#include <limbo/tools/macros.hpp>
namespace limbo {
namespace defaults {
namespace defaults {
struct stop_maxiterations {
BO_PARAM(int, iterations, 190);
};
......
......@@ -48,28 +48,28 @@ namespace limbo {
// random vector in [0, 1]
Eigen::VectorXd random_vector(int size)
{
// Eigen returns in [-1:1] (??)
return ((Eigen::VectorXd::Random(size)).array() + 1.0) / 2.0;
// Eigen returns in [-1:1] (??)
return ((Eigen::VectorXd::Random(size)).array() + 1.0) / 2.0;
}
// usage :
// rgen_double_t(0.0, 1.0);
// double r = rgen.rand();
template<typename D>
template <typename D>
class RandomGenerator {
public:
using result_type = typename D::result_type;
RandomGenerator(result_type min, result_type max) :
_dist(min, max), _rgen(std::random_device()()) {}
result_type rand()
{
std::lock_guard<std::mutex> lock(_mutex);
return _dist(_rgen);
}
using result_type = typename D::result_type;
RandomGenerator(result_type min, result_type max) : _dist(min, max), _rgen(std::random_device()()) {}
result_type rand()
{
std::lock_guard<std::mutex> lock(_mutex);
return _dist(_rgen);
}
private:
D _dist;
std::mt19937 _rgen;
std::mutex _mutex;
D _dist;
std::mt19937 _rgen;
std::mutex _mutex;
};
using rdist_double_t = std::uniform_real_distribution<double>;
using rdist_int_t = std::uniform_int_distribution<int>;
......
......@@ -36,12 +36,12 @@ struct Params {
BO_PARAM(int, bins, 20);
};
#ifdef USE_LIBCMAES
struct opt_cmaes : public defaults::opt_cmaes {
};
struct opt_cmaes : public defaults::opt_cmaes {
};
#endif
#ifdef USE_NLOPT
struct opt_nloptnograd : public defaults::opt_nloptnograd {
};
struct opt_nloptnograd : public defaults::opt_nloptnograd {
};
#endif
struct opt_rprop : public defaults::opt_rprop {
};
......@@ -50,7 +50,7 @@ struct Params {
};
struct init_gridsampling {
BO_PARAM(int, bins, 5);
BO_PARAM(int, bins, 5);
};
struct init_randomsampling {
......
......@@ -15,7 +15,7 @@ struct Params {
opt::eval_t fsphere(const Eigen::VectorXd& params, bool g)
{
return opt::no_grad(-params(0) * params(0) - params(1) * params(1));
return opt::no_grad(-params(0) * params(0) - params(1) * params(1));
}
BOOST_AUTO_TEST_CASE(test_cmaes_unbounded)
......@@ -26,7 +26,6 @@ BOOST_AUTO_TEST_CASE(test_cmaes_unbounded)
BOOST_CHECK_SMALL(g(1), 0.00000001);
}
BOOST_AUTO_TEST_CASE(test_cmaes_bounded)
{
Eigen::VectorXd g = limbo::opt::Cmaes<Params>()(fsphere, Eigen::VectorXd::Zero(2), true);
......
......@@ -23,7 +23,6 @@ struct Params {
};
};
// test with a standard function
int monodim_calls = 0;
opt::eval_t acqui_mono(const Eigen::VectorXd& v, bool eval_grad)
......@@ -44,8 +43,6 @@ struct FakeAcquiBi {
}
};
BOOST_AUTO_TEST_CASE(test_random_mono_dim)
{
using namespace limbo;
......@@ -54,8 +51,7 @@ BOOST_AUTO_TEST_CASE(test_random_mono_dim)
monodim_calls = 0;
for (int i = 0; i < 1000; i++) {
Eigen::VectorXd best_point =
optimizer(acqui_mono, Eigen::VectorXd::Constant(1, 0.5), true);
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);
......@@ -70,8 +66,7 @@ BOOST_AUTO_TEST_CASE(test_random_bi_dim)
bidim_calls = 0;
for (int i = 0; i < 1000; i++) {
Eigen::VectorXd best_point =
optimizer(FakeAcquiBi(), Eigen::VectorXd::Constant(2, 0.5), true);
Eigen::VectorXd best_point = optimizer(FakeAcquiBi(), Eigen::VectorXd::Constant(2, 0.5), true);
BOOST_CHECK_EQUAL(best_point.size(), 2);
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);
......@@ -87,9 +82,7 @@ BOOST_AUTO_TEST_CASE(test_grid_search_mono_dim)
opt::GridSearch<Params> optimizer;
monodim_calls = 0;
Eigen::VectorXd best_point =
optimizer(acqui_mono, Eigen::VectorXd::Constant(1, 0.5), true);
Eigen::VectorXd best_point = optimizer(acqui_mono, Eigen::VectorXd::Constant(1, 0.5), true);
BOOST_CHECK_EQUAL(best_point.size(), 1);
BOOST_CHECK_CLOSE(best_point(0), 1, 0.0001);
......@@ -103,9 +96,7 @@ BOOST_AUTO_TEST_CASE(test_grid_search_bi_dim)
opt::GridSearch<Params> optimizer;
bidim_calls = 0;
Eigen::VectorXd best_point =
optimizer(FakeAcquiBi(), Eigen::VectorXd::Constant(2, 0.5), true);
Eigen::VectorXd best_point = optimizer(FakeAcquiBi(), Eigen::VectorXd::Constant(2, 0.5), true);
BOOST_CHECK_EQUAL(best_point.size(), 2);
BOOST_CHECK_CLOSE(best_point(0), 1, 0.0001);
......@@ -113,4 +104,3 @@ 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);
}
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