Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bayes_opt/bayesian_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,10 @@ def _load_state_dict(self, state: dict[str, Any]) -> None:
state : dict
Dictionary containing the optimizer state.
"""
if state["keys"] != self._space.keys:
msg = "Saved state must have the same parameter keys in the same order as the optimizer."
raise ValueError(msg)

params_array = np.asarray(state["params"], dtype=np.float64)
target_array = np.asarray(state["target"], dtype=np.float64)
constraint_array = (
Expand Down
24 changes: 24 additions & 0 deletions tests/test_bayesian_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,30 @@ def test_save_load_state(tmp_path):
np.testing.assert_array_equal(optimizer.space.target, new_optimizer.space.target)


@pytest.mark.parametrize("saved_points", [False, True])
@pytest.mark.parametrize("existing_points", [False, True])
@pytest.mark.parametrize("pbounds", [{"p2": (0, 10), "p1": (0, 10)}, {"width": (0, 10), "height": (0, 10)}])
def test_load_state_rejects_incompatible_keys(tmp_path, saved_points, existing_points, pbounds):
"""Reject incompatible keys without changing results or random-state progression."""
original = BayesianOptimization(target_func, PBOUNDS, random_state=1, verbose=0)
if saved_points:
original.probe({"p1": 1, "p2": 2}, lazy=False)
state_path = tmp_path / "optimizer_state.json"
original.save_state(state_path)

restored = BayesianOptimization(target_func, pbounds, random_state=7, verbose=0)
if existing_points:
restored.probe(dict.fromkeys(pbounds, 0), lazy=False)
before = restored.res

with pytest.raises(ValueError, match="parameter keys.*same order"):
restored.load_state(state_path)

assert restored.res == before
control = BayesianOptimization(target_func, pbounds, random_state=7, verbose=0)
assert restored.random_sample(2) == control.random_sample(2)


def test_save_load_w_categorical_params(tmp_path):
"""Test saving and loading optimizer state with categorical parameters."""

Expand Down