Esempio n. 1
0
def test_check_distribution_suggest_float(
        storage_init_func: Callable[[], storages.BaseStorage]) -> None:

    sampler = samplers.RandomSampler()
    study = create_study(storage_init_func(), sampler=sampler)
    trial = Trial(study, study._storage.create_new_trial(study._study_id))

    x1 = trial.suggest_float("x1", 10, 20)
    x2 = trial.suggest_uniform("x1", 10, 20)

    assert x1 == x2

    x3 = trial.suggest_float("x2", 1e-5, 1e-3, log=True)
    x4 = trial.suggest_loguniform("x2", 1e-5, 1e-3)

    assert x3 == x4

    x5 = trial.suggest_float("x3", 10, 20, step=1.0)
    x6 = trial.suggest_discrete_uniform("x3", 10, 20, 1.0)

    assert x5 == x6
    with pytest.raises(ValueError):
        trial.suggest_float("x4", 1e-5, 1e-2, step=1e-5, log=True)

    with pytest.raises(ValueError):
        trial.suggest_int("x1", 10, 20)

    trial = Trial(study, study._storage.create_new_trial(study._study_id))
    with pytest.raises(ValueError):
        trial.suggest_int("x1", 10, 20)
Esempio n. 2
0
def test_suggest_low_equals_high(storage_init_func):
    # type: (Callable[[], storages.BaseStorage]) -> None

    study = create_study(storage_init_func(), sampler=samplers.TPESampler(n_startup_trials=0))
    trial = Trial(study, study._storage.create_new_trial(study._study_id))

    # Parameter values are determined without suggestion when low == high.
    with patch.object(trial, "_suggest", wraps=trial._suggest) as mock_object:
        assert trial.suggest_uniform("a", 1.0, 1.0) == 1.0  # Suggesting a param.
        assert trial.suggest_uniform("a", 1.0, 1.0) == 1.0  # Suggesting the same param.
        assert mock_object.call_count == 0
        assert trial.suggest_loguniform("b", 1.0, 1.0) == 1.0  # Suggesting a param.
        assert trial.suggest_loguniform("b", 1.0, 1.0) == 1.0  # Suggesting the same param.
        assert mock_object.call_count == 0
        assert trial.suggest_discrete_uniform("c", 1.0, 1.0, 1.0) == 1.0  # Suggesting a param.
        assert (
            trial.suggest_discrete_uniform("c", 1.0, 1.0, 1.0) == 1.0
        )  # Suggesting the same param.
        assert mock_object.call_count == 0
        assert trial.suggest_int("d", 1, 1) == 1  # Suggesting a param.
        assert trial.suggest_int("d", 1, 1) == 1  # Suggesting the same param.
        assert mock_object.call_count == 0
        assert trial.suggest_float("e", 1.0, 1.0) == 1.0  # Suggesting a param.
        assert trial.suggest_float("e", 1.0, 1.0) == 1.0  # Suggesting the same param.
        assert mock_object.call_count == 0
        assert trial.suggest_float("f", 0.5, 0.5, log=True) == 0.5  # Suggesting a param.
        assert trial.suggest_float("f", 0.5, 0.5, log=True) == 0.5  # Suggesting the same param.
        assert mock_object.call_count == 0
        assert trial.suggest_float("g", 0.5, 0.5, log=False) == 0.5  # Suggesting a param.
        assert trial.suggest_float("g", 0.5, 0.5, log=False) == 0.5  # Suggesting the same param.
        assert mock_object.call_count == 0
Esempio n. 3
0
 def objective(trial: Trial) -> float:
     ret: float = trial.suggest_int("x1", 0, 10)
     ret += trial.suggest_int("x2", 1, 10, log=True)
     ret += trial.suggest_float("x3", 0, 10)
     ret += trial.suggest_float("x4", 1, 10, log=True)
     ret += trial.suggest_float("x5", 1, 10, step=3)
     _ = trial.suggest_categorical("x6", [1, 4, 7, 10])
     return ret
