Completed
Push — master ( f62864...bebaaf )
by Wojtek
02:41
created

Optimizer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
dl 0
loc 33
rs 10
ccs 20
cts 20
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A set_custom_optimizing_order() 0 7 3
A __init__() 0 3 1
B optimize_process() 0 18 5
1
"""Basic optimizer."""
2 1
import networkx as nx
0 ignored issues
show
Configuration introduced by
The import networkx 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...
3 1
from grortir.externals.pyswarm.pso import pso
4 1
from grortir.main.model.core.optimization_status import OptimizationStatus
5
6
7 1
class Optimizer(object):
8
    """Optimizer is object which optimize process."""
9
10 1
    def __init__(self, process):
11 1
        self.process = process
12 1
        self.ordered_stages = nx.topological_sort(self.process)
13
14 1
    def set_custom_optimizing_order(self, ordered_stages):
15
        """Set custom order."""
16 1
        if set(self.ordered_stages) == set(ordered_stages) and len(
17
                self.ordered_stages) == len(ordered_stages):
18 1
            self.ordered_stages = ordered_stages
19
        else:
20 1
            raise ValueError("List of stages must contain all stages.")
21
22 1
    def optimize_process(self):
23
        """Optimize process.
24
        Returns:
25
            True if success, False otherwise."""
26 1
        for stage in self.ordered_stages:
27 1
            if not self.process.predecessors(stage):
28 1
                pso(stage)
29 1
            elif len(self.process.predecessors(stage)) == 1:
30 1
                predecessor = self.process.predecessors(stage)[0]
31 1
                stage.input_vector = predecessor.get_output_of_stage()
32 1
                pso(stage)
33
            else:
34 1
                raise AttributeError('Incorrect process structure.')
35
36 1
            if stage.optimization_status != OptimizationStatus.success:
37 1
                return False
38
39
        return True
40