Passed
Push — master ( bac9d4...455948 )
by Simon
04:25
created

tests.test_optimizers.test_parameter.test_bayesian_optimizer_para_init   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 26.47 %

Importance

Changes 0
Metric Value
eloc 40
dl 18
loc 68
rs 10
c 0
b 0
f 0
wmc 5

2 Functions

Rating   Name   Duplication   Size   Complexity  
A test_hill_climbing_para() 0 3 1
A objective_function() 0 3 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A GPR.predict() 2 2 1
A GPR.fit() 2 2 1
A GPR.__init__() 10 10 1

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
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import pytest
6
import numpy as np
7
8
from gradient_free_optimizers import BayesianOptimizer
9
from sklearn.gaussian_process import GaussianProcessRegressor
10
from sklearn.gaussian_process.kernels import Matern, WhiteKernel, RBF
11
from ._base_para_test import _base_para_test_func
12
13
14
def objective_function(para):
15
    score = -para["x1"] * para["x1"]
16
    return score
17
18
19
search_space = {"x1": np.arange(-10, 11, 1)}
20
21
22
warm_start_smbo = (
23
    np.array([[-10, -10], [30, 30], [0, 0]]),
24
    np.array([-1, 0, 1]),
25
)
26
27
28 View Code Duplication
class GPR:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
29
    def __init__(self):
30
        nu_param = 0.5
31
        matern = Matern(
32
            # length_scale=length_scale_param,
33
            # length_scale_bounds=length_scale_bounds_param,
34
            nu=nu_param,
35
        )
36
37
        self.gpr = GaussianProcessRegressor(
38
            kernel=matern + RBF() + WhiteKernel(), n_restarts_optimizer=0
39
        )
40
41
    def fit(self, X, y):
42
        self.gpr.fit(X, y)
43
44
    def predict(self, X, return_std=False):
45
        return self.gpr.predict(X, return_std=return_std)
46
47
48
bayesian_optimizer_para = [
49
    ({"gpr": GPR()}),
50
    ({"xi": 0.001}),
51
    ({"xi": 0.5}),
52
    ({"xi": 0.9}),
53
    ({"warm_start_smbo": None}),
54
    ({"warm_start_smbo": warm_start_smbo}),
55
    ({"rand_rest_p": 0}),
56
    ({"rand_rest_p": 0.5}),
57
    ({"rand_rest_p": 1}),
58
    ({"rand_rest_p": 10}),
59
]
60
61
62
pytest_wrapper = ("opt_para", bayesian_optimizer_para)
63
64
65
@pytest.mark.parametrize(*pytest_wrapper)
66
def test_hill_climbing_para(opt_para):
67
    _base_para_test_func(opt_para, BayesianOptimizer)
68