1
|
|
|
"""Representation of a particle in swarm.""" |
2
|
|
|
|
3
|
|
|
import numpy as np |
|
|
|
|
4
|
|
|
|
5
|
|
|
from grortir.main.pso.position_updater import PositionUpdater |
6
|
|
|
from grortir.main.pso.velocity_calculator import VelocityUpdater |
|
|
|
|
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class Particle(object): |
|
|
|
|
10
|
|
|
"""Implementation of https://gitlab.com/OPUS/matlabs-simulations/blob/master/src/PSO_G.m loop from 24th line""" |
|
|
|
|
11
|
|
|
|
12
|
|
|
def __init__(self, stages, process): |
13
|
|
|
self.stages = stages |
14
|
|
|
self.process = process |
15
|
|
|
self.position_updaters = {} |
16
|
|
|
self.velocity_calculators = {} |
17
|
|
|
self.current_velocities = {} |
18
|
|
|
self.current_quality = {} |
19
|
|
|
self.best_quality = np.inf |
20
|
|
|
self.best_positions = {} |
21
|
|
|
|
22
|
|
|
def initialize(self): |
|
|
|
|
23
|
|
|
for stage in self.stages: |
24
|
|
|
self.position_updaters[stage] = PositionUpdater(stage) |
25
|
|
|
self.velocity_updaters[stage] = VelocityUpdater(stage) |
|
|
|
|
26
|
|
|
self._set_initial_positions() |
27
|
|
|
self._set_initial_velocities() |
28
|
|
|
|
29
|
|
|
def _set_initial_positions(self): |
|
|
|
|
30
|
|
|
for stage in self.stages: |
31
|
|
|
self.position_updaters[stage].set_initial_control_params() |
32
|
|
|
|
33
|
|
|
def _set_initial_velocities(self): |
|
|
|
|
34
|
|
|
for stage in self.stages: |
35
|
|
|
self.current_velocities[stage] = self.velocity_updaters[ |
|
|
|
|
36
|
|
|
stage].calculate_initial_velocity() |
37
|
|
|
|
38
|
|
|
def update_values(self): |
|
|
|
|
39
|
|
|
self.update_input_vectors() |
40
|
|
|
self.calculate_current_quality() |
41
|
|
|
self._update_best_position() |
42
|
|
|
|
43
|
|
|
def _update_best_position(self): |
|
|
|
|
44
|
|
|
current_quality = self.get_the_overall_quality() |
45
|
|
|
if current_quality < self.best_quality: |
46
|
|
|
self.best_quality = current_quality |
47
|
|
|
for stage in self.stages: |
48
|
|
|
self.best_positions[stage] = stage.control_params |
49
|
|
|
|
50
|
|
|
def update_input_vectors(self): |
|
|
|
|
51
|
|
|
for stage in self.stages: |
52
|
|
|
current_output = stage.get_output_of_stage() |
53
|
|
|
successors = self.process.successors(stage) |
54
|
|
|
for successor in successors: |
55
|
|
|
successor.input_vector = current_output |
56
|
|
|
|
57
|
|
|
def calculate_current_quality(self): |
|
|
|
|
58
|
|
|
for stage in self.stages: |
59
|
|
|
self.current_quality[stage] = stage.get_quality() |
60
|
|
|
|
61
|
|
|
def get_the_overall_quality(self): |
|
|
|
|
62
|
|
|
stage = max(self.current_quality, key=self.current_quality.get) |
63
|
|
|
return self.current_quality[stage] |
64
|
|
|
|
65
|
|
|
def move(self): |
|
|
|
|
66
|
|
|
for stage in self.stages: |
67
|
|
|
velocity = self.current_velocities[stage] |
68
|
|
|
self.position_updaters[stage].update_position(velocity) |
69
|
|
|
pass |
|
|
|
|
70
|
|
|
|
71
|
|
|
def update_velocieties(self, best_particle): |
|
|
|
|
72
|
|
|
for stage in self.stages: |
73
|
|
|
velocity = self.velocity_calculators[stage].calculate( |
74
|
|
|
self.best_positions[stage], |
75
|
|
|
best_particle.best_positions[stage]) |
76
|
|
|
self.current_velocities[stage] = velocity |
77
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.py
files in your module folders. Make sure that you place one file in each sub-folder.