Commit 614026e7 authored by Konstantinos Chatzilygeroudis's avatar Konstantinos Chatzilygeroudis Committed by GitHub
Browse files

Merge pull request #149 from resibots/format_whole

Ran clang-format on whole repo
parents 95967d51 8444ab2d
......@@ -54,210 +54,219 @@
#include <stdio.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)
/* returns true if 'point1' dominates 'points2' with respect to the
int Dominates(double point1[], double point2[], int noObjectives)
/* returns true if 'point1' dominates 'points2' with respect to the
to the first 'noObjectives' objectives */
{
int i;
int betterInAnyObjective;
int i;
int betterInAnyObjective;
betterInAnyObjective = 0;
for (i = 0; i < noObjectives && point1[i] >= point2[i]; i++)
if (point1[i] > point2[i])
betterInAnyObjective = 1;
return (i >= noObjectives && betterInAnyObjective);
betterInAnyObjective = 0;
for (i = 0; i < noObjectives && point1[i] >= point2[i]; i++)
if (point1[i] > point2[i])
betterInAnyObjective = 1;
return (i >= noObjectives && betterInAnyObjective);
} /* Dominates */
void Swap(double *front[], int i, int j)
void Swap(double* front[], int i, int j)
{
double *temp;
double* temp;
temp = front[i];
front[i] = front[j];
front[j] = temp;
temp = front[i];
front[i] = front[j];
front[j] = temp;
} /* Swap */
int FilterNondominatedSet(double *front[], int noPoints, int noObjectives)
/* all nondominated points regarding the first 'noObjectives' dimensions
int FilterNondominatedSet(double* front[], int noPoints, int noObjectives)
/* all nondominated points regarding the first 'noObjectives' dimensions
are collected; the points referenced by 'front[0..noPoints-1]' are
considered; 'front' is resorted, such that 'front[0..n-1]' contains
the nondominated points; n is returned */
{
int i, j;
int n;
int i, j;
int n;
n = noPoints;
i = 0;
while (i < n) {
j = i + 1;
while (j < n) {
if (Dominates(front[i], front[j], noObjectives)) {
/* remove point 'j' */
n--;
Swap(front, j, n);
}
else if (Dominates(front[j], front[i], noObjectives)) {
/* remove point 'i'; ensure that the point copied to index 'i'
n = noPoints;
i = 0;
while (i < n) {
j = i + 1;
while (j < n) {
if (Dominates(front[i], front[j], noObjectives)) {
/* remove point 'j' */
n--;
Swap(front, j, n);
}
else if (Dominates(front[j], front[i], noObjectives)) {
/* remove point 'i'; ensure that the point copied to index 'i'
is considered in the next outer loop (thus, decrement i) */
n--;
Swap(front, i, n);
i--;
break;
}
else
j++;
n--;
Swap(front, i, n);
i--;
break;
}
else
j++;
}
i++;
}
i++;
}
return n;
return n;
} /* FilterNondominatedSet */
double SurfaceUnchangedTo(double *front[], int noPoints, int objective)
/* calculate next value regarding dimension 'objective'; consider
double SurfaceUnchangedTo(double* front[], int noPoints, int objective)
/* calculate next value regarding dimension 'objective'; consider
points referenced in 'front[0..noPoints-1]' */
{
int i;
double minValue, value;
int i;
double minValue, value;
if (noPoints < 1) ERROR("run-time error");
minValue = front[0][objective];
for (i = 1; i < noPoints; i++) {
value = front[i][objective];
if (value < minValue) minValue = value;
}
return minValue;
if (noPoints < 1)
ERROR("run-time error");
minValue = front[0][objective];
for (i = 1; i < noPoints; i++) {
value = front[i][objective];
if (value < minValue)
minValue = value;
}
return minValue;
} /* SurfaceUnchangedTo */
int ReduceNondominatedSet(double *front[], int noPoints, int objective,
double threshold)
/* remove all points which have a value <= 'threshold' regarding the
int ReduceNondominatedSet(double* front[], int noPoints, int objective,
double threshold)
/* remove all points which have a value <= 'threshold' regarding the
dimension 'objective'; the points referenced by
'front[0..noPoints-1]' are considered; 'front' is resorted, such that
'front[0..n-1]' contains the remaining points; 'n' is returned */
{
int n;
int i;
int n;
int i;
n = noPoints;
for (i = 0; i < n; i++)
if (front[i][objective] <= threshold) {
n--;
Swap(front, i, n);
}
return n;
n = noPoints;
for (i = 0; i < n; i++)
if (front[i][objective] <= threshold) {
n--;
Swap(front, i, n);
}
return n;
} /* ReduceNondominatedSet */
double CalculateHypervolume(double *front[], int noPoints,
int noObjectives)
double CalculateHypervolume(double* front[], int noPoints,
int noObjectives)
{
int n;
double volume, distance;
int n;
double volume, distance;
volume = 0;
distance = 0;
n = noPoints;
while (n > 0) {
int noNondominatedPoints;
double tempVolume, tempDistance;
volume = 0;
distance = 0;
n = noPoints;
while (n > 0) {
int noNondominatedPoints;
double tempVolume, tempDistance;
noNondominatedPoints = FilterNondominatedSet(front, n, noObjectives - 1);
tempVolume = 0;
if (noObjectives < 3) {
if (noNondominatedPoints < 1) ERROR("run-time error");
tempVolume = front[0][0];
noNondominatedPoints = FilterNondominatedSet(front, n, noObjectives - 1);
tempVolume = 0;
if (noObjectives < 3) {
if (noNondominatedPoints < 1)
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
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;
return volume;
} /* CalculateHypervolume */
int ReadFront(double **frontPtr[], FILE *file, int noObjectives)
int ReadFront(double** frontPtr[], FILE* file, int noObjectives)
{
int noPoints;
int i;
double value;
int noPoints;
int i;
double value;
/* check file and count points */
noPoints = 0;
while (!feof(file)) {
for (i = 0; i < noObjectives && fscanf(file, "%lf", &value) != EOF; i++);
if (i > 0 && i < noObjectives) ERROR("data in file incomplete");
noPoints++;
}
/* allocate memory */
*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;
/* check file and count points */
noPoints = 0;
while (!feof(file)) {
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");
noPoints++;
}
if (noPoints < 1) ERROR("file contains no data");
return noPoints;
/* allocate memory */
*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");
noPoints++;
}
if (noPoints < 1)
ERROR("file contains no data");
return noPoints;
} /* ReadFront */
int MergeFronts(double **frontPtr[], double *front1[], int sizeFront1,
double* front2[], int sizeFront2, int noObjectives)
int MergeFronts(double** frontPtr[], double* front1[], int sizeFront1,
double* front2[], int sizeFront2, int noObjectives)
{
int i, j;
int noPoints;
int i, j;
int noPoints;
/* allocate memory */
noPoints = sizeFront1 + sizeFront2;
*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");
}
/* copy points */
noPoints = 0;
for (i = 0; i < sizeFront1; i++) {
for (j = 0; j < noObjectives; j++)
(*frontPtr)[noPoints][j] = front1[i][j];
noPoints++;
}
for (i = 0; i < sizeFront2; i++) {
for (j = 0; j < noObjectives; j++)
(*frontPtr)[noPoints][j] = front2[i][j];
noPoints++;
}
/* allocate memory */
noPoints = sizeFront1 + sizeFront2;
*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");
}
/* copy points */
noPoints = 0;
for (i = 0; i < sizeFront1; i++) {
for (j = 0; j < noObjectives; j++)
(*frontPtr)[noPoints][j] = front1[i][j];
noPoints++;
}
for (i = 0; i < sizeFront2; i++) {
for (j = 0; j < noObjectives; j++)
(*frontPtr)[noPoints][j] = front2[i][j];
noPoints++;
}
return noPoints;
return noPoints;
} /* MergeFronts */
void DeallocateFront(double** front, int noPoints)
void DeallocateFront(double** front, int noPoints)
{
int i;
int i;
if (front != NULL) {
for (i = 0; i < noPoints; i++)
if (front[i] != NULL)
free(front[i]);
free(front);
}
if (front != NULL) {
for (i = 0; i < noPoints; i++)
if (front[i] != NULL)
free(front[i]);
free(front);
}
} /* DeallocateFront */
#if 0
......
#ifndef HYPERVOL_H__
#define HYPERVOL_H__
extern "C"{
int FilterNondominatedSet(double *front[], int noPoints, int noObjectives);
double CalculateHypervolume(double *front[], int noPoints, int noObjectives);
extern "C" {
int FilterNondominatedSet(double* front[], int noPoints, int noObjectives);
double CalculateHypervolume(double* front[], int noPoints, int noObjectives);
}
#endif
......@@ -93,7 +93,6 @@ namespace limbo {
boost::parameter::optional<tag::stopcrit>,
boost::parameter::optional<tag::modelfun>> bo_multi_signature;
// clang-format off
template <class Params,
class A1 = boost::parameter::void_,
......@@ -105,18 +104,18 @@ namespace limbo {
// clang-format on
class BoMulti : public limbo::bayes_opt::BoBase<Params, A2, A3, A4, A5, A6> {
public:
struct defaults {
struct defaults {
#ifdef USE_LIBCMAES
typedef opt::Cmaes<Params> acquiopt_t;
typedef opt::Cmaes<Params> acquiopt_t;
#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
#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
};
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 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 limbo::bayes_opt::BoBase<Params, A2, A3, A4, A5, A6> base_t;
typedef typename base_t::model_t model_t;
......
......@@ -12,9 +12,9 @@ namespace limbo {
namespace experimental {
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
template <class Params,
......@@ -40,7 +40,6 @@ namespace limbo {
// nothing here !
};
// clang-format on
}
}
}
......
......@@ -5,54 +5,55 @@
#include <limbo/experimental/tools/pareto.hpp>
namespace limbo {
namespace experimental {
namespace stat {
template <typename Params>
struct ParetoFront : public limbo::stat::StatBase<Params> {
// point, obj, sigma
typedef std::tuple<Eigen::VectorXd, Eigen::VectorXd, Eigen::VectorXd> pareto_point_t;
typedef std::vector<pareto_point_t> pareto_t;
namespace experimental {
namespace stat {
template <typename Params>
struct ParetoFront : public limbo::stat::StatBase<Params> {
// point, obj, sigma
typedef std::tuple<Eigen::VectorXd, Eigen::VectorXd, Eigen::VectorXd> pareto_point_t;
typedef std::vector<pareto_point_t> pareto_t;
template <typename BO, typename AggregatorFunction>
void operator()(const BO& bo, const AggregatorFunction&, bool blacklisted)
{
if (!bo.stats_enabled() || bo.observations().empty())
return;
std::string fname = bo.res_dir() + "/" + "pareto_front_" + std::to_string(bo.current_iteration()) + ".dat";
std::ofstream ofs(fname.c_str());
auto pareto = _pareto_data(bo);
for (auto x : pareto) {
ofs << std::get<0>(x).transpose() << " "
<< std::get<1>(x).transpose() << std::endl;
template <typename BO, typename AggregatorFunction>
void operator()(const BO& bo, const AggregatorFunction&, bool blacklisted)
{
if (!bo.stats_enabled() || bo.observations().empty())
return;
std::string fname = bo.res_dir() + "/" + "pareto_front_" + std::to_string(bo.current_iteration()) + ".dat";
std::ofstream ofs(fname.c_str());
auto pareto = _pareto_data(bo);
for (auto x : pareto) {
ofs << std::get<0>(x).transpose() << " "
<< std::get<1>(x).transpose() << std::endl;
}
}
}
protected:
template<typename BO>
pareto_t _pareto_data(const BO& bo)
{
std::vector<Eigen::VectorXd> v(bo.samples().size());
size_t dim = bo.observations().size();
std::fill(v.begin(), v.end(), Eigen::VectorXd::Zero(dim));
return pareto::pareto_set<1>(
_pack_data(bo.samples(), bo.observations(), v));
}
pareto_t _pack_data(const std::vector<Eigen::VectorXd>& points,
const std::vector<Eigen::VectorXd>& objs,
const std::vector<Eigen::VectorXd>& sigma) const
{
assert(points.size() == objs.size());
assert(sigma.size() == objs.size());
pareto_t p(points.size());
tools::par::loop(0, p.size(), [&](size_t k) {
protected:
template <typename BO>
pareto_t _pareto_data(const BO& bo)
{
std::vector<Eigen::VectorXd> v(bo.samples().size());
size_t dim = bo.observations().size();
std::fill(v.begin(), v.end(), Eigen::VectorXd::Zero(dim));
return pareto::pareto_set<1>(
_pack_data(bo.samples(), bo.observations(), v));
}
pareto_t _pack_data(const std::vector<Eigen::VectorXd>& points,
const std::vector<Eigen::VectorXd>& objs,
const std::vector<Eigen::VectorXd>& sigma) const
{
assert(points.size() == objs.size());
assert(sigma.size() == objs.size());
pareto_t p(points.size());
tools::par::loop(0, p.size(), [&](size_t k) {
// clang-format off
p[k] = std::make_tuple(points[k], objs[k], sigma[k]);
// clang-format on
});
return p;
}
};
// clang-format on
});
return p;
}
};
}
}
}
}
#endif
......@@ -128,7 +128,7 @@ namespace pareto {
f.push_back(impl::new_vector(p[0]));
size_t e = 0;
for (size_t i = 1; i < p.size(); ++i) {
/* if (i % 10000 == 0) {
/* if (i % 10000 == 0) {
std::cout << i << " [" << p.size() << "] ";
std::cout.flush();
}*/
......
#ifndef LIMBO_OPT_HPP
#define LIMBO_OPT_HPP
///@defgroup opt_defaults
///@defgroup opt
......
......@@ -4,7 +4,6 @@
///@defgroup stop
///@defgroup stop_defaults
#include <limbo/stop/chain_criteria.hpp>
#include <limbo/stop/max_iterations.hpp>
#include <limbo/stop/max_predicted_value.hpp>
......
......@@ -6,7 +6,7 @@
namespace limbo {
namespace defaults {
struct stop_maxiterations {
/// @ingroup stop_defaults
/// @ingroup stop_defaults
BO_PARAM(int, iterations, 190);
};
}
......
......@@ -53,12 +53,11 @@ namespace limbo {
/// make a 1-D vector from a double (useful when we need to return vectors)
Eigen::VectorXd make_vector(double x)
{
Eigen::VectorXd res(1);
res(0) = x;
return res;
Eigen::VectorXd res(1);
res(0) = x;
return res;
}
template <typename T>
inline constexpr int signum(T x, std::false_type is_signed)
{
......@@ -88,7 +87,7 @@ namespace limbo {
{