请考虑以下问题
(declare-fun x1 () Bool)
(declare-fun x2 () Bool)
(declare-fun x3 () Bool)
(declare-fun x4 () Bool)
(define-fun conjecture () Bool
(and (= (and x2 x3) x1) (= (and x1 (not x4)) x2) (= x2 x3) (= (not x3) x4)))
(assert conjecture)
(check-sat)
(get-model)
(assert (= x4 false))
(check-sat)
(get-model)
相应的输出是
sat
(model
(define-fun x2 () Bool false)
(define-fun x4 () Bool true)
(define-fun x1 () Bool false)
(define-fun x3 () Bool false) )
sat
(model
(define-fun x3 () Bool true)
(define-fun x2 () Bool true)
(define-fun x1 () Bool true)
(define-fun x4 () Bool false) )
这个问题有两个解决方案。但是,请让我知道如何自动确定可能解决方案的数量。
其他示例:
(declare-fun x1 () Bool)
(declare-fun x2 () Bool)
(declare-fun x3 () Bool)
(declare-fun x4 () Bool)
(declare-fun x5 () Bool)
(define-fun conjecture () Bool
(and (= (and x2 x3 (not x4)) x1) (= (and x1 (not x4) (not x5)) x2)
(= (and x2 (not x5)) x3) (= (and (not x3) x1) x4) (= (and x2 x3 (not x4)) x5)))
(assert conjecture)
(check-sat)
(get-model)
输出是:
sat
(model
(define-fun x3 () Bool false)
(define-fun x1 () Bool false)
(define-fun x4 () Bool false)
(define-fun x5 () Bool false)
(define-fun x2 () Bool false) )
我怎么知道这个解决方案是否是唯一的解决方案?
其他在线示例here
我正在尝试应用Z3-number of solutions提出的方法。这种方法在以下示例中表现良好:
代码:
def add_constraints(s, model):
x = Bool('x')
s.add(Or(x, False) == True)
notAgain = []
i = 0
for val in model:
notAgain.append(x != model[val])
i = i + 1
if len(notAgain) > 0:
s.add(Or(notAgain))
print Or(notAgain)
return s
s = Solver()
i = 0
add_constraints(s, [])
while s.check() == sat:
print s.model()
i = i + 1
add_constraints(s, s.model())
print i # solutions
输出 :
[x = True]
x ≠ True
1
在此处在线运行此示例。
但是在下面的例子中该方法在线失败
def add_constraints(s, model):
x = Bool('x')
s.add(Or(x, True) == True)
notAgain = []
i = 0
for val in model:
notAgain.append(x != model[val])
i = i + 1
if len(notAgain) > 0:
s.add(Or(notAgain))
print Or(notAgain)
return s
s = Solver()
i = 0
add_constraints(s, [])
while s.check() == sat:
print s.model()
i = i + 1
add_constraints(s, s.model())
print i # solutions
输出:
F:\Applications\Rise4fun\Tools\Z3Py timed out
在此处在线运行此示例
对于最后一个示例,正确答案是 2。请您告诉我为什么这种方法在这种情况下会失败?
修改后的 Taylor 代码的其他示例:
def add_constraints(s, model):
X = BoolVector('x', 4)
s.add(Or(X[0], False) == X[1])
s.add(Or(X[1], False) == X[0])
s.add(Or(Not(X[2]), False) == Or(X[0],X[1]))
s.add(Or(Not(X[3]), False) == Or(Not(X[0]),Not(X[1]),Not(X[2])))
notAgain = []
i = 0
for val in model:
notAgain.append(X[i] != model[X[i]])
i = i + 1
if len(notAgain) > 0:
s.add(Or(notAgain))
print Or(notAgain)
return s
s = Solver()
i = 0
add_constraints(s, [])
while s.check() == sat:
print s.model()
i = i + 1
add_constraints(s, s.model())
print i # solutions
输出:
[x1 = False, x0 = False, x3 = False, x2 = True]
x0 ≠ False ∨ x1 ≠ False ∨ x2 ≠ True ∨ x3 ≠ False
[x1 = True, x0 = True, x3 = False, x2 = False]
x0 ≠ True ∨ x1 ≠ True ∨ x2 ≠ False ∨ x3 ≠ False
2
在此处在线运行此示例
非常感谢。