Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support constant_liar in multi-objective TPESampler #5021

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 1 addition & 5 deletions optuna/samplers/_tpe/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,6 @@ def objective(trial):
and the durations of the running states are significant, and/or the number of
workers is high.

.. note::
This feature can be used for only single-objective optimization; this argument is
ignored for multi-objective optimization.

.. note::
Added in v2.8.0 as an experimental feature. The interface may change in newer
versions without prior notice. See
Expand Down Expand Up @@ -437,7 +433,7 @@ def _get_internal_repr(
def _sample(
self, study: Study, trial: FrozenTrial, search_space: Dict[str, BaseDistribution]
) -> Dict[str, Any]:
if self._constant_liar and not study._is_multi_objective():
if self._constant_liar:
states = [TrialState.COMPLETE, TrialState.PRUNED, TrialState.RUNNING]
else:
states = [TrialState.COMPLETE, TrialState.PRUNED]
Expand Down
65 changes: 62 additions & 3 deletions tests/samplers_tests/tpe_tests/test_sampler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import random
from typing import Callable
from typing import Dict
Expand Down Expand Up @@ -817,6 +819,62 @@ def test_split_trials(direction: str, constant_liar: bool, constraints: bool) ->
assert above_trial_numbers == list(range(n_below, len(trials)))


@pytest.mark.parametrize(
"directions", [["minimize", "minimize"], ["maximize", "maximize"], ["minimize", "maximize"]]
)
def test_split_trials_for_multiobjective_constant_liar(directions: list[str]) -> None:
study = optuna.create_study(directions=directions)
for obj1 in [-float("inf"), 0, 1, float("inf")]:
val1 = obj1 if directions[0] == "minimize" else -obj1
for obj2 in [-float("inf"), 0, 1, float("inf")]:
val2 = obj2 if directions[1] == "minimize" else -obj2
study.add_trial(
optuna.create_trial(
state=optuna.trial.TrialState.COMPLETE,
values=[val1, val2],
params={"x": 0},
distributions={"x": optuna.distributions.FloatDistribution(-1.0, 1.0)},
)
)

for _ in range(5):
study.add_trial(
optuna.create_trial(
state=optuna.trial.TrialState.RUNNING,
params={"x": 0},
distributions={"x": optuna.distributions.FloatDistribution(-1.0, 1.0)},
)
)

study.add_trial(
optuna.create_trial(
state=optuna.trial.TrialState.FAIL,
)
)

study.add_trial(
optuna.create_trial(
state=optuna.trial.TrialState.WAITING,
)
)

states = [optuna.trial.TrialState.COMPLETE, optuna.trial.TrialState.RUNNING]
trials = study.get_trials(states=states)
finished_trials = study.get_trials(states=(optuna.trial.TrialState.COMPLETE,))
ground_truth = [0, 1, 4, 2, 8, 5, 3, 6, 9, 12, 7, 10, 13, 11, 14, 15, 16, 17, 18, 19, 20]
for n_below in range(1, len(finished_trials) + 1):
below_trials, above_trials = _tpe.sampler._split_trials(
study,
trials,
n_below,
constraints_enabled=False,
)
below_trial_numbers = [trial.number for trial in below_trials]
assert below_trial_numbers == np.sort(ground_truth[:n_below]).tolist()
above_trial_numbers = [trial.number for trial in above_trials]
assert above_trial_numbers == np.sort(ground_truth[n_below:]).tolist()


@pytest.mark.parametrize("direction", ["minimize", "maximize"])
def test_split_complete_trials_single_objective(direction: str) -> None:
study = optuna.create_study(direction=direction)
Expand Down Expand Up @@ -1052,7 +1110,8 @@ def test_constant_liar_experimental_warning() -> None:


@pytest.mark.parametrize("multivariate", [True, False])
def test_constant_liar_with_running_trial(multivariate: bool) -> None:
@pytest.mark.parametrize("multiobjective", [True, False])
def test_constant_liar_with_running_trial(multivariate: bool, multiobjective: bool) -> None:
with warnings.catch_warnings():
warnings.simplefilter("ignore", optuna.exceptions.ExperimentalWarning)
sampler = TPESampler(multivariate=multivariate, constant_liar=True, n_startup_trials=0)
Expand All @@ -1064,7 +1123,7 @@ def test_constant_liar_with_running_trial(multivariate: bool) -> None:
trial0.suggest_int("x", 0, 10)
trial0.suggest_float("y", 0, 10)
trial0.suggest_categorical("z", [0, 1, 2])
study.tell(trial0, 0)
study.tell(trial0, [0, 0] if multiobjective else 0)

# Add running trials.
trial1 = study.ask()
Expand All @@ -1079,7 +1138,7 @@ def test_constant_liar_with_running_trial(multivariate: bool) -> None:
trial.suggest_int("x", 0, 10)
trial.suggest_float("y", 0, 10)
trial.suggest_categorical("z", [0, 1, 2])
study.tell(trial, 0)
study.tell(trial, [0, 0] if multiobjective else 0)


def test_categorical_distance_func_experimental_warning() -> None:
Expand Down