Skip to content

Commit

Permalink
Add test that seed fully determines evolution process
Browse files Browse the repository at this point in the history
  • Loading branch information
donRumata03 committed Apr 16, 2024
1 parent e8035ac commit 27d96d7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
9 changes: 8 additions & 1 deletion golem/utilities/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
from joblib import cpu_count

from golem.utilities import random
import random
from golem.utilities.random import RandomStateHandler


Expand All @@ -22,6 +22,13 @@ def determine_n_jobs(n_jobs=-1, logger=None):
return n_jobs


def urandom_mock(n):
# os.random is the source of random used in the uuid library
# normally, it's „true“ random, but to stabilize tests,
# seeded `random` library is used instead.
return bytes(random.getrandbits(8) for _ in range(n))


def set_random_seed(seed: Optional[int]):
""" Sets random seed for evaluation of models. """
if seed is not None:
Expand Down
7 changes: 1 addition & 6 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@

import pytest

from golem.utilities.utilities import urandom_mock
from golem.utilities.utils import set_random_seed


@pytest.fixture(autouse=True)
def stabilize_random():
set_random_seed(42)

def urandom_mock(n):
return bytes(random.getrandbits(8) for _ in range(n))

# os.random is the source of random used in the uuid library
# normally, it's „true“ random, but to stabilize tests,
# it uses seeded `random` library
with patch('os.urandom', urandom_mock):
yield
39 changes: 39 additions & 0 deletions test/unit/utilities/random_seeds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from unittest.mock import patch

from examples.synthetic_graph_evolution.generators import generate_labeled_graph
from examples.synthetic_graph_evolution.graph_search import graph_search_setup
from golem.utilities.utilities import set_random_seed, urandom_mock
from test.integration.test_quality_improvement import run_graph_trial


def test_random_seed_fully_determines_evolution_process():
""" Tests that random seed fully determines evolution process. """
# Setup graph search
target_graph = generate_labeled_graph('gnp', 5, node_labels=['X', 'Y'])

def launch_with_seed(seed):
set_random_seed(seed)
optimizer, objective = graph_search_setup(
target_graph=target_graph,
num_iterations=3,
node_types=['X', 'Y'],
pop_size=3
)
optimizer.optimise(objective)
return optimizer.history

def history_equals(history1, history2):
return all(
(history1.generations[i] == history2.generations[i])
for i in range(len(history1.generations))
)

first_seed = 42
second_seed = first_seed + 1

first_seed_first_run = launch_with_seed(first_seed)
first_seed_second_run = launch_with_seed(first_seed)
second_seed_run = launch_with_seed(second_seed)

assert history_equals(first_seed_first_run, first_seed_second_run)
assert not history_equals(first_seed_first_run, second_seed_run)

0 comments on commit 27d96d7

Please sign in to comment.