我有以下约束(constr)我想简化:
4p + 3q< = - 10 + r AND 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 AND 4p + 3q< = - 12 + r
应该简化为:
4P + 3Q< = - 12 + R
因为它是最难实现的(暗示另一个)。
更新 尝试泰勒对约束的解决方案,它的工作原理。 当我尝试将相同的技术用于以下不同(某种程度) - 打印的约束时:
<(> [(假AND(0 <= 5 + 0epsilon + :: + + + 0p和假))OR(0 <= 5 + 0epsilon + >> 0 +和[(0 + 0epsilon + q + 0p <= 5 + 0epsilon + 0 +和0 +(0 + 0epsilon + q + 0p&lt; = 5 + 0epsilon + :: + 0p AND [false OR 0 + 0epsilon + >> + p <= 7 + 0epililon + >> + 0p))OR(false AND假)OR(假和假)])]和[(假AND(0 <= 5 + 0epsilon + :: + + + 0p和假))OR(0 <= 5 + 0epsilon + :: + + + 0 +和[(0 + 0epsilon +) q + 0p&lt; = 5 + 0epsilon + >> + 0p AND(0 + 0epsilon + q + 0p&lt; = 5 + 0epsilon + :: + + 0p AND [false OR 0 + 0epsilon + >> + p <= 7 + 0epsilon + >> + 0p ]))OR(假和假)或(假和假)])])AND epsilon&gt; = 0 AND q&gt; = 0 AND p&gt; = 0)
通过Z3_simplify,这将减少为
(q <= 5 AND p <= 7 AND epsilon&gt; = 0 AND q&gt; = 0 AND p&gt; = 0)
如果我使用ctx-solver创建一个策略 - 与目标一起简化并使用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那样的简单表示?
答案 0 :(得分:3)
对于这个例子,这可以使用最强的简化器ctx-solver-simplify
来完成,但请注意,通常这可以转换你的方程式。以下是您的示例(rise4fun链接: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
当我在你的新约束上尝试这个策略时,它也会返回你所说的简化答案(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)
)