tests.test_search_spaces.test_search_space_0()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 19
Ratio 90.48 %

Importance

Changes 0
Metric Value
eloc 14
dl 19
loc 21
rs 9.7
c 0
b 0
f 0
cc 1
nop 0
1
"""Test module for search space functionality."""
2
3
import sys
4
5
import numpy as np
6
import pandas as pd
7
import pytest
8
9
from hyperactive import Hyperactive
10
11
if sys.platform.startswith("win"):
12
    pytest.skip("skip these tests for windows", allow_module_level=True)
13
14
15 View Code Duplication
def test_search_space_0():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
16
    """Test search space with integer range."""
17
18
    def objective_function(opt):
19
        score = -opt["x1"] * opt["x1"]
20
        return score
21
22
    search_space = {
23
        "x1": list(range(0, 3, 1)),
24
    }
25
26
    hyper = Hyperactive()
27
    hyper.add_search(
28
        objective_function,
29
        search_space,
30
        n_iter=15,
31
    )
32
    hyper.run()
33
34
    assert isinstance(hyper.search_data(objective_function), pd.DataFrame)
35
    assert hyper.best_para(objective_function)["x1"] in search_space["x1"]
36
37
38 View Code Duplication
def test_search_space_1():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
39
    """Test search space with float range."""
40
41
    def objective_function(opt):
42
        score = -opt["x1"] * opt["x1"]
43
        return score
44
45
    search_space = {
46
        "x1": list(np.arange(0, 0.003, 0.001)),
47
    }
48
49
    hyper = Hyperactive()
50
    hyper.add_search(
51
        objective_function,
52
        search_space,
53
        n_iter=15,
54
    )
55
    hyper.run()
56
57
    assert isinstance(hyper.search_data(objective_function), pd.DataFrame)
58
    assert hyper.best_para(objective_function)["x1"] in search_space["x1"]
59
60
61 View Code Duplication
def test_search_space_2():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
62
    """Test search space with numpy float range."""
63
64
    def objective_function(opt):
65
        score = -opt["x1"] * opt["x1"]
66
        return score
67
68
    search_space = {
69
        "x1": list(range(0, 100, 1)),
70
        "str1": ["0", "1", "2"],
71
    }
72
73
    hyper = Hyperactive()
74
    hyper.add_search(
75
        objective_function,
76
        search_space,
77
        n_iter=15,
78
    )
79
    hyper.run()
80
81
    assert isinstance(hyper.search_data(objective_function), pd.DataFrame)
82
    assert hyper.best_para(objective_function)["str1"] in search_space["str1"]
83
84
85
def test_search_space_3():
86
    """Test search space with function objects."""
87
88
    def func1():
89
        pass
90
91
    def func2():
92
        pass
93
94
    def func3():
95
        pass
96
97
    def objective_function(opt):
98
        score = -opt["x1"] * opt["x1"]
99
        return score
100
101
    search_space = {
102
        "x1": list(range(0, 100, 1)),
103
        "func1": [func1, func2, func3],
104
    }
105
106
    hyper = Hyperactive()
107
    hyper.add_search(
108
        objective_function,
109
        search_space,
110
        n_iter=15,
111
    )
112
    hyper.run()
113
114
    assert isinstance(hyper.search_data(objective_function), pd.DataFrame)
115
    assert hyper.best_para(objective_function)["func1"] in search_space["func1"]
116
117
118
def test_search_space_4():
119
    """Test search space with class objects."""
120
121
    class class1:
122
        pass
123
124
    class class2:
125
        pass
126
127
    class class3:
128
        pass
129
130
    def objective_function(opt):
131
        score = -opt["x1"] * opt["x1"]
132
        return score
133
134
    search_space = {
135
        "x1": list(range(0, 100, 1)),
136
        "class1": [class1, class2, class3],
137
    }
138
139
    hyper = Hyperactive()
140
    hyper.add_search(
141
        objective_function,
142
        search_space,
143
        n_iter=15,
144
    )
145
    hyper.run()
146
147
    assert isinstance(hyper.search_data(objective_function), pd.DataFrame)
148
    assert hyper.best_para(objective_function)["class1"] in search_space["class1"]
149
150
151
def test_search_space_5():
152
    """Test search space with initialized class instances."""
153
154
    class class1:
155
        def __init__(self):
156
            pass
157
158
    class class2:
159
        def __init__(self):
160
            pass
161
162
    class class3:
163
        def __init__(self):
164
            pass
165
166
    def class_f1():
167
        return class1
168
169
    def class_f2():
170
        return class2
171
172
    def class_f3():
173
        return class3
174
175
    def objective_function(opt):
176
        score = -opt["x1"] * opt["x1"]
177
        return score
178
179
    search_space = {
180
        "x1": list(range(0, 100, 1)),
181
        "class1": [class_f1, class_f2, class_f3],
182
    }
183
184
    hyper = Hyperactive()
185
    hyper.add_search(
186
        objective_function,
187
        search_space,
188
        n_iter=15,
189
    )
190
    hyper.run()
191
192
    assert isinstance(hyper.search_data(objective_function), pd.DataFrame)
193
    assert hyper.best_para(objective_function)["class1"] in search_space["class1"]
194
195
196
def test_search_space_6():
197
    """Test search space with mixed parameter types."""
198
199
    def objective_function(opt):
200
        score = -opt["x1"] * opt["x1"]
201
        return score
202
203
    def list_f1():
204
        return [0, 1]
205
206
    def list_f2():
207
        return [1, 0]
208
209
    search_space = {
210
        "x1": list(range(0, 100, 1)),
211
        "list1": [list_f1, list_f2],
212
    }
213
214
    hyper = Hyperactive()
215
    hyper.add_search(
216
        objective_function,
217
        search_space,
218
        n_iter=15,
219
    )
220
    hyper.run()
221
222
    assert isinstance(hyper.search_data(objective_function), pd.DataFrame)
223
    assert hyper.best_para(objective_function)["list1"] in search_space["list1"]
224