Passed
Push — master ( 7e252e...44707a )
by Simon
01:55
created

tests.test_performance.test_global_opt   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A test_global_perf() 0 27 2
1
import pytest
2
from tqdm import tqdm
3
import numpy as np
4
5
from surfaces.test_functions import RastriginFunction
6
7
from gradient_free_optimizers import (
8
    RandomSearchOptimizer,
9
    RandomRestartHillClimbingOptimizer,
10
    RandomAnnealingOptimizer,
11
)
12
13
14
opt_global_l = (
15
    "Optimizer",
16
    [
17
        (RandomSearchOptimizer),
18
        (RandomRestartHillClimbingOptimizer),
19
        (RandomAnnealingOptimizer),
20
    ],
21
)
22
23
24
@pytest.mark.parametrize(*opt_global_l)
25
def test_global_perf(Optimizer):
26
    ackley_function = RastriginFunction(n_dim=1, metric="score")
27
28
    search_space = {"x0": np.arange(-100, 101, 1)}
29
    initialize = {"vertices": 2}
30
31
    n_opts = 33
32
    n_iter = 100
33
34
    scores = []
35
    for rnd_st in tqdm(range(n_opts)):
36
        opt = Optimizer(search_space, initialize=initialize)
37
        opt.search(
38
            ackley_function,
39
            n_iter=n_iter,
40
            random_state=rnd_st,
41
            memory=False,
42
            verbosity=False,
43
        )
44
45
        scores.append(opt.best_score)
46
    score_mean = np.array(scores).mean()
47
48
    print("\n score_mean", score_mean)
49
50
    assert score_mean > -5
51