def test_sample_independent_categorical_distributions() -> None:
    """Test samples are drawn from the specified category."""

    study = optuna.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 = 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
        categorical_suggestion = sampler.sample_independent(study, trial, "param-a", cat_dist)
    assert categorical_suggestion in categories
def test_sample_independent_ignored_states() -> None:
    """Tests FAIL, RUNNING, and WAITING states are equally."""
    study = optuna.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 = frozen_trial_factory(32, [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
        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.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 = 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
        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_uniform_distributions() -> None:
    # Prepare sample from uniform distribution for cheking other distributions.
    study = optuna.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 = 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
        uniform_suggestion = sampler.sample_independent(
            study, trial, "param-a", uni_dist)
    assert 1.0 <= uniform_suggestion < 100.0
Exemple #5
0
    def __init__(
        self,
        consider_prior: bool = True,
        prior_weight: float = 1.0,
        consider_magic_clip: bool = True,
        consider_endpoints: bool = True,
        n_startup_trials: int = 10,
        n_ehvi_candidates: int = 24,
        gamma: Callable[[int], int] = default_gamma,
        weights_above: Callable[[int], np.ndarray] = _default_weights_above,
        seed: Optional[int] = None,
    ) -> None:

        with warnings.catch_warnings():
            warnings.simplefilter("ignore", ExperimentalWarning)
            self._motpe_sampler = MOTPESampler(
                consider_prior=consider_prior,
                prior_weight=prior_weight,
                consider_magic_clip=consider_magic_clip,
                consider_endpoints=consider_endpoints,
                n_startup_trials=n_startup_trials,
                n_ehvi_candidates=n_ehvi_candidates,
                gamma=gamma,
                weights_above=weights_above,
                seed=seed,
            )
def test_reseed_rng() -> None:
    sampler = MOTPESampler()
    original_seed = sampler._rng.seed

    with patch.object(
        sampler._mo_random_sampler, "reseed_rng", wraps=sampler._mo_random_sampler.reseed_rng
    ) as mock_object:
        sampler.reseed_rng()
        assert mock_object.call_count == 1
        assert original_seed != sampler._rng.seed
def test_solve_hssp() -> None:
    sampler = MOTPESampler(seed=0)

    random.seed(128)

    # Two dimensions
    for i in range(8):
        subset_size = int(random.random() * i) + 1
        test_case = np.asarray([[random.random(), random.random()] for _ in range(8)])
        r = 1.1 * np.max(test_case, axis=0)
        truth = 0.0
        for subset in itertools.permutations(test_case, subset_size):
            truth = max(truth, sampler._compute_hypervolume(np.asarray(subset), r))
        indices = sampler._solve_hssp(test_case, np.arange(len(test_case)), subset_size, r)
        approx = sampler._compute_hypervolume(test_case[indices], r)
        assert approx / truth > 0.6321  # 1 - 1/e

    # Three dimensions
    for i in range(8):
        subset_size = int(random.random() * i) + 1
        test_case = np.asarray(
            [[random.random(), random.random(), random.random()] for _ in range(8)]
        )
        r = 1.1 * np.max(test_case, axis=0)
        truth = 0
        for subset in itertools.permutations(test_case, subset_size):
            truth = max(truth, sampler._compute_hypervolume(np.asarray(subset), r))
        indices = sampler._solve_hssp(test_case, np.arange(len(test_case)), subset_size, r)
        approx = sampler._compute_hypervolume(test_case[indices], r)
        assert approx / truth > 0.6321  # 1 - 1/e
def test_cache() -> None:
    n = 10
    sampler = MOTPESampler(seed=0, n_startup_trials=n)

    def objective(trial: optuna.trial.Trial) -> Tuple[float, float]:
        x = trial.suggest_float("x", 0, 5)

        if trial._trial_id == n:
            assert n in sampler._split_cache
            assert n in sampler._weights_below
        else:
            assert n not in sampler._split_cache
            assert n not in sampler._weights_below

        y = trial.suggest_float("y", 0, 3)
        v0 = 4 * x ** 2 + 4 * y ** 2
        v1 = (x - 5) ** 2 + (y - 5) ** 2
        return v0, v1

    study = optuna.create_study(directions=["minimize", "maximize"], sampler=sampler)

    assert n not in sampler._split_cache
    assert n not in sampler._weights_below

    study.optimize(objective, n_trials=n + 1)

    assert n not in sampler._split_cache
    assert n not in sampler._weights_below
def test_call_after_trial_of_mo_random_sampler() -> None:
    sampler = MOTPESampler()
    study = optuna.create_study(sampler=sampler)
    with patch.object(
        sampler._mo_random_sampler, "after_trial", wraps=sampler._mo_random_sampler.after_trial
    ) as mock_object:
        study.optimize(lambda _: 1.0, n_trials=1)
        assert mock_object.call_count == 1
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
def test_sample_independent_n_startup_trial() -> 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)
    ]

    trial = frozen_trial_factory(16, [0, 0])
    sampler = MOTPESampler(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.trial.Trial.system_attrs",
                        new_callable=PropertyMock) as mock1, patch(
                            "optuna.trial.FrozenTrial.system_attrs",
                            new_callable=PropertyMock,
                        ) as mock2, patch.object(
                            optuna.samplers.RandomSampler,
                            "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 = MOTPESampler(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.trial.Trial.system_attrs",
                        new_callable=PropertyMock) as mock1, patch(
                            "optuna.trial.FrozenTrial.system_attrs",
                            new_callable=PropertyMock,
                        ) as mock2, patch.object(
                            optuna.samplers.RandomSampler,
                            "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_disrete_uniform_distributions() -> None:
    """Test samples from discrete have expected intervals."""

    study = optuna.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 = 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
        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_calculate_weights_below() -> None:
    sampler = MOTPESampler()

    # Two samples.
    weights_below = sampler._calculate_weights_below(
        np.array([[0.2, 0.5], [0.9, 0.4], [1, 1]]), np.array([0, 1]))
    assert len(weights_below) == 2
    assert weights_below[0] > weights_below[1]
    assert sum(weights_below) > 0

    # Two equally contributed samples.
    weights_below = sampler._calculate_weights_below(
        np.array([[0.2, 0.8], [0.8, 0.2], [1, 1]]), np.array([0, 1]))
    assert len(weights_below) == 2
    assert weights_below[0] == weights_below[1]
    assert sum(weights_below) > 0

    # Duplicated samples.
    weights_below = sampler._calculate_weights_below(
        np.array([[0.2, 0.8], [0.2, 0.8], [1, 1]]), np.array([0, 1]))
    assert len(weights_below) == 2
    assert weights_below[0] == weights_below[1]
    assert sum(weights_below) > 0

    # Three samples.
    weights_below = sampler._calculate_weights_below(
        np.array([[0.3, 0.3], [0.2, 0.8], [0.8, 0.2], [1, 1]]),
        np.array([0, 1, 2]))
    assert len(weights_below) == 3
    assert weights_below[0] > weights_below[1]
    assert weights_below[0] > weights_below[2]
    assert weights_below[1] == weights_below[2]
    assert sum(weights_below) > 0
def test_sample_independent_handle_unsuccessful_states(
        state: optuna.trial.TrialState) -> None:
    study = optuna.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 = frozen_trial_factory(32, [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
        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 = frozen_trial_factory(32, [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
        partial_unsuccessful_suggestion = sampler.sample_independent(
            study, trial, "param-a", dist)
    assert partial_unsuccessful_suggestion != all_success_suggestion
def test_get_observation_pairs() -> None:
    def objective(trial: optuna.trial.Trial) -> Tuple[float, float]:
        trial.suggest_int("x", 5, 5)
        return 5.0, 5.0

    sampler = MOTPESampler(seed=0)
    study = optuna.create_study(directions=["minimize", "maximize"], sampler=sampler)
    study.optimize(objective, n_trials=5)

    assert multi_objective_sampler._get_observation_pairs(study, "x") == (
        [5.0, 5.0, 5.0, 5.0, 5.0],
        [[5.0, -5.0], [5.0, -5.0], [5.0, -5.0], [5.0, -5.0], [5.0, -5.0]],
    )
    assert multi_objective_sampler._get_observation_pairs(study, "y") == (
        [None, None, None, None, None],
        [[5.0, -5.0], [5.0, -5.0], [5.0, -5.0], [5.0, -5.0], [5.0, -5.0]],
    )
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) == {}
    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:
Exemple #18
0
class MOTPEMultiObjectiveSampler(BaseMultiObjectiveSampler):
    """Multi-objective sampler using the MOTPE algorithm.

    This sampler is a multiobjective version of :class:`~optuna.samplers.TPESampler`.

    For further information about MOTPE algorithm, please refer to the following paper:

    - `Multiobjective tree-structured parzen estimator for computationally expensive optimization
      problems <https://dl.acm.org/doi/abs/10.1145/3377930.3389817>`_

    Args:
        consider_prior:
            Enhance the stability of Parzen estimator by imposing a Gaussian prior when
            :obj:`True`. The prior is only effective if the sampling distribution is
            either :class:`~optuna.distributions.UniformDistribution`,
            :class:`~optuna.distributions.DiscreteUniformDistribution`,
            :class:`~optuna.distributions.LogUniformDistribution`,
            :class:`~optuna.distributions.IntUniformDistribution`,
            or :class:`~optuna.distributions.IntLogUniformDistribution`.
        prior_weight:
            The weight of the prior. This argument is used in
            :class:`~optuna.distributions.UniformDistribution`,
            :class:`~optuna.distributions.DiscreteUniformDistribution`,
            :class:`~optuna.distributions.LogUniformDistribution`,
            :class:`~optuna.distributions.IntUniformDistribution`,
            :class:`~optuna.distributions.IntLogUniformDistribution`, and
            :class:`~optuna.distributions.CategoricalDistribution`.
        consider_magic_clip:
            Enable a heuristic to limit the smallest variances of Gaussians used in
            the Parzen estimator.
        consider_endpoints:
            Take endpoints of domains into account when calculating variances of Gaussians
            in Parzen estimator. See the original paper for details on the heuristics
            to calculate the variances.
        n_startup_trials:
            The random sampling is used instead of the MOTPE algorithm until the given number
            of trials finish in the same study. 11 * number of variables - 1 is recommended in the
            original paper.
        n_ehvi_candidates:
            Number of candidate samples used to calculate the expected hypervolume improvement.
        gamma:
            A function that takes the number of finished trials and returns the number of trials to
            form a density function for samples with low grains. See the original paper for more
            details.
        weights_above:
            A function that takes the number of finished trials and returns a weight for them. As
            default, weights are automatically calculated by the MOTPE's default strategy.
        seed:
            Seed for random number generator.

    .. note::
        Initialization with Latin hypercube sampling may improve optimization performance.
        However, the current implementation only supports initialization with random sampling.

    Example:

        .. testcode::

            import optuna

            seed = 128
            num_variables = 9
            n_startup_trials = 11 * num_variables - 1


            def objective(trial):
                x = []
                for i in range(1, num_variables + 1):
                    x.append(trial.suggest_float(f"x{i}", 0.0, 2.0 * i))
                return x


            sampler = optuna.multi_objective.samplers.MOTPEMultiObjectiveSampler(
                n_startup_trials=n_startup_trials, n_ehvi_candidates=24, seed=seed
            )
            study = optuna.multi_objective.create_study(
                ["minimize"] * num_variables, sampler=sampler
            )
            study.optimize(objective, n_trials=250)
    """
    def __init__(
        self,
        consider_prior: bool = True,
        prior_weight: float = 1.0,
        consider_magic_clip: bool = True,
        consider_endpoints: bool = True,
        n_startup_trials: int = 10,
        n_ehvi_candidates: int = 24,
        gamma: Callable[[int], int] = default_gamma,
        weights_above: Callable[[int], np.ndarray] = _default_weights_above,
        seed: Optional[int] = None,
    ) -> None:

        with warnings.catch_warnings():
            warnings.simplefilter("ignore", ExperimentalWarning)
            self._motpe_sampler = MOTPESampler(
                consider_prior=consider_prior,
                prior_weight=prior_weight,
                consider_magic_clip=consider_magic_clip,
                consider_endpoints=consider_endpoints,
                n_startup_trials=n_startup_trials,
                n_ehvi_candidates=n_ehvi_candidates,
                gamma=gamma,
                weights_above=weights_above,
                seed=seed,
            )

    def reseed_rng(self) -> None:
        self._motpe_sampler.reseed_rng()

    def infer_relative_search_space(
        self,
        study: "multi_objective.study.MultiObjectiveStudy",
        trial: "multi_objective.trial.FrozenMultiObjectiveTrial",
    ) -> Dict[str, BaseDistribution]:
        return {}

    def sample_relative(
        self,
        study: "multi_objective.study.MultiObjectiveStudy",
        trial: "multi_objective.trial.FrozenMultiObjectiveTrial",
        search_space: Dict[str, BaseDistribution],
    ) -> Dict[str, Any]:
        return {}

    def sample_independent(
        self,
        study: "multi_objective.study.MultiObjectiveStudy",
        trial: "multi_objective.trial.FrozenMultiObjectiveTrial",
        param_name: str,
        param_distribution: BaseDistribution,
    ) -> Any:

        return self._motpe_sampler.sample_independent(_create_study(study),
                                                      _create_trial(trial),
                                                      param_name,
                                                      param_distribution)
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_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, {}) == {}