def test_get_observation_pairs(): # type: () -> None def objective(trial): # type: (Trial) -> float x = trial.suggest_int('x', 5, 5) if trial.number == 0: return x elif trial.number == 1: trial.report(1, 4) trial.report(2, 7) raise TrialPruned() elif trial.number == 2: trial.report(float('nan'), 3) raise TrialPruned() elif trial.number == 3: raise TrialPruned() else: raise RuntimeError() # direction=minimize. study = optuna.create_study(direction='minimize') study.optimize(objective, n_trials=5) study.storage.create_new_trial_id( study.study_id) # Create a running trial. in_trial_study = InTrialStudy(study) assert tpe.sampler._get_observation_pairs(in_trial_study, 'x') == ( [5.0, 5.0, 5.0, 5.0], [ (-float('inf'), 5.0), # COMPLETE (-7, 2), # PRUNED (with intermediate values) ( -3, float('inf') ), # PRUNED (with a NaN intermediate value; it's treated as infinity) (float('inf'), 0.0) # PRUNED (without intermediate values) ]) assert tpe.sampler._get_observation_pairs(in_trial_study, 'y') == ([], []) # direction=maximize. study = optuna.create_study(direction='maximize') study.optimize(objective, n_trials=4) study.storage.create_new_trial_id( study.study_id) # Create a running trial. in_trial_study = InTrialStudy(study) assert tpe.sampler._get_observation_pairs(in_trial_study, 'x') == ( [5.0, 5.0, 5.0, 5.0], [ (-float('inf'), -5.0), # COMPLETE (-7, -2), # PRUNED (with intermediate values) ( -3, float('inf') ), # PRUNED (with a NaN intermediate value; it's treated as infinity) (float('inf'), 0.0) # PRUNED (without intermediate values) ]) assert tpe.sampler._get_observation_pairs(in_trial_study, 'y') == ([], [])
def test_log_uniform(sampler_class, distribution): # type: (typing.Callable[[], BaseSampler], LogUniformDistribution) -> None study = optuna.study.create_study(sampler=sampler_class()) in_trial_study = InTrialStudy(study) points = np.array([ study.sampler.sample_independent(in_trial_study, _create_new_trial(study), 'x', distribution) for _ in range(100) ]) assert np.all(points >= distribution.low) assert np.all(points < distribution.high) assert not isinstance( study.sampler.sample_independent(in_trial_study, _create_new_trial(study), 'x', distribution), np.floating)
def test_categorical(sampler_class, choices): # type: (typing.Callable[[], BaseSampler], typing.Tuple[T, ...]) -> None distribution = CategoricalDistribution(choices) study = optuna.study.create_study(sampler=sampler_class()) in_trial_study = InTrialStudy(study) points = np.array([ study.sampler.sample_independent(in_trial_study, _create_new_trial(study), 'x', distribution) for _ in range(100) ]) # 'x' value is corresponding to an index of distribution.choices. assert np.all(points >= 0) assert np.all(points <= len(distribution.choices) - 1) round_points = np.round(points) np.testing.assert_almost_equal(round_points, points)
def test_in_trial_study(storage_mode): # type: (str) -> None with StorageSupplier(storage_mode) as storage: study = optuna.create_study(storage=storage) # Run ten trials. study.optimize(lambda t: t.suggest_int('x', 0, 10), n_trials=10) # Create an `InTrialStudy` instance. in_trial_study = InTrialStudy(study) # Test best trial and trials. assert in_trial_study.best_params == study.best_params assert in_trial_study.best_value == study.best_value assert in_trial_study.best_trial == study.best_trial assert in_trial_study.trials == study.trials # Test study direction. assert in_trial_study.direction == study.direction
def test_discrete_uniform(sampler_class, distribution): # type: (typing.Callable[[], BaseSampler], DiscreteUniformDistribution) -> None study = optuna.study.create_study(sampler=sampler_class()) in_trial_study = InTrialStudy(study) points = np.array([ study.sampler.sample_independent(in_trial_study, _create_new_trial(study), 'x', distribution) for _ in range(100) ]) assert np.all(points >= distribution.low) assert np.all(points <= distribution.high) assert not isinstance( study.sampler.sample_independent(in_trial_study, _create_new_trial(study), 'x', distribution), np.floating) # Check all points are multiples of distribution.q. points = points points -= distribution.low points /= distribution.q round_points = np.round(points) np.testing.assert_almost_equal(round_points, points)