1
|
|
|
"""Tests for pyswarm.""" |
2
|
|
|
from unittest import TestCase |
3
|
|
|
|
4
|
|
|
from grortir.externals.pyswarm.pso import pso |
5
|
|
|
from grortir.main.model.core.abstract_stage import AbstractStage |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class TestPso(TestCase): |
9
|
|
|
"""Class for tests pyswarm.""" |
10
|
|
|
|
11
|
|
|
def test_run_simple_pso(self): |
12
|
|
|
"""Test running library.""" |
13
|
|
|
|
14
|
|
|
stage_to_test = ExampleStage(EXAMPLE_INPUT) |
15
|
|
|
x_opt, f_opt, iterations = pso(stage=stage_to_test) |
|
|
|
|
16
|
|
|
self.assertIsNotNone(x_opt) |
17
|
|
|
self.assertIsNotNone(f_opt) |
18
|
|
|
self.assertIsNotNone(iterations) |
19
|
|
|
|
20
|
|
|
def test_not_able_to_optimize_stage(self): |
21
|
|
|
"""Test when stage is not able to optimize.""" |
22
|
|
|
stage_not_able_to_optimize = StageNotAbleToOptimize(EXAMPLE_INPUT) |
23
|
|
|
x_opt, f_opt, iterations = pso(stage=stage_not_able_to_optimize) |
|
|
|
|
24
|
|
|
self.assertIsNotNone(x_opt) |
25
|
|
|
self.assertIsNotNone(f_opt) |
26
|
|
|
self.assertIsNotNone(iterations) |
27
|
|
|
self.assertEqual(iterations, 1) |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
# Test data: |
31
|
|
|
LOWER_BOUND = [-3, -1] |
32
|
|
|
UPPER_BOUND = [2, 6] |
33
|
|
|
EXAMPLE_INPUT = (-1, 3) |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
class ExampleStage(AbstractStage): |
|
|
|
|
37
|
|
|
"""Stage for testing.""" |
38
|
|
|
|
39
|
|
|
def __init__(self, input_vector): |
40
|
|
|
super().__init__(input_vector) |
41
|
|
|
self.lower_bounds = LOWER_BOUND |
42
|
|
|
self.upper_bounds = UPPER_BOUND |
43
|
|
|
self.counter = 0 |
44
|
|
|
|
45
|
|
|
@staticmethod |
46
|
|
|
def get_cost(): |
47
|
|
|
"""No costs.""" |
48
|
|
|
return 0 |
49
|
|
|
|
50
|
|
|
def get_quality(self, vector=None): |
|
|
|
|
51
|
|
|
"""Return quality.""" |
52
|
|
|
self.control_params = vector |
53
|
|
|
self.counter += 1 |
54
|
|
|
return myfunc(self.control_params) |
55
|
|
|
|
56
|
|
|
@staticmethod |
57
|
|
|
def could_be_optimized(): |
58
|
|
|
"""Return True.""" |
59
|
|
|
return True |
60
|
|
|
|
61
|
|
|
def is_enough_quality(self, value): |
|
|
|
|
62
|
|
|
"""Return False.""" |
63
|
|
|
return False |
64
|
|
|
|
65
|
|
|
@staticmethod |
66
|
|
|
def get_output_of_stage(): |
67
|
|
|
"""Return None.""" |
68
|
|
|
return None |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
class StageNotAbleToOptimize(ExampleStage): |
|
|
|
|
72
|
|
|
"""Stage which couldn't be optimized.""" |
73
|
|
|
|
74
|
|
|
@staticmethod |
75
|
|
|
def could_be_optimized(): |
76
|
|
|
"""Returns false.""" |
77
|
|
|
return False |
78
|
|
|
|
79
|
|
|
@staticmethod |
80
|
|
|
def get_output_of_stage(): |
81
|
|
|
"""Return None.""" |
82
|
|
|
return None |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
def myfunc(input_vector): |
86
|
|
|
"""Simple function for tests. |
87
|
|
|
|
88
|
|
|
Args: |
89
|
|
|
input_vector (list): input vector |
90
|
|
|
|
91
|
|
|
Returns: |
92
|
|
|
float: value of function |
93
|
|
|
""" |
94
|
|
|
x_1 = input_vector[0] |
95
|
|
|
x_2 = input_vector[1] |
96
|
|
|
return x_1 ** 4 - 2 * x_2 * x_1 ** 2 + x_2 ** 2 + x_1 ** 2 - 2 * x_1 + 5 |
97
|
|
|
|
This happens when the amount of values does not equal the amount of labels: