我试图使用渐近来帮助我解析一些与逻辑相关的文本文件(经过额外的字符串处理:例如生成编号为x-var的x0, x1...),并且我不理解以下行为:
in_ = '( ( x1 & x2 & x3 & x4 & x5 & x6 & x7 & x8 & x9 ) | ( x1 & x2 & x3 & x4 & x5 & x6 & x7 & x10 & x9 ) | ( x1 & x11 & x3 & x12 & x5 & x13 & x14 & x15 & x9 ) ) '
from sympy.parsing.sympy_parser import parse_expr
from sympy.logic.boolalg import to_cnf, is_cnf
parsed = parse_expr(in_, evaluate=False)
cnf_candidate = to_cnf(parsed, simplify=True) # broken with simp=True; works with simp=False
cnf_status = is_cnf(cnf_candidate)
print(parsed)
print(cnf_candidate)
print(cnf_status)
assert cnf_status
> (x1 & x10 & x2 & x3 & x4 & x5 & x6 & x7 & x9) | (x1 & x11 & x12 & x13 & x14 & x15 & x3 & x5 & x9)
(x1 & x2 & x3 & x4 & x5 & x6 & x7 & x8 & x9)
> (x1 & x10 & x2 & x3 & x4 & x5 & x6 & x7 & x9) | (x1 & x11 & x12 & x13 & x14 & x15 & x3 & x5 & x9) |
(x1 & x2 & x3 & x4 & x5 & x6 & x7 & x8 & x9)
> False
> AssertionError这看起来真的很糟!
to_cnf并没有真正产生cnf,也没有警告我(使用simplify=True).)。
不简化:
。
这看起来有点像一个“我永远无法最小化它,所以我不会尝试”的东西,没有任何反馈。
我错过了什么吗?我的用法正确吗(我假设渐近解析可以使用我的编号变量)。
(现在,让我们忽略->指数爆炸的理论方面;简化的可行性)
发布于 2020-03-18 17:05:28
to_cnf函数使用simplify=True调用simplify_logic,而不传递force=True标志设置。由于高速公路有8个以上的变量,所以不尝试转换到cnf,并且例程不检查简化的结果是否为cnf形式。一个简单的补丁是
diff --git a/sympy/logic/boolalg.py b/sympy/logic/boolalg.py
index dd734ce..d544ea7 100644
--- a/sympy/logic/boolalg.py
+++ b/sympy/logic/boolalg.py
@@ -1714,7 +1714,9 @@ def to_cnf(expr, simplify=False):
return expr
if simplify:
- return simplify_logic(expr, 'cnf', True)
+ new = simplify_logic(expr, 'cnf', True)
+ if is_cnf(new):
+ return new
# Don't convert unless we have to
if is_cnf(expr):然后(如果您想简化结果)必须用force=True调用force=True。
https://stackoverflow.com/questions/60740347
复制相似问题