Commit 81ad3dc8 authored by Antoine Cully's avatar Antoine Cully
Browse files

Bug: when calling the default constructor of gp and using multidimensional...

Bug: when calling the default constructor of gp and using multidimensional inputs/outputs, raises an assert from eigen. Corrected by allowing the gp to autonomously determine the dim
parent 88ee9d49
*~
*~
*#
.DS_Store
.project
.settings
......
......@@ -64,7 +64,7 @@ namespace limbo {
template <typename StateFunction, typename AggregatorFunction = FirstElem>
void optimize(const StateFunction& sfun, const AggregatorFunction& afun = AggregatorFunction(), bool reset = true)
{
_model = model_t(StateFunction::dim_in, StateFunction::dim_out);
this->_init(sfun, afun, reset);
if (!this->_observations.empty())
......
......@@ -20,24 +20,32 @@ namespace limbo {
public:
GP() : _dim_in(-1), _dim_out(-1) {}
// useful because the model might be created before having samples
GP(int dim_in, int dim_out)
: _dim_in(dim_in), _dim_out(dim_out), _kernel_function(dim_in), _mean_function(dim_out) {}
//GP(int dim_in, int dim_out)
// : _dim_in(dim_in), _dim_out(dim_out), _kernel_function(dim_in), _mean_function(dim_out) {}
void compute(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>())
{
if (_dim_in == -1) {
assert(samples.size() != 0);
assert(observations.size() != 0);
assert(samples.size() == observations.size());
_dim_in = samples[0].size();
_dim_out = observations[0].size();
}
//should be checked each time! not only the first time
assert(samples.size() != 0);
assert(observations.size() != 0);
assert(samples.size() == observations.size());
if(_dim_in != samples[0].size())
{
_dim_in = samples[0].size();
_kernel_function=KernelFunction(_dim_in); // the cost of building a functor should be relatively low
}
if(_dim_out != observations[0].size())
{
_dim_out = observations[0].size();
_mean_function = MeanFunction(_dim_out); // the cost of building a functor should be relatively low
}
_samples = samples;
_observations.resize(observations.size(), observations[0].size());
_observations.resize(observations.size(), _dim_out);
for (int i = 0; i < _observations.rows(); ++i)
_observations.row(i) = observations[i];
......
......@@ -19,6 +19,13 @@ Eigen::VectorXd make_v1(double x)
return v1;
}
Eigen::VectorXd make_v2(double x1, double x2)
{
Eigen::VectorXd v2(2);
v2 << x1,x2;
return v2;
}
struct Params {
struct kernel_maternfivehalfs {
BO_PARAM(double, sigma, 1);
......@@ -35,6 +42,33 @@ struct Params {
};
};
BOOST_AUTO_TEST_CASE(test_gp_dim)
{
using namespace limbo;
typedef kernel::MaternFiveHalfs<Params> KF_t;
typedef mean::Constant<Params> Mean_t;
typedef model::GP<Params, KF_t, Mean_t> GP_t;
GP_t gp; // no init with dim
std::vector<Eigen::VectorXd> observations = {make_v2(5,5), make_v2(10,10),
make_v2(5,5)};
std::vector<Eigen::VectorXd> samples = {make_v2(1,1), make_v2(2,2), make_v2(3,3)};
gp.compute(samples, observations, 0.0);
Eigen::VectorXd mu;
double sigma;
std::tie(mu, sigma) = gp.query(make_v2(1,1));
BOOST_CHECK(std::abs((mu(0) - 5)) < 1);
BOOST_CHECK(std::abs((mu(1) - 5)) < 1);
BOOST_CHECK(sigma < 1e-5);
}
BOOST_AUTO_TEST_CASE(test_gp)
{
using namespace limbo;
......@@ -114,7 +148,7 @@ BOOST_AUTO_TEST_CASE(test_gp_auto)
typedef mean::Constant<Params> Mean_t;
typedef model::GP<Params, KF_t, Mean_t, model::gp::KernelLFOpt<Params>> GP_t;
GP_t gp(1, 1);
GP_t gp;
std::vector<Eigen::VectorXd> observations = {make_v1(5), make_v1(10), make_v1(5)};
std::vector<Eigen::VectorXd> samples = {make_v1(1), make_v1(2), make_v1(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