|
1
|
|
|
import numpy as np |
|
2
|
|
|
from tqdm import tqdm |
|
3
|
|
|
from hyperactive import Hyperactive |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
def objective_function(opt): |
|
7
|
|
|
score = -opt["x1"] * opt["x1"] |
|
8
|
|
|
return score |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
search_space = { |
|
12
|
|
|
"x1": np.arange(-100, 101, 1), |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
def test_n_jobs_0(): |
|
17
|
|
|
hyper = Hyperactive() |
|
18
|
|
|
hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=2) |
|
19
|
|
|
hyper.run() |
|
20
|
|
|
|
|
21
|
|
|
assert len(hyper.results_list) == 2 |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
def test_n_jobs_1(): |
|
25
|
|
|
hyper = Hyperactive() |
|
26
|
|
|
hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=4) |
|
27
|
|
|
hyper.run() |
|
28
|
|
|
|
|
29
|
|
|
assert len(hyper.results_list) == 4 |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
def test_n_jobs_2(): |
|
33
|
|
|
hyper = Hyperactive() |
|
34
|
|
|
hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=8) |
|
35
|
|
|
hyper.run() |
|
36
|
|
|
|
|
37
|
|
|
assert len(hyper.results_list) == 8 |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
def test_n_jobs_3(): |
|
41
|
|
|
hyper = Hyperactive() |
|
42
|
|
|
hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=-1) |
|
43
|
|
|
hyper.run() |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
def test_n_jobs_4(): |
|
47
|
|
|
hyper = Hyperactive() |
|
48
|
|
|
hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=100) |
|
49
|
|
|
hyper.run() |
|
50
|
|
|
|
|
51
|
|
|
assert len(hyper.results_list) == 100 |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
def test_multiprocessing_0(): |
|
55
|
|
|
hyper = Hyperactive(distribution="multiprocessing") |
|
56
|
|
|
hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=2) |
|
57
|
|
|
hyper.run() |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
def test_multiprocessing_1(): |
|
61
|
|
|
hyper = Hyperactive( |
|
62
|
|
|
distribution={ |
|
63
|
|
|
"multiprocessing": { |
|
64
|
|
|
"initializer": tqdm.set_lock, |
|
65
|
|
|
"initargs": (tqdm.get_lock(),), |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
) |
|
69
|
|
|
hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=2) |
|
70
|
|
|
hyper.run() |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
def test_joblib_0(): |
|
74
|
|
|
hyper = Hyperactive(distribution="joblib") |
|
75
|
|
|
hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=2) |
|
76
|
|
|
hyper.run() |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
def test_joblib_1(): |
|
80
|
|
|
from joblib import Parallel, delayed |
|
81
|
|
|
|
|
82
|
|
|
def joblib_wrapper(process_func, search_processes_paras, **kwargs): |
|
83
|
|
|
n_jobs = len(search_processes_paras) |
|
84
|
|
|
|
|
85
|
|
|
jobs = [ |
|
86
|
|
|
delayed(process_func)(**info_dict) for info_dict in search_processes_paras |
|
87
|
|
|
] |
|
88
|
|
|
results = Parallel(n_jobs=n_jobs, **kwargs)(jobs) |
|
89
|
|
|
|
|
90
|
|
|
return results |
|
91
|
|
|
|
|
92
|
|
|
hyper = Hyperactive(distribution=joblib_wrapper) |
|
93
|
|
|
hyper.add_search(objective_function, search_space, n_iter=15, n_jobs=2) |
|
94
|
|
|
|
|
95
|
|
|
hyper.run() |
|
96
|
|
|
|