Passed
Push — master ( 97c085...baa350 )
by Simon
04:37
created

tests.test_max_time.test_max_time_0()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
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
from gradient_free_optimizers import (
7
    RandomSearchOptimizer,
8
    HillClimbingOptimizer,
9
)
10
11
12
def objective_function(para):
13
    score = -para["x1"] * para["x1"]
14
    return score
15
16
17
search_space = {
18
    "x1": np.arange(0, 100000, 0.1),
19
}
20
21
22
def test_max_time_0():
23
    c_time1 = time.time()
24
    opt = RandomSearchOptimizer(search_space)
25
    opt.search(objective_function, n_iter=1000000, max_time=0.1)
26
    diff_time1 = time.time() - c_time1
27
28
    assert diff_time1 < 1
29
30
31
def test_max_time_1():
32
    c_time1 = time.time()
33
    opt = RandomSearchOptimizer(search_space)
34
    opt.search(objective_function, n_iter=1000000, max_time=1)
35
    diff_time1 = time.time() - c_time1
36
37
    assert 0.3 < diff_time1 < 2
38