Skip to content

Commit

Permalink
Merge branch 'dynamic-bin-val' into nevergrad_runs
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimitri Rusin committed Nov 10, 2023
2 parents 101b5b1 + 9e07ecb commit 93f5148
Show file tree
Hide file tree
Showing 10 changed files with 810 additions and 64 deletions.
7 changes: 7 additions & 0 deletions INSTALL
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
16 changes: 3 additions & 13 deletions RUN
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
#!/usr/bin/env fish

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

true

for line in (cat .env)
set -x (echo $line | cut -d '=' -f 1) (echo $line | cut -d '=' -f 2-)
end
# set -u IOH_RESOURCES

# and ipython3 tests/python/test_cec_functions.py

# The following line puts the debug log file and the binval executable file
# under the build/ tree.
cd $build_dir

and rm -f ./cec_test_log.txt ./cec_training_log.txt

true
and cd $build_dir
and rm -f ./test.log
and c++ -Wall -Wextra -pedantic -std=c++17 -lstdc++fs \
-I$include_dir \
-I$tests_dir \
Expand All @@ -32,5 +23,4 @@ and c++ -Wall -Wextra -pedantic -std=c++17 -lstdc++fs \
$tests_dir/problem/binval.cpp \
-o binval \
$build_dir/../lib/libgtest.a -lpthread

and ./binval
6 changes: 2 additions & 4 deletions conda.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
channels:
- defaults
dependencies:
- python=3.9.18
- pip=23.3
- python==3.9.18
- pip==23.3
- pip:
- .
- breathe==4.35.0
Expand Down
4 changes: 3 additions & 1 deletion include/ioh/problem/dynamic_bin_val.hpp
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 include/ioh/problem/dynamic_bin_val/dynamic_bin_val_pareto.hpp
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 include/ioh/problem/dynamic_bin_val/dynamic_bin_val_powers_of_two.hpp
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 include/ioh/problem/dynamic_bin_val/dynamic_bin_val_uniform.hpp
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
Loading

0 comments on commit 93f5148

Please sign in to comment.