Passed
Push — master ( e85313...dd7f59 )
by Simon
01:21
created

tests.test_initializers   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 105
Duplicated Lines 25.71 %

Importance

Changes 0
Metric Value
eloc 61
dl 27
loc 105
rs 10
c 0
b 0
f 0
wmc 7

7 Functions

Rating   Name   Duplication   Size   Complexity  
A test_initialize_all_0() 0 12 1
A test_initialize_grid_1() 14 14 1
A test_initialize_warm_start_0() 0 14 1
A test_initialize_grid_0() 13 13 1
A test_initialize_vertices() 0 10 1
A objective_function() 0 3 1
A test_initialize_warm_start_1() 0 17 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
from hyperactive import Hyperactive
3
4
5
def objective_function(opt):
6
    score = -opt["x1"] * opt["x1"]
7
    return score
8
9
10
search_space = {
11
    "x1": np.arange(-100, 101, 1),
12
}
13
14
15
def test_initialize_warm_start_0():
16
    init = {
17
        "x1": 0,
18
    }
19
20
    initialize = {"warm_start": [init]}
21
22
    hyper = Hyperactive()
23
    hyper.add_search(
24
        objective_function, search_space, n_iter=1, initialize=initialize,
25
    )
26
    hyper.run()
27
28
    assert abs(hyper.best_score(objective_function)) < 0.001
29
30
31
def test_initialize_warm_start_1():
32
    search_space = {
33
        "x1": np.arange(-10, 10, 1),
34
    }
35
    init = {
36
        "x1": -10,
37
    }
38
39
    initialize = {"warm_start": [init]}
40
41
    hyper = Hyperactive()
42
    hyper.add_search(
43
        objective_function, search_space, n_iter=1, initialize=initialize,
44
    )
45
    hyper.run()
46
47
    assert hyper.best_para(objective_function) == init
48
49
50
def test_initialize_vertices():
51
    initialize = {"vertices": 2}
52
53
    hyper = Hyperactive()
54
    hyper.add_search(
55
        objective_function, search_space, n_iter=2, initialize=initialize,
56
    )
57
    hyper.run()
58
59
    assert abs(hyper.best_score(objective_function)) - 10000 < 0.001
60
61
62 View Code Duplication
def test_initialize_grid_0():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
63
    search_space = {
64
        "x1": np.arange(-1, 2, 1),
65
    }
66
    initialize = {"grid": 1}
67
68
    hyper = Hyperactive()
69
    hyper.add_search(
70
        objective_function, search_space, n_iter=1, initialize=initialize,
71
    )
72
    hyper.run()
73
74
    assert abs(hyper.best_score(objective_function)) < 0.001
75
76
77 View Code Duplication
def test_initialize_grid_1():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
78
    search_space = {
79
        "x1": np.arange(-2, 3, 1),
80
    }
81
82
    initialize = {"grid": 1}
83
84
    hyper = Hyperactive()
85
    hyper.add_search(
86
        objective_function, search_space, n_iter=1, initialize=initialize,
87
    )
88
    hyper.run()
89
90
    assert abs(hyper.best_score(objective_function)) - 1 < 0.001
91
92
93
def test_initialize_all_0():
94
    search_space = {
95
        "x1": np.arange(-2, 3, 1),
96
    }
97
98
    initialize = {"grid": 100, "vertices": 100, "random": 100}
99
100
    hyper = Hyperactive()
101
    hyper.add_search(
102
        objective_function, search_space, n_iter=300, initialize=initialize,
103
    )
104
    hyper.run()
105
106