Esempio n. 4
0
 def obj(t: Trial) -> float:
     t.suggest_float("a", 1.0, 100.0)
     t.suggest_float("b", 1.0, 100.0, log=True)
     t.suggest_float("c", 1.0, 100.0, step=3.0)
     t.suggest_int("d", 1, 100)
     t.suggest_int("e", 0, 100, step=2)
     t.suggest_int("f", 1, 100, log=True)
     t.suggest_categorical("g", ["x", "y", "z"])
     return 0.0
Esempio n. 5
0
 def objective(trial: Trial) -> float:
     x0 = trial.suggest_float("x0", 0, 1)
     x1 = trial.suggest_float("x1", 0.1, 1, log=True)
     x2 = trial.suggest_float("x2", 0, 1, step=0.1)
     x3 = trial.suggest_int("x3", 0, 2)
     x4 = trial.suggest_int("x4", 2, 4, log=True)
     x5 = trial.suggest_int("x5", 0, 4, step=2)
     x6 = cast(float, trial.suggest_categorical("x6", [0.1, 0.2, 0.3]))
     return x0 + x1 + x2 + x3 + x4 + x5 + x6
Esempio n. 6
0
def objective_func_branched_search_space(trial: Trial) -> float:

    c = trial.suggest_categorical("c", ("A", "B"))
    if c == "A":
        x = trial.suggest_float("x", -10, 10)
        return (x + 5) ** 2
    else:
        y = trial.suggest_float("y", -10, 10)
        return (y + 5) ** 2
Esempio n. 7
0
 def objective(trial: Trial) -> float:
     a = trial.suggest_float("a", 1, 9)
     b = trial.suggest_float("b", 1, 9, log=True)
     c = trial.suggest_float("c", 1, 9, step=1)
     d = trial.suggest_int("d", 1, 9)
     e = trial.suggest_int("e", 1, 9, log=True)
     f = trial.suggest_int("f", 1, 9, step=2)
     g = cast(int, trial.suggest_categorical("g", range(1, 10)))
     return a + b + c + d + e + f + g
Esempio n. 8
0
def configure(cfg: DictConfig, trial: Trial) -> None:
    x_value = trial.params["x"]
    trial.suggest_float(
        "z",
        x_value - cfg.max_z_difference_from_x,
        x_value + cfg.max_z_difference_from_x,
    )
    trial.suggest_float("+w", 0.0,
                        1.0)  # note +w here, not w as w is a new parameter
Esempio n. 9
0
    def objective(trial: Trial) -> float:
        if trial.number == 0:
            trial.suggest_float("param1", 0, 1)
            raise optuna.exceptions.TrialPruned()

        if trial.number == 1:
            trial.suggest_float("param2", 0, 1)
            return 0

        return 0
Esempio n. 10
0
    def objective(trial: Trial) -> float:
        x1 = trial.suggest_float("x1", 0.1, 3)
        x2 = trial.suggest_float("x2", 0.1, 3, log=True)
        x3 = trial.suggest_float("x3", 0, 3, step=1)
        if trial.number % 2 == 0:
            x4 = trial.suggest_float("x4", 0.1, 3)

        value = x1**4 + x2 + x3
        if trial.number % 2 == 0:
            value += x4
        return value
Esempio n. 11
0
    def objective(trial: Trial) -> float:

        # Predefined parameters are sampled by `sample_relative()` method.
        assert trial.suggest_float("a", 0, 5) == 3.2
        assert trial.suggest_categorical("b", ["foo", "bar", "baz"]) == "baz"

        # Other parameters are sampled by `sample_independent()` method.
        assert trial.suggest_int("c", 20, 50) == unknown_param_value
        assert trial.suggest_float("d", 1, 100, log=True) == unknown_param_value
        assert trial.suggest_float("e", 20, 40) == unknown_param_value

        return 0.0
Esempio n. 12
0
    def objective(trial: Trial) -> float:

        a = trial.suggest_int("a", 0, 100)
        b = trial.suggest_float("b", -0.1, 0.1)
        c = trial.suggest_categorical("c", ("x", "y", None, 1, 2.0))
        d = trial.suggest_float("d", -5, 5, step=1)
        e = trial.suggest_float("e", 0.0001, 1, log=True)

        if c == "x":
            return a * d
        else:
            return b * e
