-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdimacs.py
48 lines (44 loc) · 1.33 KB
/
dimacs.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
"""
Read or write the DIMACS CNF file format.
"""
def save(filename, problem):
save_file(open(filename, 'w'), problem)
def save_file(f, problem):
nvariables = reduce(max,
(abs(literal) for clause in problem for literal in clause),
0)
nclauses = len(problem)
print >>f, 'p cnf', nvariables, nclauses
for clause in problem:
for literal in clause:
print >>f, literal,
print >>f, 0
def load(filename):
return load_file(open(filename))
def load_file(f):
nvariables = None
nclauses = None
clauses = []
clause = []
for line in f:
if line.startswith('c'):
continue
if line.startswith('p'):
f1, f2, f3, f4 = line.split()
if f1 != 'p' or f2 != 'cnf':
raise Exception('Not in DIMACS CNF format')
nvariables = int(f3)
nclauses = int(f4)
else:
lits = map(int, line.split())
for lit in lits:
if lit == 0:
clauses.append(clause)
clause = []
else:
assert 1 <= abs(lit) <= nvariables
clause.append(lit)
if clause:
clauses.append(clause)
assert nclauses == len(clauses)
return nvariables, clauses