forked from bayesian-optimization/BayesianOptimization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_util.py
177 lines (134 loc) · 4.4 KB
/
test_util.py
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import pytest
import numpy as np
from bayes_opt import BayesianOptimization
from bayes_opt.util import UtilityFunction, Colours
from bayes_opt.util import acq_max, load_logs, ensure_rng
from sklearn.gaussian_process.kernels import Matern
from sklearn.gaussian_process import GaussianProcessRegressor
def get_globals():
X = np.array([
[0.00, 0.00],
[0.99, 0.99],
[0.00, 0.99],
[0.99, 0.00],
[0.50, 0.50],
[0.25, 0.50],
[0.50, 0.25],
[0.75, 0.50],
[0.50, 0.75],
])
def get_y(X):
return -(X[:, 0] - 0.3) ** 2 - 0.5 * (X[:, 1] - 0.6)**2 + 2
y = get_y(X)
mesh = np.dstack(
np.meshgrid(np.arange(0, 1, 0.005), np.arange(0, 1, 0.005))
).reshape(-1, 2)
GP = GaussianProcessRegressor(
kernel=Matern(),
n_restarts_optimizer=25,
)
GP.fit(X, y)
return {'x': X, 'y': y, 'gp': GP, 'mesh': mesh}
def brute_force_maximum(MESH, GP, kind='ucb', kappa=1.0, xi=1.0):
uf = UtilityFunction(kind=kind, kappa=kappa, xi=xi)
mesh_vals = uf.utility(MESH, GP, 2)
max_val = mesh_vals.max()
max_arg_val = MESH[np.argmax(mesh_vals)]
return max_val, max_arg_val
GLOB = get_globals()
X, Y, GP, MESH = GLOB['x'], GLOB['y'], GLOB['gp'], GLOB['mesh']
def test_utility_fucntion():
util = UtilityFunction(kind="ucb", kappa=1.0, xi=1.0)
assert util.kind == "ucb"
util = UtilityFunction(kind="ei", kappa=1.0, xi=1.0)
assert util.kind == "ei"
util = UtilityFunction(kind="poi", kappa=1.0, xi=1.0)
assert util.kind == "poi"
with pytest.raises(NotImplementedError):
util = UtilityFunction(kind="other", kappa=1.0, xi=1.0)
def test_acq_with_ucb():
util = UtilityFunction(kind="ucb", kappa=1.0, xi=1.0)
episilon = 1e-2
y_max = 2.0
max_arg = acq_max(
util.utility,
GP,
y_max,
bounds=np.array([[0, 1], [0, 1]]),
random_state=ensure_rng(0),
n_iter=20
)
_, brute_max_arg = brute_force_maximum(MESH, GP, kind='ucb', kappa=1.0, xi=1.0)
assert all(abs(brute_max_arg - max_arg) < episilon)
def test_acq_with_ei():
util = UtilityFunction(kind="ei", kappa=1.0, xi=1e-6)
episilon = 1e-2
y_max = 2.0
max_arg = acq_max(
util.utility,
GP,
y_max,
bounds=np.array([[0, 1], [0, 1]]),
random_state=ensure_rng(0),
n_iter=200,
)
_, brute_max_arg = brute_force_maximum(MESH, GP, kind='ei', kappa=1.0, xi=1e-6)
assert all(abs(brute_max_arg - max_arg) < episilon)
def test_acq_with_poi():
util = UtilityFunction(kind="poi", kappa=1.0, xi=1e-4)
episilon = 1e-2
y_max = 2.0
max_arg = acq_max(
util.utility,
GP,
y_max,
bounds=np.array([[0, 1], [0, 1]]),
random_state=ensure_rng(0),
n_iter=200,
)
_, brute_max_arg = brute_force_maximum(MESH, GP, kind='poi', kappa=1.0, xi=1e-4)
assert all(abs(brute_max_arg - max_arg) < episilon)
def test_logs():
import pytest
def f(x, y):
return -x ** 2 - (y - 1) ** 2 + 1
optimizer = BayesianOptimization(
f=f,
pbounds={"x": (-2, 2), "y": (-2, 2)}
)
assert len(optimizer.space) == 0
load_logs(optimizer, "./tests/test_logs.json")
assert len(optimizer.space) == 5
load_logs(optimizer, ["./tests/test_logs.json"])
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, ["./tests/test_logs.json"])
def test_colours():
colour_wrappers = [
(Colours.END, Colours.black),
(Colours.BLUE, Colours.blue),
(Colours.BOLD, Colours.bold),
(Colours.CYAN, Colours.cyan),
(Colours.DARKCYAN, Colours.darkcyan),
(Colours.GREEN, Colours.green),
(Colours.PURPLE, Colours.purple),
(Colours.RED, Colours.red),
(Colours.UNDERLINE, Colours.underline),
(Colours.YELLOW, Colours.yellow),
]
for colour, wrapper in colour_wrappers:
text1 = Colours._wrap_colour("test", colour)
text2 = wrapper("test")
assert text1.split("test") == [colour, Colours.END]
assert text2.split("test") == [colour, Colours.END]
if __name__ == '__main__':
r"""
CommandLine:
python tests/test_target_space.py
"""
import pytest
pytest.main([__file__])