Passed
Push — master ( bf7425...566aaf )
by Simon
01:41
created

tests.local_test_performance.local_test_global_opt   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A test_global_perf() 0 26 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, random_state=rnd_st)
37
        opt.search(
38
            ackley_function,
39
            n_iter=n_iter,
40
            memory=False,
41
            verbosity=False,
42
        )
43
44
        scores.append(opt.best_score)
45
    score_mean = np.array(scores).mean()
46
47
    print("\n score_mean", score_mean)
48
49
    assert score_mean > -5
50