Commit 67d13e63 authored by Jean-Baptiste Mouret's avatar Jean-Baptiste Mouret
Browse files

parallelize the evaluation in CMA-ES

parent 8215d57f
......@@ -130,7 +130,9 @@ namespace limbo {
this->add_new_sample(new_sample, feval(new_sample));
std::cout << this->_iteration
<< " | new sample:" << new_sample.transpose()
<< " => " << feval(new_sample).transpose() << std::endl;
<< " => "
<< this->_observations[this->_observations.size() - 1].transpose()
<< std::endl;
_update_stats();
this->_iteration++;
......
......@@ -34,7 +34,6 @@ namespace limbo {
for (int i = 0; i < _observations.size(); ++i)
_observations(i) = observations[i];
_mean_observation = _observations.sum() / _observations.size();
_mean_vector.resize(_samples.size());
......@@ -45,7 +44,7 @@ namespace limbo {
_compute_kernel();
}
// return mu, sigma
// return mu, sigma (unormaliz)
std::tuple<double, double> query(const Eigen::VectorXd& v) const {
if (_samples.size() == 0)
return std::make_tuple(_mean_function(v, *this),
......@@ -90,6 +89,7 @@ namespace limbo {
Eigen::VectorXd _observations;
Eigen::VectorXd _mean_vector;
Eigen::VectorXd _obs_mean;
double _noise;
Eigen::VectorXd _alpha;
double _mean_observation;
......@@ -107,7 +107,7 @@ namespace limbo {
_kernel(i, j) = _kernel_function(_samples[i], _samples[j]) + _noise;
// O(n^3)
_inverted_kernel = _kernel.inverse();
// _inverted_kernel = _kernel.inverse();
_llt = Eigen::LLT<Eigen::MatrixXd>(this->_kernel);
......
......@@ -13,7 +13,7 @@
#include "cmaes/cmaes_interface.h"
#include "cmaes/boundary_transformation.h"
#include "limbo/parallel.hpp"
namespace limbo {
......@@ -61,18 +61,24 @@ namespace limbo {
(900.0 * (dim + 3.0) * (dim + 3.0))
: Params::cmaes::max_fun_evals();
Eigen::VectorXd v(acqui.dim());
int pop_size = cmaes_Get(&evo, "popsize");
double** all_x_in_bounds = new double*[pop_size];
for (int i = 0; i < pop_size; ++i)
all_x_in_bounds[i] = cmaes_NewDouble(dim);
std::vector<Eigen::VectorXd> pop_eigen(pop_size, Eigen::VectorXd(dim));
while (!(stop = cmaes_TestForTermination(&evo))) {
pop = cmaes_SamplePopulation(&evo);
for (i = 0; i < cmaes_Get(&evo, "popsize"); ++i) {
boundary_transformation(&boundaries, pop[i], x_in_bounds, dim);
for (int j = 0; j < v.size(); ++j)
v(j) = x_in_bounds[j];
fitvals[i] = -acqui(v);
}
par::loop(0, pop_size, [&](int i) {
boundary_transformation(&boundaries, pop[i], all_x_in_bounds[i], dim);
for (int j = 0; j < dim; ++j)
pop_eigen[i](j) = x_in_bounds[j];
fitvals[i] = -acqui(pop_eigen[i]);
});
cmaes_UpdateDistribution(&evo, fitvals);
}
for (int i = 0; i < pop_size; ++i)
free(all_x_in_bounds[i]);
lambda = incpopsize * cmaes_Get(&evo, "lambda");
countevals = cmaes_Get(&evo, "eval");
......@@ -82,6 +88,7 @@ namespace limbo {
xbestever = cmaes_GetInto(&evo, "xbestever", xbestever); /* alloc mem if needed */
}
const double *xmean = cmaes_GetPtr(&evo, "xmean");
Eigen::VectorXd v(acqui.dim());
for (int j = 0; j < v.size(); ++j)
v(j) = xmean[j];
......@@ -107,6 +114,8 @@ namespace limbo {
free(xbestever);
boundary_transformation_exit(&boundaries);
free(x_in_bounds);
return result;
}
......
......@@ -83,6 +83,9 @@ namespace limbo {
double z = (x1 - x2).cwiseQuotient(_ell).squaredNorm();
return _sf2 * exp(-0.5 * z);
}
const Eigen::VectorXd& ell() const {
return _ell;
}
protected:
double _sf2;
Eigen::VectorXd _ell;
......
......@@ -8,6 +8,7 @@
#include <tbb/task_scheduler_init.h>
#include <tbb/parallel_for.h>
#include <tbb/parallel_sort.h>
#include <tbb/parallel_reduce.h>
#endif
namespace par {
......
......@@ -95,6 +95,7 @@ namespace pareto {
if (non_dominated<K>(std::get<K>(p[i]), p))
pareto.push_back(p[i]);
});
std::sort(pareto.begin(), pareto.end(), compare_objs_lex<K>());
return par::convert_vector(pareto);
}
......@@ -134,10 +135,10 @@ namespace pareto {
assert(v.size());
size_t nb_objs = std::get<K>(v[0]).size();
assert(nb_objs > 1);
if (nb_objs == 2)
return impl::sort_2objs<K>(v);
else
return impl::pareto_set_std<K>(v);
/* if (nb_objs == 2)
return impl::sort_2objs<K>(v);
else*/
return impl::pareto_set_std<K>(v);
}
}
......
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