Datasets:
File size: 4,289 Bytes
0dd9df7 | 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 | """Exact counts with and without knowledge of the positive exact-one formula."""
from collections import defaultdict
from itertools import combinations, product
from decide import XorBasis
def orthogonal_count(columns):
"""Exact graph-only counter for row overlaps 0,2,4. Exponential in general."""
pairs = defaultdict(list)
bit = 1
column_switches = 0
for col in columns:
rows = sorted(col)
if not rows:
continue
column_switches += 1
forms = {rows[0]: 0}
for row in rows[1:]:
forms[row] = 1 << bit
bit += 1
for a, b in combinations(rows, 2):
pairs[a,b].append(forms[a] ^ forms[b])
basis = XorBasis()
disjunctions = []
for forms in pairs.values():
if len(forms) % 2:
return 0
if len(forms) == 2:
if not basis.add_zero(forms[0] ^ forms[1] ^ 1):
return 0
elif len(forms) == 4:
a,b,c,d = forms
if not basis.add_zero(a ^ b ^ c ^ d):
return 0
disjunctions.append((a ^ b, a ^ c))
else:
raise ValueError("Maximum supported row overlap is four.")
def count(space, clauses):
while True:
remaining = []
changed = False
for a,b in clauses:
a,b = space.reduce(a),space.reduce(b)
if a == 1 or b == 1 or a ^ b == 1:
continue
if a == 0 or b == 0 or a == b:
literal = b if a == 0 else a
if not space.add_zero(literal ^ 1):
return 0
changed = True
else:
remaining.append((a,b))
if not remaining:
return 1 << (bit - 1 - len(space.pivots) + column_switches)
clauses = remaining
if not changed:
break
a,b = clauses[0]
first = XorBasis(space.pivots)
total = count(first,clauses[1:]) if first.add_zero(a ^ 1) else 0
if space.add_zero(a) and space.add_zero(b ^ 1):
total += count(space,clauses[1:])
return total
return count(basis,disjunctions)
def formula_count(clauses, column_count):
"""The construction's weighted model-count identity."""
variables = sorted({x for clause in clauses for x in clause})
exponent = 37*len(clauses) - 3*len(variables) + column_count
total = 0
for assignment in product((0,1), repeat=len(variables)):
x = dict(zip(variables, assignment))
if all(sum(x[v] for v in clause) == 1 for clause in clauses):
total += 1 << (exponent - sum(assignment))
return total
def balance_model_weights(clauses):
"""Parsimoniously ensure that all models have the same number of true bits."""
var = sorted({x for clause in clauses for x in clause})
if not var:
raise ValueError("Input must contain a clause.")
z, one = max(var)+1, max(var)+2
new = [*clauses, (z,z,one)]
for i, x in enumerate(var):
new.append((x,one+i+1,z))
return new, len(var)+1
def matching_formula(matrix):
"""Parsimonious positive triples for a bipartite adjacency matrix (0/1)."""
n = len(matrix)
if n == 0 or any(len(row) != n or set(row)-{0,1} for row in matrix):
raise ValueError("Expected a nonempty square 0/1 matrix.")
z, one = 0,1
formula = [(z,z,one)]
incident = [[] for _ in range(2*n)]
next_var = 2
for i, row in enumerate(matrix):
for j, value in enumerate(row):
if value:
incident[i].append(next_var)
incident[n+j].append(next_var)
next_var += 1
for edges in incident:
if not edges:
formula.append((z,z,z))
continue
prev_complement = z
for index, edge in enumerate(edges):
if index + 1 == len(edges):
formula.append((edge,z,prev_complement))
else:
cur, cur_complement = next_var,next_var+1
next_var += 2
formula.extend(((edge,cur,prev_complement),(cur,cur_complement,z)))
prev_complement = cur_complement
return formula
|