SpiralOptimization.__init__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 25
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 25
rs 9.376
c 0
b 0
f 0
cc 1
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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