我认为这里有两种不同的相关情况:
案例1:
我有一组布尔变量,我想要另一个布尔变量,如果这些变量中的任何一个为真,那么它就是真的。我目前正在通过使布尔变量为整数来实现这一点,然后使用以下形式的表达式设置:
(ite (boolean_expr) 1 0)
然后,我只使用一个和和一个大于来设置整体布尔值
(> (+ b1 b2 b3...) 0)
情况2(这可能不是真正的伪布尔值):
我有两组布尔变量:
set1 = n_1,n_2....
set2 = m_1,m_2....
我想添加一个约束,说明在set1中设置为true的变量数等于在set2中设置为true的变量数。
如上所述,我目前正在通过使用整数而不是布尔值来实现这一点,并使用以下形式的ite来设置每一个:
n_1 = (ite (boolean_expr) 1 0)
然后说:
n_1+n_2+.... = m_1+m_2......
在每种情况下,使用整数变量是最有效的方法吗,还是有更好的方法?
谢谢。
z3在某个时候添加了本地伪布尔约束
def AtMost(args)
def AtLeast(args)
def PbLe(args, k)
def PbGe(args, k)
def PbEq(args, k, ctx=None)
(Python文档)
SMTLib2:
(declare-fun a () Bool)
(declare-fun b () Bool)
(declare-fun c () Bool)
(declare-fun d () Bool)
; 9 = 1a + 2b + 4c + 8d
(assert ((_ pbeq 9 1 2 4 8) a b c d))
(check-sat)
(get-model) ; 0 1 0 1
(exit)
当前可以使用整数对PB约束进行编码。您必须将变量绑定到间隔0、1中。例如:
(set-logic QF_LIA)
(declare-const n1 Int)
(declare-const n2 Int)
(assert (<= 0 n1))
(assert (<= n1 1))
(assert (<= 0 n2))
(assert (<= n2 1))
(assert (>= (+ n1 n2) 1))
(check-sat)
如果将逻辑设置为QF_LIA,那么Z3将自动尝试对这些约束进行重新编码使用比特矢量。在详细的输出中,你会看到Z3调用了一个策略pb2bv,它为你的进行重写
z3 ty.smt2 /v:10
(simplifier :num-exprs 10 :num-asts 171 :time 0.00 :before-memory 0.77 :after-memory 0.77)
(propagate-values :num-exprs 10 :num-asts 171 :time 0.00 :before-memory 0.77 :after-memory 0.77)
(ctx-simplify :num-steps 17)
(ctx-simplify :num-exprs 10 :num-asts 171 :time 0.00 :before-memory 0.77 :after-memory 0.77)
(simplifier :num-exprs 10 :num-asts 171 :time 0.00 :before-memory 0.77 :after-memory 0.77)
(solve_eqs :num-exprs 10 :num-asts 171 :time 0.00 :before-memory 0.77 :after-memory 0.77)
(elim-uncnstr-vars :num-exprs 10 :num-asts 171 :time 0.00 :before-memory 0.77 :after-memory 0.77)
(simplifier :num-exprs 10 :num-asts 173 :time 0.00 :before-memory 0.77 :after-memory 0.77)
(pb2bv :num-exprs 4 :num-asts 180 :time 0.00 :before-memory 0.77 :after-memory 0.77)
(simplifier :num-exprs 3 :num-asts 178 :time 0.00 :before-memory 0.77 :after-memory 0.77)
(propagate-values :num-exprs 3 :num-asts 178 :time 0.00 :before-memory 0.77 :after-memory 0.77)
(solve_eqs :num-exprs 3 :num-asts 178 :time 0.00 :before-memory 0.77 :after-memory 0.77)
(max-bv-sharing :num-exprs 3 :num-asts 178 :time 0.00 :before-memory 0.77 :after-memory 0.77)
(bit-blaster :num-exprs 3 :num-asts 178 :time 0.00 :before-memory 0.77 :after-memory 0.77)
(aig :num-exprs 3 :num-asts 178 :time 0.00 :before-memory 0.77 :after-memory 0.77)
(ast-table :capacity 640 :size 178)
(sat-status
:inconsistent false
:vars 2
:elim-vars 0
:lits 2
:assigned 0
:binary-clauses 1
:ternary-clauses 0
:clauses 0
:del-clause 0
:avg-clause-size 2.00
:memory 0.77)