Esempio n. 13
0
 def objective(trial: Trial) -> Tuple[float, float]:
     p0 = trial.suggest_float("p0", -10, 10)
     p1 = trial.suggest_float("p1", 3, 5)
     p2 = trial.suggest_float("p2", 0.00001, 0.1, log=True)
     p3 = trial.suggest_float("p3", 100, 200, step=5)
     p4 = trial.suggest_int("p4", -20, -15)
     p5 = cast(int, trial.suggest_categorical("p5", [7, 1, 100]))
     p6 = trial.suggest_float("p6", -10, 10, step=1.0)
     p7 = trial.suggest_int("p7", 1, 7, log=True)
     return (
         p0 + p1 + p2,
         p3 + p4 + p5 + p6 + p7,
     )
Esempio n. 14
0
def test_check_distribution_suggest_float(storage_init_func):
    # type: (typing.Callable[[], storages.BaseStorage]) -> None

    sampler = samplers.RandomSampler()
    study = create_study(storage_init_func(), sampler=sampler)
    trial = Trial(study, study._storage.create_new_trial(study._study_id))

    x1 = trial.suggest_float("x1", 10, 20)
    x2 = trial.suggest_uniform("x1", 10, 20)

    assert x1 == x2

    x3 = trial.suggest_float("x2", 1e-5, 1e-3, log=True)
    x4 = trial.suggest_loguniform("x2", 1e-5, 1e-3)

    assert x3 == x4
Esempio n. 15
0
def objective(trial: Trial):
    args = gen_args()

    # acquisition hyperparam's
    args.cluster = bool(trial.suggest_int('cluster', 0, 1))
    if not args.cluster and random() > 0.5:
        args.epsilon = trial.suggest_float('epsilon', 0.00, 0.2, step=0.05)

    args.fps = None
    if args.model in {'rf', 'nn'} or args.cluster:
        args.encoder = trial.suggest_categorical('encoder',
                                                 {'morgan', 'pair', 'rdkit'})

    try:
        exp = Explorer(**vars(args))
    except (IncompatibilityError, NotImplementedError) as e:
        print(e)
        return float('-inf')

    start = time()
    exp.run()
    total = time() - start
    m, s = divmod(total, 60)
    h, m = divmod(int(m), 60)
    print(f'Total time for trial #{trial.number}: {h}h {m}m {s:0.2f}s\n')

    return exp.top_k_avg
Esempio n. 16
0
    def objective(trial: Trial) -> float:
        x1 = trial.suggest_float("x1", 0.1, 3)
        x2 = trial.suggest_float("x2", 0.1, 3, log=True)
        x3 = trial.suggest_float("x3", 0, 3, step=1)
        x4 = trial.suggest_int("x4", -3, 3)
        x5 = trial.suggest_int("x5", 1, 5, log=True)
        x6 = trial.suggest_categorical("x6", [1.0, 1.1, 1.2])
        if trial.number % 2 == 0:
            # Conditional parameters are ignored unless `params` is specified and is not `None`.
            x7 = trial.suggest_float("x7", 0.1, 3)

        assert isinstance(x6, float)
        value = x1**4 + x2 + x3 - x4**2 - x5 + x6
        if trial.number % 2 == 0:
            value += x7
        return value
