Exemple #1
0
def test_suggested_value():
    # type: () -> None

    independent_sampler = FirstTrialOnlyRandomSampler()
    sampler = optuna.integration.SkoptSampler(
        independent_sampler=independent_sampler,
        skopt_kwargs={'n_initial_points': 5})

    # direction='minimize'
    study = optuna.create_study(sampler=sampler, direction='minimize')
    study.optimize(_objective, n_trials=10, catch=())
    for trial in study.trials:
        for param_name, param_value in trial.params_in_internal_repr.items():
            assert trial.distributions[param_name]._contains(param_value)

    # direction='maximize'
    study = optuna.create_study(sampler=sampler, direction='maximize')
    study.optimize(_objective, n_trials=10, catch=())
    for trial in study.trials:
        for param_name, param_value in trial.params_in_internal_repr.items():
            assert trial.distributions[param_name]._contains(param_value)
Exemple #2
0
from optuna.integration import SkoptSampler
from optuna.testing.sampler import FirstTrialOnlyRandomSampler

if optuna.type_checking.TYPE_CHECKING:
    from typing import Callable  # NOQA

    from optuna.samplers import BaseSampler  # NOQA

parametrize_sampler = pytest.mark.parametrize('sampler_class', [
    optuna.integration.SkoptSampler,
    optuna.integration.CmaEsSampler,
])


@pytest.mark.parametrize('sampler_class', [
    lambda: SkoptSampler(independent_sampler=FirstTrialOnlyRandomSampler(),
                         skopt_kwargs={'n_initial_points': 5}),
    lambda: CmaEsSampler(independent_sampler=FirstTrialOnlyRandomSampler()),
])
def test_suggested_value(sampler_class):
    # type: (Callable[[], BaseSampler]) -> None

    sampler = sampler_class()
    # direction='minimize'
    study = optuna.create_study(sampler=sampler, direction='minimize')
    study.optimize(_objective, n_trials=10, catch=())
    for trial in study.trials:
        for param_name, param_value in trial.params.items():
            distribution = trial.distributions[param_name]
            param_value_in_internal_repr = distribution.to_internal_repr(
                param_value)
Exemple #3
0
import optuna
from optuna.integration import PyCmaSampler
from optuna.integration import SkoptSampler
from optuna.samplers import BaseSampler
from optuna.testing.sampler import FirstTrialOnlyRandomSampler

parametrize_sampler = pytest.mark.parametrize(
    "sampler_class",
    [optuna.integration.SkoptSampler, optuna.integration.PyCmaSampler])


@pytest.mark.parametrize(
    "sampler_class",
    [
        lambda: SkoptSampler(independent_sampler=FirstTrialOnlyRandomSampler()
                             ),
        lambda: PyCmaSampler(independent_sampler=FirstTrialOnlyRandomSampler()
                             ),
    ],
)
def test_suggested_value(sampler_class: Callable[[], BaseSampler]) -> None:

    sampler = sampler_class()
    # direction='minimize'
    study = optuna.create_study(sampler=sampler, direction="minimize")
    study.optimize(_objective, n_trials=10, catch=())
    for trial in study.trials:
        for param_name, param_value in trial.params.items():
            distribution = trial.distributions[param_name]
            param_value_in_internal_repr = distribution.to_internal_repr(
Exemple #4
0
from optuna.testing.sampler import FirstTrialOnlyRandomSampler

if optuna.types.TYPE_CHECKING:
    from typing import Callable  # NOQA

    from optuna.samplers import BaseSampler  # NOQA

parametrize_sampler = pytest.mark.parametrize('sampler_class', [
    optuna.integration.SkoptSampler,
    optuna.integration.CmaEsSampler,
])


@pytest.mark.parametrize('sampler_class', [
    lambda: SkoptSampler(
        independent_sampler=FirstTrialOnlyRandomSampler(), skopt_kwargs={'n_initial_points': 5}),
    lambda: CmaEsSampler(independent_sampler=FirstTrialOnlyRandomSampler()),
])
def test_suggested_value(sampler_class):
    # type: (Callable[[], BaseSampler]) -> None

    sampler = sampler_class()
    # direction='minimize'
    study = optuna.create_study(sampler=sampler, direction='minimize')
    study.optimize(_objective, n_trials=10, catch=())
    for trial in study.trials:
        for param_name, param_value in trial.params.items():
            distribution = trial.distributions[param_name]
            param_value_in_internal_repr = distribution.to_internal_repr(param_value)
            assert distribution._contains(param_value_in_internal_repr)