1
|
|
|
"""Represents swarm.""" |
2
|
1 |
|
import logging |
3
|
|
|
|
4
|
1 |
|
import numpy as np |
|
|
|
|
5
|
|
|
|
6
|
1 |
|
from grortir.main.pso.particle import Particle |
7
|
|
|
|
8
|
1 |
|
LOG = logging.getLogger(__name__) |
9
|
|
|
|
10
|
|
|
|
11
|
1 |
|
class Swarm(object): |
12
|
|
|
"""Class which represent swarm.""" |
13
|
|
|
|
14
|
1 |
|
def __init__(self, process, stages, number_of_particles): |
15
|
1 |
|
self.stages = stages |
16
|
1 |
|
self.process = process |
17
|
1 |
|
self.number_of_particles = number_of_particles |
18
|
1 |
|
self.particles = [Particle(stages, self.process, i) for i in |
19
|
|
|
range(number_of_particles)] |
20
|
1 |
|
self.best_particle_quality = np.inf |
21
|
1 |
|
self.best_particle = self.particles[0] |
22
|
1 |
|
LOG.debug('Swarm created.') |
23
|
|
|
|
24
|
1 |
|
def initialize(self): |
25
|
|
|
"""Initialize all particles in swarm.""" |
26
|
1 |
|
LOG.debug('Initialize swarm.') |
27
|
1 |
|
for particle in self.particles: |
28
|
1 |
|
particle.initialize() |
29
|
|
|
|
30
|
1 |
|
def do_single_iteration(self): |
31
|
|
|
"""Iterate one time.""" |
32
|
1 |
|
for particle in self.particles: |
33
|
1 |
|
particle.update_values() |
34
|
1 |
|
self._update_best_particle() |
35
|
1 |
|
self._update_velocieties() |
36
|
1 |
|
for particle in self.particles: |
37
|
1 |
|
particle.move() |
38
|
|
|
|
39
|
1 |
|
def _update_best_particle(self): |
|
|
|
|
40
|
1 |
|
for particle in self.particles: |
41
|
1 |
|
if particle.best_quality < self.best_particle_quality: |
42
|
1 |
|
LOG.debug('Update best particle.') |
43
|
1 |
|
self.best_particle = particle |
44
|
1 |
|
self.best_particle_quality = particle.best_quality |
45
|
1 |
|
LOG.debug('Current best quality is: ' + str( |
46
|
|
|
self.best_particle_quality)) |
47
|
1 |
|
LOG.debug('The best particle is: ' + str(self.best_particle.number)) |
48
|
|
|
|
49
|
1 |
|
def _update_velocieties(self): |
|
|
|
|
50
|
1 |
|
for particle in self.particles: |
51
|
1 |
|
particle.update_velocities(self.best_particle) |
52
|
|
|
|
53
|
1 |
|
def post_processing(self): |
54
|
|
|
"""Method which should be done after all iterations.""" |
55
|
|
|
# calculate output: |
56
|
1 |
|
if self.best_particle_quality == np.inf: |
57
|
1 |
|
self._post_process_not_run_stages() |
58
|
|
|
else: |
59
|
1 |
|
self._post_process_run_stages(self.best_particle.best_positions) |
60
|
|
|
|
61
|
1 |
|
def _post_process_run_stages(self, best_control_params): |
|
|
|
|
62
|
1 |
|
for stage in self.stages: |
63
|
1 |
|
current_output = stage.get_output_of_stage( |
64
|
|
|
stage.input_vector, best_control_params[stage]) |
65
|
1 |
|
successors = self.process.successors(stage) |
66
|
1 |
|
for successor in successors: |
67
|
1 |
|
successor.input_vector = current_output |
68
|
1 |
|
stage.final_output = current_output |
69
|
1 |
|
stage.final_cost = stage.get_cost() |
70
|
1 |
|
stage.final_quality = stage.get_quality( |
71
|
|
|
stage.input_vector, best_control_params[stage]) |
72
|
1 |
|
LOG.debug('Final stage status: ' + str(stage)) |
73
|
|
|
|
74
|
1 |
|
def _post_process_not_run_stages(self): |
|
|
|
|
75
|
1 |
|
for stage in self.stages: |
76
|
1 |
|
successors = self.process.successors(stage) |
77
|
1 |
|
for successor in successors: |
78
|
1 |
|
successor.input_vector = None |
79
|
1 |
|
stage.final_output = None |
80
|
1 |
|
stage.final_cost = None |
81
|
1 |
|
stage.final_quality = None |
82
|
1 |
|
LOG.debug('Optimization for this stage was not triggered.') |
83
|
|
|
LOG.debug('Final stage status: ' + str(stage)) |
84
|
|
|
|
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.