Esempio n. 17
0
def suggest_from_config(trial: Trial, configuration: str):
    """
  Suggest From Config
  ===================

  Allows to generalize the `suggest_*` functions taking 
  information from a YAML configuration file.

  Parameters
  ----------
    trial : optuna.trial.Trial
      `Trial` object deriving from an Ask-and-Tell interface.

    configuration : str
      YAML file containing hyperparameters configuration.

  Returns
  -------
    None
  """
    with open(configuration) as file:
        params = yaml.full_load(file)

    for par in params.values():
        if par['type'] == 'categorical':
            trial.suggest_categorical(
                name=str(par['name']),
                choices=list(par['choices']),
            )
        elif par['type'] == 'float':
            trial.suggest_float(
                name=str(par['name']),
                low=float(par['low']),
                high=float(par['high']),
                step=float(par['step']) if par['step'] else None,
                log=bool(par['log']) if par['log'] else False,
            )
        elif par['type'] == 'int':
            trial.suggest_int(
                name=str(par['name']),
                low=float(par['low']),
                high=float(par['high']),
                step=float(par['step']) if par['step'] else 1,
                log=bool(par['log']) if par['log'] else False,
            )
        else:
            raise ValueError('Trial suggestion not implemented.')
Esempio n. 18
0
def test_suggest_low_equals_high(storage_mode: str) -> None:

    with patch.object(
        distributions, "_get_single_value", wraps=distributions._get_single_value
    ) as mock_object, StorageSupplier(storage_mode) as storage:

        study = create_study(storage=storage, sampler=samplers.TPESampler(n_startup_trials=0))

        trial = Trial(study, study._storage.create_new_trial(study._study_id))

        assert trial.suggest_uniform("a", 1.0, 1.0) == 1.0  # Suggesting a param.
        assert mock_object.call_count == 1
        assert trial.suggest_uniform("a", 1.0, 1.0) == 1.0  # Suggesting the same param.
        assert mock_object.call_count == 1

        assert trial.suggest_loguniform("b", 1.0, 1.0) == 1.0  # Suggesting a param.
        assert mock_object.call_count == 2
        assert trial.suggest_loguniform("b", 1.0, 1.0) == 1.0  # Suggesting the same param.
        assert mock_object.call_count == 2

        assert trial.suggest_discrete_uniform("c", 1.0, 1.0, 1.0) == 1.0  # Suggesting a param.
        assert mock_object.call_count == 3
        assert (
            trial.suggest_discrete_uniform("c", 1.0, 1.0, 1.0) == 1.0
        )  # Suggesting the same param.
        assert mock_object.call_count == 3

        assert trial.suggest_int("d", 1, 1) == 1  # Suggesting a param.
        assert mock_object.call_count == 4
        assert trial.suggest_int("d", 1, 1) == 1  # Suggesting the same param.
        assert mock_object.call_count == 4

        assert trial.suggest_float("e", 1.0, 1.0) == 1.0  # Suggesting a param.
        assert mock_object.call_count == 5
        assert trial.suggest_float("e", 1.0, 1.0) == 1.0  # Suggesting the same param.
        assert mock_object.call_count == 5

        assert trial.suggest_float("f", 0.5, 0.5, log=True) == 0.5  # Suggesting a param.
        assert mock_object.call_count == 6
        assert trial.suggest_float("f", 0.5, 0.5, log=True) == 0.5  # Suggesting the same param.
        assert mock_object.call_count == 6

        assert trial.suggest_float("g", 0.5, 0.5, log=False) == 0.5  # Suggesting a param.
        assert mock_object.call_count == 7
        assert trial.suggest_float("g", 0.5, 0.5, log=False) == 0.5  # Suggesting the same param.
        assert mock_object.call_count == 7

        assert trial.suggest_float("h", 0.5, 0.5, step=1.0) == 0.5  # Suggesting a param.
        assert mock_object.call_count == 8
        assert trial.suggest_float("h", 0.5, 0.5, step=1.0) == 0.5  # Suggesting the same param.
        assert mock_object.call_count == 8

        assert trial.suggest_int("i", 1, 1, log=True) == 1  # Suggesting a param.
        assert mock_object.call_count == 9
        assert trial.suggest_int("i", 1, 1, log=True) == 1  # Suggesting the same param.
        assert mock_object.call_count == 9
