1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
from __future__ import annotations
from pathlib import Path
import numpy as np
import pytest
from scipy.optimize import NonlinearConstraint
from bayes_opt import BayesianOptimization
from bayes_opt.util import load_logs
test_dir = Path(__file__).parent.resolve()
def test_logs():
def f(x, y):
return -(x**2) - (y - 1) ** 2 + 1
optimizer = BayesianOptimization(f=f, pbounds={"x": (-200, 200), "y": (-200, 200)})
assert len(optimizer.space) == 0
load_logs(optimizer, [str(test_dir / "test_logs.log")])
assert len(optimizer.space) == 5
load_logs(optimizer, [str(test_dir / "test_logs.log")])
assert len(optimizer.space) == 5
other_optimizer = BayesianOptimization(f=lambda x: -(x**2), pbounds={"x": (-2, 2)})
with pytest.raises(ValueError):
load_logs(other_optimizer, [str(test_dir / "test_logs.log")])
def test_logs_str():
def f(x, y):
return -(x**2) - (y - 1) ** 2 + 1
optimizer = BayesianOptimization(f=f, pbounds={"x": (-200, 200), "y": (-200, 200)})
assert len(optimizer.space) == 0
load_logs(optimizer, str(test_dir / "test_logs.log"))
assert len(optimizer.space) == 5
def test_logs_bounds():
def f(x, y):
return x + y
optimizer = BayesianOptimization(f=f, pbounds={"x": (-2, 2), "y": (-2, 2)})
with pytest.warns(UserWarning):
load_logs(optimizer, [str(test_dir / "test_logs_bounds.log")])
assert len(optimizer.space) == 5
def test_logs_constraint():
def f(x, y):
return -(x**2) - (y - 1) ** 2 + 1
def c(x, y):
return x**2 + y**2
constraint = NonlinearConstraint(c, -np.inf, 3)
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")])
load_logs(optimizer, [str(test_dir / "test_logs_constrained.log")])
assert len(optimizer.space) == 7
def test_logs_constraint_new_array():
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:
python tests/test_target_space.py
"""
pytest.main([__file__])
|