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