我正在研究一种研究工具的组成部分; 我有兴趣检索(对于QF_LRA)
- 多个(最小或其他)UNSAT核心和
- 多个SAT分配
我已经检查了论坛,以便早些时候讨论这个话题,例如, How to get different unsat cores when using z3 on logic QF_LRA
他们参考z3 Python教程 例如,http://rise4fun.com/Z3Py/tutorial/musmss
现在似乎处于离线状态。我已经尝试了github等的其他建议来找到所提到的教程,但没有运气。
我正在使用z3 Java API;但很高兴转向替代品。
答案 0 :(得分:4)
这是教程。您可以在MARCO找到更多信息 来自Mark Liffiton的网页。
本教程说明了如何使用Z3提取所有最小的不可满足核心 连同所有最大满足子集。
我们接下来描述的算法 代表了Liffiton和Malik独立的核心提取程序的本质 由Previti和Marques-Silva撰写:
枚举不可行:快速查找多个MUS Mark H. Liffiton和Ammar Malik 在 Proc。 10 th 人工智能一体化国际会议 约束规划中的智能(AI)和运筹学(OR)技术(CPAIOR-2013),160-175,2013年5月。
部分MUS枚举
Alessandro Previti,Joao Marques-Silva
在 Proc。 AAAI-2013 2013年7月
此实现不包含调整。 它由Mark Liffiton提供,它是其中一个版本的简化版本 他的马可波罗网站。 也可以使用eMUS代码。 该示例说明了Z3基于Python的API的以下功能:
该算法的主要思想是维持两个 逻辑上下文和它们之间的交换信息:
from Z3 import *
def main():
x, y = Reals('x y')
constraints = [x > 2, x < 1, x < 0, Or(x + y > 0, y < 0), Or(y >= 0, x >= 0), Or(y < 0, x < 0), Or(y > 0, x < 0)]
csolver = SubsetSolver(constraints)
msolver = MapSolver(n=csolver.n)
for orig, lits in enumerate_sets(csolver, msolver):
output = "%s %s" % (orig, lits)
print(output)
def get_id(x):
return Z3_get_ast_id(x.ctx.ref(),x.as_ast())
def MkOr(clause):
if clause == []:
return False
else:
return Or(clause)
SubsetSolver:
class SubsetSolver:
constraints = []
n = 0
s = Solver()
varcache = {}
idcache = {}
def __init__(self, constraints):
self.constraints = constraints
self.n = len(constraints)
for i in range(self.n):
self.s.add(Implies(self.c_var(i), constraints[i]))
def c_var(self, i):
if i not in self.varcache:
v = Bool(str(self.constraints[abs(i)]))
self.idcache[get_id(v)] = abs(i)
if i >= 0:
self.varcache[i] = v
else:
self.varcache[i] = Not(v)
return self.varcache[i]
def check_subset(self, seed):
assumptions = self.to_c_lits(seed)
return (self.s.check(assumptions) == sat)
def to_c_lits(self, seed):
return [self.c_var(i) for i in seed]
def complement(self, aset):
return set(range(self.n)).difference(aset)
def seed_from_core(self):
core = self.s.unsat_core()
return [self.idcache[get_id(x)] for x in core]
def shrink(self, seed):
current = set(seed)
for i in seed:
if i not in current:
continue
current.remove(i)
if not self.check_subset(current):
current = set(self.seed_from_core())
else:
current.add(i)
return current
def grow(self, seed):
current = seed
for i in self.complement(current):
current.append(i)
if not self.check_subset(current):
current.pop()
return current
MapSolver:
class MapSolver:
def __init__(self, n):
"""Initialization.
Args:
n: The number of constraints to map.
"""
self.solver = Solver()
self.n = n
self.all_n = set(range(n)) # used in complement fairly frequently
def next_seed(self):
"""Get the seed from the current model, if there is one.
Returns:
A seed as an array of 0-based constraint indexes.
"""
if self.solver.check() == unsat:
return None
seed = self.all_n.copy() # default to all True for "high bias"
model = self.solver.model()
for x in model:
if is_false(model[x]):
seed.remove(int(x.name()))
return list(seed)
def complement(self, aset):
"""Return the complement of a given set w.r.t. the set of mapped constraints."""
return self.all_n.difference(aset)
def block_down(self, frompoint):
"""Block down from a given set."""
comp = self.complement(frompoint)
self.solver.add( MkOr( [Bool(str(i)) for i in comp] ) )
def block_up(self, frompoint):
"""Block up from a given set."""
self.solver.add( MkOr( [Not(Bool(str(i))) for i in frompoint] ) )
def enumerate_sets(csolver, map):
"""Basic MUS/MCS enumeration, as a simple example."""
while True:
seed = map.next_seed()
if seed is None:
return
if csolver.check_subset(seed):
MSS = csolver.grow(seed)
yield ("MSS", csolver.to_c_lits(MSS))
map.block_down(MSS)
else:
MUS = csolver.shrink(seed)
yield ("MUS", csolver.to_c_lits(MUS))
map.block_up(MUS)
main()