Commit 7d291e29 authored by Konstantinos Chatzilygeroudis's avatar Konstantinos Chatzilygeroudis
Browse files

First try for expected improvement

parent 42d9ccac
......@@ -2,32 +2,32 @@
//| This project has received funding from the European Research Council (ERC) under
//| the European Union's Horizon 2020 research and innovation programme (grant
//| agreement No 637972) - see http://www.resibots.eu
//|
//|
//| Contributor(s):
//| - Jean-Baptiste Mouret (jean-baptiste.mouret@inria.fr)
//| - Antoine Cully (antoinecully@gmail.com)
//| - Kontantinos Chatzilygeroudis (konstantinos.chatzilygeroudis@inria.fr)
//| - Federico Allocati (fede.allocati@gmail.com)
//| - Vaios Papaspyros (b.papaspyros@gmail.com)
//|
//|
//| This software is a computer library whose purpose is to optimize continuous,
//| black-box functions. It mainly implements Gaussian processes and Bayesian
//| optimization.
//| Main repository: http://github.com/resibots/limbo
//| Documentation: http://www.resibots.eu/limbo
//|
//|
//| This software is governed by the CeCILL-C license under French law and
//| abiding by the rules of distribution of free software. You can use,
//| modify and/ or redistribute the software under the terms of the CeCILL-C
//| license as circulated by CEA, CNRS and INRIA at the following URL
//| "http://www.cecill.info".
//|
//|
//| As a counterpart to the access to the source code and rights to copy,
//| modify and redistribute granted by the license, users are provided only
//| with a limited warranty and the software's author, the holder of the
//| economic rights, and the successive licensors have only limited
//| liability.
//|
//|
//| In this respect, the user's attention is drawn to the risks associated
//| with loading, using, modifying and/or developing or reproducing the
//| software by the user in light of its specific status of free software,
......@@ -38,10 +38,10 @@
//| requirements in conditions enabling the security of their systems and/or
//| data to be ensured and, more generally, to use and operate it in the
//| same conditions as regards security.
//|
//|
//| The fact that you are presently reading this means that you have had
//| knowledge of the CeCILL-C license and that you accept its terms.
//|
//|
#ifndef LIMBO_ACQUI_HPP
#define LIMBO_ACQUI_HPP
......@@ -50,5 +50,6 @@
#include <limbo/acqui/ucb.hpp>
#include <limbo/acqui/gp_ucb.hpp>
#include <limbo/acqui/ei.hpp>
#endif
//| Copyright Inria May 2015
//| This project has received funding from the European Research Council (ERC) under
//| the European Union's Horizon 2020 research and innovation programme (grant
//| agreement No 637972) - see http://www.resibots.eu
//|
//| Contributor(s):
//| - Jean-Baptiste Mouret (jean-baptiste.mouret@inria.fr)
//| - Antoine Cully (antoinecully@gmail.com)
//| - Kontantinos Chatzilygeroudis (konstantinos.chatzilygeroudis@inria.fr)
//| - Federico Allocati (fede.allocati@gmail.com)
//| - Vaios Papaspyros (b.papaspyros@gmail.com)
//|
//| This software is a computer library whose purpose is to optimize continuous,
//| black-box functions. It mainly implements Gaussian processes and Bayesian
//| optimization.
//| Main repository: http://github.com/resibots/limbo
//| Documentation: http://www.resibots.eu/limbo
//|
//| This software is governed by the CeCILL-C license under French law and
//| abiding by the rules of distribution of free software. You can use,
//| modify and/ or redistribute the software under the terms of the CeCILL-C
//| license as circulated by CEA, CNRS and INRIA at the following URL
//| "http://www.cecill.info".
//|
//| As a counterpart to the access to the source code and rights to copy,
//| modify and redistribute granted by the license, users are provided only
//| with a limited warranty and the software's author, the holder of the
//| economic rights, and the successive licensors have only limited
//| liability.
//|
//| In this respect, the user's attention is drawn to the risks associated
//| with loading, using, modifying and/or developing or reproducing the
//| software by the user in light of its specific status of free software,
//| that may mean that it is complicated to manipulate, and that also
//| therefore means that it is reserved for developers and experienced
//| professionals having in-depth computer knowledge. Users are therefore
//| encouraged to load and test the software's suitability as regards their
//| requirements in conditions enabling the security of their systems and/or
//| data to be ensured and, more generally, to use and operate it in the
//| same conditions as regards security.
//|
//| The fact that you are presently reading this means that you have had
//| knowledge of the CeCILL-C license and that you accept its terms.
//|
#ifndef LIMBO_ACQUI_EI_HPP
#define LIMBO_ACQUI_EI_HPP
#include <cmath>
#include <Eigen/Core>
#include <limbo/tools/macros.hpp>
namespace limbo {
namespace defaults {
struct acqui_ei {
/// @ingroup acqui_defaults
BO_PARAM(double, jitter, 0.0);
};
}
namespace acqui {
/** @ingroup acqui
\rst
Classic EI (Expected Improvement). See :cite:`brochu2010tutorial`, p. 14
.. math::
EI(x) = (\mu(x) - f(x^+) - \xi)\Phi(Z) + \sigma(x)\phi(Z).
Z = \frac{\mu(x)-f(x^+) - \xi}{\sigma(x)}.
Parameters:
- ``double jitter`` - ..math:: \xi
\endrst
*/
template <typename Params, typename Model>
class EI {
public:
EI(const Model& model, int iteration = 0) : _model(model) {}
size_t dim_in() const { return _model.dim_in(); }
size_t dim_out() const { return _model.dim_out(); }
template <typename AggregatorFunction>
double operator()(const Eigen::VectorXd& v, const AggregatorFunction& afun) const
{
Eigen::VectorXd mu;
double sigma_sq;
std::tie(mu, sigma_sq) = _model.query(v);
double sigma = std::sqrt(sigma_sq);
if (sigma < 1e-10 || _model.observations().size() < 1)
return 0.0;
// return (afun(mu) + Params::acqui_ucb::alpha() * sqrt(sigma));
std::vector<Eigen::VectorXd> obs = _model.observations();
auto rewards = std::vector<double>(obs.size());
std::transform(obs.begin(), obs.end(), rewards.begin(), afun);
auto max_e = std::max_element(rewards.begin(), rewards.end());
double f_max = afun(obs[std::distance(rewards.begin(), max_e)]);
double Z = (afun(mu) - f_max - Params::acqui_ei::jitter()) / sigma;
double phi = std::exp(-0.5 * std::pow(Z, 2.0)) / std::sqrt(2.0 * M_PI);
double Phi = 0.5 * (1.0 + std::erf(Z / std::sqrt(2)));
return Z * Phi + sigma * phi;
}
protected:
const Model& _model;
};
}
}
#endif
......@@ -2,32 +2,32 @@
//| This project has received funding from the European Research Council (ERC) under
//| the European Union's Horizon 2020 research and innovation programme (grant
//| agreement No 637972) - see http://www.resibots.eu
//|
//|
//| Contributor(s):
//| - Jean-Baptiste Mouret (jean-baptiste.mouret@inria.fr)
//| - Antoine Cully (antoinecully@gmail.com)
//| - Kontantinos Chatzilygeroudis (konstantinos.chatzilygeroudis@inria.fr)
//| - Federico Allocati (fede.allocati@gmail.com)
//| - Vaios Papaspyros (b.papaspyros@gmail.com)
//|
//|
//| This software is a computer library whose purpose is to optimize continuous,
//| black-box functions. It mainly implements Gaussian processes and Bayesian
//| optimization.
//| Main repository: http://github.com/resibots/limbo
//| Documentation: http://www.resibots.eu/limbo
//|
//|
//| This software is governed by the CeCILL-C license under French law and
//| abiding by the rules of distribution of free software. You can use,
//| modify and/ or redistribute the software under the terms of the CeCILL-C
//| license as circulated by CEA, CNRS and INRIA at the following URL
//| "http://www.cecill.info".
//|
//|
//| As a counterpart to the access to the source code and rights to copy,
//| modify and redistribute granted by the license, users are provided only
//| with a limited warranty and the software's author, the holder of the
//| economic rights, and the successive licensors have only limited
//| liability.
//|
//|
//| In this respect, the user's attention is drawn to the risks associated
//| with loading, using, modifying and/or developing or reproducing the
//| software by the user in light of its specific status of free software,
......@@ -38,10 +38,10 @@
//| requirements in conditions enabling the security of their systems and/or
//| data to be ensured and, more generally, to use and operate it in the
//| same conditions as regards security.
//|
//|
//| The fact that you are presently reading this means that you have had
//| knowledge of the CeCILL-C license and that you accept its terms.
//|
//|
#ifndef LIMBO_MODEL_GP_HPP
#define LIMBO_MODEL_GP_HPP
......@@ -305,6 +305,16 @@ namespace limbo {
/// return the list of samples that have been tested so far
const std::vector<Eigen::VectorXd>& samples() const { return _samples; }
// return the list of observations that have been recorded so far
std::vector<Eigen::VectorXd> observations() const
{
std::vector<Eigen::VectorXd> res;
for (int i = 0; i < _observations.rows(); i++) {
res.push_back(_observations.row(i));
}
return res;
}
protected:
int _dim_in;
int _dim_out;
......
......@@ -2,32 +2,32 @@
//| This project has received funding from the European Research Council (ERC) under
//| the European Union's Horizon 2020 research and innovation programme (grant
//| agreement No 637972) - see http://www.resibots.eu
//|
//|
//| Contributor(s):
//| - Jean-Baptiste Mouret (jean-baptiste.mouret@inria.fr)
//| - Antoine Cully (antoinecully@gmail.com)
//| - Kontantinos Chatzilygeroudis (konstantinos.chatzilygeroudis@inria.fr)
//| - Federico Allocati (fede.allocati@gmail.com)
//| - Vaios Papaspyros (b.papaspyros@gmail.com)
//|
//|
//| This software is a computer library whose purpose is to optimize continuous,
//| black-box functions. It mainly implements Gaussian processes and Bayesian
//| optimization.
//| Main repository: http://github.com/resibots/limbo
//| Documentation: http://www.resibots.eu/limbo
//|
//|
//| This software is governed by the CeCILL-C license under French law and
//| abiding by the rules of distribution of free software. You can use,
//| modify and/ or redistribute the software under the terms of the CeCILL-C
//| license as circulated by CEA, CNRS and INRIA at the following URL
//| "http://www.cecill.info".
//|
//|
//| As a counterpart to the access to the source code and rights to copy,
//| modify and redistribute granted by the license, users are provided only
//| with a limited warranty and the software's author, the holder of the
//| economic rights, and the successive licensors have only limited
//| liability.
//|
//|
//| In this respect, the user's attention is drawn to the risks associated
//| with loading, using, modifying and/or developing or reproducing the
//| software by the user in light of its specific status of free software,
......@@ -38,10 +38,10 @@
//| requirements in conditions enabling the security of their systems and/or
//| data to be ensured and, more generally, to use and operate it in the
//| same conditions as regards security.
//|
//|
//| The fact that you are presently reading this means that you have had
//| knowledge of the CeCILL-C license and that you accept its terms.
//|
//|
// please see the explanation in the documentation
#include <limbo/limbo.hpp>
......@@ -59,10 +59,10 @@ struct Params {
BO_PARAM(int, iterations, 100);
};
struct stop_mintolerance {
BO_PARAM(double, tolerance, -0.02);
BO_PARAM(double, tolerance, -0.1);
};
struct acqui_ucb {
BO_PARAM(double, alpha, 0.4);
struct acqui_ei {
BO_PARAM(double, jitter, 0.0);
};
struct init_randomsampling {
BO_PARAM(int, samples, 10);
......@@ -172,7 +172,7 @@ int main()
using gp_t = model::GP<Params, kernel_t, mean_t, gp_opt_t>;
using acqui_t = acqui::UCB<Params, gp_t>;
using acqui_t = acqui::EI<Params, gp_t>;
using acqui_opt_t = opt::Cmaes<Params>;
using init_t = init::RandomSampling<Params>;
......@@ -183,10 +183,10 @@ int main()
bayes_opt::BOptimizer<Params, modelfun<gp_t>, acquifun<acqui_t>, acquiopt<acqui_opt_t>, initfun<init_t>, statsfun<stat_t>, stopcrit<stop_t>> boptimizer;
// Instantiate aggregator
DistanceToTarget<Params> aggregator({1.5, 1.5});
DistanceToTarget<Params> aggregator({2, 1.5});
boptimizer.optimize(eval_func<Params>(), aggregator);
std::cout << "New target!" << std::endl;
aggregator = DistanceToTarget<Params>({2, 1.5});
aggregator = DistanceToTarget<Params>({1.5, 1.5});
// Do not forget to pass `false` as the last parameter in `optimize`,
// so you do not reset the data in boptimizer
// i.e. keep all the previous data points in the Gaussian Process
......
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