From 00f8a26a0bd9724354d51349b08195cd22749bb0 Mon Sep 17 00:00:00 2001 From: Oskar Taubert Date: Wed, 8 May 2024 02:01:19 +0200 Subject: [PATCH] added tests for ordering and surrogate key in individuals --- tests/test_population.py | 69 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/tests/test_population.py b/tests/test_population.py index c6e9bf75..e0e4b5a7 100644 --- a/tests/test_population.py +++ b/tests/test_population.py @@ -27,11 +27,76 @@ def test_individual(): assert len(ind) == 6 assert ind.position.shape[0] == 12 - for key in ind: - print(ind[key]) assert ind["cat1"] == "e" assert ind["cat2"] == "f" ind["cat1"] = "b" assert ind.position[5] == 1.0 + assert ind.position[9] == 1.0 assert list(ind.keys()) == list(ind_map.keys()) + + +@pytest.mark.mpi_skip +def test_individual_order(): + """Test the mapping between dictionary and embedded vector representation of Individuals at different ordering.""" + limits = { + "float1": (0.0, 1.0), + "cat1": ("a", "b", "c", "d", "e"), + "float2": (-1.0, 1.0), + "int1": (0, 5), + "cat2": ("f", "g", "h"), + "int2": (1, 8), + } + ind_map = { + "cat1": "e", + "cat2": "f", + "float1": 0.1, + "float2": 0.2, + "int1": 3, + "int2": 4, + } + + ind = Individual(ind_map, limits) + assert len(ind) == 6 + assert ind.position.shape[0] == 12 + + assert ind["cat1"] == "e" + assert ind["cat2"] == "f" + + ind["cat1"] = "b" + assert ind.position[1] == 0.0 + assert ind.position[2] == 1.0 + assert ind.position[8] == 1.0 + assert ind.position[9] == 0.0 + + +@pytest.mark.mpi_skip +def test_special_key(): + """Test that using _ prefixed keys does not trigger the categorical embedding correctly.""" + limits = { + "float1": (0.0, 1.0), + "float2": (-1.0, 1.0), + "int1": (0, 5), + "int2": (1, 8), + "cat1": ("a", "b", "c", "d", "e"), + "cat2": ("f", "g", "h"), + } + ind_map = { + "float1": 0.1, + "float2": 0.2, + "int1": 3, + "int2": 4, + "cat1": "e", + "cat2": "f", + } + + ind = Individual(ind_map, limits) + assert len(ind) == 6 + assert ind.position.shape[0] == 12 + + assert ind["cat1"] == "e" + assert ind["cat2"] == "f" + + ind["cat1"] = "b" + ind["_s"] = [1, 0.2, 3] + assert ind.position[5] == 1.0