次の問題を考えてください
(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) )
この問題には 2 つの解決策があります。しかし、可能な解の数を自動的に決定する方法を教えてください。
その他の例:
(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) )
この解決策が唯一の解決策であるかどうかを知るにはどうすればよいですか?
オンラインの他の例はこちら
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
ここでこの例をオンラインで実行します
どうもありがとう。