Skip to content

Add logging with state to Bayesian Optimizer #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
58f48d3
Add functionality to save and load state of the BayesianOptimization
adrianmolzon Jan 6, 2025
71036c6
Update basic-tour with new save and load functionality
adrianmolzon Jan 6, 2025
84d036d
move load stateful path to optional argument in class instantiation
adrianmolzon Jan 28, 2025
6ae61d9
add test for string params, update tests with new load functionality
adrianmolzon Jan 28, 2025
7be3854
updated basic tour with updated paths
adrianmolzon Jan 28, 2025
2b514aa
add the random state to the set of things to list of saved items
adrianmolzon Feb 6, 2025
be73262
move state loading to separate function, add functionality for saving…
adrianmolzon Feb 6, 2025
ad2c822
use new loading schema
adrianmolzon Feb 6, 2025
aa86e2f
update tests, add integration tests for saving and loading acquisitio…
adrianmolzon Feb 6, 2025
c26504e
undo abstractmethod implementation for get and set state saving funct…
adrianmolzon Feb 6, 2025
4aab0b8
reorganize state saving and loading for consistency
adrianmolzon Feb 10, 2025
14ea11a
move integration tests into acquisition
adrianmolzon Feb 10, 2025
a782960
remove unndecessary test, add tests for domain reduction and custom p…
adrianmolzon Feb 10, 2025
b209c64
make test more comprehensive
adrianmolzon Feb 10, 2025
79b701c
add test logs
adrianmolzon Feb 10, 2025
7d6b9d6
sync execution counts from basic tour
adrianmolzon Feb 17, 2025
ab765b4
linting, whitespace removal, import structuring
adrianmolzon Mar 5, 2025
c2ea551
ruff fix for string literal in error message
adrianmolzon Mar 5, 2025
c21c6c6
fix ruff complaints
adrianmolzon Mar 5, 2025
57092d9
make all side param comparisons almost equal to account for slight nu…
adrianmolzon Mar 5, 2025
9e57bff
reformat array comparison check
adrianmolzon Mar 5, 2025
289a0d5
upgrade poetry2.0 & apply pep621 (#545)
phi-friday Feb 27, 2025
d0ef58a
Fix coverage report (#552)
till-m Mar 9, 2025
a68d727
remove unnecessary files, have acquisition baseclass functions raise …
adrianmolzon Mar 9, 2025
3d3e538
remove duplicate acquisition functions random state
adrianmolzon Mar 9, 2025
cf87c7b
ruff format
adrianmolzon Mar 9, 2025
b5ae882
add type hints for base acquisition get/set functions
adrianmolzon Mar 10, 2025
02d2643
remove noreturn
adrianmolzon Mar 10, 2025
2f8ab64
remove former saving functionality from notebooks
adrianmolzon Mar 10, 2025
413f467
increase legibility of custom acquisition example
adrianmolzon Mar 10, 2025
472fd93
explicitly stating the optionality of the saving and loading in custo…
adrianmolzon Mar 10, 2025
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
Prev Previous commit
Next Next commit
remove duplicate acquisition functions random state
  • Loading branch information
adrianmolzon committed Mar 9, 2025
commit 3d3e53819d94fc08a6bbc6f14c9706d2d6539375
10 changes: 4 additions & 6 deletions bayes_opt/acquisition.py
Original file line number Diff line number Diff line change
Expand Up @@ -1221,8 +1221,7 @@ def get_acquisition_params(self) -> dict:
"previous_candidates": self.previous_candidates.tolist()
if self.previous_candidates is not None
else None,
"random_states": [acq._serialize_random_state() for acq in self.base_acquisitions]
+ [self._serialize_random_state()],
"gphedge_random_state": self._serialize_random_state(),
}

def set_acquisition_params(self, params: dict) -> None:
Expand All @@ -1233,15 +1232,14 @@ def set_acquisition_params(self, params: dict) -> None:
params : dict
Dictionary containing the acquisition function parameters.
"""
for acq, acq_params, random_state in zip(
self.base_acquisitions, params["base_acquisitions_params"], params["random_states"][:-1]
for acq, acq_params in zip(
self.base_acquisitions, params["base_acquisitions_params"]
):
acq.set_acquisition_params(acq_params)
acq._deserialize_random_state(random_state)

self.gains = np.array(params["gains"])
self.previous_candidates = (
np.array(params["previous_candidates"]) if params["previous_candidates"] is not None else None
)

self._deserialize_random_state(params["random_states"][-1])
self._deserialize_random_state(params["gphedge_random_state"])
42 changes: 42 additions & 0 deletions tests/test_acquisition.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,3 +596,45 @@ def test_integration_constrained(target_func_x_and_y, pbounds, constraint, tmp_p
new_optimizer.load_state(state_path)

verify_optimizers_match(optimizer, new_optimizer)


def test_custom_acquisition_without_get_params():
"""Test that a custom acquisition function without get_acquisition_params raises NotImplementedError."""

class CustomAcqWithoutGetParams(acquisition.AcquisitionFunction):
def __init__(self, random_state=None):
super().__init__(random_state=random_state)

def base_acq(self, mean, std):
return mean + std

def set_acquisition_params(self, params):
pass

acq = CustomAcqWithoutGetParams()
with pytest.raises(
NotImplementedError,
match="Custom AcquisitionFunction subclasses must implement their own get_acquisition_params method",
):
acq.get_acquisition_params()


def test_custom_acquisition_without_set_params():
"""Test that a custom acquisition function without set_acquisition_params raises NotImplementedError."""

class CustomAcqWithoutSetParams(acquisition.AcquisitionFunction):
def __init__(self, random_state=None):
super().__init__(random_state=random_state)

def base_acq(self, mean, std):
return mean + std

def get_acquisition_params(self):
return {}

acq = CustomAcqWithoutSetParams()
with pytest.raises(
NotImplementedError,
match="Custom AcquisitionFunction subclasses must implement their own set_acquisition_params method",
):
acq.set_acquisition_params(params={})