diff --git a/include/ioh/common/container_utils.hpp b/include/ioh/common/container_utils.hpp index 028edcef..beadd0af 100644 --- a/include/ioh/common/container_utils.hpp +++ b/include/ioh/common/container_utils.hpp @@ -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 sorted(const int n, const int seed) + static std::vector sorted(const int n, const unsigned long seed) { const auto random_numbers = random::bbob2009::uniform(n, seed); std::vector permutations(n); diff --git a/include/ioh/common/random.hpp b/include/ioh/common/random.hpp index 003cbc46..1f6a7b8d 100644 --- a/include/ioh/common/random.hpp +++ b/include/ioh/common/random.hpp @@ -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 uniform(const size_t &n, long seed, const double lb = 0, const double ub = 1) + inline std::vector uniform(const size_t &n, unsigned long seed, const double lb = 0, const double ub = 1) { auto rand_vec = std::vector(n); long rand_seed[32] = {}; @@ -245,30 +245,30 @@ namespace ioh::common::random namespace bbob2009 { - inline std::vector uniform(const size_t n, const int initial_seed, const double lb = 0, + inline std::vector uniform(const size_t n, const unsigned long initial_seed, const double lb = 0, const double ub = 1) { - auto generators = std::array(); - auto seed = std::max(1, abs(initial_seed)); + auto generators = std::array(); + auto seed = std::max(initial_seed, {1}); // Initialize the seed and generator for (auto i = 39; i >= 0; i--) { - seed = static_cast(lcg_rand(seed)); + seed = lcg_rand(seed); if (i < 32) generators[i] = seed; } auto x = std::vector(n); - auto random_number = generators.front(); + auto random_number = static_cast(generators.front()); for (size_t i = 0; i < n; i++) { const auto index = static_cast(floor(random_number / 67108865.0)); - seed = static_cast(lcg_rand(seed)); - random_number = generators[index]; + seed = lcg_rand(seed); + random_number = static_cast(generators[index]); generators[index] = seed; x[i] = random_number / 2.147483647e9; @@ -280,7 +280,7 @@ namespace ioh::common::random return x; } - inline std::vector normal(const size_t n, const long seed, const double lb = 0, const double ub = 1) + inline std::vector 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(seed)); diff --git a/include/ioh/logger/eaf.hpp b/include/ioh/logger/eaf.hpp index a8bd6147..72b1260e 100644 --- a/include/ioh/logger/eaf.hpp +++ b/include/ioh/logger/eaf.hpp @@ -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(nb_runs) * (error_max-error_min) * static_cast(evals_max-evals_min)); } /** Computes the absolute "hypervolume" of the whole empirical attainement function. diff --git a/include/ioh/logger/eah.hpp b/include/ioh/logger/eah.hpp index 849d8c75..5400c0e2 100644 --- a/include/ioh/logger/eah.hpp +++ b/include/ioh/logger/eah.hpp @@ -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(this->max())) { return this->size() - 1; } @@ -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(this->max())) { return this->size() - 1; } @@ -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(_range_evals.min()) || static_cast(_range_evals.max()) < evaluations.value() + || err < static_cast(_range_error.min()) || static_cast(_range_error.max()) < err) { // Discard it. // FIXME we should use a more generic debug log system @@ -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(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(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); diff --git a/include/ioh/problem/bbob/bbob_problem.hpp b/include/ioh/problem/bbob/bbob_problem.hpp index 58ccb425..27f43a73 100644 --- a/include/ioh/problem/bbob/bbob_problem.hpp +++ b/include/ioh/problem/bbob/bbob_problem.hpp @@ -16,7 +16,7 @@ namespace ioh::problem struct TransformationState { //! The seed - long seed; + unsigned long seed; //! A vector with exponents std::vector exponents{}; diff --git a/include/ioh/problem/bbob/gallagher101.hpp b/include/ioh/problem/bbob/gallagher101.hpp index c32dfc48..45d1452b 100644 --- a/include/ioh/problem/bbob/gallagher101.hpp +++ b/include/ioh/problem/bbob/gallagher101.hpp @@ -14,7 +14,7 @@ namespace ioh::problem::bbob double value; std::vector 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); @@ -24,7 +24,7 @@ namespace ioh::problem::bbob static_cast(permutations[i].index) / (static_cast(n_variables) - 1.) - 0.5); } - static std::vector get_peaks(const int n, const int n_variables, const int seed, + static std::vector 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.; diff --git a/include/ioh/problem/submodular/max_influence.hpp b/include/ioh/problem/submodular/max_influence.hpp index e6d6e6da..58a74004 100644 --- a/include/ioh/problem/submodular/max_influence.hpp +++ b/include/ioh/problem/submodular/max_influence.hpp @@ -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(x[i]); if (x[i] != 0) visits.push(i); } diff --git a/ioh/src/real.cpp b/ioh/src/real.cpp index 58295cf4..c0448e65 100644 --- a/ioh/src/real.cpp +++ b/ioh/src/real.cpp @@ -541,7 +541,6 @@ void define_dynamic_bin_val_problem(py::module &m) .def_property_readonly_static( "problems", [](py::object) { return ioh::common::Factory::instance().map(); }, "All registered problems") - .def(py::init(), 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. @@ -563,7 +562,7 @@ void define_dynamic_bin_val_problem(py::module &m) .def(py::init(), py::arg("instance"), py::arg("n_variables")); py::class_>(m, "DynamicBinValPareto", doc) .def(py::init(), 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_>(m, "DynamicBinValRanking", doc) .def(py::init(), py::arg("instance"), py::arg("n_variables")) diff --git a/tests/python/test_dynamic_bin_val.py b/tests/python/test_dynamic_bin_val.py index cf00cc37..a8723ecd 100644 --- a/tests/python/test_dynamic_bin_val.py +++ b/tests/python/test_dynamic_bin_val.py @@ -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()