Total Complexity | 3 |
Total Lines | 39 |
Duplicated Lines | 0 % |
Changes | 0 |
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 |