TestPso.test_run_simple_pso()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
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)
0 ignored issues
show
Bug introduced by
The tuple unpacking with sequence seems to be unbalanced; 3 value(s) for 0 label(s)

This happens when the amount of values does not equal the amount of labels:

a, b = ("a", "b", "c")  # only 2 labels for 3 values
Loading history...
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)
0 ignored issues
show
Bug introduced by
The tuple unpacking with sequence seems to be unbalanced; 3 value(s) for 0 label(s)

This happens when the amount of values does not equal the amount of labels:

a, b = ("a", "b", "c")  # only 2 labels for 3 values
Loading history...
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):
0 ignored issues
show
Bug introduced by
The method get_maximal_acceptable_cost which was declared abstract in the super-class AbstractStage
was not overridden.

Methods which raise NotImplementedError should be overridden in concrete child classes.

Loading history...
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):
0 ignored issues
show
Bug Best Practice introduced by
Signature differs from overridden 'get_quality' method

It is generally a good practice to use signatures that are compatible with the Liskov substitution principle.

This allows to pass instances of the child class anywhere where the instances of the super-class/interface would be acceptable.

Loading history...
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):
0 ignored issues
show
Bug introduced by
Arguments number differs from overridden 'is_enough_quality' method
Loading history...
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):
0 ignored issues
show
Bug introduced by
The method get_maximal_acceptable_cost which was declared abstract in the super-class AbstractStage
was not overridden.

Methods which raise NotImplementedError should be overridden in concrete child classes.

Loading history...
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