使用Z3和smtlib计算具有混合值的配置/模型

时间:2011-12-05 15:38:37

标签: z3 smt

如何计算归因值? 这是一个例子:

(declare-fun x () bool)
(declare-fun y () bool)
(declare-fun z () bool)
(assert (AND x (OR y z)))

有了这个,我会得到2个模型:

x=true and y=true
x=true and z=true

现在,我想要的是这样的:

(declare-fun x () bool)
(declare-fun y () bool)
(declare-fun z () bool)
(declare-fun x.val () Int)
(declare-fun y.val () Int)
(declare-fun z.val () Int)
(assert (= x.val 2))
(assert (= y.val 3))
(assert (= z.val 5))
(assert (AND x (OR y z)))
(assert (> sum 6))

所以,我想得到属性总和大于6的模型:

x=true and z=true

也许使用数组是实现这个目标的一种方法......

2 个答案:

答案 0 :(得分:2)

我不确定我是否正确理解了你的问题。 您似乎想要将(整数)属性与每个布尔变量相关联。 因此,每个变量都是一对:布尔值和整数属性。 我假设sum,你的意思是指定为true的变量属性的总和。 如果是这种情况,您可以通过以下方式在Z3中对其进行建模:

;; Enable model construction
(set-option :produce-models true)

;; Declare a new type (sort) that is a pair (Bool, Int).
;; Given a variable x of type/sort WBool, we can write
;;  - (value x) for getting its Boolean value
;;  - (attr x)  for getting the integer "attribute" value
(declare-datatypes () ((WBool (mk-wbool (value Bool) (attr Int)))))

;; Now, we declare a macro int-value that returns (attr x) if
;; (value x) is true, and 0 otherwise
(define-fun int-value ((x WBool)) Int
  (ite (value x) (attr x) 0))

(declare-fun x () WBool)
(declare-fun y () WBool)
(declare-fun z () WBool)

;; Set the attribute values for x, y and z
(assert (= (attr x) 2))
(assert (= (attr y) 3))
(assert (= (attr z) 5))

;; Assert Boolean constraint on x, y and z.
(assert (and (value x) (or (value y) (value z))))

;; Assert that the sum of the attributes of the variables assigned to true is greater than 6.
(assert (> (+ (int-value x) (int-value y) (int-value z)) 6))
(check-sat)
(get-model)

(assert (not (value z)))
(check-sat)

答案 1 :(得分:1)

有三个变量,我想它会是这样的:

(define-fun cond_add ((cond Bool) (x Int) (sum Int)) Int
  (ite cond (+ sum x) sum))
(declare-fun sum () Int)
(assert (= sum (cond_add x x.val (cond_add y y.val (cond_add z z.val 0)))))
(assert (> sum 6))

这里我定义了一个宏cond_add,以便在相应条件成立时将变量添加到累加器。 sum的定义是根据x.valy.val和{{的真值来计算z.valxy的条件总和1}}。