CallsStage   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 80
ccs 31
cts 31
cp 1
rs 10
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 9 1
A get_quality() 0 12 2
A get_cost() 0 8 1
A get_output_of_stage() 0 3 1
A calculate_quality() 0 18 3
A get_maximal_acceptable_cost() 0 3 1
A is_enough_quality() 0 3 1
A could_be_optimized() 0 3 1
A __str__() 0 2 1
1
"""Module contains class CallsStage."""
2 1
from pprint import pformat
3
4 1
from grortir.main.model.core.abstract_stage import AbstractStage
5
6
7
# pylint: disable=arguments-differ,unused-argument
0 ignored issues
show
introduced by
Locally disabling arguments-differ (W0221)
Loading history...
introduced by
Locally disabling unused-argument (W0613)
Loading history...
8
9
10 1
class CallsStage(AbstractStage):
11
    """Implementation of basic stage.
12
13
    Cost is calculated by number of calls of cost function.
14
    Attributes:
15
        cost (float): Actual cost of stage.
16
        max_calls (int): Maximum possible calls of cost quality function
17
        name (str): Name of stage
18
        maximum_acceptable_quality (float): max expected quality
19
    """
20
21 1
    def __init__(self, name, max_calls, input_vector=(),
22
                 maximum_acceptable_quality=0.01):
23
        """Constructor."""
24 1
        super().__init__(input_vector)
25 1
        self.max_calls = max_calls
26 1
        self.name = name
27 1
        self.control_params = [0] * len(self.input_vector)
28 1
        self.maximum_acceptable_quality = maximum_acceptable_quality
29 1
        self.cost = 0
30
31 1
    def get_quality(self, input_vector=None, control_params=None):
32
        """
33
        Return quality of actual output.
34
35
        Returns:
36
            float: quality
37
38
        """
39 1
        if control_params is None:
40 1
            control_params = self.control_params[:]
41 1
        self.cost += 1
42 1
        return self.calculate_quality(input_vector, control_params)
43
44 1
    @staticmethod
45
    def calculate_quality(input_vector, control_params):
46
        """
47
        Function for calculating quality.
48
49
        Returns:
50
            float: quality
51
52
        Raises:
53
            AssertionError: If length of `control_params`
54
                is not equal length of `input_vector`
55
        """
56 1
        assert len(control_params) == len(input_vector)
57 1
        quality = 0
58 1
        for i in enumerate(control_params):
59 1
            quality += (control_params[i[0]] - input_vector[
60
                i[0]]) ** 2
61 1
        return quality
62
63 1
    def get_cost(self):
64
        """
65
        Return actual cost of stage.
66
67
        Returns:
68
            float: cost
69
        """
70 1
        return self.cost
71
72 1
    def could_be_optimized(self):
73
        """Return answer if it is still possible to optimize that stage."""
74 1
        return self.get_cost() < self.get_maximal_acceptable_cost()
75
76 1
    def is_enough_quality(self, value):
77
        """Return True if value is proper quality."""
78 1
        return value <= self.maximum_acceptable_quality
79
80 1
    def get_output_of_stage(self, input_vector, control_params):
81
        """Return output of stage."""
82 1
        return control_params
83
84 1
    def get_maximal_acceptable_cost(self):
85
        """Return maximum acceptable cost."""
86 1
        return self.max_calls
87
88 1
    def __str__(self):
89
        return pformat(self.__dict__)
90