def test_run_trial_pruned(storage_mode: str, caplog: LogCaptureFixture) -> None: def gen_func( intermediate: Optional[float] = None) -> Callable[[Trial], float]: def func(trial: Trial) -> float: if intermediate is not None: trial.report(step=1, value=intermediate) raise TrialPruned return func with StorageSupplier(storage_mode) as storage: study = create_study(storage=storage) caplog.clear() frozen_trial = _optimize._run_trial(study, gen_func(), catch=()) assert frozen_trial.state == TrialState.PRUNED assert frozen_trial.value is None assert "Trial 0 pruned." in caplog.text caplog.clear() frozen_trial = _optimize._run_trial(study, gen_func(intermediate=1), catch=()) assert frozen_trial.state == TrialState.PRUNED assert frozen_trial.value == 1 assert "Trial 1 pruned." in caplog.text caplog.clear() frozen_trial = _optimize._run_trial( study, gen_func(intermediate=float("nan")), catch=()) assert frozen_trial.state == TrialState.PRUNED assert frozen_trial.value is None assert "Trial 2 pruned." in caplog.text
def test_run_trial(storage_mode: str, caplog: LogCaptureFixture) -> None: with StorageSupplier(storage_mode) as storage: study = create_study(storage=storage) caplog.clear() frozen_trial = _optimize._run_trial(study, lambda _: 1.0, catch=()) assert frozen_trial.state == TrialState.COMPLETE assert frozen_trial.value == 1.0 assert "Trial 0 finished with value: 1.0 and parameters" in caplog.text caplog.clear() frozen_trial = _optimize._run_trial(study, lambda _: float("inf"), catch=()) assert frozen_trial.state == TrialState.COMPLETE assert frozen_trial.value == float("inf") assert "Trial 1 finished with value: inf and parameters" in caplog.text caplog.clear() frozen_trial = _optimize._run_trial(study, lambda _: -float("inf"), catch=()) assert frozen_trial.state == TrialState.COMPLETE assert frozen_trial.value == -float("inf") assert "Trial 2 finished with value: -inf and parameters" in caplog.text
def test_run_trial_exception(storage_mode: str) -> None: with StorageSupplier(storage_mode) as storage: study = create_study(storage=storage) with pytest.raises(ValueError): _optimize._run_trial(study, fail_objective, ()) # Test trial with unacceptable exception. with StorageSupplier(storage_mode) as storage: study = create_study(storage=storage) with pytest.raises(ValueError): _optimize._run_trial(study, fail_objective, (ArithmeticError, ))
def test_run_trial_catch_exception(storage_mode: str) -> None: with StorageSupplier(storage_mode) as storage: study = create_study(storage=storage) frozen_trial = _optimize._run_trial(study, fail_objective, catch=(ValueError, )) assert frozen_trial.state == TrialState.FAIL assert STUDY_TELL_WARNING_KEY not in frozen_trial.system_attrs
def test_run_trial_invoke_tell_with_suppressing_warning( storage_mode: str) -> None: def func_numerical(trial: Trial) -> float: return trial.suggest_float("v", 0, 10) with StorageSupplier(storage_mode) as storage: study = create_study(storage=storage) with mock.patch("optuna.study._optimize._tell_with_warning", side_effect=_tell_with_warning) as mock_obj: _optimize._run_trial(study, func_numerical, ()) mock_obj.assert_called_once_with( study=mock.ANY, trial=mock.ANY, values=mock.ANY, state=mock.ANY, suppress_warning=True, )
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
def test_run_trial_automatically_fail(storage_mode: str, caplog: LogCaptureFixture) -> None: with StorageSupplier(storage_mode) as storage: study = create_study(storage=storage) caplog.clear() frozen_trial = _optimize._run_trial(study, lambda _: float("nan"), catch=()) assert frozen_trial.state == TrialState.FAIL assert frozen_trial.value is None assert "Trial 0 failed because of the following error:" in caplog.text assert "The value nan is not acceptable." in caplog.text caplog.clear() frozen_trial = _optimize._run_trial( study, lambda _: None, catch=()) # type: ignore[arg-type,return-value] # noqa: E501 assert frozen_trial.state == TrialState.FAIL assert frozen_trial.value is None assert "Trial 1 failed because of the following error:" in caplog.text assert "The value None could not be cast to float." in caplog.text caplog.clear() frozen_trial = _optimize._run_trial( study, lambda _: object(), catch=()) # type: ignore[arg-type,return-value] # noqa: E501 assert frozen_trial.state == TrialState.FAIL assert frozen_trial.value is None assert "Trial 2 failed because of the following error:" in caplog.text assert "The value <object object at" in caplog.text assert "> could not be cast to float." in caplog.text caplog.clear() frozen_trial = _optimize._run_trial(study, lambda _: [0, 1], catch=()) assert frozen_trial.state == TrialState.FAIL assert frozen_trial.value is None assert "Trial 3 failed because of the following error: The number" in caplog.text assert "of the values 2 did not match the number of the objectives 1." in caplog.text
def test_run_trial_with_trial_pruned(trial_pruned_class: Callable[[], TrialPruned], report_value: Optional[float]) -> None: study = create_study() def func_with_trial_pruned(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