|
1
|
|
|
"""Module for testing grouped optimizer.""" |
|
2
|
|
|
|
|
3
|
|
|
from unittest import TestCase |
|
4
|
|
|
from unittest.mock import Mock, call |
|
5
|
|
|
|
|
6
|
|
|
from grortir.main.model.core.abstract_process import AbstractProcess |
|
7
|
|
|
from grortir.main.model.core.abstract_stage import AbstractStage |
|
8
|
|
|
from grortir.main.optimizers.grouped_optimizer import GroupedOptimizer |
|
9
|
|
|
from grortir.main.optimizers.grouping_strategy import GroupingStrategy |
|
10
|
|
|
from grortir.main.pso.pso_algorithm import PsoAlgorithm |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class TestGroupedOptimizer(TestCase): |
|
14
|
|
|
"""Class for testing Optimizer.""" |
|
15
|
|
|
|
|
16
|
|
|
def setUp(self): |
|
17
|
|
|
"""Set up environment.""" |
|
18
|
|
|
self.some_process = AbstractProcess() |
|
19
|
|
|
self.first_stage = AbstractStage() |
|
20
|
|
|
self.second_stage = AbstractStage() |
|
21
|
|
|
self.third_stage_a = Mock() |
|
22
|
|
|
self.third_stage_b = Mock() |
|
23
|
|
|
self.some_process.add_path([self.first_stage, self.second_stage, |
|
|
|
|
|
|
24
|
|
|
self.third_stage_a]) |
|
25
|
|
|
self.some_process.add_edge(self.second_stage, self.third_stage_b) |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
def test___init__(self): |
|
28
|
|
|
"""Testing creating object.""" |
|
29
|
|
|
optimizer = GroupedOptimizer(self.some_process, Mock(), Mock()) |
|
30
|
|
|
self.assertIsNotNone(optimizer) |
|
31
|
|
|
self.assertEqual(optimizer.process, self.some_process) |
|
32
|
|
|
|
|
33
|
|
|
def test_calling_functions(self): |
|
34
|
|
|
"""Test correct order of calling function.""" |
|
35
|
|
|
optimizer = GroupedOptimizer(TESTED_PROCESS, GROUPING_STRATEGY, |
|
36
|
|
|
PSO_ALGORITHM) |
|
37
|
|
|
expected_calls = [call.method_name('0', '__init__'), |
|
38
|
|
|
call.method_name('1', '__init__'), |
|
39
|
|
|
call.method_name('2', '__init__'), |
|
40
|
|
|
call.method_name('3', '__init__'), |
|
41
|
|
|
call.method_name('4', '__init__'), |
|
42
|
|
|
call.method_name('5', '__init__'), |
|
43
|
|
|
call.method_name('6', '__init__'), |
|
44
|
|
|
call.method_name('0', 'get_output_of_stage', 1), |
|
45
|
|
|
call.method_name('1', 'get_output_of_stage', 1), |
|
46
|
|
|
call.method_name('2', 'get_output_of_stage', 1), |
|
47
|
|
|
call.method_name('0', 'get_quality', 1), |
|
48
|
|
|
call.method_name('1', 'get_quality', 1), |
|
49
|
|
|
call.method_name('2', 'get_quality', 1), |
|
50
|
|
|
call.method_name('0', 'is_enough_quality', 1), |
|
51
|
|
|
call.method_name('0', 'could_be_optimized', 1), |
|
52
|
|
|
call.method_name('1', 'is_enough_quality', 1), |
|
53
|
|
|
call.method_name('1', 'could_be_optimized', 1), |
|
54
|
|
|
call.method_name('2', 'is_enough_quality', 1), |
|
55
|
|
|
call.method_name('2', 'could_be_optimized', 1), |
|
56
|
|
|
call.method_name('0', 'get_output_of_stage', 2), |
|
57
|
|
|
call.method_name('1', 'get_output_of_stage', 2), |
|
58
|
|
|
call.method_name('2', 'get_output_of_stage', 2), |
|
59
|
|
|
call.method_name('0', 'get_quality', 2), |
|
60
|
|
|
call.method_name('1', 'get_quality', 2), |
|
61
|
|
|
call.method_name('2', 'get_quality', 2), |
|
62
|
|
|
call.method_name('0', 'is_enough_quality', 2), |
|
63
|
|
|
call.method_name('0', 'could_be_optimized', 2), |
|
64
|
|
|
call.method_name('1', 'is_enough_quality', 2), |
|
65
|
|
|
call.method_name('1', 'could_be_optimized', 2), |
|
66
|
|
|
call.method_name('2', 'is_enough_quality', 2), |
|
67
|
|
|
call.method_name('2', 'could_be_optimized', 2), |
|
68
|
|
|
call.method_name('0', 'get_output_of_stage', 3), |
|
69
|
|
|
call.method_name('1', 'get_output_of_stage', 3), |
|
70
|
|
|
call.method_name('2', 'get_output_of_stage', 3), |
|
71
|
|
|
call.method_name('0', 'get_quality', 3), |
|
72
|
|
|
call.method_name('1', 'get_quality', 3), |
|
73
|
|
|
call.method_name('2', 'get_quality', 3), |
|
74
|
|
|
call.method_name('0', 'is_enough_quality', 3), |
|
75
|
|
|
call.method_name('0', 'could_be_optimized', 3), |
|
76
|
|
|
call.method_name('1', 'is_enough_quality', 3), |
|
77
|
|
|
call.method_name('1', 'could_be_optimized', 3), |
|
78
|
|
|
call.method_name('2', 'is_enough_quality', 3), |
|
79
|
|
|
call.method_name('2', 'could_be_optimized', 3), |
|
80
|
|
|
call.method_name('0', 'get_output_of_stage', 4), |
|
81
|
|
|
call.method_name('1', 'get_output_of_stage', 4), |
|
82
|
|
|
call.method_name('2', 'get_output_of_stage', 4), |
|
83
|
|
|
call.method_name('0', 'get_quality', 4), |
|
84
|
|
|
call.method_name('1', 'get_quality', 4), |
|
85
|
|
|
call.method_name('2', 'get_quality', 4), |
|
86
|
|
|
call.method_name('0', 'is_enough_quality', 4), |
|
87
|
|
|
call.method_name('0', 'could_be_optimized', 4), |
|
88
|
|
|
call.method_name('1', 'is_enough_quality', 4), |
|
89
|
|
|
call.method_name('1', 'could_be_optimized', 4), |
|
90
|
|
|
call.method_name('2', 'is_enough_quality', 4), |
|
91
|
|
|
call.method_name('2', 'could_be_optimized', 4), |
|
92
|
|
|
call.method_name('0', 'get_output_of_stage', 5), |
|
93
|
|
|
call.method_name('1', 'get_output_of_stage', 5), |
|
94
|
|
|
call.method_name('2', 'get_output_of_stage', 5), |
|
95
|
|
|
call.method_name('0', 'get_quality', 5), |
|
96
|
|
|
call.method_name('1', 'get_quality', 5), |
|
97
|
|
|
call.method_name('2', 'get_quality', 5), |
|
98
|
|
|
call.method_name('0', 'is_enough_quality', 5), |
|
99
|
|
|
call.method_name('0', 'could_be_optimized', 5), |
|
100
|
|
|
call.method_name('1', 'is_enough_quality', 5), |
|
101
|
|
|
call.method_name('1', 'could_be_optimized', 5), |
|
102
|
|
|
call.method_name('2', 'is_enough_quality', 5), |
|
103
|
|
|
call.method_name('2', 'could_be_optimized', 5), |
|
104
|
|
|
call.method_name('0', 'get_output_of_stage', 6), |
|
105
|
|
|
call.method_name('1', 'get_output_of_stage', 6), |
|
106
|
|
|
call.method_name('2', 'get_output_of_stage', 6), |
|
107
|
|
|
call.method_name('0', 'get_quality', 6), |
|
108
|
|
|
call.method_name('1', 'get_quality', 6), |
|
109
|
|
|
call.method_name('2', 'get_quality', 6), |
|
110
|
|
|
call.method_name('0', 'is_enough_quality', 6), |
|
111
|
|
|
call.method_name('0', 'could_be_optimized', 6), |
|
112
|
|
|
call.method_name('1', 'is_enough_quality', 6), |
|
113
|
|
|
call.method_name('1', 'could_be_optimized', 6), |
|
114
|
|
|
call.method_name('2', 'is_enough_quality', 6), |
|
115
|
|
|
call.method_name('2', 'could_be_optimized', 6), |
|
116
|
|
|
call.method_name('3', 'get_output_of_stage', 1), |
|
117
|
|
|
call.method_name('4', 'get_output_of_stage', 1), |
|
118
|
|
|
call.method_name('5', 'get_output_of_stage', 1), |
|
119
|
|
|
call.method_name('3', 'get_quality', 1), |
|
120
|
|
|
call.method_name('4', 'get_quality', 1), |
|
121
|
|
|
call.method_name('5', 'get_quality', 1), |
|
122
|
|
|
call.method_name('3', 'is_enough_quality', 1), |
|
123
|
|
|
call.method_name('3', 'could_be_optimized', 1), |
|
124
|
|
|
call.method_name('4', 'is_enough_quality', 1), |
|
125
|
|
|
call.method_name('4', 'could_be_optimized', 1), |
|
126
|
|
|
call.method_name('5', 'is_enough_quality', 1), |
|
127
|
|
|
call.method_name('5', 'could_be_optimized', 1), |
|
128
|
|
|
call.method_name('3', 'get_output_of_stage', 2), |
|
129
|
|
|
call.method_name('4', 'get_output_of_stage', 2), |
|
130
|
|
|
call.method_name('5', 'get_output_of_stage', 2), |
|
131
|
|
|
call.method_name('3', 'get_quality', 2), |
|
132
|
|
|
call.method_name('4', 'get_quality', 2), |
|
133
|
|
|
call.method_name('5', 'get_quality', 2), |
|
134
|
|
|
call.method_name('3', 'is_enough_quality', 2), |
|
135
|
|
|
call.method_name('3', 'could_be_optimized', 2), |
|
136
|
|
|
call.method_name('4', 'is_enough_quality', 2), |
|
137
|
|
|
call.method_name('4', 'could_be_optimized', 2), |
|
138
|
|
|
call.method_name('5', 'is_enough_quality', 2), |
|
139
|
|
|
call.method_name('5', 'could_be_optimized', 2), |
|
140
|
|
|
call.method_name('3', 'get_output_of_stage', 3), |
|
141
|
|
|
call.method_name('4', 'get_output_of_stage', 3), |
|
142
|
|
|
call.method_name('5', 'get_output_of_stage', 3), |
|
143
|
|
|
call.method_name('3', 'get_quality', 3), |
|
144
|
|
|
call.method_name('4', 'get_quality', 3), |
|
145
|
|
|
call.method_name('5', 'get_quality', 3), |
|
146
|
|
|
call.method_name('3', 'is_enough_quality', 3), |
|
147
|
|
|
call.method_name('3', 'could_be_optimized', 3), |
|
148
|
|
|
call.method_name('4', 'is_enough_quality', 3), |
|
149
|
|
|
call.method_name('4', 'could_be_optimized', 3), |
|
150
|
|
|
call.method_name('5', 'is_enough_quality', 3), |
|
151
|
|
|
call.method_name('5', 'could_be_optimized', 3), |
|
152
|
|
|
call.method_name('3', 'get_output_of_stage', 4), |
|
153
|
|
|
call.method_name('4', 'get_output_of_stage', 4), |
|
154
|
|
|
call.method_name('5', 'get_output_of_stage', 4), |
|
155
|
|
|
call.method_name('3', 'get_quality', 4), |
|
156
|
|
|
call.method_name('4', 'get_quality', 4), |
|
157
|
|
|
call.method_name('5', 'get_quality', 4), |
|
158
|
|
|
call.method_name('3', 'is_enough_quality', 4), |
|
159
|
|
|
call.method_name('3', 'could_be_optimized', 4), |
|
160
|
|
|
call.method_name('4', 'is_enough_quality', 4), |
|
161
|
|
|
call.method_name('4', 'could_be_optimized', 4), |
|
162
|
|
|
call.method_name('5', 'is_enough_quality', 4), |
|
163
|
|
|
call.method_name('5', 'could_be_optimized', 4), |
|
164
|
|
|
call.method_name('3', 'get_output_of_stage', 5), |
|
165
|
|
|
call.method_name('4', 'get_output_of_stage', 5), |
|
166
|
|
|
call.method_name('5', 'get_output_of_stage', 5), |
|
167
|
|
|
call.method_name('3', 'get_quality', 5), |
|
168
|
|
|
call.method_name('4', 'get_quality', 5), |
|
169
|
|
|
call.method_name('5', 'get_quality', 5), |
|
170
|
|
|
call.method_name('3', 'is_enough_quality', 5), |
|
171
|
|
|
call.method_name('3', 'could_be_optimized', 5), |
|
172
|
|
|
call.method_name('4', 'is_enough_quality', 5), |
|
173
|
|
|
call.method_name('4', 'could_be_optimized', 5), |
|
174
|
|
|
call.method_name('5', 'is_enough_quality', 5), |
|
175
|
|
|
call.method_name('5', 'could_be_optimized', 5), |
|
176
|
|
|
call.method_name('3', 'get_output_of_stage', 6), |
|
177
|
|
|
call.method_name('4', 'get_output_of_stage', 6), |
|
178
|
|
|
call.method_name('5', 'get_output_of_stage', 6), |
|
179
|
|
|
call.method_name('3', 'get_quality', 6), |
|
180
|
|
|
call.method_name('4', 'get_quality', 6), |
|
181
|
|
|
call.method_name('5', 'get_quality', 6), |
|
182
|
|
|
call.method_name('3', 'is_enough_quality', 6), |
|
183
|
|
|
call.method_name('3', 'could_be_optimized', 6), |
|
184
|
|
|
call.method_name('4', 'is_enough_quality', 6), |
|
185
|
|
|
call.method_name('4', 'could_be_optimized', 6), |
|
186
|
|
|
call.method_name('5', 'is_enough_quality', 6), |
|
187
|
|
|
call.method_name('5', 'could_be_optimized', 6), |
|
188
|
|
|
call.method_name('6', 'get_output_of_stage', 1), |
|
189
|
|
|
call.method_name('6', 'get_quality', 1), |
|
190
|
|
|
call.method_name('6', 'is_enough_quality', 1), |
|
191
|
|
|
call.method_name('6', 'could_be_optimized', 1), |
|
192
|
|
|
call.method_name('6', 'get_output_of_stage', 2), |
|
193
|
|
|
call.method_name('6', 'get_quality', 2), |
|
194
|
|
|
call.method_name('6', 'is_enough_quality', 2), |
|
195
|
|
|
call.method_name('6', 'could_be_optimized', 2), |
|
196
|
|
|
call.method_name('6', 'get_output_of_stage', 3), |
|
197
|
|
|
call.method_name('6', 'get_quality', 3), |
|
198
|
|
|
call.method_name('6', 'is_enough_quality', 3), |
|
199
|
|
|
call.method_name('6', 'could_be_optimized', 3), |
|
200
|
|
|
call.method_name('6', 'get_output_of_stage', 4), |
|
201
|
|
|
call.method_name('6', 'get_quality', 4), |
|
202
|
|
|
call.method_name('6', 'is_enough_quality', 4), |
|
203
|
|
|
call.method_name('6', 'could_be_optimized', 4), |
|
204
|
|
|
call.method_name('6', 'get_output_of_stage', 5), |
|
205
|
|
|
call.method_name('6', 'get_quality', 5), |
|
206
|
|
|
call.method_name('6', 'is_enough_quality', 5), |
|
207
|
|
|
call.method_name('6', 'could_be_optimized', 5), |
|
208
|
|
|
call.method_name('6', 'get_output_of_stage', 6), |
|
209
|
|
|
call.method_name('6', 'get_quality', 6), |
|
210
|
|
|
call.method_name('6', 'is_enough_quality', 6), |
|
211
|
|
|
call.method_name('6', 'could_be_optimized', 6)] |
|
212
|
|
|
optimizer.optimize_process() |
|
213
|
|
|
self.assertListEqual(CALLS_COUNTER.method_calls, expected_calls) |
|
214
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
CALLS_COUNTER = Mock(name="CallsCounter") |
|
217
|
|
|
|
|
218
|
|
|
|
|
219
|
|
|
class DeterministicStage(AbstractStage): |
|
|
|
|
|
|
220
|
|
|
def __init__(self, name, max_get_output_of_stage_count=10, |
|
|
|
|
|
|
221
|
|
|
max_is_enough_quality_count=10, |
|
222
|
|
|
max_could_be_optimized_count=10, max_get_quality_count=10, |
|
223
|
|
|
max_get_cost_count=10): |
|
224
|
|
|
super().__init__((0, 0, 0, 0)) |
|
225
|
|
|
self.name = name |
|
226
|
|
|
CALLS_COUNTER.method_name(self.name, "__init__") |
|
227
|
|
|
self.get_cost_count = 0 |
|
228
|
|
|
self.get_quality_count = 0 |
|
229
|
|
|
self.could_be_optimized_count = 0 |
|
230
|
|
|
self.is_enough_quality_count = 0 |
|
231
|
|
|
self.get_output_of_stage_count = 0 |
|
232
|
|
|
self.max_get_cost_count = max_get_cost_count |
|
233
|
|
|
self.max_get_quality_count = max_get_quality_count |
|
234
|
|
|
self.max_could_be_optimized_count = max_could_be_optimized_count |
|
235
|
|
|
self.max_is_enough_quality_count = max_is_enough_quality_count |
|
236
|
|
|
self.max_get_output_of_stage_count = max_get_output_of_stage_count |
|
237
|
|
|
|
|
238
|
|
|
def get_cost(self): |
|
|
|
|
|
|
239
|
|
|
self.get_cost_count += 1 |
|
240
|
|
|
CALLS_COUNTER.method_name(self.name, "get_cost", self.get_cost_count) |
|
241
|
|
|
return self.get_cost_count |
|
242
|
|
|
|
|
243
|
|
|
def get_quality(self, control_params=None): |
|
|
|
|
|
|
244
|
|
|
self.get_quality_count += 1 |
|
245
|
|
|
CALLS_COUNTER.method_name(self.name, "get_quality", |
|
246
|
|
|
self.get_quality_count) |
|
247
|
|
|
return self.get_quality_count |
|
248
|
|
|
|
|
249
|
|
|
def could_be_optimized(self): |
|
|
|
|
|
|
250
|
|
|
self.could_be_optimized_count += 1 |
|
251
|
|
|
CALLS_COUNTER.method_name(self.name, "could_be_optimized", |
|
252
|
|
|
self.could_be_optimized_count) |
|
253
|
|
|
return self.is_enough_quality_count <8 |
|
|
|
|
|
|
254
|
|
|
|
|
255
|
|
|
def is_enough_quality(self, value): |
|
|
|
|
|
|
256
|
|
|
self.is_enough_quality_count += 1 |
|
257
|
|
|
CALLS_COUNTER.method_name(self.name, "is_enough_quality", |
|
258
|
|
|
self.is_enough_quality_count) |
|
259
|
|
|
return self.is_enough_quality_count >= 5 |
|
260
|
|
|
|
|
261
|
|
|
def get_output_of_stage(self): |
|
|
|
|
|
|
262
|
|
|
self.get_output_of_stage_count += 1 |
|
263
|
|
|
output = [self.get_output_of_stage_count] * len(self.input_vector) |
|
264
|
|
|
output[0] = int(self.name) |
|
265
|
|
|
CALLS_COUNTER.method_name(self.name, "get_output_of_stage", |
|
266
|
|
|
self.get_output_of_stage_count) |
|
267
|
|
|
return output |
|
268
|
|
|
|
|
269
|
|
|
|
|
270
|
|
|
class ExampleProcess(AbstractProcess): |
|
|
|
|
|
|
271
|
|
|
pass |
|
272
|
|
|
|
|
273
|
|
|
|
|
274
|
|
|
TESTED_PROCESS = ExampleProcess() |
|
275
|
|
|
stages = {} |
|
|
|
|
|
|
276
|
|
|
for i in range(7): |
|
277
|
|
|
stages[i] = DeterministicStage(str(i)) |
|
278
|
|
|
|
|
279
|
|
|
# Our graph: |
|
280
|
|
|
# 0 |
|
281
|
|
|
# | |
|
282
|
|
|
# 1 |
|
283
|
|
|
# |\ |
|
284
|
|
|
# 2 4 |
|
285
|
|
|
# | |\ |
|
286
|
|
|
# 3 5 6 |
|
287
|
|
|
# All adges directed to down |
|
288
|
|
|
# Order of nodes is the same as names |
|
289
|
|
|
TESTED_PROCESS.add_edge(stages[0], stages[1]) |
|
|
|
|
|
|
290
|
|
|
TESTED_PROCESS.add_edge(stages[1], stages[2]) |
|
|
|
|
|
|
291
|
|
|
TESTED_PROCESS.add_edge(stages[2], stages[3]) |
|
|
|
|
|
|
292
|
|
|
|
|
293
|
|
|
TESTED_PROCESS.add_edge(stages[1], stages[4]) |
|
|
|
|
|
|
294
|
|
|
TESTED_PROCESS.add_edge(stages[4], stages[5]) |
|
|
|
|
|
|
295
|
|
|
TESTED_PROCESS.add_edge(stages[4], stages[6]) |
|
|
|
|
|
|
296
|
|
|
|
|
297
|
|
|
# Groups: |
|
298
|
|
|
# (0)0 |
|
299
|
|
|
# | |
|
300
|
|
|
# (0)1 |
|
301
|
|
|
# |\ |
|
302
|
|
|
# (0)2 4(1) |
|
303
|
|
|
# | | \ |
|
304
|
|
|
# (1)3 5(1)6(2) |
|
305
|
|
|
|
|
306
|
|
|
GROUPING_STRATEGY = GroupingStrategy(list(stages.values())) |
|
307
|
|
|
GROUPING_STRATEGY.define_group((stages[0], stages[1], stages[2])) |
|
308
|
|
|
GROUPING_STRATEGY.define_group((stages[3], stages[4], stages[5])) |
|
309
|
|
|
GROUPING_STRATEGY.define_group((stages[6],)) |
|
310
|
|
|
|
|
311
|
|
|
PSO_ALGORITHM = PsoAlgorithm(TESTED_PROCESS, GROUPING_STRATEGY, 2) |
|
312
|
|
|
|
This check looks for calls to members that are non-existent. These calls will fail.
The member could have been renamed or removed.