-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dynamic-bin-val' into nevergrad_runs
- Loading branch information
Showing
10 changed files
with
810 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env fish | ||
|
||
true | ||
and conda activate base | ||
and rm -rf ./.conda_environment | ||
and conda env create --prefix ./.conda_environment --file conda.yaml | ||
and conda activate ./.conda_environment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#pragma once | ||
|
||
#include "dynamic_bin_val/dynamic_bin_val.hpp" | ||
#include "dynamic_bin_val/dynamic_bin_val_pareto.hpp" | ||
#include "dynamic_bin_val/dynamic_bin_val_powers_of_two.hpp" | ||
#include "dynamic_bin_val/dynamic_bin_val_uniform.hpp" |
120 changes: 120 additions & 0 deletions
120
include/ioh/problem/dynamic_bin_val/dynamic_bin_val_pareto.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/** | ||
* @file dynamic_bin_val_powers_of_two.hpp | ||
* @brief Contains the declaration of the DynamicBinValPareto class, which represents dynamic binary value | ||
* problems in the context of IOH. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <iostream> | ||
#include <limits> | ||
#include <math.h> | ||
#include <random> | ||
#include <vector> | ||
|
||
#include "ioh/problem/single.hpp" | ||
#include "ioh/problem/transformation.hpp" | ||
|
||
namespace ioh::problem | ||
{ | ||
/** | ||
* @class DynamicBinValPareto | ||
* @brief This class serves to represent dynamic binary value problems within the context of Iterative | ||
* Optimization Heuristics (IOH). | ||
* | ||
* DynamicBinValPareto: takes a value from (1 - U) ** (-10) where U is uniformly distributed from [0, 1] for each weight at each timestep | ||
* Inheriting functionalities from the IntegerSingleObjective, it also integrates functionalities | ||
* for automatic registration of the problem type into various data structures. This facilitates | ||
* easier management and retrieval of problem instances, while encapsulating characteristics and | ||
* behaviours specific to dynamic binary value problems. It holds vital data members such as | ||
* timestep and weights, which are crucial in depicting the dynamic aspects and unique features | ||
* of these problem instances. | ||
*/ | ||
class DynamicBinValPareto : public | ||
IntegerSingleObjective, | ||
AutomaticProblemRegistration<DynamicBinValPareto, DynamicBinValPareto>, | ||
AutomaticProblemRegistration<DynamicBinValPareto, IntegerSingleObjective> | ||
{ | ||
public: | ||
|
||
double pareto_shape; // Alpha parameter for the Pareto distribution | ||
double pareto_upper_bound; | ||
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. */ | ||
|
||
/** | ||
* @brief Constructs a new instance of DynamicBinValPareto. | ||
* | ||
* @param n_variables The dimension of the problem, representing the size of the search space and | ||
* indicating the number of variables in the problem. | ||
*/ | ||
DynamicBinValPareto(const int instance, const int n_variables) : | ||
IntegerSingleObjective | ||
( | ||
MetaData(10003, instance, "DynamicBinValPareto", n_variables, common::OptimizationType::MAX), | ||
Bounds<int>(n_variables, 0, 1) | ||
), | ||
pareto_shape(0.1), | ||
pareto_upper_bound(static_cast<double>(std::numeric_limits<unsigned long>::max()) / static_cast<double>(n_variables)), | ||
timestep(0), | ||
random_generator(instance) | ||
{ | ||
if (n_variables == 1) { return; } | ||
|
||
this->weights.resize(n_variables); | ||
|
||
std::uniform_real_distribution<double> distribution(0.0, 1.0); | ||
|
||
for(size_t i = 0; i < this->weights.size(); ++i) | ||
{ | ||
double uniform_sample = distribution(this->random_generator); | ||
|
||
// Calculate the weight using the power-law distribution inversion formula | ||
// Truncate the distribution to prevent overflow when weights are summed | ||
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); | ||
} | ||
} | ||
|
||
int step() | ||
{ | ||
this->timestep += 1; | ||
|
||
std::uniform_real_distribution<double> distribution(0.0, 1.0); | ||
|
||
for(size_t i = 0; i < this->weights.size(); ++i) | ||
{ | ||
double uniform_sample = distribution(this->random_generator); | ||
|
||
// Calculate the weight using the power-law distribution inversion formula | ||
// Truncate the distribution to prevent overflow when weights are summed | ||
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); | ||
} | ||
|
||
return this->timestep; | ||
} | ||
|
||
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 | ||
{ | ||
double value = 0; | ||
for(size_t i = 0; i < x.size(); ++i) | ||
{ | ||
value += x[i] * this->weights[i]; | ||
} | ||
|
||
return value; | ||
} | ||
}; | ||
} // namespace ioh::problem |
113 changes: 113 additions & 0 deletions
113
include/ioh/problem/dynamic_bin_val/dynamic_bin_val_powers_of_two.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/** | ||
* @file dynamic_bin_val_powers_of_two.hpp | ||
* @brief Contains the declaration of the DynamicBinValPowersOfTwo class, which represents dynamic binary value | ||
* problems in the context of IOH. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <iostream> | ||
#include <math.h> | ||
#include <random> | ||
#include <vector> | ||
|
||
#include "ioh/problem/single.hpp" | ||
#include "ioh/problem/transformation.hpp" | ||
|
||
namespace ioh::problem | ||
{ | ||
/** | ||
* @class DynamicBinValPowersOfTwo | ||
* @brief This class serves to represent dynamic binary value problems within the context of Iterative | ||
* Optimization Heuristics (IOH). | ||
* | ||
* DynamicBinValPowersOfTwo: takes a value among the powers 2**1, 2**2, 2**3, ..., 2**31 / n_variables for each weight at each timestep | ||
* | ||
* Inheriting functionalities from the IntegerSingleObjective, it also integrates functionalities | ||
* for automatic registration of the problem type into various data structures. This facilitates | ||
* easier management and retrieval of problem instances, while encapsulating characteristics and | ||
* behaviours specific to dynamic binary value problems. It holds vital data members such as | ||
* timestep and weights, which are crucial in depicting the dynamic aspects and unique features | ||
* of these problem instances. | ||
*/ | ||
class DynamicBinValPowersOfTwo : public | ||
IntegerSingleObjective, | ||
AutomaticProblemRegistration<DynamicBinValPowersOfTwo, DynamicBinValPowersOfTwo>, | ||
AutomaticProblemRegistration<DynamicBinValPowersOfTwo, IntegerSingleObjective> | ||
{ | ||
public: | ||
|
||
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; | ||
|
||
/** | ||
* @brief Constructs a new instance of DynamicBinValPowersOfTwo. | ||
* | ||
* @param n_variables The dimension of the problem, representing the size of the search space and | ||
* indicating the number of variables in the problem. | ||
*/ | ||
DynamicBinValPowersOfTwo(const int instance, const int n_variables) : | ||
IntegerSingleObjective | ||
( | ||
MetaData(10002, instance, "DynamicBinValPowersOfTwo", n_variables, common::OptimizationType::MAX), | ||
Bounds<int>(n_variables, 0, 1) | ||
), | ||
random_generator(instance) | ||
{ | ||
if (n_variables == 1) { return; } | ||
|
||
this->timestep = 0; | ||
|
||
// Initialize the weights vector with random numbers between 0 and 1 | ||
this->weights.resize(n_variables); | ||
|
||
int subtract_bits = log2(this->weights.size()); | ||
std::uniform_int_distribution<> uniform_int_distribution(1, 31 - subtract_bits - 1); | ||
|
||
// Reinitialize the weights with random numbers between 0 and 1 after shuffling | ||
for(size_t i = 0; i < this->weights.size(); ++i) | ||
{ | ||
int exponent = uniform_int_distribution(this->random_generator); | ||
this->weights[i] = pow(2, exponent); | ||
} | ||
} | ||
|
||
int step() | ||
{ | ||
this->timestep += 1; | ||
|
||
int subtract_bits = log2(this->weights.size()); | ||
std::uniform_int_distribution<> uniform_int_distribution(1, 31 - subtract_bits - 1); | ||
|
||
// Reinitialize the weights with random numbers between 0 and 1 after shuffling | ||
for(size_t i = 0; i < this->weights.size(); ++i) | ||
{ | ||
int exponent = uniform_int_distribution(this->random_generator); | ||
this->weights[i] = pow(2, exponent); | ||
} | ||
|
||
return this->timestep; | ||
} | ||
|
||
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 | ||
{ | ||
double value = 0; | ||
for(size_t i = 0; i < x.size(); ++i) | ||
{ | ||
value += x[i] * this->weights[i]; | ||
} | ||
|
||
return value; | ||
} | ||
}; | ||
} // namespace ioh::problem |
102 changes: 102 additions & 0 deletions
102
include/ioh/problem/dynamic_bin_val/dynamic_bin_val_uniform.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* @file dynamic_bin_val_uniform.hpp | ||
* @brief Contains the declaration of the DynamicBinValUniform class, which represents dynamic binary value | ||
* problems in the context of IOH. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <iostream> | ||
#include <random> | ||
#include <vector> | ||
|
||
#include "ioh/problem/single.hpp" | ||
#include "ioh/problem/transformation.hpp" | ||
|
||
namespace ioh::problem | ||
{ | ||
/** | ||
* @class DynamicBinValUniform | ||
* @brief This class serves to represent dynamic binary value problems within the context of Iterative | ||
* Optimization Heuristics (IOH). | ||
* | ||
* DynamicBinValUniform: takes a value between 0 and 1 for each component at each timestep | ||
* | ||
* Inheriting functionalities from the IntegerSingleObjective, it also integrates functionalities | ||
* for automatic registration of the problem type into various data structures. This facilitates | ||
* easier management and retrieval of problem instances, while encapsulating characteristics and | ||
* behaviours specific to dynamic binary value problems. It holds vital data members such as | ||
* timestep and weights, which are crucial in depicting the dynamic aspects and unique features | ||
* of these problem instances. | ||
*/ | ||
class DynamicBinValUniform : public | ||
IntegerSingleObjective, | ||
AutomaticProblemRegistration<DynamicBinValUniform, DynamicBinValUniform>, | ||
AutomaticProblemRegistration<DynamicBinValUniform, IntegerSingleObjective> | ||
{ | ||
public: | ||
|
||
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; | ||
|
||
/** | ||
* @brief Constructs a new instance of DynamicBinValUniform. | ||
* | ||
* @param n_variables The dimension of the problem, representing the size of the search space and | ||
* indicating the number of variables in the problem. | ||
*/ | ||
DynamicBinValUniform(const int instance, const int n_variables) : | ||
IntegerSingleObjective | ||
( | ||
MetaData(10001, instance, "DynamicBinValUniform", n_variables, common::OptimizationType::MAX), | ||
Bounds<int>(n_variables, 0, 1) | ||
), | ||
random_generator(instance) | ||
{ | ||
if (n_variables == 1) { return; } | ||
|
||
this->timestep = 0; | ||
|
||
// Initialize the weights vector with random numbers between 0 and 1 | ||
this->weights.resize(n_variables); | ||
for(int i = 0; i < n_variables; ++i) | ||
{ | ||
this->weights[i] = std::generate_canonical<double, 10>(this->random_generator); | ||
} | ||
} | ||
|
||
int step() | ||
{ | ||
this->timestep += 1; | ||
|
||
// Reinitialize the weights with random numbers between 0 and 1 after shuffling | ||
for(size_t i = 0; i < this->weights.size(); ++i) | ||
{ | ||
this->weights[i] = std::generate_canonical<double, 10>(this->random_generator); | ||
} | ||
|
||
return this->timestep; | ||
} | ||
|
||
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 | ||
{ | ||
double value = 0; | ||
for(size_t i = 0; i < x.size(); ++i) | ||
{ | ||
value += x[i] * this->weights[i]; | ||
} | ||
|
||
return value; | ||
} | ||
}; | ||
} // namespace ioh::problem |
Oops, something went wrong.