[英]K-out-of-N constraint in Z3Py
是的,Z3Py內置了對此的支持。 有一個未記錄的API,這在Z3Py文檔中沒有提到:使用PbEq
。 特別是表達
PbEq(((x1,1),(x2,1),..,(xN,1)),K)
如果N個布爾變量中的K個被設置為true,則為真。 有一些報道稱這種編碼比手動表達約束的天真方式更快。
要表示1-out-of-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)]
#Exactly 3 of the variables should be true
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)]
#<=3 of the variables should be true
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)]
#>=3 of the variables should be true
s.add( z3.PbGe([(x,1) for x in bvars], 3) )
s.check()
m = s.model()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.