|
1
|
|
|
"""Test for intelligent credits.""" |
|
2
|
|
|
from unittest import TestCase |
|
3
|
|
|
|
|
4
|
|
|
from grortir.main.model.core.abstract_process import AbstractProcess |
|
5
|
|
|
from grortir.main.model.core.optimization_status import OptimizationStatus |
|
6
|
|
|
from grortir.main.model.stages.calls_stage import CallsStage |
|
7
|
|
|
from grortir.main.optimizers.grouping_strategy import GroupingStrategy |
|
8
|
|
|
from grortir.main.pso.credit_calls_optimization_strategy import \ |
|
9
|
|
|
CreditCallsOptimizationStrategy |
|
10
|
|
|
from grortir.main.pso.pso_algorithm import PsoAlgorithm |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class TestInteligentCredits(TestCase): |
|
|
|
|
|
|
14
|
|
|
def test_credit_strategy_success(self): |
|
|
|
|
|
|
15
|
|
|
pso_algorithm, stages = prepare_data() |
|
16
|
|
|
stages[7].max_calls += 27 |
|
17
|
|
|
pso_algorithm.run() |
|
18
|
|
|
is_success = pso_algorithm.process.optimization_status \ |
|
19
|
|
|
== OptimizationStatus.success |
|
20
|
|
|
self.assertTrue(is_success) |
|
21
|
|
|
|
|
22
|
|
|
def test_credit_strategy_fail_at_last_step(self): |
|
|
|
|
|
|
23
|
|
|
pso_algorithm, stages = prepare_data() |
|
24
|
|
|
stages[7].max_calls += 26 |
|
25
|
|
|
pso_algorithm.run() |
|
26
|
|
|
is_success = pso_algorithm.process.optimization_status \ |
|
27
|
|
|
== OptimizationStatus.success |
|
28
|
|
|
self.assertFalse(is_success) |
|
29
|
|
|
|
|
30
|
|
|
def test_credit_strategy_fail_between_groups(self): |
|
|
|
|
|
|
31
|
|
|
pso_algorithm, stages = prepare_data() |
|
32
|
|
|
stages[0].max_calls = 60 |
|
33
|
|
|
stages[1].max_calls = 60 |
|
34
|
|
|
stages[2].max_calls = 60 |
|
35
|
|
|
stages[3].max_calls = 60 |
|
36
|
|
|
stages[4].max_calls = 60 |
|
37
|
|
|
stages[5].max_calls = 0 |
|
38
|
|
|
stages[6].max_calls = 0 |
|
39
|
|
|
stages[7].max_calls = 0 |
|
40
|
|
|
pso_algorithm.run() |
|
41
|
|
|
is_success = pso_algorithm.process.optimization_status \ |
|
42
|
|
|
== OptimizationStatus.success |
|
43
|
|
|
self.assertFalse(is_success) |
|
44
|
|
|
for i in range(3, 8): |
|
45
|
|
|
self.assertIsNone(stages[i].final_output) |
|
46
|
|
|
self.assertIsNone(stages[i].final_cost) |
|
47
|
|
|
self.assertIsNone(stages[i].final_quality) |
|
48
|
|
|
self.assertEqual(stages[i].optimization_status, |
|
49
|
|
|
OptimizationStatus.failed) |
|
50
|
|
|
for i in range(0, 3): |
|
51
|
|
|
self.assertEqual(stages[i].optimization_status, |
|
52
|
|
|
OptimizationStatus.success) |
|
53
|
|
|
|
|
54
|
|
|
def test_credit_strategy_fail_on_group(self): |
|
|
|
|
|
|
55
|
|
|
pso_algorithm, stages = prepare_data() |
|
56
|
|
|
stages[0].max_calls = 60 |
|
57
|
|
|
stages[1].max_calls = 60 |
|
58
|
|
|
stages[2].max_calls = 60 |
|
59
|
|
|
stages[3].max_calls = 60 |
|
60
|
|
|
stages[4].max_calls = 60 |
|
61
|
|
|
stages[5].max_calls = 3 |
|
62
|
|
|
stages[6].max_calls = 0 |
|
63
|
|
|
stages[7].max_calls = 0 |
|
64
|
|
|
pso_algorithm.run() |
|
65
|
|
|
is_success = pso_algorithm.process.optimization_status \ |
|
66
|
|
|
== OptimizationStatus.success |
|
67
|
|
|
self.assertFalse(is_success) |
|
68
|
|
|
for i in range(3, 6): |
|
69
|
|
|
self.assertEqual(stages[i].final_cost, 2) |
|
70
|
|
|
self.assertEqual(stages[i].final_quality, 10000) |
|
71
|
|
|
self.assertEqual(stages[i].optimization_status, |
|
72
|
|
|
OptimizationStatus.failed) |
|
73
|
|
|
for i in range(6, 8): |
|
74
|
|
|
self.assertIsNone(stages[i].final_output) |
|
75
|
|
|
self.assertIsNone(stages[i].final_cost) |
|
76
|
|
|
self.assertIsNone(stages[i].final_quality) |
|
77
|
|
|
self.assertEqual(stages[i].optimization_status, |
|
78
|
|
|
OptimizationStatus.failed) |
|
79
|
|
|
for i in range(0, 3): |
|
80
|
|
|
self.assertEqual(stages[i].optimization_status, |
|
81
|
|
|
OptimizationStatus.success) |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
class ExampleProcess(AbstractProcess): |
|
|
|
|
|
|
85
|
|
|
pass |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
class FixedCallsStage(CallsStage): |
|
|
|
|
|
|
89
|
|
|
def __init__(self, name, max_calls, input_vector, on_which_cost_success): |
|
90
|
|
|
super().__init__(name, max_calls, input_vector) |
|
91
|
|
|
self.on_which_cost_success = on_which_cost_success |
|
92
|
|
|
|
|
93
|
|
|
def is_enough_quality(self, value): |
|
94
|
|
|
return self.on_which_cost_success <= self.get_cost() |
|
95
|
|
|
|
|
96
|
|
|
def calculate_quality(self, input_vector, control_params): |
|
|
|
|
|
|
97
|
|
|
if self.is_enough_quality(1): |
|
98
|
|
|
return 0 |
|
99
|
|
|
return 10000 |
|
100
|
|
|
|
|
101
|
|
|
def get_output_of_stage(self, input_vector, control_params): |
|
102
|
|
|
return input_vector |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
def prepare_data(): |
|
|
|
|
|
|
106
|
|
|
stages = {} |
|
107
|
|
|
for i in range(8): |
|
108
|
|
|
stages[i] = FixedCallsStage(str(i), 70, (0, 0, 0), (100 - i * 10)) |
|
109
|
|
|
# Summary max_calls is equal to 560 |
|
110
|
|
|
tested_process = ExampleProcess() |
|
111
|
|
|
# Our graph: |
|
112
|
|
|
# 0 |
|
113
|
|
|
# | |
|
114
|
|
|
# 1 |
|
115
|
|
|
# |\ |
|
116
|
|
|
# 2 4 |
|
117
|
|
|
# | |\ |
|
118
|
|
|
# 3 5 6 |
|
119
|
|
|
# \ |
|
120
|
|
|
# 7 |
|
121
|
|
|
# All edges directed to down |
|
122
|
|
|
# Order of nodes is the same as names |
|
123
|
|
|
tested_process.add_edge(stages[0], stages[1]) |
|
|
|
|
|
|
124
|
|
|
tested_process.add_edge(stages[1], stages[2]) |
|
|
|
|
|
|
125
|
|
|
tested_process.add_edge(stages[2], stages[3]) |
|
|
|
|
|
|
126
|
|
|
tested_process.add_edge(stages[1], stages[4]) |
|
|
|
|
|
|
127
|
|
|
tested_process.add_edge(stages[4], stages[5]) |
|
|
|
|
|
|
128
|
|
|
tested_process.add_edge(stages[4], stages[6]) |
|
|
|
|
|
|
129
|
|
|
tested_process.add_edge(stages[6], stages[7]) |
|
|
|
|
|
|
130
|
|
|
# Groups: |
|
131
|
|
|
# (0)0 |
|
132
|
|
|
# | |
|
133
|
|
|
# (0)1 |
|
134
|
|
|
# |\ |
|
135
|
|
|
# (0)2 4(1) |
|
136
|
|
|
# | | \ |
|
137
|
|
|
# (1)3 5(1)6(2) |
|
138
|
|
|
# \ |
|
139
|
|
|
# 7(3) |
|
140
|
|
|
# Minimum required steps to success: 3*101+3*71+1*41+1*31= 588 |
|
141
|
|
|
grouping_strategy = GroupingStrategy(list(stages.values())) |
|
142
|
|
|
grouping_strategy.define_group((stages[0], stages[1], stages[2])) |
|
143
|
|
|
grouping_strategy.define_group((stages[3], stages[4], stages[5])) |
|
144
|
|
|
grouping_strategy.define_group((stages[6],)) |
|
145
|
|
|
grouping_strategy.define_group((stages[7],)) |
|
146
|
|
|
optimization_strategy = CreditCallsOptimizationStrategy() |
|
147
|
|
|
pso_algorithm = PsoAlgorithm(tested_process, grouping_strategy, |
|
148
|
|
|
optimization_strategy, 2) |
|
149
|
|
|
return pso_algorithm, stages |
|
150
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.