Skip to content

Commit

Permalink
Save the true distance from the optimum in ioh.get_problem().current_…
Browse files Browse the repository at this point in the history
…best_internal.

To compute the misleading objective value of the string, we store the string in the intermediate storage this->transformed_x. Otherwise, we'd have to compute the sum of weights at correctly guessed indices just from the number of correct indices.
  • Loading branch information
Dimitri Rusin committed Nov 13, 2023
1 parent 9e07ecb commit a0f3b59
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 29 deletions.
29 changes: 20 additions & 9 deletions include/ioh/problem/dynamic_bin_val/dynamic_bin_val_pareto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace ioh::problem
int timestep; /**< The current timestep in the dynamic binary value problem scenario. */
std::default_random_engine random_generator;
std::vector<unsigned long> weights; /**< A vector of weights used in the evaluation of the problem. */
std::vector<int> transformed_x;

/**
* @brief Constructs a new instance of DynamicBinValPareto.
Expand Down Expand Up @@ -77,6 +78,11 @@ namespace ioh::problem
auto pareto_distributed = std::min(std::pow(1.0 - uniform_sample, -1.0 / pareto_shape), pareto_upper_bound);
this->weights[i] = static_cast<unsigned long>(pareto_distributed);
}

this->transformed_x = std::vector<int>(n_variables, 1);
this->optimum_.y = transform_objectives(0);
transform_variables(this->transformed_x);
this->optimum_.x = this->transformed_x;
}

int step()
Expand All @@ -100,20 +106,25 @@ namespace ioh::problem

protected:

/**
* @brief Evaluates the problem instance using the given input vector.
*
* @param x The input vector which represents a potential solution to the problem.
* @return The evaluation result as a double value.
*/
double evaluate(const std::vector<int> &x) override
{
return std::accumulate(x.begin(), x.end(), 0.0);
}

std::vector<int> transform_variables(std::vector<int> x) override
{
transformation::variables::random_flip(x, this->meta_data_.instance);
this->transformed_x = x;
return x;
}

double transform_objectives(const double y) override
{
double value = 0;
for(size_t i = 0; i < x.size(); ++i)
for(size_t i = 0; i < this->transformed_x.size(); ++i)
{
value += x[i] * this->weights[i];
value += this->transformed_x[i] * this->weights[i];
}

return value;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace ioh::problem
int timestep; /**< The current timestep in the dynamic binary value problem scenario. */
std::vector<int> weights; /**< A vector of weights used in the evaluation of the problem. */
std::mt19937 random_generator;
std::vector<int> transformed_x;

/**
* @brief Constructs a new instance of DynamicBinValPowersOfTwo.
Expand Down Expand Up @@ -72,6 +73,11 @@ namespace ioh::problem
int exponent = uniform_int_distribution(this->random_generator);
this->weights[i] = pow(2, exponent);
}

this->transformed_x = std::vector<int>(n_variables, 1);
this->optimum_.y = transform_objectives(0);
transform_variables(this->transformed_x);
this->optimum_.x = this->transformed_x;
}

int step()
Expand All @@ -93,20 +99,25 @@ namespace ioh::problem

protected:

/**
* @brief Evaluates the problem instance using the given input vector.
*
* @param x The input vector which represents a potential solution to the problem.
* @return The evaluation result as a double value.
*/
double evaluate(const std::vector<int> &x) override
{
return std::accumulate(x.begin(), x.end(), 0.0);
}

std::vector<int> transform_variables(std::vector<int> x) override
{
transformation::variables::random_flip(x, this->meta_data_.instance);
this->transformed_x = x;
return x;
}

double transform_objectives(const double y) override
{
double value = 0;
for(size_t i = 0; i < x.size(); ++i)
for(size_t i = 0; i < this->transformed_x.size(); ++i)
{
value += x[i] * this->weights[i];
value += this->transformed_x[i] * this->weights[i];
}

return value;
}
};
Expand Down
29 changes: 20 additions & 9 deletions include/ioh/problem/dynamic_bin_val/dynamic_bin_val_uniform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace ioh::problem
int timestep; /**< The current timestep in the dynamic binary value problem scenario. */
std::vector<double> weights; /**< A vector of weights used in the evaluation of the problem. */
std::mt19937 random_generator;
std::vector<int> transformed_x;

/**
* @brief Constructs a new instance of DynamicBinValUniform.
Expand All @@ -65,6 +66,11 @@ namespace ioh::problem
{
this->weights[i] = std::generate_canonical<double, 10>(this->random_generator);
}

this->transformed_x = std::vector<int>(n_variables, 1);
this->optimum_.y = transform_objectives(0);
transform_variables(this->transformed_x);
this->optimum_.x = this->transformed_x;
}

int step()
Expand All @@ -82,20 +88,25 @@ namespace ioh::problem

protected:

/**
* @brief Evaluates the problem instance using the given input vector.
*
* @param x The input vector which represents a potential solution to the problem.
* @return The evaluation result as a double value.
*/
double evaluate(const std::vector<int> &x) override
{
return std::accumulate(x.begin(), x.end(), 0.0);
}

std::vector<int> transform_variables(std::vector<int> x) override
{
transformation::variables::random_flip(x, this->meta_data_.instance);
this->transformed_x = x;
return x;
}

double transform_objectives(const double y) override
{
double value = 0;
for(size_t i = 0; i < x.size(); ++i)
for(size_t i = 0; i < this->transformed_x.size(); ++i)
{
value += x[i] * this->weights[i];
value += this->transformed_x[i] * this->weights[i];
}

return value;
}
};
Expand Down
4 changes: 2 additions & 2 deletions tests/cpp/problem/binval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

int main()
{
const auto &dyn_problem_factory = ioh::problem::ProblemRegistry<ioh::problem::DynamicBinValPareto>::instance();
auto d = dyn_problem_factory.create(10002, 1, 5);
const auto &dyn_problem_factory = ioh::problem::ProblemRegistry<ioh::problem::DynamicBinValUniform>::instance();
auto d = dyn_problem_factory.create(10001, 1, 5);
auto x = std::vector<int>{1, 0, 1, 1, 1};
double value = (*d)(x);

Expand Down

0 comments on commit a0f3b59

Please sign in to comment.