Make PreferentialGPSampler the default for preferential optimization

This commit is contained in:
Naoto Mizuno
2023-10-05 17:17:15 +09:00
parent 09fa32b914
commit a5e102d9fa
+23 -15
View File
@@ -7,9 +7,9 @@ from typing import Iterable
import optuna
from optuna import logging
from optuna._imports import try_import
from optuna.distributions import BaseDistribution
from optuna.samplers import BaseSampler
from optuna.samplers import RandomSampler
from optuna.trial import FrozenTrial
from optuna.trial import TrialState
from optuna_dashboard.preferential._system_attrs import get_n_generate
@@ -20,6 +20,10 @@ from optuna_dashboard.preferential._system_attrs import report_preferences
from optuna_dashboard.preferential._system_attrs import set_n_generate
with try_import() as _imports:
from optuna_dashboard.preferential.samplers.gp import PreferentialGPSampler
_logger = logging.get_logger(__name__)
_SYSTEM_ATTR_PREFERENTIAL_STUDY = "preference:is_preferential"
@@ -340,11 +344,10 @@ def create_study(
sampler:
A sampler object that implements background algorithm for value suggestion.
If :obj:`None` is specified, `RandomSampler`_ is used. Please note that
most Optuna samplers does not work efficiently for preferential optimization.
.. _RandomSampler: https://optuna.readthedocs.io/en/stable/reference/\
samplers/generated/optuna.samplers.RandomSampler.html
If :obj:`None` is specified,
:class:`~optuna_dashboard.preferential.samplers.gp.PreferentialGPSampler` is used.
Please note that most Optuna samplers does not work efficiently for preferential
optimization.
study_name:
Study's name. If this argument is set to None, a unique name is generated
@@ -361,9 +364,13 @@ def create_study(
A :class:`~optuna_dashboard.preferential.PreferentialStudy` object.
"""
try:
if sampler is None:
_imports.check() # If BoTorch is not installed, raise ImportError.
sampler = PreferentialGPSampler()
study = optuna.create_study(
storage=storage,
sampler=sampler or RandomSampler(),
sampler=sampler,
study_name=study_name,
)
study._storage.set_study_system_attr(
@@ -433,18 +440,19 @@ def load_study(
:func:`~optuna.study.create_study` for further details.
sampler:
A sampler object that implements background algorithm for value suggestion.
If :obj:`None` is specified, `RandomSampler`_ is used. Please note that
most Optuna samplers does not work efficiently for preferential optimization.
.. _RandomSampler: https://optuna.readthedocs.io/en/stable/reference/samplers/\
generated/optuna.samplers.RandomSampler.html
If :obj:`None` is specified,
:class:`~optuna_dashboard.preferential.samplers.gp.PreferentialGPSampler` is used.
Please note that most Optuna samplers does not work efficiently for preferential
optimization.
Returns:
A :class:`~optuna_dashboard.preferential.PreferentialStudy` object.
"""
study = optuna.load_study(
study_name=study_name, storage=storage, sampler=sampler or RandomSampler()
)
if sampler is None:
_imports.check() # If BoTorch is not installed, raise ImportError.
sampler = PreferentialGPSampler()
study = optuna.load_study(study_name=study_name, storage=storage, sampler=sampler)
system_attrs = study._storage.get_study_system_attrs(study._study_id)
if not system_attrs.get(_SYSTEM_ATTR_PREFERENTIAL_STUDY):
raise ValueError("The study is not a PreferentialStudy.")