2

我想在另一个模型中使用模型的输出(在我的例子中,只有 sat 和 unsat)。在这里,模型是对一组逻辑公式(在这种情况下为 Z3 表达式)中涉及的常数的令人满意的分配。我的目标可以简单解释如下。

我的问题可以详细描述如下:我有一个形式化的问题P,一组逻辑公式(表达式)对应多个约束(C)。在这些表达式中,一个(例如,Ai > 0)是我的目标。如果所有约束都可满足,则执行模型/形式化 P 返回 sat。请注意,Ai = 0 总是可能的。现在,我想找到一组特定变量的赋值对应于约束 (C),以确保 Ai > 0(对于任何 i)是不可能的。目前,我正在通过编写一个程序(在 C# 中)来解决这个问题,该程序开发了一个基于 DFS 的约束搜索算法(即约束值)并在“push/pop”的帮助下执行 P 以查看结果是否为假”。虽然我试图让搜索变得更好,但这对我没有多大帮助。对于较大的问题规模,这是非常低效的。如果我能创建另一个 SMT 程序(模型)利用 P 来搜索这样一个可满足的集合,那就太好了。

问题 P(原始问题的简短 SMT LIB 2 版本)的当前形式化如下:

(declare-fun th1 () Real)
(declare-fun th2 () Real)
(declare-fun th3 () Real)
(declare-fun th4 () Real)
(declare-fun th5 () Real)

(declare-fun l1 () Real)
(declare-fun l2 () Real)
(declare-fun l3 () Real)
(declare-fun l4 () Real)
(declare-fun l5 () Real)
(declare-fun l6 () Real)
(declare-fun l7 () Real)

(declare-fun p1 () Real)
(declare-fun p2 () Real)
(declare-fun p3 () Real)
(declare-fun p4 () Real)
(declare-fun p5 () Real)

(declare-fun sl1 () Int)
(declare-fun sl2 () Int)
(declare-fun sl3 () Int)
(declare-fun sl4 () Int)
(declare-fun sl5 () Int)
(declare-fun sl6 () Int)
(declare-fun sl7 () Int)

(declare-fun sp1 () Int)
(declare-fun sp2 () Int)
(declare-fun sp3 () Int)
(declare-fun sp4 () Int)
(declare-fun sp5 () Int)

(declare-fun a1 () Int)
(declare-fun a2 () Int)
(declare-fun a3 () Int)
(declare-fun a4 () Int)
(declare-fun a5 () Int)

(declare-fun na () Int)
(declare-fun ns () Int)
(declare-fun attack () Bool)

;;;; System
(assert (and      (= l1 (* (- th2 th1) 17.0))
        (= l2 (* (- th5 th1) 4.5))
        (= l3 (* (- th3 th2) 5.05))
        (= l4 (* (- th4 th2) 5.65))
        (= l5 (* (- th5 th2) 5.75))
        (= l6 (* (- th4 th3) 5.85))
        (= l7 (* (- th5 th4) 23.75))        

        (= p1 (+ l1 l2))
        (= p2 (+ l1 l3 l4 l5))
        (= p3 (+ l3 l6))
        (= p4 (+ l4 l6 l7))
        (= p5 (+ l2 l5 l7))
        )
)

;;;; Secured measurements
(assert (and    (or (= sl1 0) (= sl1 1))
        (or (= sl2 0) (= sl2 1))
        (or (= sl3 0) (= sl3 1))
        (or (= sl4 0) (= sl4 1))
        (or (= sl5 0) (= sl5 1))
        (or (= sl6 0) (= sl6 1))
        (or (= sl7 0) (= sl7 1))

        (or (= sp1 0) (= sp1 1))
        (or (= sp2 0) (= sp2 1))
        (or (= sp3 0) (= sp3 1))
        (or (= sp4 0) (= sp4 1))
        (or (= sp5 0) (= sp5 1))        
        )
)        

(assert (and    (=> (not (= l1 0.0)) (= sl1 0))
        (=> (not (= l2 0.0)) (= sl2 0))
        (=> (not (= l3 0.0)) (= sl3 0))
        (=> (not (= l4 0.0)) (= sl4 0))
        (=> (not (= l5 0.0)) (= sl5 0))
        (=> (not (= l6 0.0)) (= sl6 0))
        (=> (not (= l7 0.0)) (= sl7 0))     

        (=> (not (= p1 0.0)) (= sp1 0))
        (=> (not (= p2 0.0)) (= sp2 0))
        (=> (not (= p3 0.0)) (= sp3 0))
        (=> (not (= p4 0.0)) (= sp4 0))
        (=> (not (= p5 0.0)) (= sp5 0))          
    )
)

(assert (and (= sl1 1) (= sl2 1)))

;;;; Attacks
(assert (and    (or (= a1 0) (= a1 1))
        (or (= a2 0) (= a2 1))
        (or (= a3 0) (= a3 1))
        (or (= a4 0) (= a4 1))
        (or (= a5 0) (= a5 1))      
    )
)

(assert (and
        (= (not (= th1 0.0)) (= a1 1))
        (= (not (= th2 0.0)) (= a2 1))
        (= (not (= th3 0.0)) (= a3 1))
        (= (not (= th4 0.0)) (= a4 1))
        (= (not (= th5 0.0)) (= a5 1))      
    )
)

(assert (= th1 0.0)) // Base condition
(assert (= na (+ a1 a2 a3 a4 a5)))
(assert (=> attack (> na 1)))


;;;; Check for satisfiable model

(assert attack)

(check-sat)
(get-model)
(exit)

我想综合安全测量(即,找到“sl”和“sp”项的分配),以便在给定约束的情况下不会受到攻击(即,na 将为 0),例如,如下所示:

(assert (= ns (+ sl1 sl2 sl3 sl4 sl5 sl6 sl7 sp1 sp2 sp3 sp4 sp5)))
(assert (<= ns 4))

在这种情况下,断言(即 '(assert (and (= sl1 1) (= sl2 1)))' 将被注释。目前,我开发了一个 C# 程序,它接受 'sl' 和 'sp' 的赋值,像 (assert (and (= sl1 1) (= sl2 1) ...))' 一样断言它们,然后执行给定的程序看看是否有任何攻击可能。当程序返回 unsat 时,我就完成了(即 na > 1 是不可能的)。有什么方法可以仅使用 SMT (Z3) 解决问题吗?

4

2 回答 2

3

感谢您澄清问题。如果我明白了,您可以使用 Z3 执行搜索slispj值,但您不能仅使用 SMT-LIB 执行此操作,您需要使用 API。这个想法是使用来自一次SAT检查的模型(满足分配)作为未来检查的约束,如这些答案中详细解释的那样:

Z3:找到所有满意的模型

Z3:检查模型是否唯一

(Z3Py) 检查方程的所有解

这是您在 Python API 中编码的示例(z3py 链接:http ://rise4fun.com/Z3Py/KHzm ):

s = Solver()

th1, th2, th3, th4, th5 = Reals('th1 th2 th3 th4 th5')
th = { 'th1' : th1, 'th2' : th2, 'th3' : th3, 'th4' : th4, 'th5' : th5}
l1, l2, l3, l4, l5, l6, l7 = Reals('l1 l2 l3 l4 l5 l6 l7')
l = { 'l1' : l1, 'l2' : l2, 'l3' : l3, 'l4' : l4, 'l5' : l5, 'l6' : l6, 'l7' : l7 }
p1, p2, p3, p4, p5 = Reals('p1 p2 p3 p4 p5')
p = { 'p1' : p1, 'p2' : p2, 'p3' : p3, 'p4' : p4, 'p5' : p5 }
sl1, sl2, sl3, sl4, sl5, sl6, sl7 = Ints('sl1 sl2 sl3 sl4 sl5 sl6 sl7')
sl = { 'sl1' : sl1, 'sl2' : sl2, 'sl3' : sl3, 'sl4' : sl4, 'sl5' : sl5, 'sl6' : sl6, 'sl7' : sl7 }
sp1, sp2, sp3, sp4, sp5 = Ints('sp1 sp2 sp3 sp4 sp5')
sp = { 'sp1' : sp1, 'sp2' : sp2, 'sp3' : sp3, 'sp4' : sp4, 'sp5' : sp5 }
a1, a2, a3, a4, a5 = Ints('a1 a2 a3 a4 a5')
a = { 'a1' : a1, 'a2' : a2, 'a3' : a3, 'a4' : a4, 'a5' : a5 }
na, ns = Ints('na ns')
attack = Bool('attack')
n = { 'na' : na, 'ns' : ns, 'attack' : attack}
dict_decl = dict(th.items() + l.items() + p.items() + sl.items() + sp.items() + a.items() + n.items() )

assertions = []
assertions.append(parse_smt2_string('(assert (and      (= l1 (* (- th2 th1) 17.0)) (= l2 (* (- th5 th1) 4.5)) (= l3 (* (- th3 th2) 5.05)) (= l4 (* (- th4 th2) 5.65)) (= l5 (* (- th5 th2) 5.75)) (= l6 (* (- th4 th3) 5.85)) (= l7 (* (- th5 th4) 23.75)) (= p1 (+ l1 l2)) (= p2 (+ l1 l3 l4 l5)) (= p3 (+ l3 l6)) (= p4 (+ l4 l6 l7)) (= p5 (+ l2 l5 l7))))', decls=dict_decl))
assertions.append(parse_smt2_string('(assert (and    (or (= sl1 0) (= sl1 1)) (or (= sl2 0) (= sl2 1)) (or (= sl3 0) (= sl3 1)) (or (= sl4 0) (= sl4 1)) (or (= sl5 0) (= sl5 1)) (or (= sl6 0) (= sl6 1)) (or (= sl7 0) (= sl7 1)) (or (= sp1 0) (= sp1 1)) (or (= sp2 0) (= sp2 1)) (or (= sp3 0) (= sp3 1)) (or (= sp4 0) (= sp4 1)) (or (= sp5 0) (= sp5 1))        ))', decls=dict_decl))
assertions.append(parse_smt2_string('(assert (and    (=> (not (= l1 0.0)) (= sl1 0)) (=> (not (= l2 0.0)) (= sl2 0)) (=> (not (= l3 0.0)) (= sl3 0)) (=> (not (= l4 0.0)) (= sl4 0)) (=> (not (= l5 0.0)) (= sl5 0)) (=> (not (= l6 0.0)) (= sl6 0)) (=> (not (= l7 0.0)) (= sl7 0))      (=> (not (= p1 0.0)) (= sp1 0)) (=> (not (= p2 0.0)) (= sp2 0)) (=> (not (= p3 0.0)) (= sp3 0)) (=> (not (= p4 0.0)) (= sp4 0)) (=> (not (= p5 0.0)) (= sp5 0))           ))', decls=dict_decl))
assertions.append(parse_smt2_string('(assert (and (= sl1 1) (= sl2 1)))', decls=dict_decl))
assertions.append(parse_smt2_string('(assert (and    (or (= a1 0) (= a1 1))(or (= a2 0) (= a2 1))(or (= a3 0) (= a3 1))(or (= a4 0) (= a4 1))(or (= a5 0) (= a5 1))      ))', decls=dict_decl))
assertions.append(parse_smt2_string('(assert (and (= (not (= th1 0.0)) (= a1 1))(= (not (= th2 0.0)) (= a2 1))(= (not (= th3 0.0)) (= a3 1))(= (not (= th4 0.0)) (= a4 1))(= (not (= th5 0.0)) (= a5 1))      ))', decls=dict_decl))
assertions.append(parse_smt2_string('(assert (= ns (+ sl1 sl2 sl3 sl4 sl5 sl6 sl7 sp1 sp2 sp3 sp4 sp5)))', decls=dict_decl))
assertions.append(parse_smt2_string('(assert (<= ns 4))', decls=dict_decl))
#assertions.append(parse_smt2_string('(assert (and (= sl1 1) (= sl2 1)))', decls=dict_decl)) # commented as suggested
assertions.append(parse_smt2_string('(assert (= th1 0.0))', decls=dict_decl))
assertions.append(parse_smt2_string('(assert (= na (+ a1 a2 a3 a4 a5)))', decls=dict_decl))
assertions.append(parse_smt2_string('(assert (=> attack (> na 1)))', decls=dict_decl))
assertions.append(parse_smt2_string('(assert attack)', decls=dict_decl))

print assertions

s.add(assertions)

synthesized = []

iters = 0
while s.check() == sat:
  print "Iteration " + str(iters)
  print s.model()
  avoid = []
  # key step: add constraint to prevent any values assigned (if possible) to constants from being equal to their satisfying assignments (models) in this sat iteration
  for sli in sl.values():
    avoid.append(sli != s.model()[sli])
  for spi in sp.values():
    avoid.append(spi != s.model()[spi])
  s.add(Or(avoid))
  # end key step
  synthesized.append(avoid)
  print avoid
  iters = iters + 1
  # unless you know how to guarantee termination (e.g., there is a constraint ensuring the slis and spis take values in finite sets)
  if iters >= 1000:
    break

print "Done"
print synthesized # all the constraints

为所有常量和数字道歉,我只是使用了您的 SMT-LIB 脚本的最快翻译,但它最终变得相当麻烦,我会在任何地方使用迭代器。sli这对和spj常量产生了以下约束:

[[sl4 ≠ 0, sl5 ≠ 0, sl6 ≠ 0, sl7 ≠ 0, sl1 ≠ 1, sl2 ≠ 1, sl3 ≠ 0, sp1 ≠ 0, sp2 ≠ 0, sp3 ≠ 0, sp4 ≠ 1, sp5 ≠ 0], [sl4 ≠ 0, sl5 ≠ 0, sl6 ≠ 0, sl7 ≠ 0, sl1 ≠ 1, sl2 ≠ 1, sl3 ≠ 0, sp1 ≠ 0, sp2 ≠ 0, sp3 ≠ 0, sp4 ≠ 0, sp5 ≠ 0], [sl4 ≠ 0, sl5 ≠ 1, sl6 ≠ 0, sl7 ≠ 0, sl1 ≠ 1, sl2 ≠ 1, sl3 ≠ 0, sp1 ≠ 0, sp2 ≠ 0, sp3 ≠ 0, sp4 ≠ 0, sp5 ≠ 0], [sl4 ≠ 0, sl5 ≠ 1, sl6 ≠ 0, sl7 ≠ 0, sl1 ≠ 1, sl2 ≠ 1, sl3 ≠ 0, sp1 ≠ 0, sp2 ≠ 0, sp3 ≠ 1, sp4 ≠ 0, sp5 ≠ 0], [sl4 ≠ 0, sl5 ≠ 1, sl6 ≠ 0, sl7 ≠ 0, sl1 ≠ 1, sl2 ≠ 1, sl3 ≠ 0, sp1 ≠ 0, sp2 ≠ 1, sp3 ≠ 0, sp4 ≠ 0, sp5 ≠ 0], [sl4 ≠ 0, sl5 ≠ 1, sl6 ≠ 0, sl7 ≠ 0, sl1 ≠ 1, sl2 ≠ 1, sl3 ≠ 0, sp1 ≠ 1, sp2 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, sl2 ≠ 1, sl3 ≠ 0, sp1 ≠ 0, sp2 ≠ 0, sp3 ≠ 0, sp4 ≠ 1, sp5 ≠ 0], [sl4 ≠ 0, sl5 ≠ 0, sl6 ≠ 0, sl7 ≠ 0, sl1 ≠ 1, sl2 ≠ 1, sl3 ≠ 0, sp1 ≠ 0, sp2 ≠ 1, sp3 ≠ 0, sp4 ≠ 0, sp5 ≠ 0], [sl4 ≠ 0, sl5 ≠ 0, sl6 ≠ 0, sl7 ≠ 0, sl1 ≠ 1, sl2 ≠ 1, sl3 ≠ 0, sp1 ≠ 1, sp2 ≠ 1, sp3 ≠ 0, sp4 ≠ 0, sp5 ≠ 0]]
于 2013-05-02T02:46:32.230 回答
2

如果我理解正确,那么您正在寻找(通用)量化。请原谅我的伪符号,但您不是在寻找对以下自由变量 ( config_params) 的令人满意的分配吗?

config_constraints(config_params) -> forall attack_params: not attack_constraints(attack_params, config_params)

其中()符号仅指示约束依赖于哪个变量(组)。我很确定 .Net API 支持量词,就像它们在 Java API 中一样。

于 2013-05-01T07:24:30.777 回答