Commit 8444ab2d authored by Konstantinos Chatzilygeroudis's avatar Konstantinos Chatzilygeroudis
Browse files

Ran clang-format on whole repo

parent 95967d51
...@@ -54,210 +54,219 @@ ...@@ -54,210 +54,219 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#define ERROR(x) fprintf(stderr, x), fprintf(stderr, "\n"), exit(1) #define ERROR(x) fprintf(stderr, x), fprintf(stderr, "\n"), exit(1)
int Dominates(double point1[], double point2[], int noObjectives)
int Dominates(double point1[], double point2[], int noObjectives) /* returns true if 'point1' dominates 'points2' with respect to the
/* returns true if 'point1' dominates 'points2' with respect to the
to the first 'noObjectives' objectives */ to the first 'noObjectives' objectives */
{ {
int i; int i;
int betterInAnyObjective; int betterInAnyObjective;
betterInAnyObjective = 0; betterInAnyObjective = 0;
for (i = 0; i < noObjectives && point1[i] >= point2[i]; i++) for (i = 0; i < noObjectives && point1[i] >= point2[i]; i++)
if (point1[i] > point2[i]) if (point1[i] > point2[i])
betterInAnyObjective = 1; betterInAnyObjective = 1;
return (i >= noObjectives && betterInAnyObjective); return (i >= noObjectives && betterInAnyObjective);
} /* Dominates */ } /* Dominates */
void Swap(double *front[], int i, int j) void Swap(double* front[], int i, int j)
{ {
double *temp; double* temp;
temp = front[i]; temp = front[i];
front[i] = front[j]; front[i] = front[j];
front[j] = temp; front[j] = temp;
} /* Swap */ } /* Swap */
int FilterNondominatedSet(double *front[], int noPoints, int noObjectives) int FilterNondominatedSet(double* front[], int noPoints, int noObjectives)
/* all nondominated points regarding the first 'noObjectives' dimensions /* all nondominated points regarding the first 'noObjectives' dimensions
are collected; the points referenced by 'front[0..noPoints-1]' are are collected; the points referenced by 'front[0..noPoints-1]' are
considered; 'front' is resorted, such that 'front[0..n-1]' contains considered; 'front' is resorted, such that 'front[0..n-1]' contains
the nondominated points; n is returned */ the nondominated points; n is returned */
{ {
int i, j; int i, j;
int n; int n;
n = noPoints; n = noPoints;
i = 0; i = 0;
while (i < n) { while (i < n) {
j = i + 1; j = i + 1;
while (j < n) { while (j < n) {
if (Dominates(front[i], front[j], noObjectives)) { if (Dominates(front[i], front[j], noObjectives)) {
/* remove point 'j' */ /* remove point 'j' */
n--; n--;
Swap(front, j, n); Swap(front, j, n);
} }
else if (Dominates(front[j], front[i], noObjectives)) { else if (Dominates(front[j], front[i], noObjectives)) {
/* remove point 'i'; ensure that the point copied to index 'i' /* remove point 'i'; ensure that the point copied to index 'i'
is considered in the next outer loop (thus, decrement i) */ is considered in the next outer loop (thus, decrement i) */
n--; n--;
Swap(front, i, n); Swap(front, i, n);
i--; i--;
break; break;
} }
else else
j++; j++;
}
i++;
} }
i++; return n;
}
return n;
} /* FilterNondominatedSet */ } /* FilterNondominatedSet */
double SurfaceUnchangedTo(double* front[], int noPoints, int objective)
double SurfaceUnchangedTo(double *front[], int noPoints, int objective) /* calculate next value regarding dimension 'objective'; consider
/* calculate next value regarding dimension 'objective'; consider
points referenced in 'front[0..noPoints-1]' */ points referenced in 'front[0..noPoints-1]' */
{ {
int i; int i;
double minValue, value; double minValue, value;
if (noPoints < 1) ERROR("run-time error"); if (noPoints < 1)
minValue = front[0][objective]; ERROR("run-time error");
for (i = 1; i < noPoints; i++) { minValue = front[0][objective];
value = front[i][objective]; for (i = 1; i < noPoints; i++) {
if (value < minValue) minValue = value; value = front[i][objective];
} if (value < minValue)
return minValue; minValue = value;
}
return minValue;
} /* SurfaceUnchangedTo */ } /* SurfaceUnchangedTo */
int ReduceNondominatedSet(double *front[], int noPoints, int objective, int ReduceNondominatedSet(double* front[], int noPoints, int objective,
double threshold) double threshold)
/* remove all points which have a value <= 'threshold' regarding the /* remove all points which have a value <= 'threshold' regarding the
dimension 'objective'; the points referenced by dimension 'objective'; the points referenced by
'front[0..noPoints-1]' are considered; 'front' is resorted, such that 'front[0..noPoints-1]' are considered; 'front' is resorted, such that
'front[0..n-1]' contains the remaining points; 'n' is returned */ 'front[0..n-1]' contains the remaining points; 'n' is returned */
{ {
int n; int n;
int i; int i;
n = noPoints; n = noPoints;
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
if (front[i][objective] <= threshold) { if (front[i][objective] <= threshold) {
n--; n--;
Swap(front, i, n); Swap(front, i, n);
} }
return n; return n;
} /* ReduceNondominatedSet */ } /* ReduceNondominatedSet */
double CalculateHypervolume(double *front[], int noPoints, double CalculateHypervolume(double* front[], int noPoints,
int noObjectives) int noObjectives)
{ {
int n; int n;
double volume, distance; double volume, distance;
volume = 0; volume = 0;
distance = 0; distance = 0;
n = noPoints; n = noPoints;
while (n > 0) { while (n > 0) {
int noNondominatedPoints; int noNondominatedPoints;
double tempVolume, tempDistance; double tempVolume, tempDistance;
noNondominatedPoints = FilterNondominatedSet(front, n, noObjectives - 1); noNondominatedPoints = FilterNondominatedSet(front, n, noObjectives - 1);
tempVolume = 0; tempVolume = 0;
if (noObjectives < 3) { if (noObjectives < 3) {
if (noNondominatedPoints < 1) ERROR("run-time error"); if (noNondominatedPoints < 1)
tempVolume = front[0][0]; ERROR("run-time error");
tempVolume = front[0][0];
}
else
tempVolume = CalculateHypervolume(front, noNondominatedPoints,
noObjectives - 1);
tempDistance = SurfaceUnchangedTo(front, n, noObjectives - 1);
volume += tempVolume * (tempDistance - distance);
distance = tempDistance;
n = ReduceNondominatedSet(front, n, noObjectives - 1, distance);
} }
else return volume;
tempVolume = CalculateHypervolume(front, noNondominatedPoints,
noObjectives - 1);
tempDistance = SurfaceUnchangedTo(front, n, noObjectives - 1);
volume += tempVolume * (tempDistance - distance);
distance = tempDistance;
n = ReduceNondominatedSet(front, n, noObjectives - 1, distance);
}
return volume;
} /* CalculateHypervolume */ } /* CalculateHypervolume */
int ReadFront(double **frontPtr[], FILE *file, int noObjectives) int ReadFront(double** frontPtr[], FILE* file, int noObjectives)
{ {
int noPoints; int noPoints;
int i; int i;
double value; double value;
/* check file and count points */ /* check file and count points */
noPoints = 0; noPoints = 0;
while (!feof(file)) { while (!feof(file)) {
for (i = 0; i < noObjectives && fscanf(file, "%lf", &value) != EOF; i++); for (i = 0; i < noObjectives && fscanf(file, "%lf", &value) != EOF; i++)
if (i > 0 && i < noObjectives) ERROR("data in file incomplete"); ;
noPoints++; if (i > 0 && i < noObjectives)
} ERROR("data in file incomplete");
/* allocate memory */ noPoints++;
*frontPtr = malloc(noPoints * sizeof(double *));
if (*frontPtr == NULL) ERROR("memory allocation failed");
for (i = 0; i < noPoints; i++) {
(*frontPtr)[i] = malloc(noObjectives * sizeof(double));
if ((*frontPtr)[i] == NULL) ERROR("memory allocation failed");
}
/* read data */
rewind(file);
noPoints = 0;
while (!feof(file)) {
for (i = 0; i < noObjectives; i++) {
if (fscanf(file, "%lf", &value) != EOF)
(*frontPtr)[noPoints][i] = value;
else
break;
} }
if (i > 0 && i < noObjectives) ERROR("data in file incomplete"); /* allocate memory */
noPoints++; *frontPtr = malloc(noPoints * sizeof(double*));
} if (*frontPtr == NULL)
if (noPoints < 1) ERROR("file contains no data"); ERROR("memory allocation failed");
return noPoints; for (i = 0; i < noPoints; i++) {
(*frontPtr)[i] = malloc(noObjectives * sizeof(double));
if ((*frontPtr)[i] == NULL)
ERROR("memory allocation failed");
}
/* read data */
rewind(file);
noPoints = 0;
while (!feof(file)) {
for (i = 0; i < noObjectives; i++) {
if (fscanf(file, "%lf", &value) != EOF)
(*frontPtr)[noPoints][i] = value;
else
break;
}
if (i > 0 && i < noObjectives)
ERROR("data in file incomplete");
noPoints++;
}
if (noPoints < 1)
ERROR("file contains no data");
return noPoints;
} /* ReadFront */ } /* ReadFront */
int MergeFronts(double **frontPtr[], double *front1[], int sizeFront1, int MergeFronts(double** frontPtr[], double* front1[], int sizeFront1,
double* front2[], int sizeFront2, int noObjectives) double* front2[], int sizeFront2, int noObjectives)
{ {
int i, j; int i, j;
int noPoints; int noPoints;
/* allocate memory */ /* allocate memory */
noPoints = sizeFront1 + sizeFront2; noPoints = sizeFront1 + sizeFront2;
*frontPtr = malloc(noPoints * sizeof(double *)); *frontPtr = malloc(noPoints * sizeof(double*));
if (*frontPtr == NULL) ERROR("memory allocation failed"); if (*frontPtr == NULL)
for (i = 0; i < noPoints; i++) { ERROR("memory allocation failed");
(*frontPtr)[i] = malloc(noObjectives * sizeof(double)); for (i = 0; i < noPoints; i++) {
if ((*frontPtr)[i] == NULL) ERROR("memory allocation failed"); (*frontPtr)[i] = malloc(noObjectives * sizeof(double));
} if ((*frontPtr)[i] == NULL)
/* copy points */ ERROR("memory allocation failed");
noPoints = 0; }
for (i = 0; i < sizeFront1; i++) { /* copy points */
for (j = 0; j < noObjectives; j++) noPoints = 0;
(*frontPtr)[noPoints][j] = front1[i][j]; for (i = 0; i < sizeFront1; i++) {
noPoints++; for (j = 0; j < noObjectives; j++)
} (*frontPtr)[noPoints][j] = front1[i][j];
for (i = 0; i < sizeFront2; i++) { noPoints++;
for (j = 0; j < noObjectives; j++) }
(*frontPtr)[noPoints][j] = front2[i][j]; for (i = 0; i < sizeFront2; i++) {
noPoints++; for (j = 0; j < noObjectives; j++)
} (*frontPtr)[noPoints][j] = front2[i][j];
noPoints++;
}
return noPoints; return noPoints;
} /* MergeFronts */ } /* MergeFronts */
void DeallocateFront(double** front, int noPoints) void DeallocateFront(double** front, int noPoints)
{ {
int i; int i;
if (front != NULL) { if (front != NULL) {
for (i = 0; i < noPoints; i++) for (i = 0; i < noPoints; i++)
if (front[i] != NULL) if (front[i] != NULL)
free(front[i]); free(front[i]);
free(front); free(front);
} }
} /* DeallocateFront */ } /* DeallocateFront */
#if 0 #if 0
......
#ifndef HYPERVOL_H__ #ifndef HYPERVOL_H__
#define HYPERVOL_H__ #define HYPERVOL_H__
extern "C"{ extern "C" {
int FilterNondominatedSet(double *front[], int noPoints, int noObjectives); int FilterNondominatedSet(double* front[], int noPoints, int noObjectives);
double CalculateHypervolume(double *front[], int noPoints, int noObjectives); double CalculateHypervolume(double* front[], int noPoints, int noObjectives);
} }
#endif #endif
...@@ -93,7 +93,6 @@ namespace limbo { ...@@ -93,7 +93,6 @@ namespace limbo {
boost::parameter::optional<tag::stopcrit>, boost::parameter::optional<tag::stopcrit>,
boost::parameter::optional<tag::modelfun>> bo_multi_signature; boost::parameter::optional<tag::modelfun>> bo_multi_signature;
// clang-format off // clang-format off
template <class Params, template <class Params,
class A1 = boost::parameter::void_, class A1 = boost::parameter::void_,
...@@ -105,18 +104,18 @@ namespace limbo { ...@@ -105,18 +104,18 @@ namespace limbo {
// clang-format on // clang-format on
class BoMulti : public limbo::bayes_opt::BoBase<Params, A2, A3, A4, A5, A6> { class BoMulti : public limbo::bayes_opt::BoBase<Params, A2, A3, A4, A5, A6> {
public: public:
struct defaults { struct defaults {
#ifdef USE_LIBCMAES #ifdef USE_LIBCMAES
typedef opt::Cmaes<Params> acquiopt_t; typedef opt::Cmaes<Params> acquiopt_t;
#elif defined(USE_NLOPT) #elif defined(USE_NLOPT)
typedef opt::NLOptNoGrad<Params, nlopt::GN_DIRECT_L_RAND> acquiopt_t; typedef opt::NLOptNoGrad<Params, nlopt::GN_DIRECT_L_RAND> acquiopt_t;
#else #else
#warning NO NLOpt, and NO Libcmaes: the acquisition function will be optimized by a grid search algorithm (which is usually bad). Please install at least NLOpt or libcmaes to use limbo!. #warning NO NLOpt, and NO Libcmaes: the acquisition function will be optimized by a grid search algorithm (which is usually bad). Please install at least NLOpt or libcmaes to use limbo!.
typedef opt::GridSearch<Params> acquiopt_t; typedef opt::GridSearch<Params> acquiopt_t;
#endif #endif
}; };
typedef typename bo_multi_signature::bind<A1, A2, A3, A4, A5, A6>::type args; typedef typename bo_multi_signature::bind<A1, A2, A3, A4, A5, A6>::type args;
typedef typename boost::parameter::binding<args, tag::acquiopt, typename defaults::acquiopt_t>::type acqui_optimizer_t; typedef typename boost::parameter::binding<args, tag::acquiopt, typename defaults::acquiopt_t>::type acqui_optimizer_t;
typedef limbo::bayes_opt::BoBase<Params, A2, A3, A4, A5, A6> base_t; typedef limbo::bayes_opt::BoBase<Params, A2, A3, A4, A5, A6> base_t;
typedef typename base_t::model_t model_t; typedef typename base_t::model_t model_t;
......
...@@ -12,9 +12,9 @@ namespace limbo { ...@@ -12,9 +12,9 @@ namespace limbo {
namespace experimental { namespace experimental {
namespace bayes_opt { namespace bayes_opt {
BOOST_PARAMETER_TEMPLATE_KEYWORD(parego_modelfun) BOOST_PARAMETER_TEMPLATE_KEYWORD(parego_modelfun)
typedef boost::parameter::parameters<boost::parameter::optional<tag::parego_modelfun>> parego_signature; typedef boost::parameter::parameters<boost::parameter::optional<tag::parego_modelfun>> parego_signature;
// clang-format off // clang-format off
template <class Params, template <class Params,
...@@ -40,7 +40,6 @@ namespace limbo { ...@@ -40,7 +40,6 @@ namespace limbo {
// nothing here ! // nothing here !
}; };
// clang-format on // clang-format on
} }
} }
} }
......
...@@ -5,54 +5,55 @@ ...@@ -5,54 +5,55 @@
#include <limbo/experimental/tools/pareto.hpp> #include <limbo/experimental/tools/pareto.hpp>
namespace limbo { namespace limbo {
namespace experimental { namespace experimental {
namespace stat { namespace stat {
template <typename Params> template <typename Params>
struct ParetoFront : public limbo::stat::StatBase<Params> { struct ParetoFront : public limbo::stat::StatBase<Params> {
// point, obj, sigma // point, obj, sigma
typedef std::tuple<Eigen::VectorXd, Eigen::VectorXd, Eigen::VectorXd> pareto_point_t; typedef std::tuple<Eigen::VectorXd, Eigen::VectorXd, Eigen::VectorXd> pareto_point_t;
typedef std::vector<pareto_point_t> pareto_t; typedef std::vector<pareto_point_t> pareto_t;
template <typename BO, typename AggregatorFunction> template <typename BO, typename AggregatorFunction>
void operator()(const BO& bo, const AggregatorFunction&, bool blacklisted) void operator()(const BO& bo, const AggregatorFunction&, bool blacklisted)
{ {
if (!bo.stats_enabled() || bo.observations().empty()) if (!bo.stats_enabled() || bo.observations().empty())
return; return;
std::string fname = bo.res_dir() + "/" + "pareto_front_" + std::to_string(bo.current_iteration()) + ".dat"; std::string fname = bo.res_dir() + "/" + "pareto_front_" + std::to_string(bo.current_iteration()) + ".dat";
std::ofstream ofs(fname.c_str()); std::ofstream ofs(fname.c_str());
auto pareto = _pareto_data(bo); auto pareto = _pareto_data(bo);
for (auto x : pareto) { for (auto x : pareto) {
ofs << std::get<0>(x).transpose() << " " ofs << std::get<0>(x).transpose() << " "
<< std::get<1>(x).transpose() << std::endl; << std::get<1>(x).transpose() << std::endl;
}
} }
}
protected: protected:
template<typename BO> template <typename BO>
pareto_t _pareto_data(const BO& bo) pareto_t _pareto_data(const BO& bo)
{ {
std::vector<Eigen::VectorXd> v(bo.samples().size()); std::vector<Eigen::VectorXd> v(bo.samples().size());
size_t dim = bo.observations().size(); size_t dim = bo.observations().size();
std::fill(v.begin(), v.end(), Eigen::VectorXd::Zero(dim)); std::fill(v.begin(), v.end(), Eigen::VectorXd::Zero(dim));
return pareto::pareto_set<1>( return pareto::pareto_set<1>(
_pack_data(bo.samples(), bo.observations(), v)); _pack_data(bo.samples(), bo.observations(), v));
} }
pareto_t _pack_data(const std::vector<Eigen::VectorXd>& points, pareto_t _pack_data(const std::vector<Eigen::VectorXd>& points,
const std::vector<Eigen::VectorXd>& objs, const std::vector<Eigen::VectorXd>& objs,
const std::vector<Eigen::VectorXd>& sigma) const const std::vector<Eigen::VectorXd>& sigma) const
{ {
assert(points.size() == objs.size()); assert(points.size() == objs.size());
assert(sigma.size() == objs.size()); assert(sigma.size() == objs.size());
pareto_t p(points.size()); pareto_t p(points.size());
tools::par::loop(0, p.size(), [&](size_t k) { tools::par::loop(0, p.size(), [&](size_t k) {
// clang-format off // clang-format off
p[k] = std::make_tuple(points[k], objs[k], sigma[k]); p[k] = std::make_tuple(points[k], objs[k], sigma[k]);
// clang-format on // clang-format on
}); });
return p; return p;
} }
}; };
}
} }
}
} }
#endif #endif
...@@ -128,7 +128,7 @@ namespace pareto { ...@@ -128,7 +128,7 @@ namespace pareto {
f.push_back(impl::new_vector(p[0])); f.push_back(impl::new_vector(p[0]));
size_t e = 0; size_t e = 0;
for (size_t i = 1; i < p.size(); ++i) { for (size_t i = 1; i < p.size(); ++i) {
/* if (i % 10000 == 0) { /* if (i % 10000 == 0) {
std::cout << i << " [" << p.size() << "] ";