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

improve ns_ego

parent 10b854f2
*~
.DS_Store
.project
.settings
.classpath
.metadata
*.o
*.a
*.pyc
*.err
*.log
......@@ -107,7 +107,9 @@ namespace limbo {
template<typename BO>
void operator()(BO& opt) {
opt.update_pareto_data();
opt.template update_pareto_model<F::dim>();//2 = hack..
#ifdef PAREGO // this is already done is NS_EGO
opt.template update_pareto_model<F::dim>();
#endif
auto dir = opt.res_dir() + "/";
auto p_model = opt.pareto_model();
auto p_data = opt.pareto_data();
......@@ -146,7 +148,7 @@ int main() {
typedef zdt1 func_t;
#elif defined ZDT2
typedef zdt2 func_t;
#elif defined ZDT3
#elif defined ZDT3
typedef zdt3 func_t;
#elif defined MOP2
typedef mop2 func_t;
......
......@@ -16,7 +16,8 @@ sys.argv.pop(0)
for i in sys.argv:
data = np.loadtxt(i)
plot(data[:, 0], data[:, 1], '-', label=i)
if not "obs_" in i:
plot(data[:, 0], data[:, 1], '-', label=i)
plot(data[:, 0], data[:, 1], 'o', label=i)
if data.shape[1] > 2:
plot(data[:,0] - data[:,2]*100000, data[:,1] - data[:,3]*100000, '-', label='p')
......
#ifndef BO_MULTI_HPP_
#define BO_MULTI_HPP_
#define VERSION "xxx"
#include <sferes/phen/parameters.hpp>
#include <sferes/gen/evo_float.hpp>
......@@ -28,7 +27,7 @@ namespace limbo {
};
struct pop {
SFERES_CONST unsigned size = 100;
SFERES_CONST unsigned nb_gen = 2000;
SFERES_CONST unsigned nb_gen = 1000;
SFERES_CONST int dump_period = -1;
SFERES_CONST int initial_aleat = 1;
};
......@@ -56,9 +55,10 @@ namespace limbo {
Eigen::VectorXd v(indiv.size());
for (size_t j = 0; j < indiv.size(); ++j)
v[j] = indiv.data(j);
// we protect against overestimation because this has some spurious effect
for (size_t i = 0; i < _models.size(); ++i)
this->_objs[i] = std::min(_models[i].mu(v), 1.0);
}
this->_objs[i] = std::min(_models[i].mu(v), _models[i].max_observation());
}
protected:
std::vector<M> _models;
std::vector<float> _objs;
......@@ -103,7 +103,7 @@ namespace limbo {
std::vector<Eigen::VectorXd> v(this->_samples.size());
size_t dim = this->_observations[0].size();
std::fill(v.begin(), v.end(), Eigen::VectorXd::Zero(dim));
_pareto_data = pareto::pareto_set(_pack_data(this->_samples, this->_observations, v));
_pareto_data = pareto::pareto_set<1>(_pack_data(this->_samples, this->_observations, v));
}
// will be called at the end of the algo
......
......@@ -72,7 +72,7 @@ namespace limbo {
const MeanFunction& mean_function() const {
return _mean_function;
}
double max_observation() const { return _observations.maxCoeff(); }
protected:
int _dim;
KernelFunction _kernel_function;
......@@ -109,13 +109,13 @@ namespace limbo {
double _mu(const Eigen::VectorXd& v, const Eigen::VectorXd& k) const {
return _mean_function(v) + k.transpose() * _alpha;
// return _mean_function(v)
// + (k.transpose() * _inverted_kernel * (_obs_mean))[0];
// return _mean_function(v)
// + (k.transpose() * _inverted_kernel * (_obs_mean))[0];
}
double _sigma(const Eigen::VectorXd& v, const Eigen::VectorXd& k) const {
Eigen::VectorXd z = _llt.matrixL().solve(k);
return _kernel_function(v, v) - z.dot(z);
// return _kernel_function(v, v) - (k.transpose() * _inverted_kernel * k)[0];
// return _kernel_function(v, v) - (k.transpose() * _inverted_kernel * k)[0];
}
Eigen::VectorXd _compute_k(const Eigen::VectorXd& v) const {
Eigen::VectorXd k(_samples.size());
......
......@@ -28,20 +28,12 @@ namespace limbo {
while (this->_samples.size() == 0 || this->_pursue()) {
this->template update_pareto_model<EvalFunction::dim>();
auto pareto = this->pareto_model();
// this is hack to test wether we need a bound
pareto.erase(std::remove_if(pareto.begin(), pareto.end(),
[](const pareto_point_t& x) {
for (int i = 0; i < std::get<1>(x).size(); ++i)
if (std::get<1>(x)(i) > 1)
return true;
return false;
}), pareto.end());
auto best = std::max_element(pareto.begin(), pareto.end(),
[](const pareto_point_t& x1, const pareto_point_t& x2) {
return std::get<2>(x1).sum() < std::get<2>(x2).sum();
});
Eigen::VectorXd best_v = std::get<0>(*best);
// best_v = std::get<0>(pareto[(int)misc::rand<double>(0, pareto.size())]);
// Pareto front of the variances
auto p_variance = pareto::pareto_set<2>(pareto);
auto best = p_variance[rand() % p_variance.size()];
Eigen::VectorXd best_v = std::get<0>(best);
this->add_new_sample(best_v, feval(best_v));
this->_iteration++;
std::cout << this->_iteration << " | " << best_v.transpose()
......
......@@ -38,24 +38,25 @@ namespace pareto {
return (dominate_flag(i1, i2) == 1);
}
template<typename T, typename T2>
template<int K, typename T, typename T2>
static bool non_dominated(const T& p_objs, const T2& objs) {
for (auto x : objs)
if (dominate(std::get<1>(x), p_objs))
if (dominate(std::get<K>(x), p_objs))
return false;
return true;
}
// lexical order
template<int K>
struct compare_objs_lex {
compare_objs_lex() {}
template<typename T>
bool operator()(const T& i1, const T& i2) const {
for (int i = 0; i < std::get<1>(i1).size(); ++i)
if (std::get<1>(i1)(i) > std::get<1>(i2)(i))
for (int i = 0; i < std::get<K>(i1).size(); ++i)
if (std::get<K>(i1)(i) > std::get<K>(i2)(i))
return true;
else if (std::get<1>(i1)(i) < std::get<1>(i2)(i))
else if (std::get<K>(i1)(i) < std::get<K>(i2)(i))
return false;
return false;
}
......@@ -67,14 +68,15 @@ namespace pareto {
v.push_back(t);
return v;
}
template<int K>
struct comp_fronts {
// this functor is ONLY for sort2objs
template<typename T>
bool operator()(const T& f2, const T& f1) const {
assert(f1.size() == 1);
assert(std::get<1>(f1[0]).size() == 2);
assert(std::get<K>(f1[0]).size() == 2);
// we only need to compare f1 to the value of the last element of f2
if (std::get<1>(f1[0])(1) < std::get<1>(f2.back())(1))
if (std::get<K>(f1[0])(1) < std::get<K>(f2.back())(1))
return true;
else
return false;
......@@ -82,7 +84,7 @@ namespace pareto {
};
// O(n^2) procedure, for > 2 objectives
template<typename T>
template<int K, typename T>
T pareto_set_std(const T& p) {
par::vector<typename T::value_type> pareto;
par::loop(0, p.size(), [&](size_t i) {
......@@ -90,7 +92,7 @@ namespace pareto {
std::cout << i << '[' << p.size() << "] ";
std::cout.flush();
}
if (non_dominated(std::get<1>(p[i]), p))
if (non_dominated<K>(std::get<K>(p[i]), p))
pareto.push_back(p[i]);
});
return par::convert_vector(pareto);
......@@ -98,10 +100,10 @@ namespace pareto {
// O(n lg n), for 2 objectives ONLY
// see M. T. Jensen, 2003
template<typename T>
template<int K, typename T>
T sort_2objs(const T& v) {
T p = v;
par::sort(p.begin(), p.end(), compare_objs_lex());
par::sort(p.begin(), p.end(), compare_objs_lex<K>());
std::vector<T> f;
f.push_back(impl::new_vector(p[0]));
......@@ -111,9 +113,9 @@ namespace pareto {
std::cout << i << " [" << p.size() << "] ";
std::cout.flush();
}
if (std::get<1>(p[i])(1) > std::get<1>(f[e].back())(1)) { // !dominate(si, f_e)
if (std::get<K>(p[i])(1) > std::get<K>(f[e].back())(1)) { // !dominate(si, f_e)
auto b = std::lower_bound(f.begin(), f.end(), impl::new_vector(p[i]),
impl::comp_fronts());
impl::comp_fronts<K>());
assert(b != f.end());
b->push_back(p[i]);
} else {
......@@ -126,16 +128,16 @@ namespace pareto {
}
// argument vector of P (std::vector<P>)
// where P is a tuple with the objective values in std::get<1>(p);
template<typename T>
// where P is a tuple with the objective values in std::get<K>(p);
template<int K, typename T>
static T pareto_set(const T& v) {
assert(v.size());
size_t nb_objs = std::get<1>(v[0]).size();
size_t nb_objs = std::get<K>(v[0]).size();
assert(nb_objs > 1);
if (nb_objs == 2)
return impl::sort_2objs(v);
return impl::sort_2objs<K>(v);
else
return impl::pareto_set_std(v);
return impl::pareto_set_std<K>(v);
}
}
......
......@@ -27,7 +27,7 @@ def configure(conf):
conf.load('tbb')
conf.load('sferes')
common_flags = "-Wall -std=c++11"
common_flags = "-Wall -std=c++11 "
cxxflags = conf.env['CXXFLAGS']
conf.check_boost(lib='serialization timer filesystem \
......@@ -38,7 +38,7 @@ def configure(conf):
conf.check_tbb()
conf.check_sferes()
if conf.is_defined('USE_TBB'):
common_flags += " -DUSE_TBB "
common_flags += " -DUSE_TBB"
if conf.is_defined('USE_SFERES'):
common_flags += " -DUSE_SFERES -DSFERES_FAST_DOMSORT"
......
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