From 27fc422f77c57e92072aa2cfb6c63682ee024ca9 Mon Sep 17 00:00:00 2001 From: till-m Date: Thu, 20 Jun 2024 16:12:13 +0200 Subject: [PATCH 1/3] Fix 478, fix data out-of-sync issue in target space --- bayes_opt/logger.py | 4 ++++ bayes_opt/target_space.py | 21 +++++++++++++++------ tests/test_util.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/bayes_opt/logger.py b/bayes_opt/logger.py index 84182c184..54b381671 100644 --- a/bayes_opt/logger.py +++ b/bayes_opt/logger.py @@ -2,6 +2,7 @@ from __future__ import print_function import os import json +import numpy as np from .observer import _Tracker from .event import Events from colorama import Fore, just_fix_windows_console @@ -291,6 +292,9 @@ def update(self, event, instance): if "allowed" in data: # fix: github.com/fmfn/BayesianOptimization/issues/361 data["allowed"] = bool(data["allowed"]) + + if "constraint" in data and isinstance(data["constraint"], np.ndarray): + data["constraint"] = data["constraint"].tolist() with open(self._path, "a") as f: f.write(json.dumps(data) + "\n") diff --git a/bayes_opt/target_space.py b/bayes_opt/target_space.py index 89f523dfc..1e7c858ab 100644 --- a/bayes_opt/target_space.py +++ b/bayes_opt/target_space.py @@ -312,21 +312,30 @@ def register(self, params, target, constraint_value=None): if not np.all((self._bounds[:, 0] <= x) & (x <= self._bounds[:, 1])): warn(f'\nData point {x} is outside the bounds of the parameter space. ', stacklevel=2) - self._params = np.concatenate([self._params, x.reshape(1, -1)]) - self._target = np.concatenate([self._target, [target]]) + # Make copies of the data, so as not to modify the originals incase something fails + # during the registration process. This prevents out-of-sync data. + params_copy = np.concatenate([self._params, x.reshape(1, -1)]) + target_copy = np.concatenate([self._target, [target]]) + cache_copy = self._cache.copy() # shallow copy suffices if self._constraint is None: # Insert data into unique dictionary - self._cache[_hashable(x.ravel())] = target + cache_copy[_hashable(x.ravel())] = target else: if constraint_value is None: msg = ("When registering a point to a constrained TargetSpace" + " a constraint value needs to be present.") raise ValueError(msg) # Insert data into unique dictionary - self._cache[_hashable(x.ravel())] = (target, constraint_value) - self._constraint_values = np.concatenate([self._constraint_values, - [constraint_value]]) + cache_copy[_hashable(x.ravel())] = (target, constraint_value) + constraint_values_copy = np.concatenate([self._constraint_values, + [constraint_value]]) + self._constraint_values = constraint_values_copy + + # Operations passed, update the variables + self._params = params_copy + self._target = target_copy + self._cache = cache_copy def probe(self, params): """Evaluate the target function on a point and register the result. diff --git a/tests/test_util.py b/tests/test_util.py index 89b79c01e..b3cce2f23 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -189,6 +189,40 @@ def c(x, y): assert len(optimizer.space) == 7 +def test_logs_constraint(): + + def f(x, y): + return -x ** 2 - (y - 1) ** 2 + 1 + + def c(x, y): + return np.array([ + - np.cos(x) * np.cos(y) + np.sin(x) * np.sin(y), + - np.cos(x) * np.cos(-y) + np.sin(x) * np.sin(-y) + ]) + + constraint_lower = np.array([-np.inf, -np.inf]) + constraint_upper = np.array([0.6, 0.6]) + + constraint = NonlinearConstraint(c, constraint_lower, constraint_upper) + + optimizer = BayesianOptimization( + f=f, + pbounds={"x": (-200, 200), "y": (-200, 200)}, + constraint=constraint + ) + + with pytest.raises(KeyError): + load_logs(optimizer, [str(test_dir / "test_logs.log")]) + + with pytest.raises(ValueError): + load_logs(optimizer, [str(test_dir / "test_logs_constrained.log")]) + + load_logs(optimizer, [str(test_dir / "test_logs_multiple_constraints.log")]) + + print(optimizer.space) + assert len(optimizer.space) == 12 + + if __name__ == '__main__': r""" CommandLine: From 37646c7ac7db6d32fe855d19a8e4839ff2049377 Mon Sep 17 00:00:00 2001 From: till-m Date: Thu, 20 Jun 2024 16:14:33 +0200 Subject: [PATCH 2/3] Add test logs file for multiple constraints --- tests/test_logs_multiple_constraints.log | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 tests/test_logs_multiple_constraints.log diff --git a/tests/test_logs_multiple_constraints.log b/tests/test_logs_multiple_constraints.log new file mode 100644 index 000000000..de68d0030 --- /dev/null +++ b/tests/test_logs_multiple_constraints.log @@ -0,0 +1,12 @@ +{"target": -34660.799399896976, "constraint": [0.2708972584125664, 0.42357209835559106], "params": {"x": -50.183952461055, "y": 180.28572256396646}, "allowed": true, "datetime": {"datetime": "2024-06-20 15:45:44", "elapsed": 0.0, "delta": 0.0}} +{"target": -10089.822899242456, "constraint": [-0.9510813312336377, 0.9973445494788389], "params": {"x": 92.79757672456203, "y": 39.46339367881464}, "allowed": false, "datetime": {"datetime": "2024-06-20 15:45:44", "elapsed": 0.000321, "delta": 0.000321}} +{"target": -10506.089080732436, "constraint": [0.6131701761022414, -0.9564999545661308], "params": {"x": 95.21519271989214, "y": 38.96256255900324}, "allowed": false, "datetime": {"datetime": "2024-06-20 15:45:44", "elapsed": 0.056496, "delta": 0.056175}} +{"target": -997.8139138494062, "constraint": [-0.9916913121401841, -0.6495843128640307], "params": {"x": 18.482175449759072, "y": -24.636362934194256}, "allowed": true, "datetime": {"datetime": "2024-06-20 15:45:44", "elapsed": 0.556892, "delta": 0.500396}} +{"target": -19477.39999548628, "constraint": [0.4566191751114183, 0.10531295488159137], "params": {"x": -30.134980186677655, "y": -135.2728254812192}, "allowed": true, "datetime": {"datetime": "2024-06-20 15:45:45", "elapsed": 1.025239, "delta": 0.468347}} +{"target": -2557.6177973621116, "constraint": [0.9347247196809806, 0.9180512307035334], "params": {"x": -49.8799963378478, "y": 9.402604518743091}, "allowed": false, "datetime": {"datetime": "2024-06-20 15:45:45", "elapsed": 1.359698, "delta": 0.334459}} +{"target": -39604.24318900592, "constraint": [0.8549175086147126, 0.9001717563474866], "params": {"x": -197.96772313017533, "y": -19.347574490837093}, "allowed": false, "datetime": {"datetime": "2024-06-20 15:45:45", "elapsed": 1.595204, "delta": 0.235506}} +{"target": -80400.0, "constraint": [-0.9999999999999999, 0.5252963386425359], "params": {"x": 200.0, "y": -200.0}, "allowed": true, "datetime": {"datetime": "2024-06-20 15:45:45", "elapsed": 1.788137, "delta": 0.192933}} +{"target": -79600.0, "constraint": [0.5252963386425359, -0.9999999999999999], "params": {"x": 200.0, "y": 200.0}, "allowed": true, "datetime": {"datetime": "2024-06-20 15:45:46", "elapsed": 2.146403, "delta": 0.358266}} +{"target": -80400.0, "constraint": [0.5252963386425359, -0.9999999999999999], "params": {"x": -200.0, "y": -200.0}, "allowed": true, "datetime": {"datetime": "2024-06-20 15:45:46", "elapsed": 2.518581, "delta": 0.372178}} +{"target": -79600.0, "constraint": [-0.9999999999999999, 0.5252963386425359], "params": {"x": -200.0, "y": 200.0}, "allowed": true, "datetime": {"datetime": "2024-06-20 15:45:47", "elapsed": 3.028643, "delta": 0.510062}} +{"target": -4290.396951749328, "constraint": [-0.32857141283617775, -0.3643611387948609], "params": {"x": -8.207850284687554, "y": 65.99252376584158}, "allowed": true, "datetime": {"datetime": "2024-06-20 15:45:47", "elapsed": 3.494066, "delta": 0.465423}} From 5fd0d165fba0b1373f5a41039a2bea1503760d3c Mon Sep 17 00:00:00 2001 From: till-m Date: Thu, 20 Jun 2024 16:26:20 +0200 Subject: [PATCH 3/3] Fix an unrelated bug in the visualization script. --- examples/visualization.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/visualization.ipynb b/examples/visualization.ipynb index b64dc1d1a..7a16568c9 100644 --- a/examples/visualization.ipynb +++ b/examples/visualization.ipynb @@ -140,7 +140,7 @@ " steps = len(optimizer.space)\n", " fig.suptitle(\n", " 'Gaussian Process and Utility Function After {} Steps'.format(steps),\n", - " fontdict={'size':30}\n", + " fontsize=30\n", " )\n", " \n", " gs = gridspec.GridSpec(2, 1, height_ratios=[3, 1]) \n",