|
1
|
|
|
"""Representation of a particle in swarm.""" |
|
2
|
|
|
# pylint: disable=too-many-instance-attributes |
|
|
|
|
|
|
3
|
|
|
# pylint: disable=redefined-variable-type |
|
|
|
|
|
|
4
|
|
|
|
|
5
|
1 |
|
import numpy as np |
|
|
|
|
|
|
6
|
|
|
|
|
7
|
1 |
|
from grortir.main.pso.position_updater import PositionUpdater |
|
8
|
1 |
|
from grortir.main.pso.velocity_calculator import VelocityCalculator |
|
9
|
1 |
|
from grortir.main.model.core.optimization_status import OptimizationStatus |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
1 |
|
class Particle(object): |
|
13
|
|
|
"""Implementation of particle.""" |
|
14
|
|
|
|
|
15
|
1 |
|
def __init__(self, stages, process): |
|
16
|
1 |
|
self.stages = stages |
|
17
|
1 |
|
self.process = process |
|
18
|
1 |
|
self.position_updaters = {} |
|
19
|
1 |
|
self.velocity_calculators = {} |
|
20
|
1 |
|
self.current_velocities = {} |
|
21
|
1 |
|
self.current_quality = {} |
|
22
|
1 |
|
self.best_quality = np.inf |
|
23
|
1 |
|
self.best_positions = {} |
|
24
|
|
|
|
|
25
|
1 |
|
def initialize(self): |
|
26
|
|
|
"""Initialization of single particle.""" |
|
27
|
1 |
|
for stage in self.stages: |
|
28
|
1 |
|
self.position_updaters[stage] = PositionUpdater(stage) |
|
29
|
1 |
|
self.velocity_calculators[stage] = VelocityCalculator(stage) |
|
30
|
1 |
|
stage.optimization_status = OptimizationStatus.in_progress |
|
31
|
1 |
|
self.current_quality[stage] = np.inf |
|
32
|
1 |
|
self._set_initial_positions() |
|
33
|
1 |
|
self._set_initial_velocities() |
|
34
|
|
|
|
|
35
|
1 |
|
def _set_initial_positions(self): |
|
|
|
|
|
|
36
|
1 |
|
for stage in self.stages: |
|
37
|
1 |
|
self.position_updaters[stage].set_initial_control_params() |
|
38
|
|
|
|
|
39
|
1 |
|
def _set_initial_velocities(self): |
|
|
|
|
|
|
40
|
1 |
|
for stage in self.stages: |
|
41
|
1 |
|
self.current_velocities[stage] = self.velocity_calculators[ |
|
42
|
|
|
stage].calculate_initial_velocity() |
|
43
|
|
|
|
|
44
|
1 |
|
def update_values(self): |
|
45
|
|
|
"""Update values in swarm.""" |
|
46
|
1 |
|
self.update_input_vectors() |
|
47
|
1 |
|
self.calculate_current_quality() |
|
48
|
1 |
|
self._update_stages_status() |
|
49
|
1 |
|
self._update_best_position() |
|
50
|
|
|
|
|
51
|
1 |
|
def _update_best_position(self): |
|
|
|
|
|
|
52
|
1 |
|
current_quality = self.get_the_overall_quality() |
|
53
|
1 |
|
if current_quality < self.best_quality: |
|
54
|
1 |
|
self.best_quality = current_quality |
|
55
|
1 |
|
for stage in self.stages: |
|
56
|
1 |
|
self.best_positions[stage] = stage.control_params |
|
57
|
|
|
|
|
58
|
1 |
|
def update_input_vectors(self): |
|
59
|
|
|
"""Update input vectors in all stages.""" |
|
60
|
1 |
|
for stage in self.stages: |
|
61
|
1 |
|
current_output = stage.get_output_of_stage() |
|
62
|
1 |
|
successors = self.process.successors(stage) |
|
63
|
1 |
|
for successor in successors: |
|
64
|
1 |
|
successor.input_vector = current_output |
|
65
|
|
|
|
|
66
|
1 |
|
def calculate_current_quality(self): |
|
67
|
|
|
"""Calculate current quality.""" |
|
68
|
1 |
|
for stage in self.stages: |
|
69
|
1 |
|
self.current_quality[stage] = stage.get_quality() |
|
70
|
|
|
|
|
71
|
1 |
|
def get_the_overall_quality(self): |
|
72
|
|
|
"""Return overall quality of stages.""" |
|
73
|
1 |
|
stage = max(self.current_quality, key=self.current_quality.get) |
|
74
|
1 |
|
return self.current_quality[stage] |
|
75
|
|
|
|
|
76
|
1 |
|
def move(self): |
|
77
|
|
|
"""Move particle.""" |
|
78
|
1 |
|
for stage in self.stages: |
|
79
|
1 |
|
velocity = self.current_velocities[stage] |
|
80
|
1 |
|
self.position_updaters[stage].update_position(velocity) |
|
81
|
|
|
|
|
82
|
1 |
|
def update_velocieties(self, best_particle): |
|
83
|
|
|
"""Update velocities in swarm.""" |
|
84
|
1 |
|
for stage in self.stages: |
|
85
|
1 |
|
velocity = self.velocity_calculators[stage].calculate( |
|
86
|
|
|
self.best_positions[stage], |
|
87
|
|
|
best_particle.best_positions[stage]) |
|
88
|
1 |
|
self.current_velocities[stage] = velocity |
|
89
|
|
|
|
|
90
|
1 |
|
def _update_stages_status(self): |
|
|
|
|
|
|
91
|
1 |
|
for stage in self.stages: |
|
92
|
1 |
|
if stage.is_enough_quality(self.current_quality[stage]): |
|
93
|
1 |
|
stage.optimization_status = OptimizationStatus.success |
|
94
|
1 |
|
if not stage.could_be_optimized(): |
|
95
|
|
|
stage.optimization_status = OptimizationStatus.failed |
|
96
|
|
|
|