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

tests.test_performance.test_grid_search   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 67
dl 0
loc 98
rs 10
c 0
b 0
f 0
wmc 6

2 Functions

Rating   Name   Duplication   Size   Complexity  
A test_global_perf_1() 0 31 3
A test_global_perf_0() 0 35 3
1
import pytest
2
from tqdm import tqdm
3
import numpy as np
4
import pandas as pd
5
from functools import reduce
6
7
from gradient_free_optimizers import GridSearchOptimizer
8
9
from surfaces.test_functions import SphereFunction, RastriginFunction
10
11
12
obj_func_l = (
13
    "objective_function",
14
    [
15
        (SphereFunction(n_dim=1, metric="score")),
16
        (RastriginFunction(n_dim=1, metric="score")),
17
    ],
18
)
19
20
21
@pytest.mark.parametrize(*obj_func_l)
22
def test_global_perf_0(objective_function):
23
    search_space = {"x0": np.arange(-10, 10, 0.1)}
24
    initialize = {"vertices": 2}
25
26
    print(
27
        "\n np.array(search_space.values()) \n",
28
        np.array(search_space.values()),
29
        np.array(search_space.values()).shape,
30
    )
31
32
    dim_sizes_list = [len(array) for array in search_space.values()]
33
    ss_size = reduce((lambda x, y: x * y), dim_sizes_list)
34
35
    n_opts = 10
36
    n_iter = ss_size
37
38
    scores = []
39
    for rnd_st in tqdm(range(n_opts)):
40
        opt = GridSearchOptimizer(search_space, initialize=initialize)
41
        opt.search(
42
            objective_function,
43
            n_iter=n_iter,
44
            random_state=rnd_st,
45
            memory=False,
46
            verbosity=False,
47
        )
48
49
        scores.append(opt.best_score)
50
    score_mean = np.array(scores).mean()
51
52
    print("\n score_mean", score_mean)
53
    print("\n n_iter", n_iter)
54
55
    assert score_mean > -0.001
56
57
58
obj_func_l = (
59
    "objective_function",
60
    [
61
        (SphereFunction(n_dim=2, metric="score")),
62
        (RastriginFunction(n_dim=2, metric="score")),
63
    ],
64
)
65
66
67
@pytest.mark.parametrize(*obj_func_l)
68
def test_global_perf_1(objective_function):
69
    search_space = {
70
        "x0": np.arange(-2, 1, 0.1),
71
        "x1": np.arange(-1, 2, 0.1),
72
    }
73
    initialize = {"vertices": 2}
74
75
    dim_sizes_list = [len(array) for array in search_space.values()]
76
    ss_size = reduce((lambda x, y: x * y), dim_sizes_list)
77
78
    n_opts = 10
79
    n_iter = ss_size
80
81
    scores = []
82
    for rnd_st in tqdm(range(n_opts)):
83
        opt = GridSearchOptimizer(search_space, initialize=initialize)
84
        opt.search(
85
            objective_function,
86
            n_iter=n_iter,
87
            random_state=rnd_st,
88
            memory=False,
89
            verbosity=False,
90
        )
91
92
        scores.append(opt.best_score)
93
    score_mean = np.array(scores).mean()
94
95
    print("\n score_mean", score_mean)
96
97
    assert score_mean > -0.001
98