File: test_gp_opt.py

package info (click to toggle)
scikit-optimize 0.10.2-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,736 kB
  • sloc: python: 10,668; javascript: 438; makefile: 139; sh: 6
file content (205 lines) | stat: -rw-r--r-- 5,615 bytes parent folder | download | duplicates (2)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import numpy as np
import pytest
from numpy.testing import assert_almost_equal, assert_array_equal

from skopt import gp_minimize
from skopt.benchmarks import bench1, bench2, bench3, bench4, branin
from skopt.space.space import Categorical, Real, Space
from skopt.utils import cook_estimator


def check_minimize(
    func,
    y_opt,
    bounds,
    acq_optimizer,
    acq_func,
    margin,
    n_calls,
    n_initial_points=10,
    init_gen="random",
):
    r = gp_minimize(
        func,
        bounds,
        acq_optimizer=acq_optimizer,
        acq_func=acq_func,
        n_initial_points=n_initial_points,
        n_calls=n_calls,
        random_state=1,
        initial_point_generator=init_gen,
        noise=1e-10,
    )
    assert r.fun < y_opt + margin


SEARCH = ["sampling", "lbfgs"]
ACQUISITION = ["LCB", "EI"]
INITGEN = ["random", "lhs", "halton", "hammersly", "sobol"]


@pytest.mark.slow_test
@pytest.mark.parametrize("search", SEARCH)
@pytest.mark.parametrize("acq", ACQUISITION)
def test_gp_minimize_bench1(search, acq):
    check_minimize(bench1, 0.0, [(-2.0, 2.0)], search, acq, 0.05, 20)


@pytest.mark.slow_test
@pytest.mark.parametrize("search", ["sampling"])
@pytest.mark.parametrize("acq", ["LCB", "MES"])
@pytest.mark.parametrize("initgen", INITGEN)
def test_gp_minimize_bench1_initgen(search, acq, initgen):
    check_minimize(bench1, 0.0, [(-2.0, 2.0)], search, acq, 0.05, 20, init_gen=initgen)


@pytest.mark.slow_test
@pytest.mark.parametrize("search", SEARCH)
@pytest.mark.parametrize("acq", ACQUISITION)
def test_gp_minimize_bench2(search, acq):
    check_minimize(bench2, -5, [(-6.0, 6.0)], search, acq, 0.05, 20)


@pytest.mark.slow_test
@pytest.mark.parametrize("search", SEARCH)
@pytest.mark.parametrize("acq", ACQUISITION)
def test_gp_minimize_bench3(search, acq):
    check_minimize(bench3, -0.9, [(-2.0, 2.0)], search, acq, 0.05, 20)


@pytest.mark.fast_test
@pytest.mark.parametrize("search", ["sampling"])
@pytest.mark.parametrize("acq", ["LCB", "EI", "MES"])
def test_gp_minimize_bench4(search, acq):
    # this particular random_state picks "2" twice so we can make an extra
    # call to the objective without repeating options
    check_minimize(bench4, 0, [["-2", "-1", "0", "1", "2"]], search, acq, 1.05, 20)


@pytest.mark.fast_test
def test_n_jobs():
    r_single = gp_minimize(
        bench3,
        [(-2.0, 2.0)],
        acq_optimizer="lbfgs",
        acq_func="EI",
        n_calls=4,
        n_initial_points=2,
        random_state=1,
        noise=1e-10,
    )
    r_double = gp_minimize(
        bench3,
        [(-2.0, 2.0)],
        acq_optimizer="lbfgs",
        acq_func="EI",
        n_calls=4,
        n_initial_points=2,
        random_state=1,
        noise=1e-10,
        n_jobs=2,
    )
    assert_array_equal(r_single.x_iters, r_double.x_iters)


@pytest.mark.fast_test
def test_gpr_default():
    """Smoke test that gp_minimize does not fail for default values."""
    gp_minimize(branin, ((-5.0, 10.0), (0.0, 15.0)), n_initial_points=2, n_calls=2)


@pytest.mark.fast_test
def test_use_given_estimator():
    """Test that gp_minimize does not use default estimator if one is passed in
    explicitly."""
    domain = [(1.0, 2.0), (3.0, 4.0)]
    noise_correct = 1e5
    noise_fake = 1e-10
    estimator = cook_estimator("GP", domain, noise=noise_correct)
    res = gp_minimize(
        branin,
        domain,
        n_calls=4,
        n_initial_points=2,
        base_estimator=estimator,
        noise=noise_fake,
    )

    assert res['models'][-1].noise == noise_correct


@pytest.mark.fast_test
def test_use_given_estimator_with_max_model_size():
    """Test that gp_minimize does not use default estimator if one is passed in
    explicitly."""
    domain = [(1.0, 2.0), (3.0, 4.0)]
    noise_correct = 1e5
    noise_fake = 1e-10
    estimator = cook_estimator("GP", domain, noise=noise_correct)
    res = gp_minimize(
        branin,
        domain,
        n_calls=4,
        n_initial_points=2,
        base_estimator=estimator,
        noise=noise_fake,
        model_queue_size=1,
    )
    assert len(res['models']) == 1
    assert res['models'][-1].noise == noise_correct


@pytest.mark.fast_test
def test_categorical_integer():
    def f(params):
        return np.random.uniform()

    dims = [[1]]
    res = gp_minimize(f, dims, n_calls=2, n_initial_points=2, random_state=1)
    assert res.x_iters[0][0] == dims[0][0]


@pytest.mark.parametrize("initgen", INITGEN)
def test_mixed_categoricals(initgen):

    space = Space(
        [
            Categorical(name="x", categories=["1", "2", "3"]),
            Categorical(name="y", categories=[4, 5, 6]),
            Real(name="z", low=1.0, high=5.0),
        ]
    )

    def objective(param_list):
        x = param_list[0]
        y = param_list[1]
        z = param_list[2]
        loss = int(x) + y * z
        return loss

    res = gp_minimize(
        objective, space, n_calls=20, random_state=1, initial_point_generator=initgen
    )
    assert res["x"][:2] in [['1', 4], ['2', 4]]
    assert_almost_equal(res["x"][2], 1.0, decimal=3)


@pytest.mark.parametrize("initgen", INITGEN)
def test_mixed_categoricals2(initgen):
    space = Space(
        [
            Categorical(name="x", categories=["1", "2", "3"]),
            Categorical(name="y", categories=[4, 5, 6]),
        ]
    )

    def objective(param_list):
        x = param_list[0]
        y = param_list[1]
        loss = int(x) + y
        return loss

    res = gp_minimize(
        objective, space, n_calls=12, random_state=1, initial_point_generator=initgen
    )
    assert res["x"] == ['1', 4]