Passed
Push — master ( 268666...521b2e )
by Simon
01:18
created

tests.test_optimizers.test_convex_convergence   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 84.85 %

Importance

Changes 0
Metric Value
eloc 49
dl 56
loc 66
rs 10
c 0
b 0
f 0
wmc 4

2 Functions

Rating   Name   Duplication   Size   Complexity  
A test_convex_convergence_noSBOM() 28 28 2
A test_convex_convergence_SBOM() 28 28 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
import pytest
2
from tqdm import tqdm
3
import numpy as np
4
5
from ._parametrize import optimizers_noSBOM, optimizers_SBOM
6
7
8 View Code Duplication
@pytest.mark.parametrize(*optimizers_noSBOM)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
9
def test_convex_convergence_noSBOM(Optimizer):
10
    def objective_function(para):
11
        score = -para["x1"] * para["x1"]
12
        return score
13
14
    search_space = {"x1": np.arange(-33, 33, 1)}
15
    initialize = {"vertices": 2}
16
17
    n_opts = 33
18
19
    scores = []
20
    for rnd_st in tqdm(range(n_opts)):
21
        opt = Optimizer(search_space)
22
        opt.search(
23
            objective_function,
24
            n_iter=50,
25
            random_state=rnd_st,
26
            memory=False,
27
            verbosity={"print_results": False, "progress_bar": False},
28
            initialize=initialize,
29
        )
30
31
        scores.append(opt.best_score)
32
    score_mean = np.array(scores).mean()
33
    print("scores", scores)
34
35
    assert -500 < score_mean
36
37
38 View Code Duplication
@pytest.mark.parametrize(*optimizers_SBOM)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
39
def test_convex_convergence_SBOM(Optimizer):
40
    def objective_function(para):
41
        score = -para["x1"] * para["x1"]
42
        return score
43
44
    search_space = {"x1": np.arange(-33, 33, 1)}
45
    initialize = {"vertices": 2}
46
47
    n_opts = 10
48
49
    scores = []
50
    for rnd_st in tqdm(range(n_opts)):
51
        opt = Optimizer(search_space)
52
        opt.search(
53
            objective_function,
54
            n_iter=30,
55
            random_state=rnd_st,
56
            memory=False,
57
            verbosity={"print_results": False, "progress_bar": False},
58
            initialize=initialize,
59
        )
60
61
        scores.append(opt.best_score)
62
    score_mean = np.array(scores).mean()
63
    print("scores", scores)
64
65
    assert -500 < score_mean
66
67