Skip to content

Commit

Permalink
Code to generate test cases for DynamicBinVal into a separate filepath.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimitri Rusin committed May 8, 2024
1 parent 01458ca commit c6c20fb
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
84 changes: 84 additions & 0 deletions tests/cpp/problem/generate_test_dynamic_bin_val.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "../utils.hpp"
#include "ioh/problem/dynamic_bin_val.hpp"

#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <random>
#include <sstream>
#include <vector>

int main(int, char**)
{
std::vector<int> problem_ids = {10'001, 10'002, 10'003, 10'004};
std::vector<int> instances = {1, 2, 55, 667};
std::vector<int> n_variables = {2, 3, 50};
std::vector<int> numbers_of_timesteps = {0, 7, 22, 34};
std::vector<int> 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<ioh::problem::DynamicBinVal>::instance();
auto landscape = problem_factory.create(problem_id, instance, n_variables);

std::vector<int> x(n_variables);
std::uniform_int_distribution<int> 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<std::vector<int>> input_bitstrings;
for (int i = 0; i < num_ranked_bitstrings; ++i) {
std::vector<int> input_bitstring(n_variables);
std::generate(input_bitstring.begin(), input_bitstring.end(), [&]() { return distribution(generator); });
input_bitstrings.push_back(input_bitstring);
}

std::vector<std::vector<int>> ranked_bitstrings = std::dynamic_pointer_cast<ioh::problem::DynamicBinValRanking>(landscape)->rank(input_bitstrings);
std::vector<int> ranking_indices = std::dynamic_pointer_cast<ioh::problem::DynamicBinValRanking>(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();
}

0 comments on commit c6c20fb

Please sign in to comment.