Yes, Z3Py has built-in support for this. There is an undocumented API for this that is not mentioned in the Z3Py documentation: use PbEq
. In particular, the expression
PbEq(((x1,1),(x2,1),..,(xN,1)),K)
, K N true. , , .
1 N, K = 1 PbEq
. K-out-of-N, PbLe
. K-out-of-N, PbGe
.
Python :
import z3
s = z3.Solver()
bvars = [z3.Bool("Var {0}".format(x)) for x in range(10)]
s.add( z3.PbEq([(x,1) for x in bvars], 3) )
s.check()
m = s.model()
s = z3.Solver()
bvars = [z3.Bool("Var {0}".format(x)) for x in range(10)]
s.add( z3.PbLe([(x,1) for x in bvars], 3) )
s.check()
m = s.model()
s = z3.Solver()
bvars = [z3.Bool("Var {0}".format(x)) for x in range(10)]
s.add( z3.PbGe([(x,1) for x in bvars], 3) )
s.check()
m = s.model()