From c6c20fb4337bb2e594264afb7cf521be2acb8310 Mon Sep 17 00:00:00 2001 From: Dimitri Rusin Date: Wed, 8 May 2024 13:21:57 +0200 Subject: [PATCH] Code to generate test cases for DynamicBinVal into a separate filepath. --- tests/CMakeLists.txt | 5 ++ .../problem/generate_test_dynamic_bin_val.cpp | 84 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 tests/cpp/problem/generate_test_dynamic_bin_val.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 02e019648..795b3010d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -45,3 +45,8 @@ foreach(test_source ${SOURCES}) get_filename_component(test_fname "${test_source}" NAME_WE) register_test(${test_fname} "${test_source}") endforeach() + +# Define new executable for generating test cases +add_executable(generate_test_cases ${CMAKE_CURRENT_SOURCE_DIR}/cpp/problem/generate_test_dynamic_bin_val.cpp) +target_include_directories(generate_test_cases PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/cpp) +target_link_libraries(generate_test_cases PRIVATE ${PROJECT_NAME} gtest) diff --git a/tests/cpp/problem/generate_test_dynamic_bin_val.cpp b/tests/cpp/problem/generate_test_dynamic_bin_val.cpp new file mode 100644 index 000000000..e0abe532e --- /dev/null +++ b/tests/cpp/problem/generate_test_dynamic_bin_val.cpp @@ -0,0 +1,84 @@ +#include "../utils.hpp" +#include "ioh/problem/dynamic_bin_val.hpp" + +#include +#include +#include +#include +#include +#include +#include + +int main(int, char**) +{ + std::vector problem_ids = {10'001, 10'002, 10'003, 10'004}; + std::vector instances = {1, 2, 55, 667}; + std::vector n_variables = {2, 3, 50}; + std::vector numbers_of_timesteps = {0, 7, 22, 34}; + std::vector sequence_of_num_of_ranked_bitstrings = {1, 10}; + const int repetitions = 3; + const int generator_seed = 42; + std::default_random_engine generator(generator_seed); + + const auto static_root = ioh::common::file::utils::get_static_root(); + const auto file_path = static_root / "generated_dynamic_bin_val.in"; + + // Open the file for writing + std::ofstream outfile(file_path); + + for (int problem_id : problem_ids) { + for (int instance : instances) { + for (int n_variables : n_variables) { + for (int number_of_timesteps : numbers_of_timesteps) { + for (int rep = 0; rep < repetitions; ++rep) { + + const auto &problem_factory = ioh::problem::ProblemRegistry::instance(); + auto landscape = problem_factory.create(problem_id, instance, n_variables); + + std::vector x(n_variables); + std::uniform_int_distribution distribution(0, 1); + std::generate(x.begin(), x.end(), [&]() { return distribution(generator); }); + + for (int i = 0; i < number_of_timesteps; ++i) { + landscape->step(); + } + + double y = (*landscape)(x); + + std::string x_str; + for (int bit : x) { + x_str += std::to_string(bit); + } + + // Output results directly during test execution + outfile << std::fixed << std::setprecision(5); + outfile << landscape->meta_data().problem_id << " " << instance << " " << number_of_timesteps + << " operator_call " << x_str << " " << y << std::endl; + + if (landscape->meta_data().problem_id == 10'004) { + for (int num_ranked_bitstrings : sequence_of_num_of_ranked_bitstrings) { + std::vector> input_bitstrings; + for (int i = 0; i < num_ranked_bitstrings; ++i) { + std::vector input_bitstring(n_variables); + std::generate(input_bitstring.begin(), input_bitstring.end(), [&]() { return distribution(generator); }); + input_bitstrings.push_back(input_bitstring); + } + + std::vector> ranked_bitstrings = std::dynamic_pointer_cast(landscape)->rank(input_bitstrings); + std::vector ranking_indices = std::dynamic_pointer_cast(landscape)->rank_indices(input_bitstrings); + + outfile << problem_id << " " << instance << " " << number_of_timesteps << " rank " + << format_vector_of_vectors(input_bitstrings) << " " << format_vector_of_vectors(ranked_bitstrings) << std::endl; + + outfile << problem_id << " " << instance << " " << number_of_timesteps << " rank_indices " + << format_vector_of_vectors(input_bitstrings) << " " << vector_to_string(ranking_indices) << std::endl; + } + } + } + } + } + } + } + + outfile.close(); +}