Skip to content

Commit

Permalink
add comments and replace shiftfunc and rotatefunc with existing varia…
Browse files Browse the repository at this point in the history
…ble transformations functions from the toolkit of IOHexperimenter
  • Loading branch information
Dimitri Rusin committed Sep 20, 2023
1 parent caf03fb commit 5c681e3
Show file tree
Hide file tree
Showing 19 changed files with 451 additions and 113 deletions.
2 changes: 0 additions & 2 deletions INSTALL_IOH → BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ and mkdir build/
and cd build/
and cmake ..
and make install

ln -fs (pwd)/static/cec_transformations build/tests/input_data
Empty file modified INSTALL
100644 → 100755
Empty file.
7 changes: 6 additions & 1 deletion RUN
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

# Set the directory paths for the project and its includes
set build_dir (pwd)"/build/tests"
set include_dir (pwd)"/include"
set c_implementation_cec_transformations_folder (pwd)"/build/tests/input_data"
set external_dir (pwd)"/external"
set include_dir (pwd)"/include"
set ioh_cec_transformations_folder (pwd)"/static/cec_transformations"
set tests_dir (pwd)"/tests/cpp"

true
Expand Down Expand Up @@ -52,5 +54,8 @@ and c++ -Wall -Wextra -pedantic -std=c++17 -lstdc++fs \
-o ./set_cec_problems \
$build_dir/../lib/libgtest.a -lpthread

and rm -f c_implementation_cec_transformations_folder
and ln -s ioh_cec_transformations_folder c_implementation_cec_transformations_folder
and ./set_cec_problems

and ./test_cec_problem
6 changes: 6 additions & 0 deletions conda.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ dependencies:
- python=3.9
- pip
- pip:
- .
- breathe
- cmake
- colored
- furo
- jupyter
- m2r2
- mypy
- ninja
- pybind11
- setuptools
- sphinx
- sphinx-automodapi
- wheel
- xmltodict
63 changes: 44 additions & 19 deletions include/ioh/problem/cec/cec_problem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,23 @@

namespace ioh::problem
{
class CEC : public RealSingleObjective
{
/// \brief The CEC class represents a problem in the CEC benchmark suite.
/// It inherits from the RealSingleObjective class and contains methods to load transformation data from static files and apply transformations to problem variables and objectives.
class CEC : public RealSingleObjective {
public:

int objective_shift_;
std::vector<std::vector<double>> variables_shifts_;
std::vector<int> input_permutation_;
std::vector<std::vector<std::vector<double>>> linear_transformations_;
int objective_shift_; ///< The shift value applied to the objective function to transform it.
std::vector<std::vector<double>> variables_shifts_; ///< A collection of shift values applied to the problem variables during transformation.
std::vector<int> input_permutation_; ///< A permutation vector applied to the input variables during transformation.
std::vector<std::vector<std::vector<double>>> linear_transformations_; ///< A collection of matrices representing linear transformations applied to the problem variables.

/**
* @brief Construct a new CEC object.
* @brief Constructs a new CEC problem instance.
*
* @param problem_id The id of the problem (should be unique)
* @param instance The instance of the problem (ignored for cec for now)
* IGNORED, because the instance id is used as a random seed in BBOB problems,
* but we employ no randomness in CEC,
* since we take all transformation data from static files
* @param n_variables the dimension of the problem (the size of the search space, how many x varables)
* @param name the name of the problem (should be unique)
* @param problem_id Unique identifier for the problem.
* @param instance The instance number of the problem (currently ignored for CEC problems).
* @param n_variables The number of variables in the problem, representing the dimensionality of the search space.
* @param name A unique name for the problem.
*/
CEC
(
Expand Down Expand Up @@ -54,6 +52,7 @@ namespace ioh::problem
this->optimum_.x.assign(this->variables_shifts_[0].begin(), this->variables_shifts_[0].begin() + n_variables);
}

/// \brief Sets the optimum value for the problem based on the current transformation data.
void set_optimum()
{
const int problem_id = this->meta_data_.problem_id;
Expand All @@ -71,13 +70,19 @@ namespace ioh::problem
this->optimum_.y = (*this)(this->optimum_.x);
}

/**
* @brief Transforms the objective function value using the current objective shift value.
*
* @param y The original objective function value.
* @return The transformed objective function value.
*/
double transform_objectives(const double y) override
{
auto&& transformed = y + this->objective_shift_;
return transformed;
}

//! Handler for reading all static data
/// \brief Loads the transformation data from static files based on the problem ID and the number of variables.
void load_transformation_data()
{
const int n_variables = this->meta_data_.n_variables;
Expand Down Expand Up @@ -154,6 +159,11 @@ namespace ioh::problem
}
}

/**
* @brief Loads the shuffle data from a file and stores it in the input_permutation_ member variable.
*
* @param shuffle_data_filepath The path to the file containing the shuffle data.
*/
void load_shuffle_data(const std::filesystem::path& shuffle_data_filepath)
{
std::ifstream file(shuffle_data_filepath); // Open the file
Expand All @@ -175,6 +185,11 @@ namespace ioh::problem
}
}

/**
* @brief Loads the objective shift data from a file and stores it in the objective_shift_ member variable.
*
* @param F_i_star_filepath The path to the file containing the objective shift data.
*/
void load_objective_shift(const std::filesystem::path& F_i_star_filepath)
{
const unsigned int cec_function_identifier = this->meta_data_.problem_id - 1000;
Expand Down Expand Up @@ -208,6 +223,11 @@ namespace ioh::problem
throw std::out_of_range("CEC function identifier out of range.");
}

/**
* @brief Loads the linear transformation data from a file and stores it in the linear_transformations_ member variable.
*
* @param M_filepath The path to the file containing the linear transformation data.
*/
void load_linear_transformation(const std::filesystem::path& M_filepath)
{
const size_t n_variables = this->meta_data_.n_variables;
Expand Down Expand Up @@ -253,6 +273,11 @@ namespace ioh::problem
}
}

