Skip to content

Commit

Permalink
fix interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobdenobel committed May 13, 2024
1 parent 9bb7dc1 commit 4217a62
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 56 deletions.
2 changes: 1 addition & 1 deletion include/ioh/common/container_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ namespace ioh
bool operator<(const Permutation &b) const { return value < b.value; }

//! sort a set of random permutations
static std::vector<Permutation> sorted(const int n, const int seed)
static std::vector<Permutation> sorted(const int n, const unsigned long seed)
{
const auto random_numbers = random::bbob2009::uniform(n, seed);
std::vector<Permutation> permutations(n);
Expand Down
18 changes: 9 additions & 9 deletions include/ioh/common/random.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ namespace ioh::common::random
* \param lb lower bound for the random numbers
* \param ub upper bound for the random numbers
*/
inline std::vector<double> uniform(const size_t &n, long seed, const double lb = 0, const double ub = 1)
inline std::vector<double> uniform(const size_t &n, unsigned long seed, const double lb = 0, const double ub = 1)
{
auto rand_vec = std::vector<double>(n);
long rand_seed[32] = {};
Expand Down Expand Up @@ -245,30 +245,30 @@ namespace ioh::common::random

namespace bbob2009
{
inline std::vector<double> uniform(const size_t n, const int initial_seed, const double lb = 0,
inline std::vector<double> uniform(const size_t n, const unsigned long initial_seed, const double lb = 0,
const double ub = 1)
{
auto generators = std::array<int, 32>();
auto seed = std::max(1, abs(initial_seed));
auto generators = std::array<unsigned long, 32>();
auto seed = std::max(initial_seed, {1});

// Initialize the seed and generator
for (auto i = 39; i >= 0; i--)
{
seed = static_cast<int>(lcg_rand(seed));
seed = lcg_rand(seed);
if (i < 32)
generators[i] = seed;
}


auto x = std::vector<double>(n);
auto random_number = generators.front();
auto random_number = static_cast<double>(generators.front());

for (size_t i = 0; i < n; i++)
{
const auto index = static_cast<int>(floor(random_number / 67108865.0));

seed = static_cast<int>(lcg_rand(seed));
random_number = generators[index];
seed = lcg_rand(seed);
random_number = static_cast<double>(generators[index]);
generators[index] = seed;

x[i] = random_number / 2.147483647e9;
Expand All @@ -280,7 +280,7 @@ namespace ioh::common::random
return x;
}

inline std::vector<double> normal(const size_t n, const long seed, const double lb = 0, const double ub = 1)
inline std::vector<double> normal(const size_t n, const unsigned long seed, const double lb = 0, const double ub = 1)
{
assert(2 * n < 6000);
const auto uniform_random = uniform(2 * n, static_cast<int>(seed));
Expand Down
2 changes: 1 addition & 1 deletion include/ioh/logger/eaf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ namespace logger {

}
assert(nb_runs > 0);
return v / (nb_runs * (error_max-error_min) * (evals_max-evals_min));
return v / (static_cast<double>(nb_runs) * (error_max-error_min) * static_cast<double>(evals_max-evals_min));
}

/** Computes the absolute "hypervolume" of the whole empirical attainement function.
Expand Down
12 changes: 6 additions & 6 deletions include/ioh/logger/eah.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ namespace ioh
{
assert(this->_min <= x);
assert(x <= this->max()); // FIXME do we want that?
if (x >= this->max())
if (x >= static_cast<double>(this->max()))
{
return this->size() - 1;
}
Expand Down Expand Up @@ -283,7 +283,7 @@ namespace ioh
{
assert(this->_min <= x);
assert(x <= this->max()); // FIXME do we want that?
if (x >= this->max())
if (x >= static_cast<double>(this->max()))
{
return this->size() - 1;
}
Expand Down Expand Up @@ -549,8 +549,8 @@ namespace ioh
}

// If this target is worst than the domain.
if (evaluations.value() < _range_evals.min() || _range_evals.max() < evaluations.value()
|| err < _range_error.min() || _range_error.max() < err)
if (evaluations.value() < static_cast<double>(_range_evals.min()) || static_cast<double>(_range_evals.max()) < evaluations.value()
|| err < static_cast<double>(_range_error.min()) || static_cast<double>(_range_error.max()) < err)
{
// Discard it.
// FIXME we should use a more generic debug log system
Expand Down Expand Up @@ -1105,10 +1105,10 @@ namespace ioh
assert(0 <= w_proba and w_proba <= 1);
// Within the loop because widths of buckets vary for log ranges.
const double w_error = (range_error.bounds(i).second - range_error.bounds(i).first) /
range_error.length();
static_cast<double>(range_error.length());
assert(0 <= w_error and w_error <= 1);
const double w_evals = (range_evals.bounds(j).second - range_evals.bounds(j).first) /
range_evals.length();
static_cast<double>(range_evals.length());
assert(0 <= w_evals and w_evals <= 1);
// TODO allow to multiply by a weight each axis?
res = op(res, w_proba * w_error * w_evals);
Expand Down
2 changes: 1 addition & 1 deletion include/ioh/problem/bbob/bbob_problem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace ioh::problem
struct TransformationState
{
//! The seed
long seed;
unsigned long seed;

//! A vector with exponents
std::vector<double> exponents{};
Expand Down
4 changes: 2 additions & 2 deletions include/ioh/problem/bbob/gallagher101.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace ioh::problem::bbob
double value;
std::vector<double> scales;

Peak(const double value, const int seed, const int n_variables, const double condition) :
Peak(const double value, const unsigned long seed, const int n_variables, const double condition) :
value(value), scales(n_variables)
{
auto permutations = common::Permutation::sorted(n_variables, seed);
Expand All @@ -24,7 +24,7 @@ namespace ioh::problem::bbob
static_cast<double>(permutations[i].index) / (static_cast<double>(n_variables) - 1.) - 0.5);
}

static std::vector<Peak> get_peaks(const int n, const int n_variables, const int seed,
static std::vector<Peak> get_peaks(const int n, const int n_variables, const unsigned long seed,
const double max_condition)
{
static const auto f0 = 1.1, f1 = 9.1, mc = 1000.;
Expand Down
2 changes: 1 addition & 1 deletion include/ioh/problem/submodular/max_influence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace ioh::problem

for (size_t i = 0; i < x.size(); i++)
{
is_activated[i] = x[i];
is_activated[i] = static_cast<uint8_t>(x[i]);
if (x[i] != 0)
visits.push(i);
}
Expand Down
3 changes: 1 addition & 2 deletions ioh/src/real.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,6 @@ void define_dynamic_bin_val_problem(py::module &m)
.def_property_readonly_static(
"problems", [](py::object) { return ioh::common::Factory<DynamicBinVal, int, int>::instance().map(); },
"All registered problems")
.def(py::init<int, int>(), py::arg("instance"), py::arg("n_variables"))
.def("step", &DynamicBinVal::step, R"pbdoc(
Step the dynamic binary value problem forward by one timestep, and permute the weights randomly.
Expand All @@ -563,7 +562,7 @@ void define_dynamic_bin_val_problem(py::module &m)
.def(py::init<int, int>(), py::arg("instance"), py::arg("n_variables"));
py::class_<Pareto, DynamicBinVal, std::shared_ptr<Pareto>>(m, "DynamicBinValPareto", doc)
.def(py::init<int, int>(), py::arg("instance"), py::arg("n_variables"))
.def_property_readonly("pareto_shape", &DynamicBinValPareto::get_pareto_shape);
.def_property_readonly("pareto_shape", &Pareto::get_pareto_shape);

py::class_<Ranking, DynamicBinVal, std::shared_ptr<Ranking>>(m, "DynamicBinValRanking", doc)
.def(py::init<int, int>(), py::arg("instance"), py::arg("n_variables"))
Expand Down
72 changes: 39 additions & 33 deletions tests/python/test_dynamic_bin_val.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,63 @@
import os
import ast
import ioh
import unittest


DATA_DIR = os.path.join(
os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "static"
)

class TestDynamicBinVal(unittest.TestCase):

def test_pairs_from_static_file(self):
file = open('./static/dynamic_bin_val.in', 'r')
with open(os.path.join(DATA_DIR, 'dynamic_bin_val.in'), 'r') as file:

for line in file:
data = line.strip().split()
if not data: continue
for line in file:
data = line.strip().split()

if not data: continue

problem_id = int(data[0]) # e.g., 10001
instance = int(data[1]) # e.g., 1
num_timesteps = int(data[2]) # e.g., 34
function_name = data[3] # e.g., 'operator_call'
problem_id = int(data[0]) # e.g., 10001
instance = int(data[1]) # e.g., 1
num_timesteps = int(data[2]) # e.g., 34
function_name = data[3] # e.g., 'operator_call'

if function_name == 'operator_call':
x = [int(char) for char in data[4]] # e.g., 10
target_y = float(data[5]) # e.g., 0.69188
if function_name == 'operator_call':
x = [int(char) for char in data[4]] # e.g., 10
target_y = float(data[5]) # e.g., 0.69188

dynamic_bin_val_landscape = ioh.get_problem(problem_id, instance, len(x), ioh.ProblemClass.INTEGER)
dynamic_bin_val_landscape = ioh.get_problem(problem_id, instance, len(x), ioh.ProblemClass.INTEGER)

for _ in range(num_timesteps):
dynamic_bin_val_landscape.step()
real_y = dynamic_bin_val_landscape(x)
self.assertAlmostEqual(real_y, target_y, places=4)
for _ in range(num_timesteps):
dynamic_bin_val_landscape.step()
real_y = dynamic_bin_val_landscape(x)
self.assertAlmostEqual(real_y, target_y, places=4)

if function_name == 'rank_indices':
input_bitstrings = ast.literal_eval(data[4]) # e.g., [[1,1]]
target_ranked_bitstrings = ast.literal_eval(data[5]) # [[1,1]]
if function_name == 'rank_indices':
input_bitstrings = ast.literal_eval(data[4]) # e.g., [[1,1]]
target_ranked_bitstrings = ast.literal_eval(data[5]) # [[1,1]]

dynamic_bin_val_landscape = ioh.get_problem(problem_id, instance, len(input_bitstrings[0]), ioh.ProblemClass.INTEGER)
dynamic_bin_val_landscape = ioh.get_problem(problem_id, instance, len(input_bitstrings[0]), ioh.ProblemClass.INTEGER)

for _ in range(num_timesteps):
dynamic_bin_val_landscape.step()
for _ in range(num_timesteps):
dynamic_bin_val_landscape.step()

real_ranked_bitstrings = dynamic_bin_val_landscape.rank_indices(input_bitstrings)
self.assertEqual(target_ranked_bitstrings, real_ranked_bitstrings)
real_ranked_bitstrings = dynamic_bin_val_landscape.rank_indices(input_bitstrings)
self.assertEqual(target_ranked_bitstrings, real_ranked_bitstrings)

if function_name == 'rank':
input_bitstrings = ast.literal_eval(data[4]) # [[1,1]]
target_ranked_bitstrings = ast.literal_eval(data[5]) # [0]
if function_name == 'rank':
input_bitstrings = ast.literal_eval(data[4]) # [[1,1]]
target_ranked_bitstrings = ast.literal_eval(data[5]) # [0]

dynamic_bin_val_landscape = ioh.get_problem(problem_id, instance, len(input_bitstrings[0]), ioh.ProblemClass.INTEGER)
dynamic_bin_val_landscape = ioh.get_problem(problem_id, instance, len(input_bitstrings[0]), ioh.ProblemClass.INTEGER)

for _ in range(num_timesteps):
dynamic_bin_val_landscape.step()
for _ in range(num_timesteps):
dynamic_bin_val_landscape.step()

real_ranked_bitstrings = dynamic_bin_val_landscape.rank(input_bitstrings)
self.assertEqual(target_ranked_bitstrings, real_ranked_bitstrings)
real_ranked_bitstrings = dynamic_bin_val_landscape.rank(input_bitstrings)
self.assertEqual(target_ranked_bitstrings, real_ranked_bitstrings)

file.close()

if __name__ == '__main__':
unittest.main()

0 comments on commit 4217a62

Please sign in to comment.