Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Meta problem opt transformation #117

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/problem/base_meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ class __PAGMO_VISIBLE base_meta : public base
bool compare_fc_impl(const fitness_vector &f1, const constraint_vector &c1, const fitness_vector &f2, const constraint_vector &c2) const
{return m_original_problem->compare_fc_impl(f1,c1,f2,c2);}
private:
// Computing new optima from the optima of the original problem.
// Applicable to only some transformations.
virtual std::vector<decision_vector> transform_x(const std::vector<decision_vector> &) const
{ return std::vector<decision_vector>(); }

friend class boost::serialization::access;
template <class Archive>
void serialize(Archive &ar, const unsigned int)
Expand Down
30 changes: 29 additions & 1 deletion src/problem/himmelblau.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
namespace pagmo { namespace problem {

/// Default constructor.
himmelblau::himmelblau():base(-6.,6.,2) {}
himmelblau::himmelblau():base(-6.,6.,2)
{
// initialize best solution
initialize_best();
}

/// Clone method.
base_ptr himmelblau::clone() const
Expand All @@ -51,6 +55,30 @@ std::string himmelblau::get_name() const
return "Himmelblau";
}

/// Initialize the Himmelblau's function four local minima.
void himmelblau::initialize_best(void)
{
const int min_count = 4;
const int x_dimension = 2;

const double x_vector[][x_dimension] = {
{3.0, 2.0},
{-2.805118, 3.131312},
{-3.779310, -3.283186},
{3.584428, -1.848126}
};

std::vector<decision_vector> best_x(min_count);

for (int i = 0; i < min_count; ++i) {
decision_vector x(x_dimension);
std::copy(x_vector[i],x_vector[i] + x_dimension,x.begin());
best_x[i] = x;
}

set_best_x(best_x);
}

}}

BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::problem::himmelblau)
2 changes: 2 additions & 0 deletions src/problem/himmelblau.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class __PAGMO_VISIBLE himmelblau: public base
protected:
void objfun_impl(fitness_vector &, const decision_vector &) const;
private:
void initialize_best(void);

friend class boost::serialization::access;
template <class Archive>
void serialize(Archive &ar, const unsigned int)
Expand Down
21 changes: 21 additions & 0 deletions src/problem/normalized.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ normalized::normalized(const base & p):
m_normalization_scale(p.get_dimension(),0)
{
configure_new_bounds();
std::vector<decision_vector> new_best_x = transform_x(p.get_best_x());
set_best_x(new_best_x);
}

/// Clone method.
Expand All @@ -73,6 +75,25 @@ void normalized::configure_new_bounds()
set_bounds(-1, 1);
}

/// Compute normalized vectors for the meta-problem from the original problem.
/*
* @param[in] x vectors of the original problem
* @param[out] vectors x normalized
*/
std::vector<decision_vector> normalized::transform_x(const std::vector<decision_vector> &x) const
{
const base::size_type cnt = x.size();
std::vector<decision_vector> new_x = x;

for (base::size_type i = 0; i < cnt; ++i) {
for (base::size_type j = 0; j < x[i].size(); ++j) {
new_x[i][j] = (x[i][j] - m_normalization_center[j]) / m_normalization_scale[j];
}
}

return new_x;
}

/// Returns the de-normalized version of the decision variables
decision_vector normalized::denormalize(const decision_vector& x) const
{
Expand Down
1 change: 1 addition & 0 deletions src/problem/normalized.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class __PAGMO_VISIBLE normalized : public base_meta
void compute_constraints_impl(constraint_vector &, const decision_vector &) const;
private:
void configure_new_bounds();
std::vector<decision_vector> transform_x(const std::vector<decision_vector> &) const;

friend class boost::serialization::access;
template <class Archive>
Expand Down
39 changes: 39 additions & 0 deletions src/problem/rotated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ rotated::rotated(const base &p, const Eigen::MatrixXd &rotation ):
pagmo_throw(value_error,"Input problem has an integer dimension. Cannot rotate it.");
}
configure_new_bounds();
std::vector<decision_vector> new_best_x = transform_x(p.get_best_x());
set_best_x(new_best_x);
}

