Passed
Pull Request — master (#110)
by
unknown
01:48 queued 33s
created

tests.test_api.test_search_spaces   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 186
Duplicated Lines 24.73 %

Importance

Changes 0
Metric Value
eloc 124
dl 46
loc 186
rs 10
c 0
b 0
f 0
wmc 8

7 Functions

Rating   Name   Duplication   Size   Complexity  
A test_search_space_3() 0 25 1
A test_search_space_0() 15 15 1
A test_search_space_2() 16 16 1
A test_search_space_4() 0 25 1
A test_search_space_1() 15 15 1
A test_search_space_5() 0 37 1
A test_search_space_6() 0 23 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A Experiment.objective_function() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
import numpy as np
2
import pandas as pd
3
4
from hyperactive.optimizers import HillClimbingOptimizer
5
from hyperactive.experiment import BaseExperiment
6
from hyperactive.search_config import SearchConfig
7
8
9
class Experiment(BaseExperiment):
10
    def objective_function(self, opt):
11
        score = -opt["x1"] * opt["x1"]
12
        return score
13
14
15
experiment = Experiment()
16
17
18 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...
19
    search_config = SearchConfig(
20
        x1=list(np.arange(0, 3, 1)),
21
    )
22
23
    hyper = HillClimbingOptimizer()
24
    hyper.add_search(
25
        experiment,
26
        search_config,
27
        n_iter=15,
28
    )
29
    hyper.run()
30
31
    assert isinstance(hyper.search_data(experiment), pd.DataFrame)
32
    assert hyper.best_para(experiment)["x1"] in search_config["x1"]
33
34
35 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...
36
    search_config = SearchConfig(
37
        x1=list(np.arange(0, 0.003, 0.001)),
38
    )
39
40
    hyper = HillClimbingOptimizer()
41
    hyper.add_search(
42
        experiment,
43
        search_config,
44
        n_iter=15,
45
    )
46
    hyper.run()
47
48
    assert isinstance(hyper.search_data(experiment), pd.DataFrame)
49
    assert hyper.best_para(experiment)["x1"] in search_config["x1"]
50
51
52 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...
53
    search_config = SearchConfig(
54
        x1=list(np.arange(0, 100, 1)),
55
        str1=["0", "1", "2"],
56
    )
57
58
    hyper = HillClimbingOptimizer()
59
    hyper.add_search(
60
        experiment,
61
        search_config,
62
        n_iter=15,
63
    )
64
    hyper.run()
65
66
    assert isinstance(hyper.search_data(experiment), pd.DataFrame)
67
    assert hyper.best_para(experiment)["str1"] in search_config["str1"]
68
69
70
def test_search_space_3():
71
    def func1():
72
        pass
73
74
    def func2():
75
        pass
76
77
    def func3():
78
        pass
79
80
    search_config = SearchConfig(
81
        x1=list(np.arange(0, 100, 1)),
82
        func1=[func1, func2, func3],
83
    )
84
85
    hyper = HillClimbingOptimizer()
86
    hyper.add_search(
87
        experiment,
88
        search_config,
89
        n_iter=15,
90
    )
91
    hyper.run()
92
93
    assert isinstance(hyper.search_data(experiment), pd.DataFrame)
94
    assert hyper.best_para(experiment)["func1"] in search_config["func1"]
95
96
97
def test_search_space_4():
98
    class class1:
99
        pass
100
101
    class class2:
102
        pass
103
104
    class class3:
105
        pass
106
107
    search_config = SearchConfig(
108
        x1=list(np.arange(0, 100, 1)),
109
        class1=[class1, class2, class3],
110
    )
111
112
    hyper = HillClimbingOptimizer()
113
    hyper.add_search(
114
        experiment,
115
        search_config,
116
        n_iter=15,
117
    )
118
    hyper.run()
119
120
    assert isinstance(hyper.search_data(experiment), pd.DataFrame)
121
    assert hyper.best_para(experiment)["class1"] in search_config["class1"]
122
123
124
def test_search_space_5():
125
    class class1:
126
        def __init__(self):
127
            pass
128
129
    class class2:
130
        def __init__(self):
131
            pass
132
133
    class class3:
134
        def __init__(self):
135
            pass
136
137
    def class_f1():
138
        return class1
139
140
    def class_f2():
141
        return class2
142
143
    def class_f3():
144
        return class3
145
146
    search_config = SearchConfig(
147
        x1=list(np.arange(0, 100, 1)),
148
        class1=[class_f1, class_f2, class_f3],
149
    )
150
151
    hyper = HillClimbingOptimizer()
152
    hyper.add_search(
153
        experiment,
154
        search_config,
155
        n_iter=15,
156
    )
157
    hyper.run()
158
159
    assert isinstance(hyper.search_data(experiment), pd.DataFrame)
160
    assert hyper.best_para(experiment)["class1"] in search_config["class1"]
161
162
163
def test_search_space_6():
164
165
    def list_f1():
166
        return [0, 1]
167
168
    def list_f2():
169
        return [1, 0]
170
171
    search_config = SearchConfig(
172
        x1=list(np.arange(0, 100, 1)),
173
        list1=[list_f1, list_f2],
174
    )
175
176
    hyper = HillClimbingOptimizer()
177
    hyper.add_search(
178
        experiment,
179
        search_config,
180
        n_iter=15,
181
    )
182
    hyper.run()
183
184
    assert isinstance(hyper.search_data(experiment), pd.DataFrame)
185
    assert hyper.best_para(experiment)["list1"] in search_config["list1"]
186