gradient_free_optimizers.optimizers.pop_opt.spiral_optimization   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 11.22 %

Importance

Changes 0
Metric Value
wmc 12
eloc 69
dl 11
loc 98
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A centeroid() 11 11 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A SpiralOptimization.init_pos() 0 8 1
A SpiralOptimization.iterate() 0 15 3
A SpiralOptimization.__init__() 0 25 1
A SpiralOptimization.evaluate() 0 8 3
A SpiralOptimization.finish_initialization() 0 6 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 numpy as np
6
7
from .base_population_optimizer import BasePopulationOptimizer
8
from ._spiral import Spiral
9
10
11 View Code Duplication
def centeroid(array_list):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
12
    centeroid = []
13
    for idx in range(array_list[0].shape[0]):
14
        center_dim_pos = []
15
        for array in array_list:
16
            center_dim_pos.append(array[idx])
17
18
        center_dim_mean = np.array(center_dim_pos).mean()
19
        centeroid.append(center_dim_mean)
20
21
    return centeroid
22
23
24
class SpiralOptimization(BasePopulationOptimizer):
25
    name = "Spiral Optimization"
26
    _name_ = "spiral_optimization"
27
    __name__ = "SpiralOptimization"
28
29
    optimizer_type = "population"
30
    computationally_expensive = False
31
32
    def __init__(
33
        self,
34
        search_space,
35
        initialize={"grid": 4, "random": 2, "vertices": 4},
36
        constraints=[],
37
        random_state=None,
38
        rand_rest_p=0,
39
        nth_process=None,
40
        population=10,
41
        decay_rate=0.99,
42
    ):
43
        super().__init__(
44
            search_space=search_space,
45
            initialize=initialize,
46
            constraints=constraints,
47
            random_state=random_state,
48
            rand_rest_p=rand_rest_p,
49
            nth_process=nth_process,
50
        )
51
52
        self.population = population
53
        self.decay_rate = decay_rate
54
55
        self.particles = self._create_population(Spiral)
56
        self.optimizers = self.particles
57
58
    @BasePopulationOptimizer.track_new_pos
59
    def init_pos(self):
60
        nth_pop = self.nth_trial % len(self.particles)
61
62
        self.p_current = self.particles[nth_pop]
63
        self.p_current.decay_rate = self.decay_rate
64
65
        return self.p_current.init_pos()
66
67
    def finish_initialization(self):
68
        self.sort_pop_best_score()
69
        self.center_pos = self.pop_sorted[0].pos_current
70
        self.center_score = self.pop_sorted[0].score_current
71
72
        self.search_state = "iter"
73
74
    @BasePopulationOptimizer.track_new_pos
75
    def iterate(self):
76
        while True:
77
            self.p_current = self.particles[
78
                self.nth_trial % len(self.particles)
79
            ]
80
81
            self.sort_pop_best_score()
82
            self.p_current.global_pos_best = self.pop_sorted[0].pos_current
83
84
            pos_new = self.p_current.move_spiral(self.center_pos)
85
86
            if self.conv.not_in_constraint(pos_new):
87
                return pos_new
88
            return self.p_current.iterate()
89
90
    @BasePopulationOptimizer.track_new_score
91
    def evaluate(self, score_new):
92
        if self.search_state == "iter":
93
            if self.pop_sorted[0].score_current > self.center_score:
94
                self.center_pos = self.pop_sorted[0].pos_current
95
                self.center_score = self.pop_sorted[0].score_current
96
97
        self.p_current.evaluate(score_new)
98