-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dimitri Rusin
committed
May 9, 2024
1 parent
72ef119
commit aa0585e
Showing
1 changed file
with
48 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,57 @@ | ||
import unittest | ||
from unittest.mock import patch | ||
import ast | ||
import ioh | ||
import unittest | ||
|
||
class TestDynamicBinVal(unittest.TestCase): | ||
|
||
@patch('ioh.get_problem') | ||
def test_dynamic_bin_val_uniform(self, mock_get_problem): | ||
mock_get_problem.return_value = mock_problem = MagicMock() | ||
def test_pairs_from_static_file(self): | ||
file = open('./static/dynamic_bin_val.in', 'r') | ||
|
||
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' | ||
|
||
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) | ||
|
||
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]] | ||
|
||
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() | ||
|
||
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] | ||
|
||
dynamic_bin_val_landscape = ioh.get_problem(problem_id, instance, len(input_bitstrings[0]), ioh.ProblemClass.INTEGER) | ||
|
||
f1 = mock_problem | ||
x1 = [0, 1, 1, 1, 0] | ||
expected_calls = [([0, 1, 1, 1, 0],), ([0, 1, 1, 1, 0],), ([0, 1, 1, 1, 0],), ([0, 1, 1, 1, 0],), ([0, 1, 1, 1, 0],)] | ||
for _ in range(num_timesteps): | ||
dynamic_bin_val_landscape.step() | ||
|
||
# Test instance 1 | ||
with patch('builtins.print') as mock_print: | ||
self.assertEqual(f1(x1), mock_print.call_args[0][0]) | ||
f1.step() | ||
self.assertEqual(f1(x1), mock_print.call_args[0][0]) | ||
f1.step() | ||
self.assertEqual(f1(x1), mock_print.call_args[0][0]) | ||
self.assertEqual(f1(x1), mock_print.call_args[0][0]) | ||
f1.step() | ||
self.assertEqual(f1(x1), mock_print.call_args[0][0]) | ||
real_ranked_bitstrings = dynamic_bin_val_landscape.rank(input_bitstrings) | ||
self.assertEqual(target_ranked_bitstrings, real_ranked_bitstrings) | ||
|
||
mock_get_problem.assert_called_with(10001, 1, 5, ioh.ProblemClass.INTEGER) | ||
mock_problem.assert_called_with(*expected_calls) | ||
file.close() | ||
|
||
if __name__ == '__main__': | ||
unittest.main() | ||
unittest.main() |