def test_sample_independent_n_startup_trial() -> None: study = optuna.multi_objective.create_study( directions=["minimize", "maximize"]) dist = optuna.distributions.UniformDistribution(1.0, 100.0) random.seed(128) past_trials = [ frozen_trial_factory( i, [random.random(), random.random()]) for i in range(16) ] trial = multi_objective.trial.FrozenMultiObjectiveTrial( 2, frozen_trial_factory(16, [0, 0])) sampler = MOTPEMultiObjectiveSampler(n_startup_trials=16, seed=0) attrs = MockSystemAttr() with patch.object( study._storage, "get_all_trials", return_value=past_trials[:15] ), patch.object( study._storage, "set_trial_system_attr", side_effect=attrs.set_trial_system_attr ), patch.object(study._storage, "get_trial", return_value=trial), patch( "optuna.multi_objective.trial.MultiObjectiveTrial.system_attrs", new_callable=PropertyMock ) as mock1, patch( "optuna.multi_objective.trial.FrozenMultiObjectiveTrial.system_attrs", new_callable=PropertyMock, ) as mock2, patch.object( optuna.multi_objective.samplers.RandomMultiObjectiveSampler, "sample_independent", return_value=1.0, ) as sample_method: mock1.return_value = attrs.value mock2.return_value = attrs.value sampler.sample_independent(study, trial, "param-a", dist) assert sample_method.call_count == 1 sampler = MOTPEMultiObjectiveSampler(n_startup_trials=16, seed=0) attrs = MockSystemAttr() with patch.object( study._storage, "get_all_trials", return_value=past_trials ), patch.object( study._storage, "set_trial_system_attr", side_effect=attrs.set_trial_system_attr ), patch.object(study._storage, "get_trial", return_value=trial), patch( "optuna.multi_objective.trial.MultiObjectiveTrial.system_attrs", new_callable=PropertyMock ) as mock1, patch( "optuna.multi_objective.trial.FrozenMultiObjectiveTrial.system_attrs", new_callable=PropertyMock, ) as mock2, patch.object( optuna.multi_objective.samplers.RandomMultiObjectiveSampler, "sample_independent", return_value=1.0, ) as sample_method: mock1.return_value = attrs.value mock2.return_value = attrs.value sampler.sample_independent(study, trial, "param-a", dist) assert sample_method.call_count == 0
def test_sample_independent_prior() -> None: study = optuna.multi_objective.create_study(directions=["minimize", "maximize"]) dist = optuna.distributions.UniformDistribution(1.0, 100.0) random.seed(128) past_trials = [frozen_trial_factory(i, [random.random(), random.random()]) for i in range(16)] # Prepare a trial and a sample for later checks. trial = multi_objective.trial.FrozenMultiObjectiveTrial(2, frozen_trial_factory(16, [0, 0])) sampler = MOTPEMultiObjectiveSampler(seed=0) attrs = MockSystemAttr() with patch.object(study._storage, "get_all_trials", return_value=past_trials), patch.object( study._storage, "set_trial_system_attr", side_effect=attrs.set_trial_system_attr ), patch.object(study._storage, "get_trial", return_value=trial), patch( "optuna.multi_objective.trial.MultiObjectiveTrial.system_attrs", new_callable=PropertyMock ) as mock1, patch( "optuna.multi_objective.trial.FrozenMultiObjectiveTrial.system_attrs", new_callable=PropertyMock, ) as mock2: mock1.return_value = attrs.value mock2.return_value = attrs.value suggestion = sampler.sample_independent(study, trial, "param-a", dist) sampler = MOTPEMultiObjectiveSampler(consider_prior=False, seed=0) attrs = MockSystemAttr() with patch.object(study._storage, "get_all_trials", return_value=past_trials), patch.object( study._storage, "set_trial_system_attr", side_effect=attrs.set_trial_system_attr ), patch.object(study._storage, "get_trial", return_value=trial), patch( "optuna.multi_objective.trial.MultiObjectiveTrial.system_attrs", new_callable=PropertyMock ) as mock1, patch( "optuna.multi_objective.trial.FrozenMultiObjectiveTrial.system_attrs", new_callable=PropertyMock, ) as mock2: mock1.return_value = attrs.value mock2.return_value = attrs.value assert sampler.sample_independent(study, trial, "param-a", dist) != suggestion sampler = MOTPEMultiObjectiveSampler(prior_weight=0.5, seed=0) attrs = MockSystemAttr() with patch.object(study._storage, "get_all_trials", return_value=past_trials), patch.object( study._storage, "set_trial_system_attr", side_effect=attrs.set_trial_system_attr ), patch.object(study._storage, "get_trial", return_value=trial), patch( "optuna.multi_objective.trial.MultiObjectiveTrial.system_attrs", new_callable=PropertyMock ) as mock1, patch( "optuna.multi_objective.trial.FrozenMultiObjectiveTrial.system_attrs", new_callable=PropertyMock, ) as mock2: mock1.return_value = attrs.value mock2.return_value = attrs.value assert sampler.sample_independent(study, trial, "param-a", dist) != suggestion
def test_sample_independent_uniform_distributions() -> None: # Prepare sample from uniform distribution for cheking other distributions. study = optuna.multi_objective.create_study( directions=["minimize", "maximize"]) random.seed(128) past_trials = [ frozen_trial_factory( i, [random.random(), random.random()]) for i in range(16) ] uni_dist = optuna.distributions.UniformDistribution(1.0, 100.0) trial = multi_objective.trial.FrozenMultiObjectiveTrial( 2, frozen_trial_factory(16, [0, 0])) sampler = MOTPEMultiObjectiveSampler(seed=0) attrs = MockSystemAttr() with patch.object( study._storage, "get_all_trials", return_value=past_trials ), patch.object( study._storage, "set_trial_system_attr", side_effect=attrs.set_trial_system_attr ), patch.object(study._storage, "get_trial", return_value=trial), patch( "optuna.multi_objective.trial.MultiObjectiveTrial.system_attrs", new_callable=PropertyMock ) as mock1, patch( "optuna.multi_objective.trial.FrozenMultiObjectiveTrial.system_attrs", new_callable=PropertyMock, ) as mock2: mock1.return_value = attrs.value mock2.return_value = attrs.value uniform_suggestion = sampler.sample_independent( study, trial, "param-a", uni_dist) assert 1.0 <= uniform_suggestion < 100.0
def test_sample_independent_ignored_states() -> None: """Tests FAIL, RUNNING, and WAITING states are equally.""" study = optuna.multi_objective.create_study(directions=["minimize", "maximize"]) dist = optuna.distributions.UniformDistribution(1.0, 100.0) suggestions = [] for state in [ optuna.trial.TrialState.FAIL, optuna.trial.TrialState.RUNNING, optuna.trial.TrialState.WAITING, ]: random.seed(128) state_fn = build_state_fn(state) past_trials = [ frozen_trial_factory(i, [random.random(), random.random()], state_fn=state_fn) for i in range(32) ] trial = multi_objective.trial.FrozenMultiObjectiveTrial( 2, frozen_trial_factory(32, [0, 0]) ) sampler = MOTPEMultiObjectiveSampler(seed=0) attrs = MockSystemAttr() with patch.object(study._storage, "get_all_trials", return_value=past_trials), patch.object( study._storage, "set_trial_system_attr", side_effect=attrs.set_trial_system_attr ), patch.object(study._storage, "get_trial", return_value=trial), patch( "optuna.multi_objective.trial.MultiObjectiveTrial.system_attrs", new_callable=PropertyMock ) as mock1, patch( "optuna.multi_objective.trial.FrozenMultiObjectiveTrial.system_attrs", new_callable=PropertyMock, ) as mock2: mock1.return_value = attrs.value mock2.return_value = attrs.value suggestions.append(sampler.sample_independent(study, trial, "param-a", dist)) assert len(set(suggestions)) == 1
def test_sample_int_uniform_distributions() -> None: """Test sampling from int distribution returns integer.""" study = optuna.multi_objective.create_study(directions=["minimize", "maximize"]) random.seed(128) def int_value_fn(idx: int) -> float: return random.randint(0, 100) int_dist = optuna.distributions.IntUniformDistribution(1, 100) past_trials = [ frozen_trial_factory( i, [random.random(), random.random()], dist=int_dist, value_fn=int_value_fn ) for i in range(16) ] trial = multi_objective.trial.FrozenMultiObjectiveTrial(2, frozen_trial_factory(16, [0, 0])) sampler = MOTPEMultiObjectiveSampler(seed=0) attrs = MockSystemAttr() with patch.object(study._storage, "get_all_trials", return_value=past_trials), patch.object( study._storage, "set_trial_system_attr", side_effect=attrs.set_trial_system_attr ), patch.object(study._storage, "get_trial", return_value=trial), patch( "optuna.multi_objective.trial.MultiObjectiveTrial.system_attrs", new_callable=PropertyMock ) as mock1, patch( "optuna.multi_objective.trial.FrozenMultiObjectiveTrial.system_attrs", new_callable=PropertyMock, ) as mock2: mock1.return_value = attrs.value mock2.return_value = attrs.value int_suggestion = sampler.sample_independent(study, trial, "param-a", int_dist) assert 1 <= int_suggestion <= 100 assert isinstance(int_suggestion, int)
def test_sample_independent_categorical_distributions() -> None: """Test samples are drawn from the specified category.""" study = optuna.multi_objective.create_study(directions=["minimize", "maximize"]) random.seed(128) categories = [i * 0.3 + 1.0 for i in range(330)] def cat_value_fn(idx: int) -> float: return categories[random.randint(0, len(categories) - 1)] cat_dist = optuna.distributions.CategoricalDistribution(categories) past_trials = [ frozen_trial_factory( i, [random.random(), random.random()], dist=cat_dist, value_fn=cat_value_fn ) for i in range(16) ] trial = multi_objective.trial.FrozenMultiObjectiveTrial(2, frozen_trial_factory(16, [0, 0])) sampler = MOTPEMultiObjectiveSampler(seed=0) attrs = MockSystemAttr() with patch.object(study._storage, "get_all_trials", return_value=past_trials), patch.object( study._storage, "set_trial_system_attr", side_effect=attrs.set_trial_system_attr ), patch.object(study._storage, "get_trial", return_value=trial), patch( "optuna.multi_objective.trial.MultiObjectiveTrial.system_attrs", new_callable=PropertyMock ) as mock1, patch( "optuna.multi_objective.trial.FrozenMultiObjectiveTrial.system_attrs", new_callable=PropertyMock, ) as mock2: mock1.return_value = attrs.value mock2.return_value = attrs.value categorical_suggestion = sampler.sample_independent(study, trial, "param-a", cat_dist) assert categorical_suggestion in categories
def test_sample_independent_handle_unsuccessful_states(state: optuna.trial.TrialState) -> None: study = optuna.multi_objective.create_study(directions=["minimize", "maximize"]) dist = optuna.distributions.UniformDistribution(1.0, 100.0) random.seed(128) # Prepare sampling result for later tests. past_trials = [frozen_trial_factory(i, [random.random(), random.random()]) for i in range(32)] trial = multi_objective.trial.FrozenMultiObjectiveTrial(2, frozen_trial_factory(32, [0, 0])) sampler = MOTPEMultiObjectiveSampler(seed=0) attrs = MockSystemAttr() with patch.object(study._storage, "get_all_trials", return_value=past_trials), patch.object( study._storage, "set_trial_system_attr", side_effect=attrs.set_trial_system_attr ), patch.object(study._storage, "get_trial", return_value=trial), patch( "optuna.multi_objective.trial.MultiObjectiveTrial.system_attrs", new_callable=PropertyMock ) as mock1, patch( "optuna.multi_objective.trial.FrozenMultiObjectiveTrial.system_attrs", new_callable=PropertyMock, ) as mock2: mock1.return_value = attrs.value mock2.return_value = attrs.value all_success_suggestion = sampler.sample_independent(study, trial, "param-a", dist) # Test unsuccessful trials are handled differently. state_fn = build_state_fn(state) past_trials = [ frozen_trial_factory(i, [random.random(), random.random()], state_fn=state_fn) for i in range(32) ] trial = multi_objective.trial.FrozenMultiObjectiveTrial(2, frozen_trial_factory(32, [0, 0])) sampler = MOTPEMultiObjectiveSampler(seed=0) attrs = MockSystemAttr() with patch.object(study._storage, "get_all_trials", return_value=past_trials), patch.object( study._storage, "set_trial_system_attr", side_effect=attrs.set_trial_system_attr ), patch.object(study._storage, "get_trial", return_value=trial), patch( "optuna.multi_objective.trial.MultiObjectiveTrial.system_attrs", new_callable=PropertyMock ) as mock1, patch( "optuna.multi_objective.trial.FrozenMultiObjectiveTrial.system_attrs", new_callable=PropertyMock, ) as mock2: mock1.return_value = attrs.value mock2.return_value = attrs.value partial_unsuccessful_suggestion = sampler.sample_independent(study, trial, "param-a", dist) assert partial_unsuccessful_suggestion != all_success_suggestion
def test_sample_independent_disrete_uniform_distributions() -> None: """Test samples from discrete have expected intervals.""" study = optuna.multi_objective.create_study( directions=["minimize", "maximize"]) random.seed(128) disc_dist = optuna.distributions.DiscreteUniformDistribution( 1.0, 100.0, 0.1) def value_fn(idx: int) -> float: return int(random.random() * 1000) * 0.1 past_trials = [ frozen_trial_factory( i, [random.random(), random.random()], dist=disc_dist, value_fn=value_fn) for i in range(16) ] trial = multi_objective.trial.FrozenMultiObjectiveTrial( 2, frozen_trial_factory(16, [0, 0])) sampler = MOTPEMultiObjectiveSampler(seed=0) attrs = MockSystemAttr() with patch.object( study._storage, "get_all_trials", return_value=past_trials ), patch.object( study._storage, "set_trial_system_attr", side_effect=attrs.set_trial_system_attr ), patch.object(study._storage, "get_trial", return_value=trial), patch( "optuna.multi_objective.trial.MultiObjectiveTrial.system_attrs", new_callable=PropertyMock ) as mock1, patch( "optuna.multi_objective.trial.FrozenMultiObjectiveTrial.system_attrs", new_callable=PropertyMock, ) as mock2: mock1.return_value = attrs.value mock2.return_value = attrs.value discrete_uniform_suggestion = sampler.sample_independent( study, trial, "param-a", disc_dist) assert 1.0 <= discrete_uniform_suggestion <= 100.0 assert abs( int(discrete_uniform_suggestion * 10) - discrete_uniform_suggestion * 10) < 1e-3
def test_sample_independent_misc_arguments() -> None: study = optuna.multi_objective.create_study( directions=["minimize", "maximize"]) dist = optuna.distributions.UniformDistribution(1.0, 100.0) random.seed(128) past_trials = [ frozen_trial_factory( i, [random.random(), random.random()]) for i in range(32) ] # Prepare a trial and a sample for later checks. trial = multi_objective.trial.FrozenMultiObjectiveTrial( 2, frozen_trial_factory(16, [0, 0])) sampler = MOTPEMultiObjectiveSampler(seed=0) attrs = MockSystemAttr() with patch.object( study._storage, "get_all_trials", return_value=past_trials ), patch.object( study._storage, "set_trial_system_attr", side_effect=attrs.set_trial_system_attr ), patch.object(study._storage, "get_trial", return_value=trial), patch( "optuna.multi_objective.trial.MultiObjectiveTrial.system_attrs", new_callable=PropertyMock ) as mock1, patch( "optuna.multi_objective.trial.FrozenMultiObjectiveTrial.system_attrs", new_callable=PropertyMock, ) as mock2: mock1.return_value = attrs.value mock2.return_value = attrs.value suggestion = sampler.sample_independent(study, trial, "param-a", dist) # Test misc. parameters. sampler = MOTPEMultiObjectiveSampler(n_ehvi_candidates=13, seed=0) attrs = MockSystemAttr() with patch.object( study._storage, "get_all_trials", return_value=past_trials ), patch.object( study._storage, "set_trial_system_attr", side_effect=attrs.set_trial_system_attr ), patch.object(study._storage, "get_trial", return_value=trial), patch( "optuna.multi_objective.trial.MultiObjectiveTrial.system_attrs", new_callable=PropertyMock ) as mock1, patch( "optuna.multi_objective.trial.FrozenMultiObjectiveTrial.system_attrs", new_callable=PropertyMock, ) as mock2: mock1.return_value = attrs.value mock2.return_value = attrs.value assert sampler.sample_independent(study, trial, "param-a", dist) != suggestion sampler = MOTPEMultiObjectiveSampler(gamma=lambda _: 5, seed=0) attrs = MockSystemAttr() with patch.object( study._storage, "get_all_trials", return_value=past_trials ), patch.object( study._storage, "set_trial_system_attr", side_effect=attrs.set_trial_system_attr ), patch.object(study._storage, "get_trial", return_value=trial), patch( "optuna.multi_objective.trial.MultiObjectiveTrial.system_attrs", new_callable=PropertyMock ) as mock1, patch( "optuna.multi_objective.trial.FrozenMultiObjectiveTrial.system_attrs", new_callable=PropertyMock, ) as mock2: mock1.return_value = attrs.value mock2.return_value = attrs.value assert sampler.sample_independent(study, trial, "param-a", dist) != suggestion sampler = MOTPEMultiObjectiveSampler(weights_above=lambda n: np.zeros(n), seed=0) attrs = MockSystemAttr() with patch.object( study._storage, "get_all_trials", return_value=past_trials ), patch.object( study._storage, "set_trial_system_attr", side_effect=attrs.set_trial_system_attr ), patch.object(study._storage, "get_trial", return_value=trial), patch( "optuna.multi_objective.trial.MultiObjectiveTrial.system_attrs", new_callable=PropertyMock ) as mock1, patch( "optuna.multi_objective.trial.FrozenMultiObjectiveTrial.system_attrs", new_callable=PropertyMock, ) as mock2: mock1.return_value = attrs.value mock2.return_value = attrs.value assert sampler.sample_independent(study, trial, "param-a", dist) != suggestion