tests.test_empty_output.non_verbose   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 78
Duplicated Lines 14.1 %

Importance

Changes 0
Metric Value
eloc 62
dl 11
loc 78
rs 10
c 0
b 0
f 0
wmc 2

2 Functions

Rating   Name   Duplication   Size   Complexity  
A ackley_function() 11 11 1
A warn() 0 2 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
def warn(*args, **kwargs):
2
    pass
3
4
5
import warnings
6
7
warnings.warn = warn
8
9
import numpy as np
10
from gradient_free_optimizers import (
11
    HillClimbingOptimizer,
12
    StochasticHillClimbingOptimizer,
13
    RepulsingHillClimbingOptimizer,
14
    SimulatedAnnealingOptimizer,
15
    DownhillSimplexOptimizer,
16
    RandomSearchOptimizer,
17
    PowellsMethod,
18
    GridSearchOptimizer,
19
    RandomRestartHillClimbingOptimizer,
20
    RandomAnnealingOptimizer,
21
    PatternSearch,
22
    DirectAlgorithm,
23
    ParallelTemperingOptimizer,
24
    ParticleSwarmOptimizer,
25
    SpiralOptimization,
26
    EvolutionStrategyOptimizer,
27
    LipschitzOptimizer,
28
    BayesianOptimizer,
29
    TreeStructuredParzenEstimators,
30
    ForestOptimizer,
31
)
32
33
optimizers = [
34
    HillClimbingOptimizer,
35
    StochasticHillClimbingOptimizer,
36
    RepulsingHillClimbingOptimizer,
37
    SimulatedAnnealingOptimizer,
38
    DownhillSimplexOptimizer,
39
    RandomSearchOptimizer,
40
    PowellsMethod,
41
    GridSearchOptimizer,
42
    RandomRestartHillClimbingOptimizer,
43
    RandomAnnealingOptimizer,
44
    PatternSearch,
45
    DirectAlgorithm,
46
    ParallelTemperingOptimizer,
47
    ParticleSwarmOptimizer,
48
    SpiralOptimization,
49
    EvolutionStrategyOptimizer,
50
    LipschitzOptimizer,
51
    BayesianOptimizer,
52
    TreeStructuredParzenEstimators,
53
    ForestOptimizer,
54
]
55
56
57 View Code Duplication
def ackley_function(para):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
58
    x, y = para["x"], para["y"]
59
60
    loss = (
61
        -20 * np.exp(-0.2 * np.sqrt(0.5 * (x * x + y * y)))
62
        - np.exp(0.5 * (np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y)))
63
        + np.exp(1)
64
        + 20
65
    )
66
67
    return -loss
68
69
70
search_space = {
71
    "x": np.arange(-10, 10, 1),
72
    "y": np.arange(-10, 10, 1),
73
}
74
75
for optimizer in optimizers:
76
    opt = optimizer(search_space)
77
    opt.search(ackley_function, n_iter=100, verbosity=False)
78