/**
Expand Down Expand Up @@ -107,6 +109,8 @@ rotated::rotated(const base &p,
pagmo_throw(value_error,"The input matrix seems not to be orthonormal (to a tolerance of 1e-5)");
}
configure_new_bounds();
std::vector<decision_vector> new_best_x = transform_x(p.get_best_x());
set_best_x(new_best_x);
}

/**
Expand Down Expand Up @@ -138,6 +142,8 @@ rotated::rotated(const base &p):
pagmo_throw(value_error,"Input problem has an integer dimension. Cannot rotate it.");
}
configure_new_bounds();
std::vector<decision_vector> new_best_x = transform_x(p.get_best_x());
set_best_x(new_best_x);
}

/// Clone method.
Expand Down Expand Up @@ -170,6 +176,39 @@ void rotated::configure_new_bounds()
set_bounds(-sqrt(2), sqrt(2));
}

/// Compute rotated vectors for the meta-problem from the original problem.
/*
* @param[in] x vectors of the original problem
* @param[out] vectors x rotated
*/
std::vector<decision_vector> rotated::transform_x(const std::vector<decision_vector> &x) const
{
const base::size_type cnt = x.size();
std::vector<decision_vector> new_x = x;

for (base::size_type i = 0; i < cnt; ++i) {
// 1. normalize the vector x[i]
decision_vector x_normed(x[i].size());
for (base::size_type j = 0; j < x[i].size(); ++j) {
x_normed[j] = (x[i][j] - m_normalize_translation[j]) / m_normalize_scale[j];
}

// 2. rotate the normalized vector
Eigen::VectorXd x_normed_vec = Eigen::VectorXd::Zero(x_normed.size());
for (base::size_type j = 0; j < x_normed.size(); ++j) {
x_normed_vec(j) = x_normed[j];
}
Eigen::VectorXd x_rotated_vec = m_Rotate * x_normed_vec;

// Store the normalized and rotated vector
for (base::size_type j = 0; j < x[i].size(); ++j) {
new_x[i][j] = x_rotated_vec(j);
}
}

return new_x;
}

// Used to normalize the original upper and lower bounds
// to [-1, 1], at each dimension
decision_vector rotated::normalize_to_center(const decision_vector& x) const
Expand Down
1 change: 1 addition & 0 deletions src/problem/rotated.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class __PAGMO_VISIBLE rotated : public base_meta

private:
void configure_new_bounds();
std::vector<decision_vector> transform_x(const std::vector<decision_vector> &) const;

decision_vector normalize_to_center(const decision_vector& x) const;
decision_vector denormalize_to_original(const decision_vector& x) const;
Expand Down
10 changes: 10 additions & 0 deletions src/problem/scaled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ void scaled::compute_constraints_impl(constraint_vector &c, const decision_vecto
m_original_problem->compute_constraints(c, x);
}

/// Compute transformed vectors for the meta-problem from the original problem with the fitness scaled.
/*
* @param[in] x vectors of the original problem
* @param[out] vectors of the scaled problem
*/
std::vector<decision_vector> scaled::transform_x(const std::vector<decision_vector> &x) const
{
// Only the fitness is scaled, not the space itself.
return x;
}

std::string scaled::get_name() const
{
Expand Down
1 change: 1 addition & 0 deletions src/problem/scaled.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class __PAGMO_VISIBLE scaled : public base_meta
void objfun_impl(fitness_vector &, const decision_vector &) const;
void compute_constraints_impl(constraint_vector &, const decision_vector &) const;
private:
std::vector<decision_vector> transform_x(const std::vector<decision_vector> &) const;

friend class boost::serialization::access;
template <class Archive>
Expand Down
25 changes: 25 additions & 0 deletions src/problem/shifted.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ shifted::shifted(const base & p,
pagmo_throw(value_error,"The size of the shifting vector must be equal to the problem dimension");
}
configure_shifted_bounds(m_translation);
std::vector<decision_vector> new_best_x = transform_x(p.get_best_x());
set_best_x(new_best_x);
}


