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

test_global_perf()   A

Complexity

Conditions 2

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 20
nop 1
dl 0
loc 27
rs 9.4
c 0
b 0
f 0
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