/**
* @brief Loads the variables shift data from a file and stores it in the variables_shifts_ member variable.
*
* @param shift_data_filepath The path to the file containing the variables shift data.
*/
void load_variables_shift(const std::filesystem::path& shift_data_filepath)
{
const int n_variables = this->meta_data_.n_variables;
Expand Down Expand Up @@ -292,17 +317,17 @@ namespace ioh::problem
};

/**
* @brief CRTP class for CEC problems. Inherit from this class when defining new CEC problems.
* This is needed for storing stuff in the hash maps.
* @brief A template class for creating new CEC problems.
* Inherit from this class when defining new CEC problems to enable automatic registration in hash maps.
*
* @tparam ProblemType The New BBOB problem class
* @tparam ProblemType The class representing the new CEC problem.
*/
template <typename ProblemType>
struct CECProblem : public CEC,
AutomaticProblemRegistration<ProblemType, CEC>,
AutomaticProblemRegistration<ProblemType, RealSingleObjective>
{
public:
using CEC::CEC;
using CEC::CEC; ///< Inherits the constructor of the CEC class.
};
} // namespace ioh::problem
23 changes: 21 additions & 2 deletions include/ioh/problem/cec/composition_function_1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,45 @@

namespace ioh::problem::cec
{
/// \brief Class representing the first composition function in the CEC benchmark suite.
class CompositionFunction1 final : public CECProblem<CompositionFunction1>
{
protected:

/**
* @brief Evaluates the objective function for the given input vector.
*
* @param x The input vector.
* @return The objective function value.
*/
double evaluate(const std::vector<double>& x) override
{
double f = cf01(x, this->variables_shifts_, this->linear_transformations_, 1);
return f;
}

/**
* @brief Transforms the input variables based on the current transformation data.
*
* @param x The original input variables.
* @return The transformed input variables.
*/
std::vector<double> transform_variables(std::vector<double> x) override
{
return x;
}

public:

inline static const int meta_problem_id = 1009;
inline static const std::string meta_name = "CEC_CompositionFunction1";
inline static const int meta_problem_id = 1009; ///< The unique identifier for this problem.
inline static const std::string meta_name = "CEC_CompositionFunction1"; ///< The unique name for this problem.

/**
* @brief Constructs a CompositionFunction1 instance.
*
* @param instance The instance number of the problem.
* @param n_variables The number of variables in the problem.
*/
CompositionFunction1(const int instance, const int n_variables) :
CECProblem(meta_problem_id, instance, n_variables, meta_name)
{
Expand Down
23 changes: 21 additions & 2 deletions include/ioh/problem/cec/composition_function_2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,45 @@

namespace ioh::problem::cec
{
/// \brief Class representing the second composition function in the CEC benchmark suite.
class CompositionFunction2 final : public CECProblem<CompositionFunction2>
{
protected:

/**
* @brief Evaluates the objective function for the given input vector.
*
* @param x The input vector.
* @return The objective function value.
*/
double evaluate(const std::vector<double>& x) override
{
double f = cf02(x, this->variables_shifts_, this->linear_transformations_, 1);
return f;
}

/**
* @brief Transforms the input variables based on the current transformation data.
*
* @param x The original input variables.
* @return The transformed input variables.
*/
std::vector<double> transform_variables(std::vector<double> x) override
{
return x;
}

public:

inline static const int meta_problem_id = 1010;
inline static const std::string meta_name = "CEC_CompositionFunction2";
inline static const int meta_problem_id = 1010; ///< The unique identifier for this problem.
inline static const std::string meta_name = "CEC_CompositionFunction2"; ///< The unique name for this problem.

/**
* @brief Constructs a CompositionFunction2 instance.
*
* @param instance The instance number of the problem.
* @param n_variables The number of variables in the problem.
*/
CompositionFunction2(const int instance, const int n_variables) :
CECProblem(meta_problem_id, instance, n_variables, meta_name)
{
Expand Down
23 changes: 21 additions & 2 deletions include/ioh/problem/cec/composition_function_3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,45 @@

namespace ioh::problem::cec
{
/// \brief Class representing the third composition function in the CEC benchmark suite.
class CompositionFunction3 final : public CECProblem<CompositionFunction3>
{
protected:

/**
* @brief Evaluates the objective function for the given input vector.
*
* @param x The input vector.
* @return The objective function value.
*/
double evaluate(const std::vector<double>& x) override
{
double f = cf06(x, this->variables_shifts_, this->linear_transformations_, 1);
return f;
}

/**
* @brief Transforms the input variables based on the current transformation data.
*
* @param x The original input variables.
* @return The transformed input variables.
*/
std::vector<double> transform_variables(std::vector<double> x) override
{
return x;
}

public:

inline static const int meta_problem_id = 1011;
inline static const std::string meta_name = "CEC_CompositionFunction3";
inline static const int meta_problem_id = 1011; ///< The unique identifier for this problem.
inline static const std::string meta_name = "CEC_CompositionFunction3"; ///< The unique name for this problem.

/**
* @brief Constructs a CompositionFunction3 instance.
*
* @param instance The instance number of the problem.
* @param n_variables The number of variables in the problem.
*/
CompositionFunction3(const int instance, const int n_variables) :
CECProblem(meta_problem_id, instance, n_variables, meta_name)
{
Expand Down
23 changes: 21 additions & 2 deletions include/ioh/problem/cec/composition_function_4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,45 @@

namespace ioh::problem::cec
{
/// \brief Class representing the fourth composition function in the CEC benchmark suite.
class CompositionFunction4 final : public CECProblem<CompositionFunction4>
{
protected:

/**
* @brief Evaluates the objective function for the given input vector.
*
* @param x The input vector.
* @return The objective function value.
*/
double evaluate(const std::vector<double>& x) override
{
double f = cf07(x, this->variables_shifts_, this->linear_transformations_, 1);
return f;
}

/**
* @brief Transforms the input variables based on the current transformation data.
*
* @param x The original input variables.
* @return The transformed input variables.
*/
std::vector<double> transform_variables(std::vector<double> x) override
{
return x;
}

public:

inline static const int meta_problem_id = 1012;
inline static const std::string meta_name = "CEC_CompositionFunction4";
inline static const int meta_problem_id = 1012; ///< The unique identifier for this problem.
inline static const std::string meta_name = "CEC_CompositionFunction4"; ///< The unique name for this problem.

/**
* @brief Constructs a CompositionFunction4 instance.
*
* @param instance The instance number of the problem.
* @param n_variables The number of variables in the problem.
*/
CompositionFunction4(const int instance, const int n_variables) :
CECProblem(meta_problem_id, instance, n_variables, meta_name)
{
Expand Down
2 changes: 1 addition & 1 deletion include/ioh/problem/cec/expanded_schaffer_f7.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace ioh::problem::cec
{
std::vector<double> y(x.size()), z(x.size());

ioh::problem::transformation::scale_and_rotate(x, z, y, this->variables_shifts_[0], this->linear_transformations_[0], 0.5 / 100.0, 1, 1);
ioh::problem::transformation::variables::scale_and_rotate(x, z, y, this->variables_shifts_[0], this->linear_transformations_[0], 0.5 / 100.0, 1, 1);

return y;
}
Expand Down
Loading

0 comments on commit 5c681e3

Please sign in to comment.