简体   繁体   English

SymPy to_cnf()评估为False

[英]SymPy to_cnf() evaluating to False

I am trying to take a Boolean expression and convert it to conjunctive normal form using to_cnf() . 我正在尝试采用布尔表达式,并使用to_cnf()将其转换为合取范式。

My Boolean expression is rather complicated, so I am having trouble debugging. 我的布尔表达式相当复杂,因此调试时遇到麻烦。 I have not been able to reproduce the output CNF: False , with any simple Boolean expression. 我无法使用任何简单的布尔表达式来重现输出CNF: False What could be causing to_cnf to output False? 是什么导致to_cnf输出False?

#TEST 1
from sympy import symbols
from sympy.logic.boolalg import to_cnf
from sympy.logic import simplify_logic

ls = symbols('a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z')
z = "~(~(~(ls[11] | ~(~(~(~(ls[5]) | ~(ls[3])) | ls[15] & ls[18]) & ~(~(~(ls[5]) | ~(ls[3])) & ls[15])) & ls[17]) & ~(ls[11] & ~(~(~(~(ls[5]) | ~(ls[3])) | ls[15] & ls[18]) & ~(~(~(ls[5]) | ~(ls[3])) & ls[15]))) & ~(ls[14] | ls[7] | ls[1])) & ~(~(~(ls[14]) | ls[7] | ls[1]) & ~(ls[11]) | ls[17] & ~(ls[18]) | ls[15] & ~(ls[3]) | ls[5] | ~(ls[18]) & ls[15] | ~(ls[11]) & ls[17]) | ~(~(ls[4]) & ls[12] | ~(~(ls[4]) | ls[12])) & ~(ls[14]) | ls[1] | ~(ls[7]) | ~(ls[17]) | ~(ls[15]) | ~(ls[5]) & ~(ls[7]) | ls[14] | ~(ls[1]) | ls[12] & ~(~(ls[12] | ls[15] | ls[5] | ls[17]) & ~(ls[14] | ls[1] | ~(ls[7])) | ~(ls[4]) & ls[12] | ~(~(ls[4]) | ls[12]) & ~(~(ls[1]) | ~(ls[14]))) & ~(~(~(ls[15] | ls[5]) | ls[14] | ls[1] | ~(ls[7]) & ~(ls[4]) | ~(ls[1]) | ls[7] & ~(ls[17]) | ls[14] | ls[1] | ~(ls[7]) & ~(ls[14]) | ls[1] | ~(ls[7]) | ~(~(ls[17]) | ~(ls[15]) | ~(ls[5]))) & ls[12]) & ~(~(~(ls[11]) | ls[17] & ~(ls[18]) | ls[15] & ~(ls[3]) | ls[5] | ~(ls[18]) & ls[15] | ~(ls[11]) & ls[17]) & ~(~(ls[14]) | ls[7] | ls[1])) & ~(~(ls[14] | ls[7] | ls[1]) & ~(~(ls[11] | ~(~(~(~(ls[5]) | ~(ls[3])) | ls[15] & ls[18]) & ~(~(~(ls[5]) | ~(ls[3])) & ls[15])) & ls[17]) & ~(ls[11] & ~(~(~(~(ls[5]) | ~(ls[3])) | ls[15] & ls[18]) & ~(~(~(ls[5]) | ~(ls[3])) & ls[15]))))) | ~(ls[4]) & ls[12] | ~(~(ls[4]) | ls[12]))"
print(eval(z))
z = to_cnf(eval(z))
print(z)

Output of Test 1 测试1的输出

Eval:  ~(b | o | ~b | ~h | ~p | ~r | (m & ~e) | (~f & ~h) | ~(m | ~e) | (~o & ~((m & ~e) | ~(m | ~e))) | (~(f | (p & ~d) | (p & ~s) | (r & ~l) | (r & ~s) | (~l & ~(b | h | ~o))) & ~(~(b | h | o) & ~(l & ~(~(p & ~(~d | ~f)) & ~((p & s) | ~(~d | ~f)))) & ~(l | (r & ~(~(p & ~(~d | ~f)) & ~((p & s) | ~(~d | ~f))))))) | (m & ~(~(b | h | ~o) & ~(f | ~l | (p & ~d) | (p & ~s) | (r & ~l) | (r & ~s))) & ~((m & ~e) | (~(m | ~e) & ~(~b | ~o)) | (~(b | o | ~h) & ~(f | m | p | r))) & ~(m & ~(b | o | ~b | ~h | (h & ~r) | ~(f | p) | (~e & ~h) | (~h & ~o) | ~(~f | ~p | ~r))) & ~(~(b | h | o) & ~(~(l & ~(~(p & ~(~d | ~f)) & ~((p & s) | ~(~d | ~f)))) & ~(l | (r & ~(~(p & ~(~d | ~f)) & ~((p & s) | ~(~d | ~f)))))))))
CNF:  False

If I replace z with something simple like z = ls[0] | ls[1] 如果我将z替换为z = ls[0] | ls[1] z = ls[0] | ls[1] , everything works as expected. z = ls[0] | ls[1] ,一切正常。

#TEST 2
from sympy import symbols
from sympy.logic.boolalg import to_cnf
from sympy.logic import simplify_logic

ls = symbols('a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z')
z = "ls[0] | ls[1]"
print("Eval: ",eval(z))
z = to_cnf(eval(z))
print("CNF: ", z)

Output of Test 2 测试2的输出

Eval:  a | b
CNF:  a | b

A logical expression may well simplify to False, for example 逻辑表达式可以很好地简化为False,例如

>>> x = symbols('x')
>>> simplify_logic(x & ~x)
False

Perhaps the same is true for your expression? 也许您的表情也是如此? The current dev version of SymPy thinks so, too: SymPy的当前开发版本也这样认为:

>>> z = simplify_logic(eval(z), form='cnf')
>>> z
False

Unlike SymPy 1.1.1, where to_cnf(eval(z)) returns False, the current development version, available by cloning from GitHub , evaluates the line z = to_cnf(eval(z)) after a very long wait (you can go for a walk meanwhile). 与SymPy 1.1.1的to_cnf(eval(z))返回False不同,当前的开发版本(可通过从GitHub克隆获得 z = to_cnf(eval(z))在经过很长的等待后评估z = to_cnf(eval(z)) (您可以同时散步)。 I don't include the output here, because it far exceeds the post length limit. 我不在这里包括输出,因为它远远超出了帖子长度的限制。

>>> len(str(z))
1013494

But given how often a symbol is combined with the negation of self, I'm not discounting the possibility of False being the correct simplified answer. 但是考虑到符号与自我的否定相结合的频率,我并不轻视False是正确的简化答案的可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM