Skip to content

Commit

Permalink
Add GridSampler test for failed trials (optuna#4721)
Browse files Browse the repository at this point in the history
  • Loading branch information
not522 committed Jun 13, 2023
1 parent 290d6a2 commit 703bb5a
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tests/samplers_tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from optuna import samplers
from optuna.samplers._grid import GridValueType
from optuna.storages import RetryFailedTrialCallback
from optuna.testing.objectives import fail_objective
from optuna.testing.objectives import pruned_objective
from optuna.testing.storages import StorageSupplier
from optuna.trial import Trial
Expand Down Expand Up @@ -94,6 +95,18 @@ def test_study_optimize_with_pruning() -> None:
assert len(study.trials) == 2


def test_study_optimize_with_fail() -> None:
def objective(trial: Trial) -> float:
return trial.suggest_int("a", 0, 100)

# Failed trials should count towards grid consumption.
search_space: Dict[str, List[GridValueType]] = {"a": [0, 50]}
study = optuna.create_study(sampler=samplers.GridSampler(search_space))
study.optimize(fail_objective, n_trials=1, catch=ValueError)
study.optimize(objective, n_trials=None)
assert len(study.trials) == 2


def test_study_optimize_with_numpy_related_search_space() -> None:
def objective(trial: Trial) -> float:
a = trial.suggest_float("a", 0, 10)
Expand Down

0 comments on commit 703bb5a

Please sign in to comment.