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

tests.test_api.test_max_time   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 3

2 Functions

Rating   Name   Duplication   Size   Complexity  
A test_max_time_0() 0 8 1
A test_max_time_1() 0 8 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A Experiment.objective_function() 0 3 1
1
import time
2
import numpy as np
3
from hyperactive.optimizers import HillClimbingOptimizer
4
from hyperactive.experiment import BaseExperiment
5
from hyperactive.search_config import SearchConfig
6
7
8
class Experiment(BaseExperiment):
9
    def objective_function(self, para):
10
        score = -para["x1"] * para["x1"]
11
        return score
12
13
14
experiment = Experiment()
15
16
search_config = SearchConfig(
17
    x1=list(np.arange(0, 100000, 1)),
18
)
19
20
21
def test_max_time_0():
22
    c_time1 = time.perf_counter()
23
    hyper = HillClimbingOptimizer()
24
    hyper.add_search(experiment, search_config, n_iter=1000000)
25
    hyper.run(max_time=0.1)
26
    diff_time1 = time.perf_counter() - c_time1
27
28
    assert diff_time1 < 1
29
30
31
def test_max_time_1():
32
    c_time1 = time.perf_counter()
33
    hyper = HillClimbingOptimizer()
34
    hyper.add_search(experiment, search_config, n_iter=1000000)
35
    hyper.run(max_time=1)
36
    diff_time1 = time.perf_counter() - c_time1
37
38
    assert 0.3 < diff_time1 < 2
39