|
1
|
|
|
# Author: Simon Blanke |
|
2
|
|
|
# Email: [email protected] |
|
3
|
|
|
# License: MIT License |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
import numpy as np |
|
7
|
|
|
|
|
8
|
|
|
from .base_population_optimizer import BasePopulationOptimizer |
|
9
|
|
|
from ._particle import Particle |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class ParticleSwarmOptimizer(BasePopulationOptimizer): |
|
13
|
|
|
name = "Particle Swarm Optimization" |
|
14
|
|
|
_name_ = "particle_swarm_optimization" |
|
15
|
|
|
__name__ = "ParticleSwarmOptimizer" |
|
16
|
|
|
|
|
17
|
|
|
optimizer_type = "population" |
|
18
|
|
|
computationally_expensive = False |
|
19
|
|
|
|
|
20
|
|
|
def __init__( |
|
21
|
|
|
self, |
|
22
|
|
|
search_space, |
|
23
|
|
|
initialize={"grid": 4, "random": 2, "vertices": 4}, |
|
24
|
|
|
constraints=[], |
|
25
|
|
|
random_state=None, |
|
26
|
|
|
rand_rest_p=0, |
|
27
|
|
|
nth_process=None, |
|
28
|
|
|
population=10, |
|
29
|
|
|
inertia=0.5, |
|
30
|
|
|
cognitive_weight=0.5, |
|
31
|
|
|
social_weight=0.5, |
|
32
|
|
|
temp_weight=0.2, |
|
33
|
|
|
): |
|
34
|
|
|
super().__init__( |
|
35
|
|
|
search_space=search_space, |
|
36
|
|
|
initialize=initialize, |
|
37
|
|
|
constraints=constraints, |
|
38
|
|
|
random_state=random_state, |
|
39
|
|
|
rand_rest_p=rand_rest_p, |
|
40
|
|
|
nth_process=nth_process, |
|
41
|
|
|
) |
|
42
|
|
|
|
|
43
|
|
|
self.population = population |
|
44
|
|
|
self.inertia = inertia |
|
45
|
|
|
self.cognitive_weight = cognitive_weight |
|
46
|
|
|
self.social_weight = social_weight |
|
47
|
|
|
self.temp_weight = temp_weight |
|
48
|
|
|
|
|
49
|
|
|
self.particles = self._create_population(Particle) |
|
50
|
|
|
self.optimizers = self.particles |
|
51
|
|
|
|
|
52
|
|
|
@BasePopulationOptimizer.track_new_pos |
|
53
|
|
|
def init_pos(self): |
|
54
|
|
|
nth_pop = self.nth_trial % len(self.particles) |
|
55
|
|
|
|
|
56
|
|
|
self.p_current = self.particles[nth_pop] |
|
57
|
|
|
|
|
58
|
|
|
self.p_current.inertia = self.inertia |
|
59
|
|
|
self.p_current.cognitive_weight = self.cognitive_weight |
|
60
|
|
|
self.p_current.social_weight = self.social_weight |
|
61
|
|
|
self.p_current.temp_weight = self.temp_weight |
|
62
|
|
|
self.p_current.rand_rest_p = self.rand_rest_p |
|
63
|
|
|
|
|
64
|
|
|
self.p_current.velo = np.zeros(len(self.conv.max_positions)) |
|
65
|
|
|
|
|
66
|
|
|
return self.p_current.init_pos() |
|
67
|
|
|
|
|
68
|
|
|
@BasePopulationOptimizer.track_new_pos |
|
69
|
|
|
def iterate(self): |
|
70
|
|
|
while True: |
|
71
|
|
|
self.p_current = self.particles[ |
|
72
|
|
|
self.nth_trial % len(self.particles) |
|
73
|
|
|
] |
|
74
|
|
|
|
|
75
|
|
|
self.sort_pop_best_score() |
|
76
|
|
|
self.p_current.global_pos_best = self.pop_sorted[0].pos_best |
|
77
|
|
|
|
|
78
|
|
|
pos_new = self.p_current.move_linear() |
|
79
|
|
|
|
|
80
|
|
|
if self.conv.not_in_constraint(pos_new): |
|
81
|
|
|
return pos_new |
|
82
|
|
|
pos_new = self.p_current.move_climb(pos_new) |
|
83
|
|
|
|
|
84
|
|
|
@BasePopulationOptimizer.track_new_score |
|
85
|
|
|
def evaluate(self, score_new): |
|
86
|
|
|
self.p_current.evaluate(score_new) |
|
87
|
|
|
|