Completed
Pull Request — master (#36)
by Wojtek
09:58
created

Optimizer.set_custom_optimizing_order()   A

Complexity

Conditions 3

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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