Skip to content

Commit

Permalink
Make PreferentialGPSampler the default for preferential optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
not522 committed Oct 5, 2023
1 parent 09fa32b commit a5e102d
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions optuna_dashboard/preferential/_study.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

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
Expand All @@ -20,6 +20,10 @@
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"

Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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.")
Expand Down

0 comments on commit a5e102d

Please sign in to comment.