-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
461 lines (349 loc) · 9.42 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
This is JavaScript port of the perfect-matching alghorithm of PyTheus
See https://github.com/artificial-scientist-lab/PyTheus for the original
python code
*/
/*function: edgeBleach
Turns a array of colored edges into a list of objects where edges are grouped by covering in the following format:
{
cover: [node1, node2],
colors: [[color1, color2], [color3, color4] ...]
}
Parameters:
coloredEdges - array of edges in the following format:
[node1, node2, color1, color2]
Returns:
bleachedEdges - array of objects in the following format:
{
cover: [node1, node2],
colors: [[color1, color2], [color3, color4] ...]
}
*/
function edgeBleach(coloredEdges) {
coverings = [];
colors = [];
coloredEdges.forEach((edge) => {
coverings.push(edge.slice(0, 2));
});
rawEdges = unique(coverings);
bleachedEdges = [];
rawEdges.forEach((raw) => {
bleachedEdges.push({
cover: raw,
colors: coloredEdges
.filter((edge) => arraysEqual(edge.slice(0, 2), raw))
.map((edge) => edge.slice(2)),
});
});
return bleachedEdges;
}
/*function: edgePaint
takes a list of uncolored subgraphs into colored subgraphs by assigning available colors to the edges.
Parameters:
rawSubGraphs - array of uncolored PMs
availColors - array of objects containing available colors for each cover
Returns:
paintedCovers - array of colored subgraphs
*/
function edgePaint(rawSubGraphs, availColors) {
rawSubGraphs = unique(rawSubGraphs);
paintedCovers = [];
rawSubGraphs.forEach((cover) => {
colors = [];
cover.forEach((edge) => {
colors.push(
...availColors
.filter((c) => arraysEqual(edge, c.cover))
.map((c) => c.colors)
);
});
coloring = cartesianProduct(colors);
coloring.forEach((color) => {
colored_cover = [];
for (let i = 0; i < cover.length; i++) {
const edge = cover[i];
const colors = color[i];
colored_cover.push(edge.concat(colors));
}
paintedCovers.push(colored_cover);
});
});
return unique(paintedCovers);
}
/*function: recursivePerfectMatchings
Recursively finds all perfect matchings in a graph.
Performs inplace modification of the store parameter.
Parameters:
graph - array of edges
store - ToDo
matches - ToDo
edges_left - ToDo
Returns:
none
*/
function recursivePerfectMatchings(
graph,
store,
matches = [],
edges_left = null
) {
if (edges_left == null) {
edges_left =
[...new Set(graph.map((g) => g.slice(0, 2)).flat())].length / 2;
}
if (graph.length > 0) {
tmpGraph = targetEdges([nodeDegrees(graph)[0][0]], graph);
tmpGraph.forEach((edge) => {
recursivePerfectMatchings(
removeNodes(graph, edge.slice(0, 2)),
store,
matches.concat([edge]),
edges_left - 1
);
});
} else {
if (edges_left == 0 && graph.length == 0) {
store.push(matches);
} else {
return;
}
}
}
/*function: findPerfectMatchings
Finds all perfect matchings in a graph.
Wrapper function for recursivePerfectMatchings.
Parameters:
graph - array of edges
Returns:
paintedMatchings - array of colored perfect matchings
*/
function findPerfectMatchings(graph) {
availColors = edgeBleach(graph);
rawMatchings = []; //storage array where the recursive loops stores the found matchings
recursivePerfectMatchings(
availColors.map((c) => c.cover),
rawMatchings
);
paintedMatchings = edgePaint(rawMatchings, availColors);
return paintedMatchings;
}
/*function: targetEdges
Finds all edges in a graph that are connected to a given set of nodes.
Parameters:
nodes - array of nodes
graph - array of edges
Returns:
edges - array of edges connected to the given nodes
*/
function targetEdges(nodes, graph) {
//purely written by github copilot
edges = [];
graph.forEach((edge) => {
if (nodes.includes(edge[0]) || nodes.includes(edge[1])) {
edges.push(edge);
}
});
return edges;
}
/*function: removeNodes
Removes a set of nodes from a graph.
Parameters:
graph - array of edges
nodes - array of nodes
Returns:
graph - array of edges without the given nodes
*/
function removeNodes(graph, nodes) {
//purely written by github copilot
return graph.filter(
(edge) => !nodes.includes(edge[0]) && !nodes.includes(edge[1])
);
}
/*function: nodeDegrees
Computes the degree of each node in a graph.
Parameters:
graph - array of edges
Returns:
degrees - array of arrays of nodes and their degree
*/
function nodeDegrees(graph) {
//mostly written by github copilot
degrees = [];
nodes = [];
graph.forEach((edge) => {
nodes.push(edge[0]);
nodes.push(edge[1]);
});
degrees = [...new Set(nodes)];
degrees = degrees.map((degree) => [
degree,
nodes.filter((d) => d == degree).length,
]);
return degrees;
}
/*function: cartesianProduct
Computes the cartesian product of a set of arrays.
Parameters:
arrayOfArrays - array of arrays
Returns:
shapedArr - array of arrays containing the cartesian product
*/
function cartesianProduct(arrayOfArrays) {
//this was github copilot code, looks similar to this answer https://stackoverflow.com/a/43053803 at https://stackoverflow.com/questions/12303989/cartesian-product-of-multiple-arrays-in-javascript
flatArr = arrayOfArrays.reduce((a, b) =>
a.flatMap((d) => b.map((e) => [d, e].flat()))
);
return flatArr.map((f) => {
shapedArr = [];
while (f.length > 0) {
shapedArr.push(f.splice(0, 2));
}
return shapedArr;
});
}
/*function: unique(arrayOfArrays)
Removes duplicates from an array of arrays.
Parameters:
arrayOfArrays - array of arrays
Returns:
uniques - array of arrays without duplicates
*/
function unique(arrayOfArrays) {
uniques = [];
arrayOfArrays.forEach((array) => {
if (!overlap(uniques, array)) {
uniques.push(array);
}
});
return uniques;
}
/*function: overlap
Checks if an array is contained in an array of arrays.
Parameters:
arrayOfArrays - array of arrays
array - array to check
Returns:
true if array is contained in arrayOfArrays, false otherwise
*/
function overlap(arrayOfArrays, array) {
//purely written by github copilot
return arrayOfArrays.some((a) => a.every((v, i) => v === array[i]));
}
/*function: arraysEqual
Checks if two arrays are equal.
Parameters:
a - array
b - array
Returns:
true if a and b are equal, false otherwise
*/
function arraysEqual(a, b) {
//purely written by github copilot
return (
Array.isArray(a) &&
Array.isArray(b) &&
a.length === b.length &&
a.every((val, index) => val === b[index])
);
}
/*function: range
Generates an array of integers from start to end.
(Like the python range function)
Parameters:
start - integer
end - integer
Returns:
array of integers from start to end
*/
function range(start, end) {
//purely written by github copilot
return Array(end - start)
.fill()
.map((_, idx) => start + idx);
}
//------------end of perfect matching alghoritm------------
/*function: cleanUpObject
Recursively removes all children of an THREE.js object3D and disposes of their geometry and material.
Parameters:
object - object to clean up
Returns:
none
*/
function cleanUpObject(object) {
object.children.forEach((child) => {
cleanUpObject(child);
});
if (object.geometry != undefined && object.material != undefined) {
console.log("cleaning up", object);
let material = object.material;
if (material instanceof THREE.Material) {
material.dispose();
} else if (Array.isArray(material)) {
for (let i = 0; i < material.length; ++i) {
material[i].dispose();
}
}
object.geometry.dispose();
}
object.removeFromParent();
return;
}
/*function: buildEntries4Config
Builds the entries for the pytheus search config file from the graph data.
Parameters:
graphData - graph data object from the graph-component
Returns:
result - object containing the entries for the config file
*/
function buildEntries4Config(graphData) {
let edges = graphData.graph.edges.map((edge) => edge.getConfig().config);
let vertices = graphData.graph.vertices;
let result = {
removed_connections: [],
init_graph: [],
nodes2connect: [],
};
// colored edges remain fixed
let fixedEdges = edges.filter(
(edgeConf) => edgeConf[2] != 99 && edgeConf[3] != 99
);
// black edges in the template are to tbe optimised
let edgesToBeOptimised = edges
.filter((edgeConf) => edgeConf[2] == 99 || edgeConf[3] == 99)
.map((edgeConf) => [edgeConf[0], edgeConf[1]]);
if (fixedEdges.length != 0) {
// if there is an initial graph init_graph and nodes2connect are used to specify the optimisation
result["init_graph"] = fixedEdges;
result.nodes2connect = edgesToBeOptimised;
} else {
// if there is no initial graph, the optimisation is done with the removedConnections keyword
let notNeighbours
for (let i = 0; i < vertices.length; i++) {
const vertex = vertices[i];
notNeighbours = vertices.filter(
(v) => !vertex.data.neighbours.includes(v) & v.data.numId > vertex.data.numId
);
result.removed_connections.push(...notNeighbours.map((v) => [vertex.data.numId, v.data.numId]));
}
}
return result;
}
/*function: download
Downloads a file to the local machine.
Parameters:
content - content of the file
fileName - name of the file
contentType - type of the file [eg. 'json']
Returns:
none
*/
function download(content, fileName, contentType) {
var a = document.createElement("a");
var file = new Blob([content], { type: contentType });
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
console.log('download initiated')
URL.revokeObjectURL(a.href)
a.remove();
}