-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_by_hash.py
156 lines (134 loc) · 4.63 KB
/
search_by_hash.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
from __future__ import print_function
import random as py_random
import sys
import time
from sage.all import *
from binary_matroid import BinaryMatroid2
# Search for two non-isomorphic matroids with the same
# configurations of lattices of cyclic flats.
def find_collision(rows, cols, strategy='exhaustive', print_progress=True):
global n
n = 0
results = {}
def iteration(matrix_):
global n
n += 1
if print_progress:
print('\r{}'.format(n), end='', file=sys.stderr)
matroid = BinaryMatroid2(matrix=matrix_)
# Only consider simple matroids with no isthmuses
if not matroid.is_simple() or matroid.coloops():
return
config = matroid.cf_lattice_config()
if config in results:
if not matroid.is_isomorphic(results[config][0]):
print("\nFound:")
print(matrix_)
print('--------')
print(results[config][1])
config.show(index=True)
return config
else:
results[config] = (matroid, matrix_)
start = time.time()
if strategy == 'exhaustive':
print("Searching {}x{} matrices exhaustively...".format(ROWS, COLS),
file=sys.stderr)
for matrix_ in MatrixSpace(GF(2), rows, cols):
if iteration(matrix_) is not None:
break
elif strategy == 'random':
print("Searching {}x{} matrices at random...".format(ROWS, COLS),
file=sys.stderr)
while True:
matrix_ = matrix(GF(2), [[py_random.randint(0, 1)
for _ in range(cols)]
for _ in range(rows)])
if iteration(matrix_) is not None:
break
end = time.time()
if print_progress:
print()
print('{} different configurations'.format(len(results)),
file=sys.stderr)
print("Completed in {} seconds".format(round(end - start, 2)),
file=sys.stderr)
for config in results:
config.show(title="{}\n{}".format(*results[config]))
# Find a matroid with a given LCF configuration.
def find_matroid_by_config(rows, cols, search_config, print_progress=True):
global n
n = 0
def iteration(matrix_):
global n
n += 1
if print_progress:
print('\r{}'.format(n), end='', file=sys.stderr)
matroid = BinaryMatroid2(matrix=matrix_)
# Only consider simple matroids with no isthmuses
if not matroid.is_simple() or matroid.coloops():
return
candidate_config = matroid.cf_lattice_config()
if candidate_config == search_config:
print("\nFound:")
print(matroid)
print(matrix_)
print('--------')
return matroid
start = time.time()
print("Searching {}x{} matrices exhaustively...".format(ROWS, COLS),
file=sys.stderr)
for matrix_ in MatrixSpace(GF(2), rows, cols):
if iteration(matrix_) is not None:
break
end = time.time()
if print_progress:
print()
print("Completed in {} seconds".format(round(end - start, 2)),
file=sys.stderr)
# Find a matroid with given cyclic_flats.
def find_matroid_by_cf(rows, cols, search_cf, print_progress=True):
global n
n = 0
def iteration(matrix_):
global n
n += 1
if print_progress:
print('\r{}'.format(n), end='', file=sys.stderr)
matroid = BinaryMatroid2(matrix=matrix_)
# Only consider simple matroids with no isthmuses
#if not matroid.is_simple() or matroid.coloops():
#return
candidate_cf = matroid.cyclic_flats()
if candidate_cf == search_cf:
print("\nFound:")
print(matroid)
print(matrix_)
print('--------')
return matroid
start = time.time()
print("Searching {}x{} matrices exhaustively...".format(ROWS, COLS),
file=sys.stderr)
for matrix_ in MatrixSpace(GF(2), rows, cols):
if iteration(matrix_) is not None:
break
end = time.time()
if print_progress:
print()
print("Completed in {} seconds".format(round(end - start, 2)),
file=sys.stderr)
if __name__ == '__main__':
search_cf = {
frozenset({0,1,2,3,4,5,6}),
frozenset({0,1,2,3,4}),
frozenset({0,1,5,6}),
frozenset({0,2,5,6}),
frozenset({0,2,3}),
frozenset({1,2,4}),
frozenset({}),
}
ROWS = 4
COLS = 7
STRATEGY = 'exhaustive'
PRINT_PROGRESS = True
find_matroid_by_cf(ROWS, COLS, search_cf, PRINT_PROGRESS)