Passed
Push — master ( 2d5f27...fd0b7f )
by Simon
01:30
created

tests.ParticleSwarm   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 84
Duplicated Lines 44.05 %

Importance

Changes 0
Metric Value
wmc 13
eloc 58
dl 37
loc 84
rs 10
c 0
b 0
f 0

7 Functions

Rating   Name   Duplication   Size   Complexity  
A _base_test() 10 10 3
A get_score() 0 2 1
A test_social_weight() 0 4 2
A _test_ParticleSwarmOptimizer() 0 5 1
A test_inertia() 0 4 2
A test_cognitive_weight() 0 4 2
A test_individuals() 27 27 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
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import numpy as np
6
7
from gradient_free_optimizers import ParticleSwarmOptimizer
8
9
n_iter = 100
10
11
12
def get_score(pos_new):
13
    return -(pos_new[0] * pos_new[0])
14
15
16
space_dim = np.array([10])
17
init_positions = [np.array([0]), np.array([1]), np.array([2]), np.array([3])]
18
19
20 View Code Duplication
def _base_test(opt, init_positions):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
21
    for nth_init in range(len(init_positions)):
22
        pos_new = opt.init_pos(nth_init)
23
        score_new = get_score(pos_new)
24
        opt.evaluate(score_new)
25
26
    for nth_iter in range(len(init_positions), n_iter):
27
        pos_new = opt.iterate(nth_iter)
28
        score_new = get_score(pos_new)
29
        opt.evaluate(score_new)
30
31
32
def _test_ParticleSwarmOptimizer(
33
    init_positions=init_positions, space_dim=space_dim, opt_para={}
34
):
35
    opt = ParticleSwarmOptimizer(init_positions, space_dim, opt_para)
36
    _base_test(opt, init_positions)
37
38
39 View Code Duplication
def test_individuals():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
40
    for init_positions in [
41
        [np.array([0])],
42
        [np.array([0]), np.array([0])],
43
        [np.array([0]), np.array([0])],
44
        [
45
            np.array([0]),
46
            np.array([0]),
47
            np.array([0]),
48
            np.array([0]),
49
            np.array([0]),
50
            np.array([0]),
51
            np.array([0]),
52
            np.array([0]),
53
            np.array([0]),
54
            np.array([0]),
55
            np.array([0]),
56
            np.array([0]),
57
            np.array([0]),
58
            np.array([0]),
59
            np.array([0]),
60
            np.array([0]),
61
            np.array([0]),
62
            np.array([0]),
63
        ],
64
    ]:
65
        _test_ParticleSwarmOptimizer(init_positions)
66
67
68
def test_inertia():
69
    for inertia in [0.1, 0.9]:
70
        opt_para = {"inertia": inertia}
71
        _test_ParticleSwarmOptimizer(opt_para=opt_para)
72
73
74
def test_cognitive_weight():
75
    for cognitive_weight in [0.1, 0.9]:
76
        opt_para = {"cognitive_weight": cognitive_weight}
77
        _test_ParticleSwarmOptimizer(opt_para=opt_para)
78
79
80
def test_social_weight():
81
    for social_weight in [0.1, 0.9]:
82
        opt_para = {"social_weight": social_weight}
83
        _test_ParticleSwarmOptimizer(opt_para=opt_para)
84