Esempio n. 19
0
def test_check_distribution_suggest_int(storage_init_func: Callable[
    [], storages.BaseStorage], enable_log: bool) -> None:

    sampler = samplers.RandomSampler()
    study = create_study(storage_init_func(), sampler=sampler)
    trial = Trial(study, study._storage.create_new_trial(study._study_id))

    with pytest.warns(None) as record:
        trial.suggest_int("x", 10, 20, log=enable_log)
        trial.suggest_int("x", 10, 20, log=enable_log)
        trial.suggest_int("x", 10, 22, log=enable_log)

    # We expect exactly one warning.
    assert len(record) == 1

    with pytest.raises(ValueError):
        trial.suggest_float("x", 10, 20, log=enable_log)

    trial = Trial(study, study._storage.create_new_trial(study._study_id))
    with pytest.raises(ValueError):
        trial.suggest_float("x", 10, 20, log=enable_log)
Esempio n. 20
0
def test_check_distribution_suggest_int(storage_mode: str,
                                        enable_log: bool) -> None:

    sampler = samplers.RandomSampler()
    with StorageSupplier(storage_mode) as storage:
        study = create_study(storage=storage, sampler=sampler)
        trial = Trial(study, study._storage.create_new_trial(study._study_id))

        with pytest.warns() as record:
            trial.suggest_int("x", 10, 20, log=enable_log)
            trial.suggest_int("x", 10, 20, log=enable_log)
            trial.suggest_int("x", 10, 22, log=enable_log)

        # We expect exactly one warning (not counting ones caused by deprecation).
        assert len([r for r in record if r.category != FutureWarning]) == 1

        with pytest.raises(ValueError):
            trial.suggest_float("x", 10, 20, log=enable_log)

        trial = Trial(study, study._storage.create_new_trial(study._study_id))
        with pytest.raises(ValueError):
            trial.suggest_float("x", 10, 20, log=enable_log)
Esempio n. 21
0
    def objective(trial: Trial) -> float:

        trial.suggest_float("a", 0, 10)
        trial.suggest_float("b", 0.1, 10, log=True)
        trial.suggest_float("c", 0, 10, step=1)
        trial.suggest_int("d", 0, 10)
        trial.suggest_categorical("e", ["foo", "bar", "baz"])
        trial.suggest_int("f", 1, 10, log=True)

        return 1.0
Esempio n. 22
0
 def objective(trial: Trial) -> float:
     x = trial.suggest_float("x", -1, 1)
     y = trial.suggest_int("y", -1, 1)
     z = trial.suggest_float("z", -1, 1)
     return x + y + z
Esempio n. 23
0
    def objective(trial: Trial, base_value: float) -> float:

        return trial.suggest_float("x", 0.1, 0.2) + base_value
Esempio n. 24
0
    def objective(trial: Trial, exception: Exception) -> float:

        trial.suggest_float("z", 0, 1)
        raise exception
 def _multi_objective_function(trial: Trial) -> Tuple[float, float]:
     x1 = trial.suggest_float("x1", 0.1, 3)
     x2 = trial.suggest_float("x2", 0.1, 3, log=True)
     x3 = trial.suggest_float("x3", 2, 4, log=True)
     return x1, x2 * x3
 def _objective(trial: Trial) -> float:
     x1 = trial.suggest_float("x1", 0.1, 3)
     x2 = trial.suggest_float("x2", 0.1, 3, log=True)
     x3 = trial.suggest_float("x3", 2, 4, log=True)
     return x1 + x2 * x3
Esempio n. 27
0
 def objective(trial: Trial, low: int, high: int) -> float:
     v = trial.suggest_float("x", low, high)
     v += trial.suggest_int("y", low, high)
     return v
Esempio n. 28
0
 def objective(trial: Trial) -> float:
     x = trial.suggest_categorical("x", [True, False])
     if x:
         return trial.suggest_float("y", 0, 1)
     return trial.suggest_float("z", 0, 1)
Esempio n. 29
0
 def objective(trial: Trial) -> float:
     x = trial.suggest_float("x", 0, 5)
     y = trial.suggest_float("y", 1, 1)
     return 4 * x**2 + 4 * y**2
Esempio n. 30
0
 def objective(trial: Trial) -> float:
     x1 = trial.suggest_float("x1", 0.1, trial.number + 0.1)
     return x1**2