# Buggy code
def _restore_or_init_optimizer(
    self,
    completed_trials: "List[optuna.trial.FrozenTrial]",
    search_space: Dict[str, BaseDistribution],
    ordered_keys: List[str],
) -> CMA:
    # Restore a previous CMA object.
    for trial in reversed(completed_trials):
        serialized_optimizer = trial.system_attrs.get("cma:optimizer", None)  # type: Optional[str]
        if serialized_optimizer is None:
            continue
        return pickle.loads(bytes.fromhex(serialized_optimizer))

    # Init a CMA object.
    if self._x0 is None:
        self._x0 = _initialize_x0(search_space)

    if self._sigma0 is None:
        sigma0 = _initialize_sigma0(search_space)
    else:
        sigma0 = self._sigma0
    sigma0 = max(sigma0, _MIN_SIGMA0)
    mean = np.array([self._x0[k] for k in ordered_keys])
    bounds = _get_search_space_bound(ordered_keys, search_space)
    n_dimension = len(ordered_keys)
    return CMA(
        mean=mean,
        sigma=sigma0,
        bounds=bounds,
        seed=self._cma_rng.randint(1, 2**32),
        n_max_resampling=10 * n_dimension,
    )


# Traceback (ValueError)
Traceback (most recent call last):

File "C:\Users\User\work\untitled0.py", line 10, in <module>
study.optimize(objective, n_trials=20)

File "C:\Users\User\Anaconda3\envs\base_clone\lib\site-packages\optuna\study.py", line 334, in optimize
func, n_trials, timeout, catch, callbacks, gc_after_trial, None

File "C:\Users\User\Anaconda3\envs\base_clone\lib\site-packages\optuna\study.py", line 648, in _optimize_sequential
self._run_trial_and_callbacks(func, catch, callbacks, gc_after_trial)

File "C:\Users\User\Anaconda3\envs\base_clone\lib\site-packages\optuna\study.py", line 678, in _run_trial_and_callbacks
trial = self._run_trial(func, catch, gc_after_trial)

File "C:\Users\User\Anaconda3\envs\base_clone\lib\site-packages\optuna\study.py", line 695, in _run_trial
trial = trial_module.Trial(self, trial_id)

File "C:\Users\User\Anaconda3\envs\base_clone\lib\site-packages\optuna\trial.py", line 409, in __init__
self._init_relative_params()

File "C:\Users\User\Anaconda3\envs\base_clone\lib\site-packages\optuna\trial.py", line 420, in _init_relative_params
self.study, trial, self.relative_search_space

File "C:\Users\User\Anaconda3\envs\base_clone\lib\site-packages\optuna\samplers\cmaes.py", line 175, in sample_relative
optimizer = self._restore_or_init_optimizer(completed_trials, search_space, ordered_keys)

File "C:\Users\User\Anaconda3\envs\base_clone\lib\site-packages\optuna\samplers\cmaes.py", line 251, in _restore_or_init_optimizer
seed=self._cma_rng.randint(1, 2 ** 32),

File "mtrand.pyx", line 745, in numpy.random.mtrand.RandomState.randint

File "_bounded_integers.pyx", line 1360, in numpy.random._bounded_integers._rand_int32

ValueError: high is out of bounds for int32

# Fixed code
def _restore_or_init_optimizer(
    self,
    completed_trials: "List[optuna.trial.FrozenTrial]",
    search_space: Dict[str, BaseDistribution],
    ordered_keys: List[str],
) -> CMA:
    # Restore a previous CMA object.
    for trial in reversed(completed_trials):
        serialized_optimizer = trial.system_attrs.get("cma:optimizer", None)  # type: Optional[str]
        if serialized_optimizer is None:
            continue
        return pickle.loads(bytes.fromhex(serialized_optimizer))

    # Init a CMA object.
    if self._x0 is None:
        self._x0 = _initialize_x0(search_space)

    if self._sigma0 is None:
        sigma0 = _initialize_sigma0(search_space)
    else:
        sigma0 = self._sigma0
    sigma0 = max(sigma0, _MIN_SIGMA0)
    mean = np.array([self._x0[k] for k in ordered_keys])
    bounds = _get_search_space_bound(ordered_keys, search_space)
    n_dimension = len(ordered_keys)
    return CMA(
        mean=mean,
        sigma=sigma0,
        bounds=bounds,
        seed=self._cma_rng.randint(1, 2**31 - 2),
        n_max_resampling=10 * n_dimension,
    )