Z3 无符号变量 - 简化

Z3 unsigned variables - simplify

我有以下约束 (constr) 我想简化:

4p+3q<=-10+r 和 4p+3q<=-12+r

p(和 r 类似)创建如下:

Z3_ast p;
Z3_sort ty = Z3_mk_int_sort(ctx)
Z3_symbol s = Z3_mk_string_symbol(ctx, "p");
p = Z3_mk_const(ctx, s, ty)

如果我这样做

Z3_simplify(ctx, constr)

没有任何变化,因为 p 和 r 是整数。

我如何编码 p 和 r 是自然数(无符号)的知识?

简单地添加约束 p >= 0 AND r >= 0 对简化我的约束没有帮助(但在寻求解决方案时当然有帮助)。

澄清一下,

4p+3q<=-10+r 和 4p+3q<=-12+r

应减少为:

4p+3q<=-12+r

因为它是最难实现的(暗示另一个)。

更新: 尝试了 Taylor 对约束的解决方案,它有效。 当我尝试对以下不同的(有点)漂亮的约束使用相同的技术时:

(([(false AND (0<=5+0epsilon+0q+0p AND false)) OR (0<=5+0epsilon+0q+0p AND [(0+0epsilon+q+0p<=5+0epsilon+0q+0p AND (0+0epsilon+q+0p<=5+0epsilon+0q+0p AND [false OR 0+0epsilon+0q+p<=7+0epsilon+0q+0p])) OR (false AND false) OR (false AND false)])] AND [(false AND (0<=5+0epsilon+0q+0p AND false)) OR (0<=5+0epsilon+0q+0p AND [(0+0epsilon+q+0p<=5+0epsilon+0q+0p AND (0+0epsilon+q+0p<=5+0epsilon+0q+0p AND [false OR 0+0epsilon+0q+p<=7+0epsilon+0q+0p])) OR (false AND false) OR (false AND false)])]) AND epsilon>=0 AND q>=0 AND p>=0)

通过 Z3_simplify 这减少到

(q<=5 AND p<=7 AND epsilon>=0 AND q>=0 AND p>=0)

如果我使用 ctx-solver-simplify 和目标一起创建策略并使用 Z3_apply_result_to_string,我得到以下结果:

(goals
(goal
  (let ((a!1 (+ 5 (* 0 epsilon) (* 0 q) (* 0 p)))
        (a!3 (or false
                 (<= (+ 0 (* 0 epsilon) (* 0 q) p)
                     (+ 7 (* 0 epsilon) (* 0 q) (* 0 p))))))
  (let ((a!2 (<= (+ 0 (* 0 epsilon) q (* 0 p)) a!1)))
    (or (and false (<= 0 a!1) false)
        (and (<= 0 a!1)
             (or (and a!2 a!2 a!3) (and false false) (and false false))))))
  (>= epsilon 0)
  (>= q 0)
  (>= p 0))
)

我怎样才能得到像 Z3_simplify 那样的简单表示?

对于这个例子,这可以使用最强的简化器 ctx-solver-simplify 来完成,但请注意,通常这可能会改变您的方程式。这是您的示例 (rise4fun link: http://rise4fun.com/Z3/tw0t ):

(declare-fun p () Int)
(declare-fun q () Int)
(declare-fun r () Int)

(assert (and (<= (+ (* 4 p) (* 3 q)) (- r 10)) (<= (+ (* 4 p) (* 3 q)) (- r 12))))

(apply ctx-solver-simplify)

(apply (then simplify ctx-simplify ctx-solver-simplify))

; (help-tactic)

输出:

(goals
(goal
  (<= (+ (* 4 p) (* 3 q)) (+ r (* (- 1) 12)))
  :precision precise :depth 1)
)
(goals
(goal
  (<= (+ (* 4 p) (* 3 q)) (+ (- 12) r))
  :precision precise :depth 3)
)

你也可以通过 C API 使用战术。

您也可以添加自然数约束:

(assert (and (<= (+ (* 4 p) (* 3 q)) (- r 10)) (<= (+ (* 4 p) (* 3 q)) (- r 12))))
(assert (>= p 0))
(assert (>= r 0))
(apply ctx-solver-simplify)
(apply (then simplify ctx-simplify ctx-solver-simplify))

输出:

(goals
(goal
  (<= (+ (* 4 p) (* 3 q)) (+ r (* (- 1) 12)))
  (>= p 0)
  (>= r 0)
  :precision precise :depth 1)
)
(goals
(goal
  (<= (+ (* 4 p) (* 3 q)) (+ (- 12) r))
  (>= p 0)
  (>= r 0)
  :precision precise :depth 3)
)

更新:

如果你需要简化的公式,你可能只需要在应用简化策略后迭代适当的子目标来得到公式,参见,例如Z3_apply_result_get_subgoal:

http://z3prover.github.io/api/html/group__capi.html#ga63813eb4cc7865f0cf714e1eff0e0c64

当我在你的新约束上尝试这个策略时,它也returns你陈述的简化答案(rise4fun link:http://rise4fun.com/Z3/T1TZ):

(declare-fun epsilon () Int)
(declare-fun q () Int)
(declare-fun p () Int)

(assert (and  (let ((a!1 (+ 5 (* 0 epsilon) (* 0 q) (* 0 p)))
        (a!3 (or false
                 (<= (+ 0 (* 0 epsilon) (* 0 q) p)
                     (+ 7 (* 0 epsilon) (* 0 q) (* 0 p))))))
  (let ((a!2 (<= (+ 0 (* 0 epsilon) q (* 0 p)) a!1)))
    (or (and false (<= 0 a!1) false)
        (and (<= 0 a!1)
             (or (and a!2 a!2 a!3) (and false false) (and false false))))))
  (>= epsilon 0)
  (>= q 0)
  (>= p 0)))

(apply (then simplify ctx-simplify ctx-solver-simplify))

产生

(goals
(goal
  (<= q 5)
  (>= epsilon 0)
  (>= q 0)
  (>= p 0)
  (<= p 7)
  :precision precise :depth 3)
)