1
|
|
|
"""Test module for memory warm start functionality.""" |
2
|
|
|
|
3
|
|
|
import sys |
4
|
|
|
|
5
|
|
|
import numpy as np |
6
|
|
|
import pytest |
7
|
|
|
|
8
|
|
|
from hyperactive import Hyperactive |
9
|
|
|
|
10
|
|
|
if sys.platform.startswith("win"): |
11
|
|
|
pytest.skip("skip these tests for windows", allow_module_level=True) |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
def func1(): |
15
|
|
|
"""Test function 1 for search space.""" |
16
|
|
|
pass |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
def func2(): |
20
|
|
|
"""Test function 2 for search space.""" |
21
|
|
|
pass |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
class class1: |
25
|
|
|
"""Test class for search space functionality.""" |
26
|
|
|
|
27
|
|
|
def __init__(self): |
28
|
|
|
pass |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
class class2: |
32
|
|
|
"""Test class for search space functionality.""" |
33
|
|
|
|
34
|
|
|
def __init__(self): |
35
|
|
|
pass |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
def class_f1(): |
39
|
|
|
"""Return class1 for search space.""" |
40
|
|
|
return class1 |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
def class_f2(): |
44
|
|
|
"""Return class2 for search space.""" |
45
|
|
|
return class2 |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
def numpy_f1(): |
49
|
|
|
"""Return numpy array [0, 1] for search space.""" |
50
|
|
|
return np.array([0, 1]) |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
def numpy_f2(): |
54
|
|
|
"""Return numpy array [1, 0] for search space.""" |
55
|
|
|
return np.array([1, 0]) |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
search_space = { |
59
|
|
|
"x0": list(range(-3, 3)), |
60
|
|
|
"x1": list(np.arange(-1, 1, 0.001)), |
61
|
|
|
"string0": ["str0", "str1"], |
62
|
|
|
"function0": [func1, func2], |
63
|
|
|
"class0": [class_f1, class_f2], |
64
|
|
|
"numpy0": [numpy_f1, numpy_f2], |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
def objective_function(opt): |
69
|
|
|
"""Return simple quadratic objective function for testing.""" |
70
|
|
|
score = -opt["x1"] * opt["x1"] |
71
|
|
|
return score |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
def test_memory_warm_start_0(): |
75
|
|
|
"""Test memory warm start from single job to single job.""" |
76
|
|
|
hyper0 = Hyperactive() |
77
|
|
|
hyper0.add_search(objective_function, search_space, n_iter=15) |
78
|
|
|
hyper0.run() |
79
|
|
|
|
80
|
|
|
search_data0 = hyper0.search_data(objective_function) |
81
|
|
|
|
82
|
|
|
hyper1 = Hyperactive() |
83
|
|
|
hyper1.add_search( |
84
|
|
|
objective_function, |
85
|
|
|
search_space, |
86
|
|
|
n_iter=15, |
87
|
|
|
memory_warm_start=search_data0, |
88
|
|
|
) |
89
|
|
|
hyper1.run() |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
def test_memory_warm_start_1(): |
93
|
|
|
"""Test memory warm start from multi-job to single job.""" |
94
|
|
|
hyper0 = Hyperactive(distribution="pathos") |
95
|
|
|
hyper0.add_search(objective_function, search_space, n_iter=15, n_jobs=2) |
96
|
|
|
hyper0.run() |
97
|
|
|
|
98
|
|
|
search_data0 = hyper0.search_data(objective_function) |
99
|
|
|
|
100
|
|
|
hyper1 = Hyperactive() |
101
|
|
|
hyper1.add_search( |
102
|
|
|
objective_function, |
103
|
|
|
search_space, |
104
|
|
|
n_iter=15, |
105
|
|
|
memory_warm_start=search_data0, |
106
|
|
|
) |
107
|
|
|
hyper1.run() |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
def test_memory_warm_start_2(): |
111
|
|
|
"""Test memory warm start from single job to multi-job.""" |
112
|
|
|
hyper0 = Hyperactive() |
113
|
|
|
hyper0.add_search(objective_function, search_space, n_iter=15) |
114
|
|
|
hyper0.run() |
115
|
|
|
|
116
|
|
|
search_data0 = hyper0.search_data(objective_function) |
117
|
|
|
|
118
|
|
|
hyper1 = Hyperactive(distribution="pathos") |
119
|
|
|
hyper1.add_search( |
120
|
|
|
objective_function, |
121
|
|
|
search_space, |
122
|
|
|
n_iter=15, |
123
|
|
|
n_jobs=2, |
124
|
|
|
memory_warm_start=search_data0, |
125
|
|
|
) |
126
|
|
|
hyper1.run() |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
def test_memory_warm_start_3(): |
130
|
|
|
"""Test memory warm start from multi-job to multi-job.""" |
131
|
|
|
hyper0 = Hyperactive(distribution="pathos") |
132
|
|
|
hyper0.add_search(objective_function, search_space, n_iter=15, n_jobs=2) |
133
|
|
|
hyper0.run() |
134
|
|
|
|
135
|
|
|
search_data0 = hyper0.search_data(objective_function) |
136
|
|
|
|
137
|
|
|
hyper1 = Hyperactive(distribution="pathos") |
138
|
|
|
hyper1.add_search( |
139
|
|
|
objective_function, |
140
|
|
|
search_space, |
141
|
|
|
n_iter=15, |
142
|
|
|
n_jobs=2, |
143
|
|
|
memory_warm_start=search_data0, |
144
|
|
|
) |
145
|
|
|
hyper1.run() |
146
|
|
|
|