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