Completed
Push — master ( b6a1c4...aa0ea9 )
by Wojtek
02:08
created

CallsStage   A

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