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
|
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright (C) 2009-2020 Authors of CryptoMiniSat, see AUTHORS file
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2
# of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
import unittest
def inv(l):
assert len(l) > 0
if l[0] == "-":
return l[1:]
else:
return "-"+l
def check_clause_sanity(c):
for i1 in range(len(c)):
l1 = c[i1]
for i2 in range(i1+1, len(c)):
l2 = c[i2]
if l1 == l2:
print("ERROR: Same literal twice in clause: %s", l1)
assert False
if l1 == inv(l2):
print("ERROR: Inverted literal in clause: %s ", l1)
assert False
return True
def can_resolve(c1, c2):
check_clause_sanity(c1)
check_clause_sanity(c2)
# find inverted
resolve_on = None
for a in c1:
for b in c2:
if a==inv(b):
if resolve_on is not None:
return None
resolve_on = a
return resolve_on
def resolve(c1, c2):
resolve_on = can_resolve(c1,c2)
assert resolve_on is not None, "Cannot resolve the two clauses!"
ret = []
for a in c1:
if a != resolve_on:
ret.append(a)
for b in c2:
if b != inv(resolve_on) and b not in ret:
ret.append(b)
ret = sorted(ret)
return ret
def ham_w(num):
w = 0
for i in range(16):
w += (num>>i)&1
return w
def gen_cls(x):
check_clause_sanity(x)
ret = []
for i in range(1<<len(x)):
if ham_w(i)%2 == (len(x)%2):
continue
cl = []
for i2 in range(len(x)):
l = x[i2]
if i>>i2&1 == 1:
cl.append(inv(l))
else:
cl.append(l)
ret.append(cl)
return ret
class TestMethods(unittest.TestCase):
def test_inv(self):
self.assertEqual(inv("a"), "-a")
self.assertEqual(inv("-a"), "a")
self.assertEqual(inv("x1"), "-x1")
self.assertEqual(inv("-x1"), "x1")
def test_sanity_good(self):
self.assertTrue(check_clause_sanity(["a"]))
self.assertTrue(check_clause_sanity(["a", "b"]))
self.assertTrue(check_clause_sanity(["a", "-b"]))
def test_sanity(self):
with self.assertRaises(AssertionError):
check_clause_sanity(["a", "a"])
with self.assertRaises(AssertionError):
check_clause_sanity(["a", "-a"])
def tets_gen_cls(self):
self.assertEqual(gen_cls(["a"]), [["a"]])
self.assertEqual(gen_cls(["-a"]), [["-a"]])
self.assertEqual(gen_cls(["a", "b"]), [["a", "b"], ["-a", "-b"]])
self.assertEqual(gen_cls(["a", "-b"]), [["a", "-b"], ["-a", "b"]])
self.assertEqual(
gen_cls(["a", "b", "c"]),
[["a", "b", "c"], ["-a", "-b", "c"], ["-a", "b", "-c"], ["a", "-b", "-c"]])
def test_ham_w(self):
self.assertEqual(ham_w(1), 1)
self.assertEqual(ham_w(0), 0)
self.assertEqual(ham_w(2), 1)
self.assertEqual(ham_w(3), 2)
self.assertEqual(ham_w(9), 2)
self.assertEqual(ham_w(8), 1)
self.assertEqual(ham_w(11), 3)
def test_resolve(self):
with self.assertRaises(AssertionError):
resolve(["-a", "b"], ["-a", "c"])
with self.assertRaises(AssertionError):
resolve(["-a", "-b"], ["a", "b"])
self.assertEqual(resolve(["a", "b", "c"], ["-a", "b"]), ["b", "c"])
def num_inside(cl,lits):
num = 0
for l in cl:
for l2 in lits:
if l == l2:
num+=1
return num
def find_most_inside(xors, to_prove):
most_inside = 0
most_inside_cl = None
for i_x in range(len(xors)):
x = xors[i_x]
cls = gen_cls(x)
for i_c in range(len(cls)):
cl = cls[i_c]
n = num_inside(cl, to_prove)
if n > most_inside:
most_inside = n
most_inside_cl = cl
most_inside_at = i_x
return most_inside, most_inside_cl, most_inside_at
def find_resolvent_most_inside(xors, cl_other, to_prove):
most_inside = 0
most_inside_cl = None
for i_x in range(len(xors)):
x = xors[i_x]
cls = gen_cls(x)
for i in range(len(cls)):
cl = cls[i]
if can_resolve(cl,cl_other) is None:
continue
n = num_inside(cl, to_prove)
if n > most_inside:
most_inside = n
most_inside_cl = cl
most_inside_at = i_x
return most_inside, most_inside_cl, most_inside_at
def prove(xors, to_prove):
to_prove = sorted(to_prove)
ret = []
most_inside, cl, most_inside_at = find_most_inside(xors, to_prove)
assert most_inside > 0
print("cl picked:", cl)
xors.remove(xors[most_inside_at])
while cl != to_prove:
most_inside, cl2, most_inside_at = find_resolvent_most_inside(xors, cl, to_prove)
assert most_inside > 0
print("cl picked:", cl2)
cl = resolve(cl, cl2)
cl = sorted(cl)
print("resolvent:", cl)
if __name__ == '__main__':
# XORs to add together (we know this)
xors = [
["a", "b", "c"],
["a", "b", "d"]
]
# so c+d = 0
# so c=1 d=0 must be banned
# generate clause
to_prove = ["-c", "d"]
print("xors:", xors)
print("to_prove:", to_prove)
prove(xors, to_prove)
print("----------------")
print("----------------")
print(gen_cls(["v1", "-v2"]))
print(gen_cls(["v1", "v2", "v3"]))
print("----------------")
print("----------------")
unittest.main()
|