VelocityCalculator.__init__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
ccs 5
cts 5
cp 1
crap 1
1
"""Contains things related to calculating velocity."""
2
3 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...
4
5
6 1
class VelocityCalculator:
7
    """Calculate Velocity for a single stage.
8
9
    Attributes:
10
        current_velocity (list) : list of coordinates of speed for current stage
11
        """
12
13 1
    def __init__(self):
14 1
        self.c_1 = 1
15 1
        self.c_2 = 1
16 1
        self.w_factor = 0.8
17 1
        self.current_velocity = None
18
19 1
    @staticmethod
20
    def calculate_initial_velocity(control_params):
21
        """Calculate initial velocity."""
22 1
        velocities = {}
23 1
        for stage in control_params:
24 1
            random_factor = np.random.rand(len(control_params[stage]))
25 1
            velocity = 0.02 * random_factor - 0.01
26 1
            velocities[stage] = velocity
27 1
        return velocities
28
29 1
    def calculate(self, current_velocities, particle_best_positions,
30
                  leader_best_positions,
31
                  control_params):
32
        """Calculate velocity."""
33 1
        rand_1 = np.random.rand()
34 1
        rand_2 = np.random.rand()
35 1
        velocities = {}
36 1
        for stage in control_params:
37 1
            s_0 = self._s0(current_velocities[stage])
38 1
            s_1 = self.c_1 * self._s1(rand_1, leader_best_positions[stage],
39
                                      control_params[stage])
40 1
            s_2 = self.c_2 * self._s2(rand_2, particle_best_positions[stage],
41
                                      control_params[stage])
42 1
            velocity = s_0 + s_1 + s_2
43 1
            velocities[stage] = velocity.tolist()
44 1
        return velocities
45
46 1
    @staticmethod
47
    def _s2(random_factor, particle_best_position, control_params):
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...
48 1
        return random_factor * (
49
            np.asarray(particle_best_position) - np.asarray(
50
                control_params))
51
52 1
    @staticmethod
53
    def _s1(random_factor, leader_best_position, control_params):
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 1
        return random_factor * (
55
            np.asarray(leader_best_position) - np.asarray(
56
                control_params))
57
58 1
    def _s0(self, current_velocity):
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...
59
        return self.w_factor * np.asarray(current_velocity)
60