Completed
Pull Request — master (#43)
by Wojtek
09:22
created

Particle.initialize()   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
1
"""Representation of a particle in swarm."""
2
3
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...
4
5
from grortir.main.pso.position_updater import PositionUpdater
6
from grortir.main.pso.velocity_calculator import VelocityUpdater
0 ignored issues
show
Bug introduced by
The name VelocityUpdater does not seem to exist in module grortir.main.pso.velocity_calculator.
Loading history...
7
8
9
class Particle(object):
0 ignored issues
show
best-practice introduced by
Too many instance attributes (8/7)
Loading history...
10
    """Implementation of https://gitlab.com/OPUS/matlabs-simulations/blob/master/src/PSO_G.m loop from 24th line"""
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (115/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
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):
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...
23
        for stage in self.stages:
24
            self.position_updaters[stage] = PositionUpdater(stage)
25
            self.velocity_updaters[stage] = VelocityUpdater(stage)
0 ignored issues
show
Bug introduced by
The Instance of Particle does not seem to have a member named velocity_updaters.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
26
        self._set_initial_positions()
27
        self._set_initial_velocities()
28
29
    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...
30
        for stage in self.stages:
31
            self.position_updaters[stage].set_initial_control_params()
32
33
    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...
34
        for stage in self.stages:
35
            self.current_velocities[stage] = self.velocity_updaters[
0 ignored issues
show
Bug introduced by
The Instance of Particle does not seem to have a member named velocity_updaters.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
36
                stage].calculate_initial_velocity()
37
38
    def update_values(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...
39
        self.update_input_vectors()
40
        self.calculate_current_quality()
41
        self._update_best_position()
42
43
    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...
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):
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...
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):
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...
58
        for stage in self.stages:
59
            self.current_quality[stage] = stage.get_quality()
60
61
    def get_the_overall_quality(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...
62
        stage = max(self.current_quality, key=self.current_quality.get)
63
        return self.current_quality[stage]
64
65
    def move(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...
66
        for stage in self.stages:
67
            velocity = self.current_velocities[stage]
68
            self.position_updaters[stage].update_position(velocity)
69
        pass
0 ignored issues
show
Unused Code introduced by
Unnecessary pass statement
Loading history...
70
71
    def update_velocieties(self, best_particle):
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...
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