-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomorphism_finder.py
383 lines (342 loc) · 16 KB
/
automorphism_finder.py
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
import logging
import threading
from types import SimpleNamespace
from typing import List, Union, Set, Callable
import numpy as np
from sympy.combinatorics import PermutationGroup, Permutation
import kernel_density
import verify_transformations as vt
from mipsym.mip import Norm, Solver
from mipsym.mip_reduced import create_reduced_mip_model
from mipsym.mip import create_mip_solver
from mipsym.tools import to_ndarray, to_list, to_matrix, matshow, deviation_value
logger = logging.getLogger("pyAGA_presolving")
def print_permutation(text: str, norm: Norm, a: np.ndarray, perm: List[int]):
p = vt.to_matrix(perm)
logger.info(text)
logger.info(f"Error Norm = {deviation_value(norm, p, a)}")
logger.info(f"Permutation = {perm}")
def find_automorphisms(
adjacency_matrix: np.ndarray,
fault_tolerance: int,
round_decimals: int,
quiet: bool,
bandwidth: float,
norm: Norm,
error_value_limit,
use_integer_programming: bool,
stop_thread: Callable[[None], bool] = None,
) -> List[List[Union[int, None]]]:
"""
Find approximate automorphisms (i.e. graph symmetries) of a given graph.
:param adjacency_matrix: The adjacency matrix of the graph.
:param fault_tolerance: The number of tolerated unmappable nodes.
:param round_decimals: The number of decimals left after rounding the adjacency
matrix values. If None, no rounding takes place.
:param quiet: Whether to print debugging information to the terminal.
:param bandwidth: The bandwidth parameter for the kernel density estimation and
subsequent bin calculation.
:param norm: The norm used to calculate the deviation value (the error term).
:param error_value_limit: The limit a permutation can deviate from a perfect
graph symmetry in order for it to be included in the permutation group.
:param use_integer_programming: Whether or not to use the integer programming
routines to fill out partial transformations.
:param stop_thread: This function passes along a lambda callback to tell this
thread to terminate.
:return: A tuple containing the found transformations as its first entry,
and the average matchrate over the found transformations as its second.
Matchrates are the ratios of correctly mapped nodes to unmappable ones.
"""
if round_decimals is not None:
adjacency_matrix = adjacency_matrix.round(round_decimals)
bins = kernel_density.bins(
adjacency_matrix,
bandwidth=bandwidth,
plot=logger.level == logging.DEBUG
and threading.current_thread() is threading.main_thread(),
)
labels = np.digitize(adjacency_matrix, bins=bins)
unique_values, _ = np.unique(labels, return_counts=True)
equivalency_classes = []
for value in unique_values:
s = np.argwhere(labels == value)
equivalency_classes.append([j for j in s if j[0] <= j[1]])
n = np.shape(adjacency_matrix)[0]
# possible_mappings: entry i is set of all possible mappings based on the
# adjacency matrix
possible_mappings = [set(range(n))] * n
# this is where the algorithm starts
automorphisms = []
# Quick and dirty way to implement mutable python ints. Yeah, I know.
number_of_MIP_calls = SimpleNamespace(valid=[0], invalid=[0])
calculate_automorphisms(
adjacency_matrix,
equivalency_classes,
possible_mappings,
quiet,
fault_tolerance,
norm,
error_value_limit,
use_integer_programming,
number_of_MIP_calls,
automorphisms,
stop_thread,
)
return automorphisms, number_of_MIP_calls
def calculate_automorphisms( # noqa: C901
adjacency_matrix: np.ndarray,
equivalency_classes: List[List[np.ndarray]],
possible_mappings: List[Set],
quiet: bool,
fault_tolerance: int,
norm: Norm,
error_value_limit,
use_integer_programming: bool,
number_of_MIP_calls: List[int],
result: List[List[Union[int, None]]],
stop_thread,
) -> None:
"""Calculate the transformations with the given possible mappings. This function
is called recursively, until all sets in the possible mappings have at most one
entry.
:param adjacency_matrix: The adjacency matrix of the graph.
:param equivalency_classes: A list of lists of edges which can be mapped onto
each other freely, as they are considered to be equivalent, i.e. their weights
fall into the same bin.
:param possible_mappings: A list containing all possible mappings of the node at
each position.
:param quiet: Whether to print debugging information to the terminal.
:param fault_tolerance: The number of tolerated unmappable nodes.
:param norm: The norm used to calculate the deviation value (the error term).
:param error_value_limit: The limit a permutation can deviate from a perfect
graph symmetry in order for it to be included in the permutation group.
:param use_integer_programming: Whether or not to use the integer programming
routines to fill out partial transformations.
:param number_of_MIP_calls: A hacked mutable int (a list containing only one
number) to track how many times the MIP has been called.
:param result: The list containing all currently found transformations.
:param stop_thread: This function passes along a lambda callback to tell this
thread to terminate.
:return: None
"""
if stop_thread is not None and stop_thread():
# If the stop_thread lambda callback is activated, exit early.
return
number_of_matches = {
"impossible": sum(1 for i in possible_mappings if len(i) == 0),
"perfect": sum(1 for i in possible_mappings if len(i) == 1),
"unsure": sum(1 for i in possible_mappings if len(i) > 1),
}
assert sum(number_of_matches.values()) == len(possible_mappings)
if number_of_matches["impossible"] > fault_tolerance:
return # number of unmatched nodes exceeds fault tolerance
if number_of_matches["unsure"] > 0:
# search for the shortest remaining possible mapping...
node_of_shortest = -1
length_of_shortest = 2 * len(possible_mappings)
for i, m in enumerate(possible_mappings):
if 1 < len(m) < length_of_shortest:
length_of_shortest = len(m)
node_of_shortest = i
# ... and just try all candidates in there via a recursive call
for potential_target in possible_mappings[node_of_shortest]:
new_poss = filter_perms(
equivalency_classes,
list(possible_mappings),
node_of_shortest,
potential_target,
)
if new_poss == possible_mappings:
logger.error(
"This should not happen. Please assure that all "
"self-concurrences have fallen into the same bin."
)
assert False
calculate_automorphisms(
adjacency_matrix,
equivalency_classes,
new_poss,
quiet,
fault_tolerance,
norm,
error_value_limit,
use_integer_programming,
number_of_MIP_calls,
result,
stop_thread,
)
else:
# Check if possible_mappings contains identical single_element entries
perfectly_matched = [
list(mapping)[0] for mapping in possible_mappings if len(mapping) == 1
]
if len(set(perfectly_matched)) < len(perfectly_matched):
return # supplied possible_mappings does not allow for a valid permutation
if number_of_matches["impossible"] == 0:
# for every node, we exactly have one target node left - this is a complete permutation
permutation = [s.pop() for s in possible_mappings]
if permutation not in result:
# Calculate all powers of every permutation for which we have been able
# to find all entries during pre-solving.
new_permutation = Permutation(permutation)
permutations_already_found = [Permutation(perm) for perm in result]
permutations = [new_permutation] + permutations_already_found
# Calculate all powers of every permutation which we could complete
# using MIP.
all_group_elements = map(
lambda perm: list(perm), PermutationGroup(permutations).elements
)
for perm in all_group_elements:
if perm not in result:
result.append(perm)
if logger.level <= logging.INFO:
print_permutation(
f"Permutation number {len(result)} matched all nodes.",
norm,
adjacency_matrix,
permutation,
)
logger.debug("\n" + matshow(to_matrix(permutation)))
else:
logger.info(
"The transformation generated in the presolving step is"
" consistent with a transformation already found."
)
else:
# for at least one node there is no target node left
# nodes either have exactly one match target or cannot be matched at all
permutation = [s.pop() if len(s) > 0 else None for s in possible_mappings]
logger.info(
f"Incomplete permutation number {len(result)} matched "
f'{number_of_matches["perfect"]} nodes.'
)
logger.info(permutation)
if vt.verify_transformation(permutation, result):
logger.info(
"The partial transformation generated in the presolving step is"
" consistent with at least one transformation already found."
)
return
if use_integer_programming:
# Count the number of total MIP calls, starting from 1
logger.info(
f"MIP call #"
f"{number_of_MIP_calls.valid[0] + number_of_MIP_calls.invalid[0] + 1}"
)
logger.info("Trying to fill missing entries in permutation using MIP")
# Construct a reduced MIP only containing rows/cols that are still not resolved
# Mappings for identifying the rows/cols of the reduced problem and corresponding matrices
row_index_map = [i for i, p in enumerate(permutation) if p is None]
col_index_map = [
i for i, _ in enumerate(permutation) if i not in permutation
]
# Calculate a temporary complete permutation that assigns unmapped vertices in some way
# Idea is to apply this to the adjacency matrix s.t. one obtains correctly permuted A_row, A_col
tmp_filled_permutation = permutation[:]
for i in range(len(col_index_map)):
tmp_filled_permutation[row_index_map[i]] = col_index_map[i]
tmp_p_matrix = to_matrix(tmp_filled_permutation)
A_l = tmp_p_matrix @ adjacency_matrix
A_r = adjacency_matrix @ tmp_p_matrix
# Solve the reduced problem
# Currently WIP, not fully integrated yet
solver = Solver.SCIP
model = create_reduced_mip_model(
norm, A_l, A_r, col_index_map, row_index_map
)
ip_solver, solve_params = create_mip_solver(solver, norm)
try:
logger.info(f"Solving using {solver}")
ip_results = ip_solver.solve(
model,
tee=not quiet,
timelimit=7200,
report_timing=True,
**solve_params,
)
reduced_p = to_ndarray(
model.P, len(col_index_map), len(row_index_map)
)
reduced_permutation = to_list(reduced_p)
filled_permutation = permutation[:]
for i, p in enumerate(reduced_permutation):
assert filled_permutation[row_index_map[i]] is None
filled_permutation[row_index_map[i]] = col_index_map[p]
logger.info("Solver Result:\n" + str(ip_results))
print_permutation(
"Filled Permutation.",
norm,
adjacency_matrix,
filled_permutation,
)
permutation = filled_permutation
deviation = deviation_value(
norm, to_matrix(permutation), adjacency_matrix
)
if deviation < error_value_limit:
number_of_MIP_calls.valid[0] += 1
new_permutation = Permutation(permutation)
permutations_already_found = [
Permutation(perm) for perm in result
]
permutations = [new_permutation] + permutations_already_found
# Calculate all powers of every permutation which we could complete
# using MIP.
all_group_elements = map(
lambda perm: list(perm),
PermutationGroup(permutations).elements,
)
for perm in all_group_elements:
if perm not in result:
result.append(perm)
else:
number_of_MIP_calls.invalid[0] += 1
logger.info(
f"Filled permutation {permutation} with MIP, "
f"but the deviation value {deviation} is "
f"higher than the limit of {error_value_limit}."
)
except RuntimeError:
logger.warning(f"No solution found for {permutation}")
logger.warning("Solver Result:\n" + str(ip_results))
else:
result.append(permutation)
logger.debug("\n" + matshow(to_matrix(permutation)))
def filter_perms(
equivalence_classes: List[List[np.ndarray]],
possible_mappings: List[Set],
start_node: int,
target_node: int,
) -> List[Set]:
"""
Filter the given list of possible mappings under the assumption that start_node is mapped
to target_node.
:param equivalence_classes: A list of lists of edges which can be mapped onto
each other freely, as they are considered to be equivalent, i.e. their weights
fall into the same bin.
:param possible_mappings: A list containing all possible mappings of the node at
each position.
:param start_node: The original node of the assumed map.
:param target_node: The image node of the assumed map.
:return: The possible mappings under the given assumed map.
"""
# Iterate over all possible weights w
for equivalence_class in equivalence_classes:
s_w = set()
# Calculate set s_w of vertices i such that the edge (i, target_node) has weight w
for edge in equivalence_class:
if edge[0] == target_node:
s_w.add(edge[1])
elif edge[1] == target_node:
s_w.add(edge[0])
# Look for vertex z such that edge (start_node, z) also has weight w
for edge in equivalence_class:
if edge[0] == start_node:
z = edge[1]
elif edge[1] == start_node:
z = edge[0]
else:
continue
# Vertex z can only be mapped to vertices in s_w if start_node is mapped to target_node
possible_mappings[z] = possible_mappings[z].intersection(s_w)
return possible_mappings