1
|
|
|
import numpy as np |
2
|
|
|
import pytest |
3
|
|
|
|
4
|
|
|
from ._parametrize import optimizers |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
@pytest.mark.parametrize(*optimizers) |
8
|
|
|
def test_constr_opt_0(Optimizer): |
9
|
|
|
def objective_function(para): |
10
|
|
|
score = -para["x1"] * para["x1"] |
11
|
|
|
return score |
12
|
|
|
|
13
|
|
|
search_space = { |
14
|
|
|
"x1": np.arange(-15, 15, 1), |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
def constraint_1(para): |
18
|
|
|
return para["x1"] > -5 |
19
|
|
|
|
20
|
|
|
constraints_list = [constraint_1] |
21
|
|
|
|
22
|
|
|
opt = Optimizer(search_space, constraints=constraints_list) |
23
|
|
|
opt.search(objective_function, n_iter=20) |
24
|
|
|
|
25
|
|
|
search_data = opt.search_data |
26
|
|
|
x0_values = search_data["x1"].values |
27
|
|
|
|
28
|
|
|
print("\n search_data \n", search_data, "\n") |
29
|
|
|
|
30
|
|
|
assert np.all(x0_values > -5) |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
@pytest.mark.parametrize(*optimizers) |
34
|
|
|
def test_constr_opt_1(Optimizer): |
35
|
|
|
def objective_function(para): |
36
|
|
|
score = -(para["x1"] * para["x1"] + para["x2"] * para["x2"]) |
37
|
|
|
return score |
38
|
|
|
|
39
|
|
|
search_space = { |
40
|
|
|
"x1": np.arange(-10, 10, 1), |
41
|
|
|
"x2": np.arange(-10, 10, 1), |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
def constraint_1(para): |
45
|
|
|
return para["x1"] > -5 |
46
|
|
|
|
47
|
|
|
constraints_list = [constraint_1] |
48
|
|
|
|
49
|
|
|
opt = Optimizer(search_space, constraints=constraints_list) |
50
|
|
|
opt.search(objective_function, n_iter=50) |
51
|
|
|
|
52
|
|
|
search_data = opt.search_data |
53
|
|
|
x0_values = search_data["x1"].values |
54
|
|
|
|
55
|
|
|
print("\n search_data \n", search_data, "\n") |
56
|
|
|
|
57
|
|
|
assert np.all(x0_values > -5) |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
@pytest.mark.parametrize(*optimizers) |
61
|
|
|
def test_constr_opt_2(Optimizer): |
62
|
|
|
n_iter = 50 |
63
|
|
|
|
64
|
|
|
def objective_function(para): |
65
|
|
|
score = -para["x1"] * para["x1"] |
66
|
|
|
return score |
67
|
|
|
|
68
|
|
|
search_space = { |
69
|
|
|
"x1": np.arange(-10, 10, 0.1), |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
def constraint_1(para): |
73
|
|
|
return para["x1"] > -5 |
74
|
|
|
|
75
|
|
|
def constraint_2(para): |
76
|
|
|
return para["x1"] < 5 |
77
|
|
|
|
78
|
|
|
constraints_list = [constraint_1, constraint_2] |
79
|
|
|
|
80
|
|
|
opt = Optimizer(search_space, constraints=constraints_list) |
81
|
|
|
opt.search(objective_function, n_iter=n_iter) |
82
|
|
|
|
83
|
|
|
search_data = opt.search_data |
84
|
|
|
x0_values = search_data["x1"].values |
85
|
|
|
|
86
|
|
|
print("\n search_data \n", search_data, "\n") |
87
|
|
|
|
88
|
|
|
assert np.all(x0_values > -5) |
89
|
|
|
assert np.all(x0_values < 5) |
90
|
|
|
|
91
|
|
|
n_new_positions = 0 |
92
|
|
|
n_new_scores = 0 |
93
|
|
|
|
94
|
|
|
n_current_positions = 0 |
95
|
|
|
n_current_scores = 0 |
96
|
|
|
|
97
|
|
|
n_best_positions = 0 |
98
|
|
|
n_best_scores = 0 |
99
|
|
|
|
100
|
|
|
for optimizer in opt.optimizers: |
101
|
|
|
n_new_positions = n_new_positions + len(optimizer.pos_new_list) |
102
|
|
|
n_new_scores = n_new_scores + len(optimizer.score_new_list) |
103
|
|
|
|
104
|
|
|
n_current_positions = n_current_positions + len(optimizer.pos_current_list) |
105
|
|
|
n_current_scores = n_current_scores + len(optimizer.score_current_list) |
106
|
|
|
|
107
|
|
|
n_best_positions = n_best_positions + len(optimizer.pos_best_list) |
108
|
|
|
n_best_scores = n_best_scores + len(optimizer.score_best_list) |
109
|
|
|
|
110
|
|
|
print("\n optimizer", optimizer) |
111
|
|
|
print(" n_new_positions", optimizer.pos_new_list) |
112
|
|
|
print(" n_new_scores", optimizer.score_new_list) |
113
|
|
|
|
114
|
|
|
assert n_new_positions == n_iter |
115
|
|
|
assert n_new_scores == n_iter |
116
|
|
|
|
117
|
|
|
assert n_current_positions == n_current_scores |
118
|
|
|
assert n_current_positions <= n_new_positions |
119
|
|
|
|
120
|
|
|
assert n_best_positions == n_best_scores |
121
|
|
|
assert n_best_positions <= n_new_positions |
122
|
|
|
|
123
|
|
|
assert n_new_positions == n_new_scores |
124
|
|
|
|