1
|
|
|
"""!
|
2
|
|
|
|
3
|
|
|
@brief Abstract network representation that is used as a basic class.
|
4
|
|
|
|
5
|
|
|
@authors Andrei Novikov ([email protected])
|
6
|
|
|
@date 2014-2016
|
7
|
|
|
@copyright GNU Public License
|
8
|
|
|
|
9
|
|
|
@cond GNU_PUBLIC_LICENSE
|
10
|
|
|
PyClustering is free software: you can redistribute it and/or modify
|
11
|
|
|
it under the terms of the GNU General Public License as published by
|
12
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
13
|
|
|
(at your option) any later version.
|
14
|
|
|
|
15
|
|
|
PyClustering is distributed in the hope that it will be useful,
|
16
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
|
|
GNU General Public License for more details.
|
19
|
|
|
|
20
|
|
|
You should have received a copy of the GNU General Public License
|
21
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
22
|
|
|
@endcond
|
23
|
|
|
|
24
|
|
|
"""
|
25
|
|
|
|
26
|
|
|
import unittest;
|
27
|
|
|
|
28
|
|
|
import math;
|
29
|
|
|
|
30
|
|
|
from pyclustering.nnet import network, conn_type, conn_represent;
|
31
|
|
|
|
32
|
|
|
class Test(unittest.TestCase):
|
33
|
|
|
# All to All connection suite
|
34
|
|
|
def templateAllToAllConnectionsTest(self, network):
|
35
|
|
|
assert network.structure == conn_type.ALL_TO_ALL;
|
36
|
|
|
|
37
|
|
|
for i in range(0, len(network), 1):
|
38
|
|
|
for j in range(0, len(network), 1):
|
39
|
|
|
if (i != j):
|
40
|
|
|
assert network.has_connection(i, j) == True;
|
41
|
|
|
assert network.has_connection(j, i) == True;
|
42
|
|
|
else:
|
43
|
|
|
assert network.has_connection(i, j) == False;
|
44
|
|
|
assert network.has_connection(j, i) == False;
|
45
|
|
|
|
46
|
|
|
def testAllToAll1Connections(self):
|
47
|
|
|
net = network(1, type_conn = conn_type.ALL_TO_ALL);
|
48
|
|
|
self.templateAllToAllConnectionsTest(net);
|
49
|
|
|
|
50
|
|
|
def testAllToAll10Connections(self):
|
51
|
|
|
net = network(10, type_conn = conn_type.ALL_TO_ALL);
|
52
|
|
|
self.templateAllToAllConnectionsTest(net);
|
53
|
|
|
|
54
|
|
|
def testAllToAll25Connections(self):
|
55
|
|
|
net = network(25, type_conn = conn_type.ALL_TO_ALL);
|
56
|
|
|
self.templateAllToAllConnectionsTest(net);
|
57
|
|
|
|
58
|
|
|
def testAllToAll1ConnectionsListRepresentation(self):
|
59
|
|
|
net = network(1, type_conn = conn_type.ALL_TO_ALL, conn_represent = conn_represent.LIST);
|
60
|
|
|
self.templateAllToAllConnectionsTest(net);
|
61
|
|
|
|
62
|
|
|
def testAllToAll10ConnectionsListRepresentation(self):
|
63
|
|
|
net = network(10, type_conn = conn_type.ALL_TO_ALL, conn_represent = conn_represent.LIST);
|
64
|
|
|
self.templateAllToAllConnectionsTest(net);
|
65
|
|
|
|
66
|
|
|
def testAllToAll25ConnectionsListRepresentation(self):
|
67
|
|
|
net = network(25, type_conn = conn_type.ALL_TO_ALL, conn_represent = conn_represent.LIST);
|
68
|
|
|
self.templateAllToAllConnectionsTest(net);
|
69
|
|
|
|
70
|
|
|
|
71
|
|
|
# None connection suite
|
72
|
|
|
def templateNoneConnectionsTest(self, network):
|
73
|
|
|
assert network.structure == conn_type.NONE;
|
74
|
|
|
|
75
|
|
|
for i in range(0, len(network), 1):
|
76
|
|
|
for j in range(0, len(network), 1):
|
77
|
|
|
assert network.has_connection(i, j) == False;
|
78
|
|
|
assert network.has_connection(j, i) == False;
|
79
|
|
|
|
80
|
|
|
def testNoneConnections(self):
|
81
|
|
|
net = network(10, type_conn = conn_type.NONE);
|
82
|
|
|
self.templateNoneConnectionsTest(net);
|
83
|
|
|
|
84
|
|
|
def testNoneConnectionsListRepresentation(self):
|
85
|
|
|
net = network(10, type_conn = conn_type.NONE, conn_represent = conn_represent.LIST);
|
86
|
|
|
self.templateNoneConnectionsTest(net);
|
87
|
|
|
|
88
|
|
|
|
89
|
|
|
# Bidirectional list connection suite
|
90
|
|
|
def templateBidirListConnectionsTest(self, network):
|
91
|
|
|
assert network.structure == conn_type.LIST_BIDIR;
|
92
|
|
|
|
93
|
|
|
for index in range(0, len(network), 1):
|
94
|
|
|
if (index > 0):
|
95
|
|
|
assert network.has_connection(index, index - 1) == True;
|
96
|
|
|
assert network.has_connection(index - 1, index) == True;
|
97
|
|
|
|
98
|
|
|
if (index < (len(network) - 1)):
|
99
|
|
|
assert network.has_connection(index, index + 1) == True;
|
100
|
|
|
assert network.has_connection(index + 1, index) == True;
|
101
|
|
|
|
102
|
|
|
def testBidirListConnections(self):
|
103
|
|
|
net = network(10, type_conn = conn_type.LIST_BIDIR);
|
104
|
|
|
self.templateBidirListConnectionsTest(net);
|
105
|
|
|
|
106
|
|
|
def testBidirListConnectionsListRepresentation(self):
|
107
|
|
|
net = network(10, type_conn = conn_type.LIST_BIDIR, conn_represent = conn_represent.LIST);
|
108
|
|
|
self.templateBidirListConnectionsTest(net);
|
109
|
|
|
|
110
|
|
|
|
111
|
|
|
# Grid four connection suite
|
112
|
|
|
def templateGridFourConnectionsTest(self, network):
|
113
|
|
|
assert network.structure == conn_type.GRID_FOUR;
|
114
|
|
|
|
115
|
|
|
for index in range(0, len(network), 1):
|
116
|
|
|
upper_index = index - network.width;
|
117
|
|
|
lower_index = index + network.width;
|
118
|
|
|
left_index = index - 1;
|
119
|
|
|
right_index = index + 1;
|
120
|
|
|
|
121
|
|
|
node_row_index = math.ceil(index / network.width);
|
122
|
|
|
if (upper_index >= 0):
|
123
|
|
|
assert network.has_connection(index, upper_index) == True;
|
124
|
|
|
assert network.has_connection(upper_index, index) == True;
|
125
|
|
|
|
126
|
|
|
if (lower_index < len(network)):
|
127
|
|
|
assert network.has_connection(index, lower_index) == True;
|
128
|
|
|
assert network.has_connection(lower_index, index) == True;
|
129
|
|
|
|
130
|
|
|
if ( (left_index >= 0) and (math.ceil(left_index / network.width) == node_row_index) ):
|
131
|
|
|
assert network.has_connection(index, left_index) == True;
|
132
|
|
|
assert network.has_connection(left_index, index) == True;
|
133
|
|
|
|
134
|
|
|
if ( (right_index < network._num_osc) and (math.ceil(right_index / network.width) == node_row_index) ):
|
135
|
|
|
assert network.has_connection(index, right_index) == True;
|
136
|
|
|
assert network.has_connection(right_index, index) == True;
|
137
|
|
|
|
138
|
|
|
def testGridFourConnectionsMatrixRepresentation(self):
|
139
|
|
|
net = network(25, type_conn = conn_type.GRID_FOUR);
|
140
|
|
|
self.templateGridFourConnectionsTest(net);
|
141
|
|
|
|
142
|
|
|
def testGridFourConnectionsListRepresentation(self):
|
143
|
|
|
net = network(25, type_conn = conn_type.GRID_FOUR, conn_represent = conn_represent.LIST);
|
144
|
|
|
self.templateGridFourConnectionsTest(net);
|
145
|
|
|
|
146
|
|
|
def testGridFourConnections1MatrixRepresentation(self):
|
147
|
|
|
net = network(1, type_conn = conn_type.GRID_FOUR);
|
148
|
|
|
self.templateGridFourConnectionsTest(net);
|
149
|
|
|
|
150
|
|
|
def testGridFourConnections1ListRepresentation(self):
|
151
|
|
|
net = network(1, type_conn = conn_type.GRID_FOUR, conn_represent = conn_represent.LIST);
|
152
|
|
|
self.templateGridFourConnectionsTest(net);
|
153
|
|
|
|
154
|
|
|
def testGridFourConnectionsRectange40MatrixRepresentation(self):
|
155
|
|
|
net = network(40, type_conn = conn_type.GRID_FOUR, conn_represent = conn_represent.MATRIX, height = 4, width = 10);
|
156
|
|
|
self.templateGridFourConnectionsTest(net);
|
157
|
|
|
|
158
|
|
|
net = network(40, type_conn = conn_type.GRID_FOUR, conn_represent = conn_represent.MATRIX, height = 10, width = 4);
|
159
|
|
|
self.templateGridFourConnectionsTest(net);
|
160
|
|
|
|
161
|
|
|
def testGridFourConnectionsRectangeList40Representation(self):
|
162
|
|
|
net = network(40, type_conn = conn_type.GRID_FOUR, conn_represent = conn_represent.LIST, height = 4, width = 10);
|
163
|
|
|
self.templateGridFourConnectionsTest(net);
|
164
|
|
|
|
165
|
|
|
net = network(40, type_conn = conn_type.GRID_FOUR, conn_represent = conn_represent.LIST, height = 10, width = 4);
|
166
|
|
|
self.templateGridFourConnectionsTest(net);
|
167
|
|
|
|
168
|
|
|
def testGridFourConnectionsRectange10MatrixRepresentation(self):
|
169
|
|
|
net = network(10, type_conn = conn_type.GRID_FOUR, conn_represent = conn_represent.MATRIX, height = 1, width = 10);
|
170
|
|
|
self.templateGridFourConnectionsTest(net);
|
171
|
|
|
|
172
|
|
|
net = network(10, type_conn = conn_type.GRID_FOUR, conn_represent = conn_represent.MATRIX, height = 10, width = 1);
|
173
|
|
|
self.templateGridFourConnectionsTest(net);
|
174
|
|
|
|
175
|
|
|
def testGridFourConnectionsRectangeList10Representation(self):
|
176
|
|
|
net = network(10, type_conn = conn_type.GRID_FOUR, conn_represent = conn_represent.LIST, height = 1, width = 10);
|
177
|
|
|
self.templateGridFourConnectionsTest(net);
|
178
|
|
|
|
179
|
|
|
net = network(10, type_conn = conn_type.GRID_FOUR, conn_represent = conn_represent.LIST, height = 10, width = 1);
|
180
|
|
|
self.templateGridFourConnectionsTest(net);
|
181
|
|
|
|
182
|
|
|
def testGridFourConnectionsRectange1MatrixRepresentation(self):
|
183
|
|
|
net = network(1, type_conn = conn_type.GRID_FOUR, conn_represent = conn_represent.MATRIX, height = 1, width = 1);
|
184
|
|
|
self.templateGridFourConnectionsTest(net);
|
185
|
|
|
|
186
|
|
|
net = network(1, type_conn = conn_type.GRID_FOUR, conn_represent = conn_represent.MATRIX, height = 1, width = 1);
|
187
|
|
|
self.templateGridFourConnectionsTest(net);
|
188
|
|
|
|
189
|
|
|
def testGridFourConnectionsRectangeList1Representation(self):
|
190
|
|
|
net = network(1, type_conn = conn_type.GRID_FOUR, conn_represent = conn_represent.LIST, height = 1, width = 1);
|
191
|
|
|
self.templateGridFourConnectionsTest(net);
|
192
|
|
|
|
193
|
|
|
net = network(1, type_conn = conn_type.GRID_FOUR, conn_represent = conn_represent.LIST, height = 1, width = 1);
|
194
|
|
|
self.templateGridFourConnectionsTest(net);
|
195
|
|
|
|
196
|
|
|
# Grid four connection suite
|
197
|
|
|
def templateGridEightConnectionsTest(self, network):
|
198
|
|
|
assert network.structure == conn_type.GRID_EIGHT;
|
199
|
|
|
|
200
|
|
|
for index in range(0, len(network), 1):
|
201
|
|
|
upper_index = index - network.width;
|
202
|
|
|
lower_index = index + network.width;
|
203
|
|
|
left_index = index - 1;
|
204
|
|
|
right_index = index + 1;
|
205
|
|
|
|
206
|
|
|
node_row_index = math.ceil(index / network.width);
|
207
|
|
|
if (upper_index >= 0):
|
208
|
|
|
assert network.has_connection(index, upper_index) == True;
|
209
|
|
|
assert network.has_connection(upper_index, index) == True;
|
210
|
|
|
|
211
|
|
|
if (lower_index < len(network)):
|
212
|
|
|
assert network.has_connection(index, lower_index) == True;
|
213
|
|
|
assert network.has_connection(lower_index, index) == True;
|
214
|
|
|
|
215
|
|
|
if ( (left_index >= 0) and (math.ceil(left_index / network.width) == node_row_index) ):
|
216
|
|
|
assert network.has_connection(index, left_index) == True;
|
217
|
|
|
assert network.has_connection(left_index, index) == True;
|
218
|
|
|
|
219
|
|
|
if ( (right_index < len(network)) and (math.ceil(right_index / network.width) == node_row_index) ):
|
220
|
|
|
assert network.has_connection(index, right_index) == True;
|
221
|
|
|
assert network.has_connection(right_index, index) == True;
|
222
|
|
|
|
223
|
|
|
side_size = network.width;
|
224
|
|
|
|
225
|
|
|
upper_left_index = index - side_size - 1;
|
226
|
|
|
upper_right_index = index - side_size + 1;
|
227
|
|
|
|
228
|
|
|
lower_left_index = index + side_size - 1;
|
229
|
|
|
lower_right_index = index + side_size + 1;
|
230
|
|
|
|
231
|
|
|
node_row_index = math.floor(index / side_size);
|
232
|
|
|
upper_row_index = node_row_index - 1;
|
233
|
|
|
lower_row_index = node_row_index + 1;
|
234
|
|
|
|
235
|
|
|
if ( (upper_left_index >= 0) and (math.floor(upper_left_index / side_size) == upper_row_index) ):
|
236
|
|
|
assert network.has_connection(index, upper_left_index) == True;
|
237
|
|
|
assert network.has_connection(upper_left_index, index) == True;
|
238
|
|
|
|
239
|
|
|
if ( (upper_right_index >= 0) and (math.floor(upper_right_index / side_size) == upper_row_index) ):
|
240
|
|
|
assert network.has_connection(index, upper_right_index) == True;
|
241
|
|
|
assert network.has_connection(upper_right_index, index) == True;
|
242
|
|
|
|
243
|
|
|
if ( (lower_left_index < len(network)) and (math.floor(lower_left_index / side_size) == lower_row_index) ):
|
244
|
|
|
assert network.has_connection(index, lower_left_index) == True;
|
245
|
|
|
assert network.has_connection(lower_left_index, index) == True;
|
246
|
|
|
|
247
|
|
|
if ( (lower_right_index < len(network)) and (math.floor(lower_right_index / side_size) == lower_row_index) ):
|
248
|
|
|
assert network.has_connection(index, lower_right_index) == True;
|
249
|
|
|
assert network.has_connection(lower_right_index, index) == True;
|
250
|
|
|
|
251
|
|
|
def testGridEightConnectionsMatrixRepresentation(self):
|
252
|
|
|
net = network(25, type_conn = conn_type.GRID_EIGHT);
|
253
|
|
|
self.templateGridEightConnectionsTest(net);
|
254
|
|
|
|
255
|
|
|
def testGridEightConnectionsListRepresentation(self):
|
256
|
|
|
net = network(25, type_conn = conn_type.GRID_EIGHT, conn_represent = conn_represent.LIST);
|
257
|
|
|
self.templateGridEightConnectionsTest(net);
|
258
|
|
|
|
259
|
|
|
def testGridEightConnections1MatrixRepresentation(self):
|
260
|
|
|
net = network(1, type_conn = conn_type.GRID_EIGHT);
|
261
|
|
|
self.templateGridEightConnectionsTest(net);
|
262
|
|
|
|
263
|
|
|
def testGridEightConnections1ListRepresentation(self):
|
264
|
|
|
net = network(1, type_conn = conn_type.GRID_EIGHT, conn_represent = conn_represent.LIST);
|
265
|
|
|
self.templateGridEightConnectionsTest(net);
|
266
|
|
|
|
267
|
|
|
def testGridEightConnectionsRectange40MatrixRepresentation(self):
|
268
|
|
|
net = network(40, type_conn = conn_type.GRID_EIGHT, conn_represent = conn_represent.MATRIX, height = 4, width = 10);
|
269
|
|
|
self.templateGridEightConnectionsTest(net);
|
270
|
|
|
|
271
|
|
|
net = network(40, type_conn = conn_type.GRID_EIGHT, conn_represent = conn_represent.MATRIX, height = 10, width = 4);
|
272
|
|
|
self.templateGridEightConnectionsTest(net);
|
273
|
|
|
|
274
|
|
|
def testGridEightConnectionsRectangeList40Representation(self):
|
275
|
|
|
net = network(40, type_conn = conn_type.GRID_EIGHT, conn_represent = conn_represent.LIST, height = 4, width = 10);
|
276
|
|
|
self.templateGridEightConnectionsTest(net);
|
277
|
|
|
|
278
|
|
|
net = network(40, type_conn = conn_type.GRID_EIGHT, conn_represent = conn_represent.LIST, height = 10, width = 4);
|
279
|
|
|
self.templateGridEightConnectionsTest(net);
|
280
|
|
|
|
281
|
|
|
def testGridEightConnectionsRectange10MatrixRepresentation(self):
|
282
|
|
|
net = network(10, type_conn = conn_type.GRID_EIGHT, conn_represent = conn_represent.MATRIX, height = 1, width = 10);
|
283
|
|
|
self.templateGridEightConnectionsTest(net);
|
284
|
|
|
|
285
|
|
|
net = network(10, type_conn = conn_type.GRID_EIGHT, conn_represent = conn_represent.MATRIX, height = 10, width = 1);
|
286
|
|
|
self.templateGridEightConnectionsTest(net);
|
287
|
|
|
|
288
|
|
|
def testGridEightConnectionsRectangeList10Representation(self):
|
289
|
|
|
net = network(10, type_conn = conn_type.GRID_EIGHT, conn_represent = conn_represent.LIST, height = 1, width = 10);
|
290
|
|
|
self.templateGridEightConnectionsTest(net);
|
291
|
|
|
|
292
|
|
|
net = network(10, type_conn = conn_type.GRID_EIGHT, conn_represent = conn_represent.LIST, height = 10, width = 1);
|
293
|
|
|
self.templateGridEightConnectionsTest(net);
|
294
|
|
|
|
295
|
|
|
def testGridEightConnectionsRectange1MatrixRepresentation(self):
|
296
|
|
|
net = network(1, type_conn = conn_type.GRID_EIGHT, conn_represent = conn_represent.MATRIX, height = 1, width = 1);
|
297
|
|
|
self.templateGridEightConnectionsTest(net);
|
298
|
|
|
|
299
|
|
|
net = network(1, type_conn = conn_type.GRID_EIGHT, conn_represent = conn_represent.MATRIX, height = 1, width = 1);
|
300
|
|
|
self.templateGridEightConnectionsTest(net);
|
301
|
|
|
|
302
|
|
|
def testGridEightConnectionsRectangeList1Representation(self):
|
303
|
|
|
net = network(1, type_conn = conn_type.GRID_EIGHT, conn_represent = conn_represent.LIST, height = 1, width = 1);
|
304
|
|
|
self.templateGridEightConnectionsTest(net);
|
305
|
|
|
|
306
|
|
|
net = network(1, type_conn = conn_type.GRID_EIGHT, conn_represent = conn_represent.LIST, height = 1, width = 1);
|
307
|
|
|
self.templateGridEightConnectionsTest(net);
|
308
|
|
|
|
309
|
|
|
|
310
|
|
|
def testGridFourStructure1GridProperty(self):
|
311
|
|
|
net = network(1, type_conn = conn_type.GRID_FOUR, conn_represent = conn_represent.LIST, height = 1, width = 1);
|
312
|
|
|
assert(net.height == 1);
|
313
|
|
|
assert(net.width == 1);
|
314
|
|
|
|
315
|
|
|
def testGridEightStructure1GridProperty(self):
|
316
|
|
|
net = network(1, type_conn = conn_type.GRID_EIGHT, conn_represent = conn_represent.LIST, height = 1, width = 1);
|
317
|
|
|
assert(net.height == 1);
|
318
|
|
|
assert(net.width == 1);
|
319
|
|
|
|
320
|
|
|
def testGridFourStructure40GridProperty(self):
|
321
|
|
|
net = network(40, type_conn = conn_type.GRID_FOUR, conn_represent = conn_represent.LIST, height = 20, width = 2);
|
322
|
|
|
assert(net.height == 20);
|
323
|
|
|
assert(net.width == 2);
|
324
|
|
|
|
325
|
|
|
def testGridEightStructure40GridProperty(self):
|
326
|
|
|
net = network(40, type_conn = conn_type.GRID_EIGHT, conn_represent = conn_represent.LIST, height = 20, width = 2);
|
327
|
|
|
assert(net.height == 20);
|
328
|
|
|
assert(net.width == 2);
|
329
|
|
|
|
330
|
|
|
def templateAssertRaises(self, size, type_conn, height, width):
|
331
|
|
|
try:
|
332
|
|
|
network(size, type_conn, height, width);
|
333
|
|
|
assert(False); # assert must occure
|
334
|
|
|
|
335
|
|
|
except:
|
336
|
|
|
pass;
|
337
|
|
|
|
338
|
|
|
def testInvalidGridFourDescription(self):
|
339
|
|
|
self.templateAssertRaises(10, type_conn = conn_type.GRID_FOUR, height = 10, width = 5);
|
340
|
|
|
self.templateAssertRaises(10, type_conn = conn_type.GRID_FOUR, height = 6, width = 2);
|
341
|
|
|
self.templateAssertRaises(10, type_conn = conn_type.GRID_FOUR, height = 1, width = 11);
|
342
|
|
|
self.templateAssertRaises(10, type_conn = conn_type.GRID_FOUR, height = 0, width = 0);
|
343
|
|
|
|
344
|
|
|
def testInvalidGridEightDescription(self):
|
345
|
|
|
self.templateAssertRaises(10, type_conn = conn_type.GRID_EIGHT, height = 5, width = 8);
|
346
|
|
|
self.templateAssertRaises(10, type_conn = conn_type.GRID_EIGHT, height = 1, width = 2);
|
347
|
|
|
self.templateAssertRaises(10, type_conn = conn_type.GRID_EIGHT, height = 1, width = 1);
|
348
|
|
|
self.templateAssertRaises(10, type_conn = conn_type.GRID_EIGHT, height = 0, width = 0);
|
349
|
|
|
|
350
|
|
|
|
351
|
|
|
if __name__ == "__main__":
|
352
|
|
|
unittest.main(); |