1
|
|
|
"""Contains things related to calculating velocity.""" |
2
|
|
|
|
3
|
1 |
|
import numpy as np |
|
|
|
|
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): |
|
|
|
|
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): |
|
|
|
|
54
|
1 |
|
return random_factor * ( |
55
|
|
|
np.asarray(leader_best_position) - np.asarray( |
56
|
|
|
control_params)) |
57
|
|
|
|
58
|
1 |
|
def _s0(self, current_velocity): |
|
|
|
|
59
|
|
|
return self.w_factor * np.asarray(current_velocity) |
60
|
|
|
|
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.
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.