我正在尝试使用布尔表达式并使用$cart = Order::where('user_id', $user->id)->with('orderItems')->get();
$cart_items = $cart->orderItems;
将其转换为合取范式。
我的布尔表达式相当复杂,所以调试时遇到问题。我无法使用任何简单的布尔表达式重现输出to_cnf()
。什么可能导致to_cnf输出False?
CNF: False
测试1的输出
#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)
如果我用简单的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
替换z,一切都按预期工作。
z = ls[0] | ls[1]
测试2的输出
#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)
答案 0 :(得分:0)
逻辑表达式很可能简化为False,例如
>>> x = symbols('x')
>>> simplify_logic(x & ~x)
False
也许你的表达也是如此? SymPy的当前开发版本也是这样认为的:
>>> z = simplify_logic(eval(z), form='cnf')
>>> z
False
与SymPy 1.1.1不同,其中to_cnf(eval(z))
返回False,克隆from GitHub可用的当前开发版本在等待很长时间后评估行z = to_cnf(eval(z))
(你可以去同时散步)。我这里没有包含输出,因为它远远超过了帖子长度限制。
>>> len(str(z))
1013494
但考虑到符号与自我否定相结合的频率,我并没有忽视False成为正确简化答案的可能性。