Z3 具有两个以上变量的异或奇偶约束

Z3 具有两个以上变量的异或奇偶约束,z3,z3py,Z3,Z3py,我正在使用以下Python函数为Microsoft的Z3解算器的Python API建模Xor约束,并使用任意数量的变量: # parity function # break up long input lists in two smaller lists def odd(solver, lits): length = len(lits) check(length > 0, "Odd needs argument(s)") if length == 1:

我正在使用以下
Python
函数为Microsoft的
Z3
解算器的
Python API
建模
Xor
约束,并使用任意数量的变量:

#  parity function
#  break up long input lists in two smaller lists
def odd(solver, lits):
    length = len(lits)
    check(length > 0, "Odd needs argument(s)")
    if length == 1:
        solver.add(lits[0])
    elif length == 2:
        solver.add(Xor(lits[0], lits[1]))
    elif length == 3:
        solver.add(Xor(lits[0], Xor(lits[1], lits[2])))
    elif length == 4:
        #  this symmetric form is much faster than the chained forms
        solver.add(Xor(Xor(lits[0], lits[1]), Xor(lits[2], lits[3])))
    else:
        aux = get_aux_variable()

        #  cf. http://www.gregorybard.com/papers/bard_thesis.pdf
        cut_len = 3
        odd(solver, lits[:cut_len] + [aux])
        odd(solver, [aux] + lits[cut_len:])

auxVariableIdx = 0

def get_aux_variable():
    global auxVariableIdx

    auxVariableIdx += 1
    aux = Bool('t%s' % auxVariableIdx)
    return aux
我注意到,整体解算器性能在很大程度上取决于方法,较小的
Xor
子案例是建模的。示例:我使用“
Xor(Xor(a,b),Xor(c,d))
”来声明具有四个输入的
Xor
。在我的例子中,另一种选择“
Xor(a,Xor(b,Xor(c,d)))
”的速度慢了十倍

另一个例子:如果为链接子表达式而创建的辅助切换变量添加到列表前面或后面的输入列表中,则会产生很大的差异

z3py
中,使用多个变量对
Xor
约束建模的推荐方法是什么


没有推荐的模型,xor约束对于DPLL(-like)解算器来说很难,例如,请参阅各种(尝试的)加密问题应用程序。在本例中,我怀疑更改随机种子可能会对运行时产生类似的影响。