Completed
Push — master ( 45e830...996a6f )
by Wojtek
02:50
created

CallsGroupOptimizationStrategy.finalize()   A

Complexity

Conditions 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
"""Represents optimization strategy for group in PSO."""
2
# pylint: disable=redefined-variable-type
0 ignored issues
show
introduced by
Bad option value 'redefined-variable-type'
Loading history...
3 1
import numpy as np
0 ignored issues
show
Configuration introduced by
The import numpy could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
4
5 1
from grortir.main.model.core.optimization_status import OptimizationStatus
6 1
from grortir.main.pso.group_optimization_strategy import \
7
    GroupOptimizationStrategy
8
9
10 1
class CallsGroupOptimizationStrategy(GroupOptimizationStrategy):
11
    """Represents optimization strategy for group in PSO."""
12
13 1
    def __init__(self, stages_in_group):
14 1
        self.stages_in_group = stages_in_group
15 1
        self.max_cost = 0
16 1
        self.expected_quality = np.inf
17
18 1
    def initialize(self):
0 ignored issues
show
Bug introduced by
Arguments number differs from overridden 'initialize' method
Loading history...
19
        """
20
            Initialize strategy.
21
        """
22 1
        max_cost = 0
23 1
        for stage in self.stages_in_group:
24 1
            max_cost += stage.get_maximal_acceptable_cost()
25 1
            if self.expected_quality > stage.maximum_acceptable_quality:
26 1
                self.expected_quality = stage.maximum_acceptable_quality
27 1
        self.max_cost = max_cost
28
29 1
    def should_continue(self, best_particle):
0 ignored issues
show
Bug introduced by
Arguments number differs from overridden 'should_continue' method
Loading history...
30
        """
31
        Return true if optimization should be continued for Calls Process.
32
        Args:
33
            best_particle Particle: best particle in swarm.
34
35
        Returns:
36
            bool: true if continuation is required.
37
38
        """
39 1
        return self._is_safe_cost() and not self._is_enough_quality(
40
            best_particle)
41
42 1
    def finalize(self, best_particle):
0 ignored issues
show
Bug introduced by
Arguments number differs from overridden 'finalize' method
Loading history...
43
        """
44
        Set proper status after finished group optimization.
45
        Args:
46
            best_particle (Particle): best particle in Swarm
47
        """
48 1
        optimization_status = OptimizationStatus.failed
49 1
        if self._is_safe_cost() and self._is_enough_quality(
50
                best_particle):
51 1
            optimization_status = OptimizationStatus.success
52 1
        for stage in self.stages_in_group:
53 1
            stage.optimization_status = optimization_status
54
55 1
    def _is_safe_cost(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
56 1
        current_cost = 0
57 1
        for stage in self.stages_in_group:
58 1
            current_cost += stage.get_cost()
59 1
        return current_cost < self.max_cost
60
61 1
    def _is_enough_quality(self, best_particle):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
62
        return best_particle.best_quality <= self.expected_quality
63