Completed
Push — master ( 40c094...b6a1c4 )
by Wojtek
02:30
created

Particle._update_best_position()   A

Complexity

Conditions 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
ccs 7
cts 7
cp 1
crap 3
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 1
from collections import OrderedDict
5
6 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...
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):
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...
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):
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...
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):
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...
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):
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...
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