Expand All @@ -82,6 +84,8 @@ shifted::shifted(const base & p,
m_translation(decision_vector(p.get_dimension(), t))
{
configure_shifted_bounds(m_translation);
std::vector<decision_vector> new_best_x = transform_x(p.get_best_x());
set_best_x(new_best_x);
}

/**
Expand All @@ -107,6 +111,8 @@ shifted::shifted(const base & p):
m_translation[i] = (2*((double) rand() / (RAND_MAX))-1) * (p.get_ub()[i]-p.get_lb()[i]);
}
configure_shifted_bounds(m_translation);
std::vector<decision_vector> new_best_x = transform_x(p.get_best_x());
set_best_x(new_best_x);
}

/// Clone method.
Expand Down Expand Up @@ -159,6 +165,25 @@ void shifted::compute_constraints_impl(constraint_vector &c, const decision_vect
m_original_problem->compute_constraints(c, x_translated);
}

/// Compute shifted vectors for the meta-problem from the original problem.
/*
* @param[in] x vectors of the original problem
* @param[out] vectors x shifted
*/
std::vector<decision_vector> shifted::transform_x(const std::vector<decision_vector> &x) const
{
const base::size_type cnt = x.size();
std::vector<decision_vector> new_x = x;

for (base::size_type i = 0; i < cnt; ++i) {
for (base::size_type j = 0; j < x[i].size(); ++j) {
new_x[i][j] = x[i][j] + m_translation[j];
}
}

return new_x;
}

/**
* Gets the shift vector which defines the problem
*
Expand Down
1 change: 1 addition & 0 deletions src/problem/shifted.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class __PAGMO_VISIBLE shifted : public base_meta
void compute_constraints_impl(constraint_vector &, const decision_vector &) const;
private:
void configure_shifted_bounds(const decision_vector &);
std::vector<decision_vector> transform_x(const std::vector<decision_vector> &) const;

friend class boost::serialization::access;
template <class Archive>
Expand Down
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ ADD_EXECUTABLE(test_tsp test_tsp.cpp)
TARGET_LINK_LIBRARIES(test_tsp ${MANDATORY_LIBRARIES} pagmo_static)
ADD_TEST(test_tsp test_tsp)

ADD_EXECUTABLE(test_optima_transformations test_optima_transformations.cpp)
TARGET_LINK_LIBRARIES(test_optima_transformations ${MANDATORY_LIBRARIES} pagmo_static)
ADD_TEST(test_optima_transformations test_optima_transformations)

ADD_EXECUTABLE(test_archipelago test_archipelago.cpp)
TARGET_LINK_LIBRARIES(test_archipelago ${MANDATORY_LIBRARIES} pagmo_static)
ADD_TEST(test_archipelago test_archipelago)
Expand Down
7 changes: 3 additions & 4 deletions tests/best_solutions_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ int main()
std::cout << " fitness passes, ";
}
else{
std::cout << " fitness failed!"<<std::endl;
std::cout << " fitness failed!" << std::endl;
return 1;
}
}
Expand All @@ -197,18 +197,17 @@ int main()
constraint_vector best_c_computed = best_tests[i].problem->get_best_c().at(j);

if(is_eq(best_c_computed, best_c, EPS)){
std::cout << " constraints passes.";
std::cout << " constraints passes." << std::endl;
}
else{
std::cout << " constraints failed!"<<std::endl;
std::cout << " constraints failed!" << std::endl;
return 1;
}
}
}
}
}

std::cout << std::endl;
return 0;
}

Expand Down
Loading