1
|
|
|
"""Represents optimization strategy for group in PSO.""" |
2
|
|
|
# pylint: disable=redefined-variable-type |
|
|
|
|
3
|
1 |
|
import numpy as np |
|
|
|
|
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): |
|
|
|
|
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): |
|
|
|
|
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): |
|
|
|
|
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): |
|
|
|
|
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): |
|
|
|
|
62
|
|
|
return best_particle.best_quality <= self.expected_quality |
63
|
|
|
|