Completed
Pull Request — master (#43)
by Wojtek
02:45
created

Particle._update_best_position()   A

Complexity

Conditions 3

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 6
cts 6
cp 1
crap 3
rs 9.4285
1
"""Representation of a particle in swarm."""
2
# pylint: disable=too-many-instance-attributes
0 ignored issues
show
introduced by
Locally disabling too-many-instance-attributes (R0902)
Loading history...
3
# pylint: disable=redefined-variable-type
0 ignored issues
show
introduced by
Bad option value 'redefined-variable-type'
Loading history...
4
5 1
import numpy as np
0 ignored issues
show
Configuration introduced by
The import numpy could not be resolved.

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.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

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.

Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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