Passed
Pull Request — master (#110)
by
unknown
01:35
created

tests_old.test_max_score   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 60
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 3

3 Functions

Rating   Name   Duplication   Size   Complexity  
A test_max_score_0() 0 33 1
A objective_function() 0 3 1
A test_max_score_1() 0 30 1
1
import time
2
import numpy as np
3
from sklearn.datasets import load_breast_cancer
4
from sklearn.model_selection import cross_val_score
5
from sklearn.tree import DecisionTreeClassifier
6
7
from hyperactive import Hyperactive
8
from hyperactive.optimizers import (
9
    RandomSearchOptimizer,
10
    HillClimbingOptimizer,
11
)
12
13
14
def objective_function(para):
15
    score = -para["x1"] * para["x1"]
16
    return score
17
18
19
search_space = {
20
    "x1": list(np.arange(0, 100000, 0.1)),
21
}
22
23
24
def test_max_score_0():
25
    def objective_function(para):
26
        score = -para["x1"] * para["x1"]
27
        return score
28
29
    search_space = {
30
        "x1": list(np.arange(0, 100, 0.1)),
31
    }
32
33
    max_score = -9999
34
35
    opt = HillClimbingOptimizer(
36
        epsilon=0.01,
37
        rand_rest_p=0,
38
    )
39
40
    hyper = Hyperactive()
41
    hyper.add_search(
42
        objective_function,
43
        search_space,
44
        optimizer=opt,
45
        n_iter=100000,
46
        initialize={"warm_start": [{"x1": 99}]},
47
        max_score=max_score,
48
    )
49
    hyper.run()
50
51
    print("\n Results head \n", hyper.search_data(objective_function).head())
52
    print("\n Results tail \n", hyper.search_data(objective_function).tail())
53
54
    print("\nN iter:", len(hyper.search_data(objective_function)))
55
56
    assert -100 > hyper.best_score(objective_function) > max_score
57
58
59
def test_max_score_1():
60
    def objective_function(para):
61
        score = -para["x1"] * para["x1"]
62
        time.sleep(0.01)
63
        return score
64
65
    search_space = {
66
        "x1": list(np.arange(0, 100, 0.1)),
67
    }
68
69
    max_score = -9999
70
71
    c_time = time.perf_counter()
72
    hyper = Hyperactive()
73
    hyper.add_search(
74
        objective_function,
75
        search_space,
76
        n_iter=100000,
77
        initialize={"warm_start": [{"x1": 99}]},
78
        max_score=max_score,
79
    )
80
    hyper.run()
81
    diff_time = time.perf_counter() - c_time
82
83
    print("\n Results head \n", hyper.search_data(objective_function).head())
84
    print("\n Results tail \n", hyper.search_data(objective_function).tail())
85
86
    print("\nN iter:", len(hyper.search_data(objective_function)))
87
88
    assert diff_time < 1
89