Passed
Push — master ( 44bd28...268666 )
by Simon
03:57
created

tests.test_random_state.test_no_random_state_0()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
import time
2
import numpy as np
3
import pandas as pd
4
from sklearn.datasets import load_breast_cancer
5
from sklearn.model_selection import cross_val_score
6
from sklearn.tree import DecisionTreeClassifier
7
from gradient_free_optimizers import RandomSearchOptimizer, HillClimbingOptimizer
8
9
10
def objective_function(para):
11
    score = -para["x1"] * para["x1"]
12
    return score
13
14
15
search_space = {
16
    "x1": np.arange(0, 100000, 0.1),
17
}
18
19
err = 0.01
20
21
22
def test_random_state_0():
23
    opt0 = RandomSearchOptimizer(search_space)
24
    opt0.search(
25
        objective_function, n_iter=100, initialize={"random": 1}, random_state=1
26
    )
27
28
    opt1 = RandomSearchOptimizer(search_space)
29
    opt1.search(
30
        objective_function, n_iter=100, initialize={"random": 1}, random_state=1
31
    )
32
33
    assert abs(opt0.best_score - opt1.best_score) < err
34
35
36
def test_random_state_1():
37
    opt0 = RandomSearchOptimizer(search_space)
38
    opt0.search(
39
        objective_function, n_iter=100, initialize={"random": 1}, random_state=10
40
    )
41
42
    opt1 = RandomSearchOptimizer(search_space)
43
    opt1.search(
44
        objective_function, n_iter=100, initialize={"random": 1}, random_state=10
45
    )
46
47
    assert abs(opt0.best_score - opt1.best_score) < err
48
49
50
def test_random_state_2():
51
    opt0 = RandomSearchOptimizer(search_space)
52
    opt0.search(
53
        objective_function, n_iter=100, initialize={"random": 1}, random_state=1
54
    )
55
56
    opt1 = RandomSearchOptimizer(search_space)
57
    opt1.search(
58
        objective_function, n_iter=100, initialize={"random": 1}, random_state=10
59
    )
60
61
    assert abs(opt0.best_score - opt1.best_score) > err
62
63
64
def test_no_random_state_0():
65
    opt0 = RandomSearchOptimizer(search_space)
66
    opt0.search(objective_function, n_iter=100, initialize={"random": 1})
67
68
    opt1 = RandomSearchOptimizer(search_space)
69
    opt1.search(objective_function, n_iter=100, initialize={"random": 1})
70
71
    assert abs(opt0.best_score - opt1.best_score) > err
72