1 | import numpy as np |
||
2 | from hyperactive import Hyperactive |
||
3 | |||
4 | |||
5 | def objective_function(opt): |
||
6 | score = -(opt["x1"] * opt["x1"] + opt["x2"] * opt["x2"]) |
||
7 | return score |
||
8 | |||
9 | |||
10 | search_space = { |
||
11 | "x1": list(np.arange(-1000, 1000, 0.1)), |
||
12 | "x2": list(np.arange(-1000, 1000, 0.1)), |
||
13 | } |
||
14 | |||
15 | |||
16 | err = 0.001 |
||
17 | |||
18 | |||
19 | def test_random_state_n_jobs_0(): |
||
20 | n_jobs = 2 |
||
21 | |||
22 | hyper = Hyperactive() |
||
23 | hyper.add_search( |
||
24 | objective_function, |
||
25 | search_space, |
||
26 | n_iter=5, |
||
27 | initialize={"random": 1}, |
||
28 | random_state=1, |
||
29 | n_jobs=n_jobs, |
||
30 | ) |
||
31 | hyper.run() |
||
32 | |||
33 | results = hyper.search_data(objective_function) |
||
34 | |||
35 | no_dup = results.drop_duplicates(subset=list(search_space.keys())) |
||
36 | print("no_dup", no_dup) |
||
37 | print("results", results) |
||
38 | |||
39 | print(int(len(results) / n_jobs)) |
||
40 | print(len(no_dup)) |
||
41 | |||
42 | assert int(len(results) / n_jobs) != len(no_dup) |
||
43 | |||
44 | |||
45 | View Code Duplication | def test_random_state_n_jobs_1(): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
46 | n_jobs = 3 |
||
47 | |||
48 | hyper = Hyperactive() |
||
49 | hyper.add_search( |
||
50 | objective_function, |
||
51 | search_space, |
||
52 | n_iter=5, |
||
53 | initialize={"random": 1}, |
||
54 | random_state=1, |
||
55 | n_jobs=n_jobs, |
||
56 | ) |
||
57 | hyper.run() |
||
58 | |||
59 | results = hyper.search_data(objective_function) |
||
60 | |||
61 | no_dup = results.drop_duplicates(subset=list(search_space.keys())) |
||
62 | print("no_dup", no_dup) |
||
63 | print("results", results) |
||
64 | |||
65 | assert int(len(results) / n_jobs) != len(no_dup) |
||
66 | |||
67 | |||
68 | View Code Duplication | def test_random_state_n_jobs_2(): |
|
0 ignored issues
–
show
|
|||
69 | n_jobs = 4 |
||
70 | |||
71 | hyper = Hyperactive() |
||
72 | hyper.add_search( |
||
73 | objective_function, |
||
74 | search_space, |
||
75 | n_iter=5, |
||
76 | initialize={"random": 1}, |
||
77 | random_state=1, |
||
78 | n_jobs=n_jobs, |
||
79 | ) |
||
80 | hyper.run() |
||
81 | |||
82 | results = hyper.search_data(objective_function) |
||
83 | |||
84 | no_dup = results.drop_duplicates(subset=list(search_space.keys())) |
||
85 | print("no_dup", no_dup) |
||
86 | print("results", results) |
||
87 | |||
88 | assert int(len(results) / n_jobs) != len(no_dup) |
||
89 | |||
90 | |||
91 | View Code Duplication | def test_random_state_0(): |
|
0 ignored issues
–
show
|
|||
92 | hyper0 = Hyperactive() |
||
93 | hyper0.add_search( |
||
94 | objective_function, |
||
95 | search_space, |
||
96 | n_iter=10, |
||
97 | initialize={"random": 1}, |
||
98 | random_state=1, |
||
99 | ) |
||
100 | hyper0.run() |
||
101 | |||
102 | hyper1 = Hyperactive() |
||
103 | hyper1.add_search( |
||
104 | objective_function, |
||
105 | search_space, |
||
106 | n_iter=10, |
||
107 | initialize={"random": 1}, |
||
108 | random_state=1, |
||
109 | ) |
||
110 | hyper1.run() |
||
111 | |||
112 | best_score0 = hyper0.best_score(objective_function) |
||
113 | best_score1 = hyper1.best_score(objective_function) |
||
114 | |||
115 | assert abs(best_score0 - best_score1) < err |
||
116 | |||
117 | |||
118 | View Code Duplication | def test_random_state_1(): |
|
0 ignored issues
–
show
|
|||
119 | hyper0 = Hyperactive() |
||
120 | hyper0.add_search( |
||
121 | objective_function, |
||
122 | search_space, |
||
123 | n_iter=10, |
||
124 | initialize={"random": 1}, |
||
125 | random_state=10, |
||
126 | ) |
||
127 | hyper0.run() |
||
128 | |||
129 | hyper1 = Hyperactive() |
||
130 | hyper1.add_search( |
||
131 | objective_function, |
||
132 | search_space, |
||
133 | n_iter=10, |
||
134 | initialize={"random": 1}, |
||
135 | random_state=10, |
||
136 | ) |
||
137 | hyper1.run() |
||
138 | |||
139 | best_score0 = hyper0.best_score(objective_function) |
||
140 | best_score1 = hyper1.best_score(objective_function) |
||
141 | |||
142 | assert abs(best_score0 - best_score1) < err |
||
143 | |||
144 | |||
145 | View Code Duplication | def test_random_state_2(): |
|
0 ignored issues
–
show
|
|||
146 | hyper0 = Hyperactive() |
||
147 | hyper0.add_search( |
||
148 | objective_function, |
||
149 | search_space, |
||
150 | n_iter=10, |
||
151 | initialize={"random": 1}, |
||
152 | random_state=1, |
||
153 | ) |
||
154 | hyper0.run() |
||
155 | |||
156 | hyper1 = Hyperactive() |
||
157 | hyper1.add_search( |
||
158 | objective_function, |
||
159 | search_space, |
||
160 | n_iter=10, |
||
161 | initialize={"random": 1}, |
||
162 | random_state=10, |
||
163 | ) |
||
164 | hyper1.run() |
||
165 | |||
166 | best_score0 = hyper0.best_score(objective_function) |
||
167 | best_score1 = hyper1.best_score(objective_function) |
||
168 | |||
169 | assert abs(best_score0 - best_score1) > err |
||
170 | |||
171 | |||
172 | def test_no_random_state_0(): |
||
173 | hyper0 = Hyperactive() |
||
174 | hyper0.add_search( |
||
175 | objective_function, |
||
176 | search_space, |
||
177 | n_iter=10, |
||
178 | initialize={"random": 1}, |
||
179 | ) |
||
180 | hyper0.run() |
||
181 | |||
182 | hyper1 = Hyperactive() |
||
183 | hyper1.add_search( |
||
184 | objective_function, |
||
185 | search_space, |
||
186 | n_iter=10, |
||
187 | initialize={"random": 1}, |
||
188 | ) |
||
189 | hyper1.run() |
||
190 | |||
191 | best_score0 = hyper0.best_score(objective_function) |
||
192 | best_score1 = hyper1.best_score(objective_function) |
||
193 | |||
194 | assert abs(best_score0 - best_score1) > err |
||
195 |