1
|
|
|
import numpy as np |
2
|
|
|
|
3
|
|
|
from hyperactive import Hyperactive |
4
|
|
|
from hyperactive.optimizers.strategies import CustomOptimizationStrategy |
5
|
|
|
from hyperactive.optimizers import HillClimbingOptimizer, RandomSearchOptimizer |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
def test_constr_opt_0(): |
9
|
|
|
def objective_function(para): |
10
|
|
|
score = -para["x1"] * para["x1"] |
11
|
|
|
return score |
12
|
|
|
|
13
|
|
|
search_space = { |
14
|
|
|
"x1": list(np.arange(-15, 15, 1)), |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
def constraint_1(para): |
18
|
|
|
print(" para", para) |
19
|
|
|
|
20
|
|
|
return para["x1"] > -5 |
21
|
|
|
|
22
|
|
|
constraints_list = [constraint_1] |
23
|
|
|
|
24
|
|
|
optimizer1 = RandomSearchOptimizer() |
25
|
|
|
optimizer2 = HillClimbingOptimizer() |
26
|
|
|
|
27
|
|
|
opt_strat = CustomOptimizationStrategy() |
28
|
|
|
opt_strat.add_optimizer(optimizer1, duration=0.7) |
29
|
|
|
opt_strat.add_optimizer(optimizer2, duration=0.3) |
30
|
|
|
|
31
|
|
|
hyper = Hyperactive() |
32
|
|
|
hyper.add_search( |
33
|
|
|
objective_function, |
34
|
|
|
search_space, |
35
|
|
|
n_iter=50, |
36
|
|
|
constraints=constraints_list, |
37
|
|
|
optimizer=opt_strat, |
38
|
|
|
) |
39
|
|
|
hyper.run() |
40
|
|
|
|
41
|
|
|
search_data = hyper.search_data(objective_function) |
42
|
|
|
x0_values = search_data["x1"].values |
43
|
|
|
|
44
|
|
|
print("\n search_data \n", search_data, "\n") |
45
|
|
|
|
46
|
|
|
assert np.all(x0_values > -5) |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
def test_constr_opt_1(): |
50
|
|
|
def objective_function(para): |
51
|
|
|
score = -(para["x1"] * para["x1"] + para["x2"] * para["x2"]) |
52
|
|
|
return score |
53
|
|
|
|
54
|
|
|
search_space = { |
55
|
|
|
"x1": list(np.arange(-10, 10, 1)), |
56
|
|
|
"x2": list(np.arange(-10, 10, 1)), |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
def constraint_1(para): |
60
|
|
|
return para["x1"] > -5 |
61
|
|
|
|
62
|
|
|
constraints_list = [constraint_1] |
63
|
|
|
|
64
|
|
|
optimizer1 = RandomSearchOptimizer() |
65
|
|
|
optimizer2 = HillClimbingOptimizer() |
66
|
|
|
|
67
|
|
|
opt_strat = CustomOptimizationStrategy() |
68
|
|
|
opt_strat.add_optimizer(optimizer1, duration=0.7) |
69
|
|
|
opt_strat.add_optimizer(optimizer2, duration=0.3) |
70
|
|
|
|
71
|
|
|
hyper = Hyperactive() |
72
|
|
|
hyper.add_search( |
73
|
|
|
objective_function, |
74
|
|
|
search_space, |
75
|
|
|
n_iter=50, |
76
|
|
|
constraints=constraints_list, |
77
|
|
|
optimizer=opt_strat, |
78
|
|
|
) |
79
|
|
|
hyper.run() |
80
|
|
|
|
81
|
|
|
search_data = hyper.search_data(objective_function) |
82
|
|
|
x0_values = search_data["x1"].values |
83
|
|
|
|
84
|
|
|
print("\n search_data \n", search_data, "\n") |
85
|
|
|
|
86
|
|
|
assert np.all(x0_values > -5) |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
def test_constr_opt_2(): |
90
|
|
|
n_iter = 50 |
91
|
|
|
|
92
|
|
|
def objective_function(para): |
93
|
|
|
score = -para["x1"] * para["x1"] |
94
|
|
|
return score |
95
|
|
|
|
96
|
|
|
search_space = { |
97
|
|
|
"x1": list(np.arange(-10, 10, 0.1)), |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
def constraint_1(para): |
101
|
|
|
return para["x1"] > -5 |
102
|
|
|
|
103
|
|
|
def constraint_2(para): |
104
|
|
|
return para["x1"] < 5 |
105
|
|
|
|
106
|
|
|
constraints_list = [constraint_1, constraint_2] |
107
|
|
|
|
108
|
|
|
optimizer1 = RandomSearchOptimizer() |
109
|
|
|
optimizer2 = HillClimbingOptimizer() |
110
|
|
|
|
111
|
|
|
opt_strat = CustomOptimizationStrategy() |
112
|
|
|
opt_strat.add_optimizer(optimizer1, duration=0.7) |
113
|
|
|
opt_strat.add_optimizer(optimizer2, duration=0.3) |
114
|
|
|
|
115
|
|
|
hyper = Hyperactive() |
116
|
|
|
hyper.add_search( |
117
|
|
|
objective_function, |
118
|
|
|
search_space, |
119
|
|
|
n_iter=50, |
120
|
|
|
constraints=constraints_list, |
121
|
|
|
optimizer=opt_strat, |
122
|
|
|
) |
123
|
|
|
hyper.run() |
124
|
|
|
|
125
|
|
|
search_data = hyper.search_data(objective_function) |
126
|
|
|
x0_values = search_data["x1"].values |
127
|
|
|
|
128
|
|
|
print("\n search_data \n", search_data, "\n") |
129
|
|
|
|
130
|
|
|
assert np.all(x0_values > -5) |
131
|
|
|
assert np.all(x0_values < 5) |
132
|
|
|
|
133
|
|
|
n_current_positions = 0 |
134
|
|
|
n_current_scores = 0 |
135
|
|
|
|
136
|
|
|
n_best_positions = 0 |
137
|
|
|
n_best_scores = 0 |
138
|
|
|
|
139
|
|
|
for optimizer_setup in list(hyper.opt_pros.values())[0].optimizer_setup_l: |
140
|
|
|
optimizer = optimizer_setup["optimizer"].gfo_optimizer |
141
|
|
|
duration = optimizer_setup["duration"] |
142
|
|
|
|
143
|
|
|
duration_sum = 1 |
144
|
|
|
n_iter_expected = round(n_iter * duration / duration_sum) |
145
|
|
|
|
146
|
|
|
n_current_positions = n_current_positions + len(optimizer.pos_current_list) |
147
|
|
|
n_current_scores = n_current_scores + len(optimizer.score_current_list) |
148
|
|
|
|
149
|
|
|
n_best_positions = n_best_positions + len(optimizer.pos_best_list) |
150
|
|
|
n_best_scores = n_best_scores + len(optimizer.score_best_list) |
151
|
|
|
|
152
|
|
|
print("\n optimizer", optimizer) |
153
|
|
|
print(" n_new_positions", optimizer.pos_new_list, len(optimizer.pos_new_list)) |
154
|
|
|
print(" n_new_scores", optimizer.score_new_list, len(optimizer.score_new_list)) |
155
|
|
|
print(" n_iter_expected", n_iter_expected) |
156
|
|
|
|
157
|
|
|
assert len(optimizer.pos_new_list) == n_iter_expected |
158
|
|
|
assert len(optimizer.score_new_list) == n_iter_expected |
159
|
|
|
|