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 ...search import Search |
9
|
|
|
from ._spiral import Spiral |
10
|
|
|
|
11
|
|
|
|
12
|
|
View Code Duplication |
def centeroid(array_list): |
|
|
|
|
13
|
|
|
centeroid = [] |
14
|
|
|
for idx in range(array_list[0].shape[0]): |
15
|
|
|
center_dim_pos = [] |
16
|
|
|
for array in array_list: |
17
|
|
|
center_dim_pos.append(array[idx]) |
18
|
|
|
|
19
|
|
|
center_dim_mean = np.array(center_dim_pos).mean() |
20
|
|
|
centeroid.append(center_dim_mean) |
21
|
|
|
|
22
|
|
|
return centeroid |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
class SpiralOptimization(BasePopulationOptimizer, Search): |
26
|
|
|
name = "Spiral Optimization" |
27
|
|
|
_name_ = "spiral_optimization" |
28
|
|
|
|
29
|
|
|
def __init__(self, *args, population=10, decay_rate=0.99, **kwargs): |
30
|
|
|
super().__init__(*args, **kwargs) |
31
|
|
|
|
32
|
|
|
self.population = population |
33
|
|
|
self.decay_rate = decay_rate |
34
|
|
|
|
35
|
|
|
self.particles = self._create_population(Spiral) |
36
|
|
|
self.optimizers = self.particles |
37
|
|
|
|
38
|
|
|
def init_pos(self, pos): |
39
|
|
|
nth_pop = self.nth_iter % len(self.particles) |
40
|
|
|
|
41
|
|
|
self.p_current = self.particles[nth_pop] |
42
|
|
|
self.p_current.init_pos(pos) |
43
|
|
|
|
44
|
|
|
self.p_current.decay_rate = self.decay_rate |
45
|
|
|
|
46
|
|
|
def finish_initialization(self): |
47
|
|
|
self.sort_pop_best_score() |
48
|
|
|
self.center_pos = self.pop_sorted[0].pos_current |
49
|
|
|
self.center_score = self.pop_sorted[0].score_current |
50
|
|
|
|
51
|
|
|
self.init_done = True |
52
|
|
|
|
53
|
|
|
def iterate(self): |
54
|
|
|
n_iter = self._iterations(self.particles) |
55
|
|
|
self.p_current = self.particles[n_iter % len(self.particles)] |
56
|
|
|
|
57
|
|
|
self.sort_pop_best_score() |
58
|
|
|
self.p_current.global_pos_best = self.pop_sorted[0].pos_current |
59
|
|
|
|
60
|
|
|
return self.p_current.move_spiral(self.center_pos) |
61
|
|
|
|
62
|
|
|
def evaluate(self, score_new): |
63
|
|
|
if self.init_done: |
64
|
|
|
if self.pop_sorted[0].score_current > self.center_score: |
65
|
|
|
self.center_pos = self.pop_sorted[0].pos_current |
66
|
|
|
self.center_score = self.pop_sorted[0].score_current |
67
|
|
|
|
68
|
|
|
self.p_current.evaluate(score_new) |
69
|
|
|
|