forked from connorbybee/hoim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsat_problems.py
More file actions
136 lines (97 loc) · 3.83 KB
/
sat_problems.py
File metadata and controls
136 lines (97 loc) · 3.83 KB
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
import dwavebinarycsp as dbcsp
from os.path import join
def load_sat(sat_dir):
with open(sat_dir, 'r') as fp:
# csp = dbcsp.cnf.load_cnf(fp)
csp = load_cnf(fp)
return csp
import re
import dimod
from dwavebinarycsp import ConstraintSatisfactionProblem
_PROBLEM_REGEX = r'^p cnf (\d+)\s*(\d+)'
_CLAUSE_REGEX = r'^-?[0-9]\d*(?:\W-?[1-9]\d*)*\W0$'
def load_cnf(fp):
fp = iter(fp) # handle lists/tuples/etc
csp = ConstraintSatisfactionProblem(dimod.BINARY)
# first look for the problem
num_clauses = num_variables = 0
problem_pattern = re.compile(_PROBLEM_REGEX)
for line in fp:
matches = problem_pattern.findall(line)
if matches:
if len(matches) > 1:
raise ValueError
nv, nc = matches[0]
num_variables, num_clauses = int(nv), int(nc)
break
# now parse the clauses, picking up where we left off looking for the header
clause_pattern = re.compile(_CLAUSE_REGEX)
for line in fp:
line = line.strip()
if clause_pattern.match(line) is not None:
clause = [int(v) for v in line.split(' ')[:-1]] # line ends with a trailing 0
# -1 is the notation for NOT(1)
variables = [abs(v) for v in clause]
f = _cnf_or(clause)
csp.add_constraint(f, variables)
for v in range(1, num_variables+1):
csp.add_variable(v)
for v in csp.variables:
if v > num_variables:
msg = ("given .cnf file's header defines variables [1, {}] and {} clauses "
"but constraints a reference to variable {}").format(num_variables, num_clauses, v)
raise ValueError(msg)
if len(csp) != num_clauses:
msg = ("given .cnf file's header defines {} "
"clauses but the file contains {}").format(num_clauses, len(csp))
raise ValueError(msg)
return csp
def _cnf_or(clause):
def f(*args):
return any(v == int(c > 0) for v, c in zip(args, clause))
return f
def reduce_cnf(fp):
fp = iter(fp) # handle lists/tuples/etc
csp = ConstraintSatisfactionProblem(dimod.BINARY)
# first look for the problem
num_clauses = num_variables = 0
problem_pattern = re.compile(_PROBLEM_REGEX)
for line in fp:
matches = problem_pattern.findall(line)
if matches:
if len(matches) > 1:
raise ValueError
nv, nc = matches[0]
num_variables, num_clauses = int(nv), int(nc)
break
# now parse the clauses, picking up where we left off looking for the header
clause_pattern = re.compile(_CLAUSE_REGEX)
for line in fp:
line = line.strip()
if clause_pattern.match(line) is not None:
clause = [int(v) for v in line.split(' ')[:-1]] # line ends with a trailing 0
# -1 is the notation for NOT(1)
variables = [abs(v) for v in clause]
f = _cnf_or(clause)
csp.add_constraint(f, variables)
for v in range(1, num_variables + 1):
csp.add_variable(v)
for v in csp.variables:
if v > num_variables:
msg = ("given .cnf file's header defines variables [1, {}] and {} clauses "
"but constraints a reference to variable {}").format(num_variables, num_clauses, v)
raise ValueError(msg)
if len(csp) != num_clauses:
msg = ("given .cnf file's header defines {} "
"clauses but the file contains {}").format(num_clauses, len(csp))
raise ValueError(msg)
return csp
def reduce_sat(sat_dir):
with open(sat_dir, 'r') as fp:
# csp = dbcsp.cnf.load_cnf(fp)
csp = reduce_cnf(fp)
return csp
if __name__ == '__main__':
problems_dir = '/home/connor/repositories/hoim/sat'
problem = 'uf20-01.cnf'
reduce_sat(join(problems_dir, problem))