-
Notifications
You must be signed in to change notification settings - Fork 0
/
mm2x2x2-xor.py
246 lines (220 loc) · 10.9 KB
/
mm2x2x2-xor.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
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 11 19:31:50 2014
@author: Eugene Petkevich
"""
import satmaker
# To make an input for a SAT-solver, we need to associate each variable with
# a number automatically, while keeping string reference for ourselves.
# For this we use a VariableFactory and ConstraintCollector classes.
vf = satmaker.VariableFactory();
cc = satmaker.ConstraintCollector();
# Here we define constants: size and numbers of multiplication vectors.
MATRIX_SIZE = 2
MULTIPLICATION_VECTORS = 15
# We start with basic variables, 24 variables for each of the 15
# final computed multiplications: 8 for each of elements of first matrix (A),
# 4 for second matrix (B), and 4 for third matrix(C).
a = [[[[vf.next()
for la in range(MATRIX_SIZE)]
for ja in range(MATRIX_SIZE)]
for ia in range(MATRIX_SIZE)]
for k in range(MULTIPLICATION_VECTORS)]
b = [[[[vf.next()
for lb in range(MATRIX_SIZE)]
for jb in range(MATRIX_SIZE)]
for ib in range(MATRIX_SIZE)]
for k in range(MULTIPLICATION_VECTORS)]
c = [[[[vf.next()
for lc in range(MATRIX_SIZE)]
for jc in range(MATRIX_SIZE)]
for ic in range(MATRIX_SIZE)]
for k in range(MULTIPLICATION_VECTORS)]
# Now we calculate result vectors of the product matrix D (D = A*B*C).
d = []
for id in range(MATRIX_SIZE):
d.append([])
for jd in range(MATRIX_SIZE):
d[id].append([])
for ld in range(MATRIX_SIZE):
d[id][jd].append([])
for ia in range(MATRIX_SIZE):
d[id][jd][ld].append([])
for ja in range(MATRIX_SIZE):
d[id][jd][ld][ia].append([])
for la in range(MATRIX_SIZE):
d[id][jd][ld][ia][ja].append([])
for ib in range(MATRIX_SIZE):
d[id][jd][ld][ia][ja][la].append([])
for jb in range(MATRIX_SIZE):
d[id][jd][ld][ia][ja][la][ib].append([])
for lb in range(MATRIX_SIZE):
d[id][jd][ld][ia][ja][la][ib][jb].append([])
for ic in range(MATRIX_SIZE):
d[id][jd][ld][ia][ja][la][ib][jb][lb].append([])
for jc in range(MATRIX_SIZE):
d[id][jd][ld][ia][ja][la][ib][jb][lb][ic].append([])
for lc in range(MATRIX_SIZE):
d[id][jd][ld][ia][ja][la][ib][jb][lb][ic][jc].append(False)
for id in range(MATRIX_SIZE):
for jd in range(MATRIX_SIZE):
for ld in range(MATRIX_SIZE):
for o in range(MATRIX_SIZE):
d[id][jd][ld][id][jd][o][id][o][ld][o][jd][ld] = True
# Now we define coefficients q_k_id_jd_ld, k in 1..15, {id, jd, ld} in 1..2,
# for linking multiplication and result vectors:
# <xor for all k in 1..15> m_k_ia_ja_la_ib_jb_lb_ic_jc_lc and q_k_id_jd_ld = d_id_jd_ld_ia_ja_la_ib_jb_lb_ic_jc_lc
# for {id, jd, ld, ia, ja, la, ib, jb, lb, ic, jc, lc} in 1..2.
q = [[[[vf.next()
for ld in range(MATRIX_SIZE)]
for jd in range(MATRIX_SIZE)]
for id in range(MATRIX_SIZE)]
for k in range(MULTIPLICATION_VECTORS)]
# Now we define second series of constraints,
# that link multiplication vectors and result vectors.
# We rewrite previous constraints as a step by step computation,
# with introducing additional variables p_k_id_jd_ld_ia_ja_la_ib_jb_lb_ic_jc_lc and t_k_id_jd_ld_ia_ja_la_ib_jb_lb_ic_jc_lc,
# such that:
# p_k_id_jd_ld_ia_ja_la_ib_jb_lb_ic_jc_lc = m_k_ia_ja_la_ib_jb_lb_ic_jc_lc and q_k_id_jd_ld,
# k in 1..15, {id, jd, ld, ia, ja, la, ib, jb, lb, ic, jc, lc} in 1..2;
# and
# t_k_id_jd_ld_ia_ja_la_ib_jb_lb_ic_jc_lc = t_(k-1)_id_jd_ld_ia_ja_la_ib_jb_lb_ic_jc_lc xor p_k_id_jd_ld_ia_ja_la_ib_jb_lb_ic_jc_lc,
# k in 2..15, {id, jd, ld, ia, ja, la, ib, jb, lb, ic, jc, lc} in 1..2,
# t_1_id_jd_ld_ia_ja_la_ib_jb_lb_ic_jc_lc = p_1_id_jd_ld_ia_ja_la_ib_jb_lb_ic_jc_lc;
# and
# t_15_id_jd_ld_ia_ja_la_ib_jb_lb_ic_jc_lc = d_id_jd_ld_ia_ja_la_ib_jb_lb_ic_jc_lc,
# {id, jd, ld, ia, ja, la, ib, jb, lb, ic, jc, lc} in 1..2;
# The last one is rewritten as
# t_15_id_jd_ld_ia_ja_la_ib_jb_lb_ic_jc_lc or not(t_15_id_jd_ld_ia_ja_la_ib_jb_lb_ic_jc_lc),
# depending on d_id_jd_ld_ia_ja_la_ib_jb_lb_ic_jc_lc value, which is known by definition.
# So we define variables p and constraints for them:
p = [[[[[[[[[[[[[vf.next()
for lc in range(MATRIX_SIZE)]
for jc in range(MATRIX_SIZE)]
for ic in range(MATRIX_SIZE)]
for lb in range(MATRIX_SIZE)]
for jb in range(MATRIX_SIZE)]
for ib in range(MATRIX_SIZE)]
for la in range(MATRIX_SIZE)]
for ja in range(MATRIX_SIZE)]
for ia in range(MATRIX_SIZE)]
for ld in range(MATRIX_SIZE)]
for jd in range(MATRIX_SIZE)]
for id in range(MATRIX_SIZE)]
for k in range(MULTIPLICATION_VECTORS)]
for k in range(MULTIPLICATION_VECTORS):
for id in range(MATRIX_SIZE):
for jd in range(MATRIX_SIZE):
for ld in range(MATRIX_SIZE):
for ia in range(MATRIX_SIZE):
for ja in range(MATRIX_SIZE):
for la in range(MATRIX_SIZE):
for ib in range(MATRIX_SIZE):
for jb in range(MATRIX_SIZE):
for lb in range(MATRIX_SIZE):
for ic in range(MATRIX_SIZE):
for jc in range(MATRIX_SIZE):
for lc in range(MATRIX_SIZE):
va = a[k][ia][ja][la]
vb = b[k][ib][jb][lb]
vc = c[k][ic][jc][lc]
vd = q[k][id][jd][ld]
ve = p[k][id][jd][ld][ia][ja][la][ib][jb][lb][ic][jc][lc]
cc.add(positive=[ve],
negative=[va, vb, vc, vd])
cc.add(positive=[va],
negative=[ve])
cc.add(positive=[vb],
negative=[ve])
cc.add(positive=[vc],
negative=[ve])
cc.add(positive=[vd],
negative=[ve])
# So we define variables t and constraints for them. First, a transformation:
# c = a xor b
# can be rewritten as
# c = (a or b) and (not(a) or not(b))
# which in turn is
# (c or not((a or b)) or not((not(a) or not(b)))) and (not(c) or (a or b)) and (not(c) or (not(a) or not(b)))
# which is equal to
# (c or (not(a) and not(b)) or (a and b)) and (not(c) or a or b) and (not(c) or not(a) or not(b))
# which is equal to
# (c or not(a) or b) and (c or a or not(b)) and (not(c) or a or b) and (not(c) or not(a) or not(b))
# This is correct, Tseitin transformations
t = [[[[[[[[[[[[vf.next()
for lc in range(MATRIX_SIZE)]
for jc in range(MATRIX_SIZE)]
for ic in range(MATRIX_SIZE)]
for lb in range(MATRIX_SIZE)]
for jb in range(MATRIX_SIZE)]
for ib in range(MATRIX_SIZE)]
for la in range(MATRIX_SIZE)]
for ja in range(MATRIX_SIZE)]
for ia in range(MATRIX_SIZE)]
for ld in range(MATRIX_SIZE)]
for jd in range(MATRIX_SIZE)]
for id in range(MATRIX_SIZE)]
for id in range(MATRIX_SIZE):
for jd in range(MATRIX_SIZE):
for ld in range(MATRIX_SIZE):
for ia in range(MATRIX_SIZE):
for ja in range(MATRIX_SIZE):
for la in range(MATRIX_SIZE):
for ib in range(MATRIX_SIZE):
for jb in range(MATRIX_SIZE):
for lb in range(MATRIX_SIZE):
for ic in range(MATRIX_SIZE):
for jc in range(MATRIX_SIZE):
for lc in range(MATRIX_SIZE):
positive = []
negative = []
for k in range(MULTIPLICATION_VECTORS-1):
positive.append(p[k][id][jd][ld][ia][ja][la][ib][jb][lb][ic][jc][lc])
k = MULTIPLICATION_VECTORS-1
if d[id][jd][ld][ia][ja][la][ib][jb][lb][ic][jc][lc]:
positive.append(p[k][id][jd][ld][ia][ja][la][ib][jb][lb][ic][jc][lc])
else:
negative.append(p[k][id][jd][ld][ia][ja][la][ib][jb][lb][ic][jc][lc])
cc.add_xor(positive=positive,
negative=negative)
# We have in the end 66016 variables, 311296 clauses, 860160 literals.
# Now we will output all the constraints to a file that will be an input to
# a SAT solver.
# For printing we will use a SatPrinter class.
sp = satmaker.SatPrinter(vf, cc);
sp.print_statistics()
file = open('input-2x2x2-xor.txt', 'wt')
sp.print(file)
file.close()
'''
# Now a SAT solver should be executed and store its output in output.txt file.
# We get back data from the output to variables.
file = open('output.txt', 'rt')
sp.decode_output(file)
file.close()
# We print the important variables into a text file in easily readable form.
file = open('solution.txt', 'wt')
for k in range(7):
file.write('M_' + str(k+1) + ' = (')
members = []
for ia in range(2):
for ja in range(2):
if (a[k][ia][ja]['value']):
members.append('A' + str(ia+1) + '' + str(ja+1))
file.write(' + '.join(members) + ')(')
members = []
for ib in range(2):
for jb in range(2):
if (b[k][ib][jb]['value']):
members.append('B' + str(ib+1) + '' + str(jb+1))
file.write(' + '.join(members) + ')\n')
for ic in range(2):
for jc in range(2):
file.write('C' + str(ic+1) + str(jc+1) + ' = ')
members = []
for k in range(7):
if q[k][ic][jc]['value']:
members.append('M' + str(k+1))
file.write(' + '.join(members) + '\n')
file.close()
'''