Exemplo n.º 1
0
def test_run_trial(storage_mode: str) -> None:

    with StorageSupplier(storage_mode) as storage:
        study = create_study(storage=storage)

        # Test trial with acceptable exception.
        def func_value_error(_: Trial) -> float:

            raise ValueError

        trial = _optimize._run_trial(study,
                                     func_value_error,
                                     catch=(ValueError, ))
        frozen_trial = study._storage.get_trial(trial._trial_id)

        assert frozen_trial.state == TrialState.FAIL

        # Test trial with unacceptable exception.
        with pytest.raises(ValueError):
            _optimize._run_trial(study,
                                 func_value_error,
                                 catch=(ArithmeticError, ))

        # Test trial with invalid objective value: None
        def func_none(_: Trial) -> float:

            return None  # type: ignore

        trial = _optimize._run_trial(study, func_none, catch=(Exception, ))
        frozen_trial = study._storage.get_trial(trial._trial_id)

        assert frozen_trial.state == TrialState.FAIL

        # Test trial with invalid objective value: nan
        def func_nan(_: Trial) -> float:

            return float("nan")

        trial = _optimize._run_trial(study, func_nan, catch=(Exception, ))
        frozen_trial = study._storage.get_trial(trial._trial_id)

        assert frozen_trial.state == TrialState.FAIL
Exemplo n.º 2
0
def test_run_trial(storage_mode: str) -> None:

    with StorageSupplier(storage_mode) as storage:
        study = optuna.create_study(storage=storage)

        # Test trial without exception.
        _optimize._run_trial(study, func, catch=(Exception, ))
        check_study(study)

        # Test trial with acceptable exception.
        def func_value_error(_: optuna.trial.Trial) -> float:

            raise ValueError

        trial = _optimize._run_trial(study,
                                     func_value_error,
                                     catch=(ValueError, ))
        frozen_trial = study._storage.get_trial(trial._trial_id)

        expected_message = "Trial 1 failed because of the following error: ValueError()"
        assert frozen_trial.state == TrialState.FAIL
        assert frozen_trial.system_attrs["fail_reason"] == expected_message

        # Test trial with unacceptable exception.
        with pytest.raises(ValueError):
            _optimize._run_trial(study,
                                 func_value_error,
                                 catch=(ArithmeticError, ))

        # Test trial with invalid objective value: None
        def func_none(_: optuna.trial.Trial) -> float:

            return None  # type: ignore

        trial = _optimize._run_trial(study, func_none, catch=(Exception, ))
        frozen_trial = study._storage.get_trial(trial._trial_id)

        expected_message = (
            "Trial 3 failed, because the returned "
            "value from the objective function cannot be cast to float. "
            "Returned value is: None")
        assert frozen_trial.state == TrialState.FAIL
        assert frozen_trial.system_attrs["fail_reason"] == expected_message

        # Test trial with invalid objective value: nan
        def func_nan(_: optuna.trial.Trial) -> float:

            return float("nan")

        trial = _optimize._run_trial(study, func_nan, catch=(Exception, ))
        frozen_trial = study._storage.get_trial(trial._trial_id)

        expected_message = "Trial 4 failed, because the objective function returned nan."
        assert frozen_trial.state == TrialState.FAIL
        assert frozen_trial.system_attrs["fail_reason"] == expected_message
Exemplo n.º 3
0
def test_run_trial_with_trial_pruned(trial_pruned_class: Callable[
    [], optuna.exceptions.TrialPruned], report_value: Optional[float]) -> None:

    study = optuna.create_study()

    def func_with_trial_pruned(trial: optuna.trial.Trial) -> float:

        if report_value is not None:
            trial.report(report_value, 1)

        raise trial_pruned_class()

    trial = _optimize._run_trial(study, func_with_trial_pruned, catch=())
    frozen_trial = study._storage.get_trial(trial._trial_id)
    assert frozen_trial.value == report_value
    assert frozen_trial.state == TrialState.PRUNED