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

Particle.update_velocieties()   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
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 VelocityCalculator
7
from main.model.core.optimization_status import OptimizationStatus
0 ignored issues
show
Bug introduced by
The name model does not seem to exist in module main.
Loading history...
Configuration introduced by
The import main.model.core.optimization_status 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...
8
9
10
class Particle(object):
0 ignored issues
show
best-practice introduced by
Too many instance attributes (8/7)
Loading history...
11
    """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...
12
13
    def __init__(self, stages, process):
14
        self.stages = stages
15
        self.process = process
16
        self.position_updaters = {}
17
        self.velocity_calculators = {}
18
        self.current_velocities = {}
19
        self.current_quality = {}
20
        self.best_quality = np.inf
21
        self.best_positions = {}
22
23
    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...
24
        for stage in self.stages:
25
            self.position_updaters[stage] = PositionUpdater(stage)
26
            self.velocity_calculators[stage] = VelocityCalculator(stage)
27
            stage.optimization_status = OptimizationStatus.in_progress
28
        self._set_initial_positions()
29
        self._set_initial_velocities()
30
31
    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...
32
        for stage in self.stages:
33
            self.position_updaters[stage].set_initial_control_params()
34
35
    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...
36
        for stage in self.stages:
37
            self.current_velocities[stage] = self.velocity_calculators[
38
                stage].calculate_initial_velocity()
39
40
    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...
41
        self.update_input_vectors()
42
        self.calculate_current_quality()
43
        self._update_stages_status()
44
        self._update_best_position()
45
46
    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...
47
        current_quality = self.get_the_overall_quality()
48
        if current_quality < self.best_quality:
49
            self.best_quality = current_quality
50
            for stage in self.stages:
51
                self.best_positions[stage] = stage.control_params
52
53
    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...
54
        for stage in self.stages:
55
            current_output = stage.get_output_of_stage()
56
            successors = self.process.successors(stage)
57
            for successor in successors:
58
                successor.input_vector = current_output
59
60
    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...
61
        for stage in self.stages:
62
            self.current_quality[stage] = stage.get_quality()
63
64
    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...
65
        stage = max(self.current_quality, key=self.current_quality.get)
66
        return self.current_quality[stage]
67
68
    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...
69
        for stage in self.stages:
70
            velocity = self.current_velocities[stage]
71
            self.position_updaters[stage].update_position(velocity)
72
73
    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...
74
        for stage in self.stages:
75
            velocity = self.velocity_calculators[stage].calculate(
76
                self.best_positions[stage],
77
                best_particle.best_positions[stage])
78
            self.current_velocities[stage] = velocity
79
80
    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...
81
        for stage in self.stages:
82
            if stage.is_enough_quality():
83
                stage.optimization_status = OptimizationStatus.success
84
            if not stage.could_be_optimized():
85
                stage.optimization_status = OptimizationStatus.failed
86