|
1
|
|
|
from unittest import TestCase |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
import numpy as np |
|
|
|
|
|
|
4
|
|
|
import unittest.mock as mo |
|
5
|
|
|
|
|
6
|
|
|
from grortir.main.pso.swarm import Swarm |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class TestSwarm(TestCase): |
|
|
|
|
|
|
10
|
|
|
def setUp(self): |
|
11
|
|
|
self.stages = mo.Mock() |
|
12
|
|
|
self.process = mo.Mock() |
|
13
|
|
|
self.number_of_particles = 40 |
|
14
|
|
|
self.tested_object = Swarm(self.process, self.stages, |
|
15
|
|
|
self.number_of_particles) |
|
16
|
|
|
self.mock_of_swarm = mo.Mock() |
|
17
|
|
|
self.mock_of_particle_1 = mo.Mock() |
|
18
|
|
|
self.mock_of_particle_2 = mo.Mock() |
|
19
|
|
|
self.mock_of_swarm.particles = [self.mock_of_particle_1, |
|
20
|
|
|
self.mock_of_particle_2] |
|
21
|
|
|
self.mock_of_best_particle = mo.Mock() |
|
22
|
|
|
self.mock_of_swarm.best_particle = self.mock_of_best_particle |
|
23
|
|
|
|
|
24
|
|
|
def test_constructor(self): |
|
|
|
|
|
|
25
|
|
|
self.assertIsNotNone(self.tested_object) |
|
26
|
|
|
self.assertEqual(self.stages, self.tested_object.stages) |
|
27
|
|
|
self.assertEqual(self.tested_object.number_of_particles, |
|
28
|
|
|
self.number_of_particles) |
|
29
|
|
|
|
|
30
|
|
|
def test_initialize(self): |
|
|
|
|
|
|
31
|
|
|
Swarm.initialize(self.mock_of_swarm) |
|
32
|
|
|
self.mock_of_particle_1.initialize.assert_any_call() |
|
33
|
|
|
self.mock_of_particle_2.initialize.assert_any_call() |
|
34
|
|
|
|
|
35
|
|
|
def test_do_single_iteration(self): |
|
|
|
|
|
|
36
|
|
|
self.mock_of_swarm.particles = [self.mock_of_particle_1, |
|
37
|
|
|
self.mock_of_particle_1] |
|
38
|
|
|
expected_calls_particle = [mo.call.update_values(), |
|
39
|
|
|
mo.call.update_values(), |
|
40
|
|
|
mo.call.move(), mo.call.move()] |
|
41
|
|
|
expected_calls_swarm = [mo.call._update_best_particle(), |
|
|
|
|
|
|
42
|
|
|
mo.call._update_velocieties()] |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
Swarm.do_single_iteration(self.mock_of_swarm) |
|
45
|
|
|
|
|
46
|
|
|
self.assertListEqual(expected_calls_particle, |
|
47
|
|
|
self.mock_of_particle_1.method_calls) |
|
48
|
|
|
self.assertListEqual(expected_calls_swarm, |
|
49
|
|
|
self.mock_of_swarm.method_calls) |
|
50
|
|
|
|
|
51
|
|
|
def test__update_best_particle_1(self): |
|
|
|
|
|
|
52
|
|
|
self.mock_of_swarm.best_particle_quality = np.inf |
|
53
|
|
|
self.mock_of_particle_1.best_quality = 7 |
|
54
|
|
|
self.mock_of_particle_2.best_quality = 6 |
|
55
|
|
|
|
|
56
|
|
|
Swarm._update_best_particle(self.mock_of_swarm) |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
self.assertEqual(self.mock_of_swarm.best_particle_quality, 6) |
|
59
|
|
|
self.assertEqual(self.mock_of_swarm.best_particle, |
|
60
|
|
|
self.mock_of_particle_2) |
|
61
|
|
|
|
|
62
|
|
|
def test__update_best_particle_2(self): |
|
|
|
|
|
|
63
|
|
|
self.mock_of_swarm.best_particle_quality = np.inf |
|
64
|
|
|
self.mock_of_particle_1.best_quality = 7 |
|
65
|
|
|
self.mock_of_particle_2.best_quality = 8 |
|
66
|
|
|
|
|
67
|
|
|
Swarm._update_best_particle(self.mock_of_swarm) |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
self.assertEqual(self.mock_of_swarm.best_particle_quality, 7) |
|
70
|
|
|
self.assertEqual(self.mock_of_swarm.best_particle, |
|
71
|
|
|
self.mock_of_particle_1) |
|
72
|
|
|
|
|
73
|
|
|
def test__update_velocieties(self): |
|
|
|
|
|
|
74
|
|
|
Swarm._update_velocieties(self.mock_of_swarm) |
|
|
|
|
|
|
75
|
|
|
self.mock_of_particle_1.update_velocities.assert_called_once_with( |
|
76
|
|
|
self.mock_of_best_particle) |
|
77
|
|
|
self.mock_of_particle_2.update_velocities.assert_called_once_with( |
|
78
|
|
|
self.mock_of_best_particle) |
|
79
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.