def test_infer_relative_search_space() -> None: sampler = MOTPESampler() # Study and frozen-trial are not supposed to be accessed. study = Mock(spec=[]) frozen_trial = Mock(spec=[]) assert sampler.infer_relative_search_space(study, frozen_trial) == {}
def test_sample_relative() -> None: sampler = MOTPESampler() # Study and frozen-trial are not supposed to be accessed. study = Mock(spec=[]) frozen_trial = Mock(spec=[]) assert sampler.sample_relative(study, frozen_trial, {}) == {}
num_variables = 3 elif args.score == "SS": num_variables = 2 n_startup_trials = args.startup multi_objective = False if args.sampler == "TPE": sampler = TPESampler(n_startup_trials=n_startup_trials, seed=seed, multivariate=False) elif args.sampler == "CMAES": sampler = CmaEsSampler(n_startup_trials=n_startup_trials, seed=seed) elif args.sampler == "MOTPE": if n_startup_trials is None: n_startup_trials = num_variables * 11 - 1 sampler = MOTPESampler(n_startup_trials=n_startup_trials, seed=seed) multi_objective = True elif args.sampler == "Random": sampler = RandomSampler(seed=seed) multi_objective = True else: print("sampler not correctly specified") exit(1) if multi_objective: study = optuna.create_study(study_name=args.name, storage=args.storage, directions=["minimize"] * num_variables, sampler=sampler, load_if_exists=True) else:
def test_sample_independent_misc_arguments() -> None: study = optuna.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 = frozen_trial_factory(16, [0, 0]) sampler = MOTPESampler(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.trial.Trial.system_attrs", new_callable=PropertyMock ) as mock1, patch( "optuna.trial.FrozenTrial.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 = MOTPESampler(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.trial.Trial.system_attrs", new_callable=PropertyMock ) as mock1, patch( "optuna.trial.FrozenTrial.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 = MOTPESampler(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.trial.Trial.system_attrs", new_callable=PropertyMock ) as mock1, patch( "optuna.trial.FrozenTrial.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 = MOTPESampler(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.trial.Trial.system_attrs", new_callable=PropertyMock ) as mock1, patch( "optuna.trial.FrozenTrial.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_prior() -> None: study = optuna.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 = frozen_trial_factory(16, [0, 0]) sampler = MOTPESampler(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.trial.Trial.system_attrs", new_callable=PropertyMock) as mock1, patch( "optuna.trial.FrozenTrial.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 = MOTPESampler(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.trial.Trial.system_attrs", new_callable=PropertyMock) as mock1, patch( "optuna.trial.FrozenTrial.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 = MOTPESampler(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.trial.Trial.system_attrs", new_callable=PropertyMock) as mock1, patch( "optuna.trial.FrozenTrial.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