我有一个DIMACS cnf格式的文件,需要将其操作为SAT解算器所需的格式。
具体来说,我需要得到:
['c horn? no', 'c forced? no', 'c mixed sat? no', 'c clause length = 3', 'c', 'p cnf 20 91', '4 -18 19 0', '3 18 -5 0', '-5 -8 -15 0', '-20 7 -16 0']
至
[[4,-18,19,0], [3,18,-5,0],[-5,-8,-15,0],[-20,7,-16,0]]
谢谢你的帮助!
作为一个快速破解,您可以简单地使用
in_data = ['c horn? no', 'c forced? no', 'c mixed sat? no', 'c clause length = 3', 'c', 'p cnf 20 91', '4 -18 19 0', '3 18 -5 0', '-5 -8 -15 0', '-20 7 -16 0']
out_data = [[int(n) for n in line.split()] for line in in_data if line[0] not in ('c', 'p')]
print(out_data)
将输出
[[4, -18, 19, 0], [3, 18, -5, 0], [-5, -8, -15, 0], [-20, 7, -16, 0]]
但是,您可能想要使用类似的东西
out_data = [[int(n) for n in line.split() if n != '0'] for line in in_data if line[0] not in ('c', 'p')]
相反,从子句中删除终止零:
[[4, -18, 19], [3, 18, -5], [-5, -8, -15], [-20, 7, -16]]
但是真正的dimacs解析器实际上应该使用终止零,而不是假设每行有一个子句。所以这里有一个合适的dimacs解析器:
in_data = ['c horn? no', 'c forced? no', 'c mixed sat? no', 'c clause length = 3', 'c', 'p cnf 20 91', '4 -18 19 0', '3 18 -5 0', '-5 -8 -15 0', '-20 7 -16 0']
cnf = list()
cnf.append(list())
maxvar = 0
for line in in_data:
tokens = line.split()
if len(tokens) != 0 and tokens[0] not in ("p", "c"):
for tok in tokens:
lit = int(tok)
maxvar = max(maxvar, abs(lit))
if lit == 0:
cnf.append(list())
else:
cnf[-1].append(lit)
assert len(cnf[-1]) == 0
cnf.pop()
print(cnf)
print(maxvar)