Issues (68)

tests/test_initializers.py (2 issues)

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": list(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,
25
        search_space,
26
        n_iter=1,
27
        initialize=initialize,
28
    )
29
    hyper.run()
30
31
    assert abs(hyper.best_score(objective_function)) < 0.001
32
33
34
def test_initialize_warm_start_1():
35
    search_space = {
36
        "x1": list(np.arange(-10, 10, 1)),
37
    }
38
    init = {
39
        "x1": -10,
40
    }
41
42
    initialize = {"warm_start": [init]}
43
44
    hyper = Hyperactive()
45
    hyper.add_search(
46
        objective_function,
47
        search_space,
48
        n_iter=1,
49
        initialize=initialize,
50
    )
51
    hyper.run()
52
53
    assert hyper.best_para(objective_function) == init
54
55
56
def test_initialize_vertices():
57
    initialize = {"vertices": 2}
58
59
    hyper = Hyperactive()
60
    hyper.add_search(
61
        objective_function,
62
        search_space,
63
        n_iter=2,
64
        initialize=initialize,
65
    )
66
    hyper.run()
67
68
    assert abs(hyper.best_score(objective_function)) - 10000 < 0.001
69
70
71 View Code Duplication
def test_initialize_grid_0():
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
72
    search_space = {
73
        "x1": list(np.arange(-1, 2, 1)),
74
    }
75
    initialize = {"grid": 1}
76
77
    hyper = Hyperactive()
78
    hyper.add_search(
79
        objective_function,
80
        search_space,
81
        n_iter=1,
82
        initialize=initialize,
83
    )
84
    hyper.run()
85
86
    assert abs(hyper.best_score(objective_function)) < 0.001
87
88
89 View Code Duplication
def test_initialize_grid_1():
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
90
    search_space = {
91
        "x1": list(np.arange(-2, 3, 1)),
92
    }
93
94
    initialize = {"grid": 1}
95
96
    hyper = Hyperactive()
97
    hyper.add_search(
98
        objective_function,
99
        search_space,
100
        n_iter=1,
101
        initialize=initialize,
102
    )
103
    hyper.run()
104
105
    assert abs(hyper.best_score(objective_function)) - 1 < 0.001
106
107
108
def test_initialize_all_0():
109
    search_space = {
110
        "x1": list(np.arange(-2, 3, 1)),
111
    }
112
113
    initialize = {"grid": 100, "vertices": 100, "random": 100}
114
115
    hyper = Hyperactive()
116
    hyper.add_search(
117
        objective_function,
118
        search_space,
119
        n_iter=300,
120
        initialize=initialize,
121
    )
122
    hyper.run()
123