【问题标题】:Use Z3 and smtlib to calculate configuration/model with mixed values使用 Z3 和 smtlib 计算混合值的配置/模型
【发布时间】:2012-01-13 08:10:24
【问题描述】:

如何计算属性值? 这是一个例子:

(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

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

【问题讨论】:

    标签: z3 smt


    【解决方案1】:

    我不确定我是否正确理解了您的问题。 您似乎想将(整数)属性与每个布尔变量相关联。 因此,每个变量都是一对:一个布尔值和一个整数属性。 我假设 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)
    

    【讨论】:

    • 哇,真快。正是我想要的。谢谢!
    【解决方案2】:

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

    (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 被定义为基于xyz 的真值计算x.valy.valz.val 的条件和。

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 2014-10-25
      • 1970-01-01
      • 2020-12-03
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多