Skip to content

Commit

Permalink
Add test for seeding algos (#1066)
Browse files Browse the repository at this point in the history
* Add test for seeding algos

* Adjust to algorithms and improve typing

---------

Co-authored-by: Setepenre <[email protected]>
  • Loading branch information
bouthilx and Delaunay authored Aug 11, 2023
1 parent eabc4f7 commit bfcde6c
Showing 1 changed file with 51 additions and 5 deletions.
56 changes: 51 additions & 5 deletions tests/functional/algos/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import copy
import os
import random
from pathlib import Path

import numpy
Expand Down Expand Up @@ -153,16 +152,26 @@ def xfail_if_not_installed(value: dict, import_optional: ImportOptional):


class CustomRosenbrock(BenchmarkTask):
def __init__(self, max_trials: int = 30, with_fidelity: bool = False):
def __init__(
self, max_trials: int = 30, with_fidelity: bool = False, seed: int | None = None
):
super().__init__(max_trials)
self.seed = seed
self.with_fidelity = with_fidelity

self.rng: numpy.random.RandomState
self.initialize()

def initialize(self):
self.rng = numpy.random.RandomState(self.seed)

def call(self, x: float, noise: float | None = None) -> list[dict]:
"""Evaluate partial information of a quadratic."""
z = x - 34.56789
if noise is not None:
noise = (1 - noise / 10) + 0.0001
z *= random.gauss(0, noise)
z *= self.rng.normal(0, noise)

y = 4 * z**2 + 23.4
dy_dx = 8 * z
return [
Expand Down Expand Up @@ -199,8 +208,8 @@ def call(self, x: float | numpy.ndarray, noise: float | None = None) -> list[dic
return super().call(x_0, noise=noise)


rosenbrock = CustomRosenbrock(max_trials=30, with_fidelity=False)
rosenbrock_with_fidelity = CustomRosenbrock(max_trials=30, with_fidelity=True)
rosenbrock = CustomRosenbrock(max_trials=30, with_fidelity=False, seed=123)
rosenbrock_with_fidelity = CustomRosenbrock(max_trials=30, with_fidelity=True, seed=123)

space = rosenbrock.get_search_space()
space_with_fidelity = rosenbrock_with_fidelity.get_search_space()
Expand Down Expand Up @@ -354,6 +363,7 @@ def test_with_nested_spaces(algorithm: dict):
)
def test_with_fidelity(algorithm: dict):
"""Test a scenario with fidelity."""
rosenbrock_with_fidelity.initialize()
exp = workon(
rosenbrock_with_fidelity,
space_with_fidelity,
Expand Down Expand Up @@ -383,6 +393,41 @@ def test_with_fidelity(algorithm: dict):
assert param.type == "real"


@pytest.mark.parametrize(
"algorithm",
algorithm_configs.values(),
ids=list(algorithm_configs.keys()),
)
def test_seeding(algorithm: dict):
"""Test seeding works properly."""

rosenbrock_with_fidelity.initialize()
exp = workon(
rosenbrock_with_fidelity,
space_with_fidelity,
algorithm=algorithm,
max_trials=30,
)

assert exp.configuration["algorithm"] == algorithm

trials = exp.fetch_trials()

rosenbrock_with_fidelity.initialize()
rep_exp = workon(
rosenbrock_with_fidelity,
space_with_fidelity,
algorithm=algorithm,
max_trials=30,
)

assert rep_exp.configuration["algorithm"] == algorithm

rep_trials = rep_exp.fetch_trials()

assert trials == rep_trials


@pytest.mark.parametrize(
"algorithm", algorithm_configs.values(), ids=list(algorithm_configs.keys())
)
Expand Down Expand Up @@ -536,6 +581,7 @@ def test_branching_algos(
storage=storage,
)

rosenbrock_with_fidelity.initialize()
exp.workon(branching_rosenbrock, n_workers=2, trial_arg="trial")

def build_params_hist(trial: Trial) -> list[str]:
Expand Down

0 comments on commit bfcde6c

Please sign in to comment.