- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 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)
关于python - python 解析dimacs CNF文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28890268/
Z3 目前支持 DIMACS 格式的输入。有没有办法在解决之前输出问题的DIMACS格式?我的意思是将问题转换为系统 CNF 并以 DIMACS 格式输出。 如果没有,任何朝这个方向发展的想法都会很有
我对 SAT 和 Z3 非常陌生(甚至还没有开始使用 SMT)。我一直在玩gophersat (一个很好的 Go 实现,可以解决一系列很好的 SAT 问题),我在那里发现了 DIMACS 格式。尽管我
我对 SAT 和 Z3 非常陌生(甚至还没有开始使用 SMT)。我一直在玩gophersat (一个很好的 Go 实现,可以解决一系列很好的 SAT 问题),我在那里发现了 DIMACS 格式。尽管我
我正在尝试重现在 this paper 中进行的实验,测量算法在 DIMACS 顶点着色基准图上的性能,可以找到 here . 图表采用 DIMACS 标准格式,我想将它们解析为 C++ Boost
解决一个set of Boolean equations ,我正在试验 Constraint-Programming Solver MiniZinc使用以下输入: % Solve system of
我是一名优秀的程序员,十分优秀!