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