简体   繁体   English

实数值的模运算 (Z3Py)

[英]Modulo operation on Real values (Z3Py)

I want to implement the modulo operation using Z3Py.我想使用 Z3Py 实现模运算。 I've found this discussion on the Z3 github page where one of the creators has the following solution.我在 Z3 github 页面上找到了这个讨论,其中一位创建者有以下解决方案。 However, I'm not sure I fully understand it.但是,我不确定我是否完全理解它。

from z3 import *

mod = z3.Function('mod', z3.RealSort(), z3.RealSort(), z3.RealSort())
quot = z3.Function('quot', z3.RealSort(), z3.RealSort(), z3.IntSort())
s = z3.Solver()


def mk_mod_axioms(X, k):
    s.add(Implies(k != 0, 0 <= mod(X, k)),
          Implies(k > 0, mod(X, k) < k),
          Implies(k < 0, mod(X, k) < -k),
          Implies(k != 0, k * quot(X, k) + mod(X, k) == X))


x, y = z3.Reals('x y')

mk_mod_axioms(x, 3)
mk_mod_axioms(y, 5)

print(s)

If you set no additional constraints the model evaluates to 0, the first solution.如果您未设置其他约束,则 model 的计算结果为 0,即第一个解决方案。 If you set additional constraints that x and y should be less than 0, it produces correct solutions.如果您设置 x 和 y 应小于 0 的附加约束,则会产生正确的解决方案。 However, if you set the constraint that x and y should be above 0 it produces incorrect results.但是,如果您设置 x 和 y 应大于 0 的约束,则会产生不正确的结果。

s.add(x > 0)
s.add(y > 0)

The model evaluates to 1/2 for x and 7/2 for y. model 的 x 值为 1/2,y 值为 7/2。

Here's the model z3 prints:这是 model z3 打印件:

sat
[y = 7/2,
 x = 1/2,
 mod = [(7/2, 5) -> 7/2, else -> 1/2],
 quot = [else -> 0]]

So, what it's telling you is that it "picked" mod and quot to be functions that are:所以,它告诉你的是它“挑选”了modquot是以下功能:

def mod (x, y):
   if x == 3.5 and && y == 5:
      return 3.5
   else:
      return 0.5

def quot (x, y):
   return 0

Now go over the axioms you put in: You'll see that the model does satisfy them just fine;现在 go 在您输入的公理之上:您会看到 model 确实很好地满足了它们; so there's nothing really wrong with this.所以这并没有什么问题。

What the answer you linked to is saying is about what sort of properties you can state to get a "reasonable" model.您链接到的答案是关于您可以通过 state 获得“合理”的 model 的属性。 Not that it's the unique such model.并不是说它是独一无二的 model。 In particular, you want quot to be the maximum such value, but there's nothing in the axioms that require that.特别是,您希望quot成为此类值的最大值,但公理中没有任何要求。

Long story short, the answer you're getting is correct;长话短说,你得到的答案是正确的; but it's perhaps not useful.但它可能没有用。 Axiomatizing will take more work, in particular you'll need quantification and SMT solvers don't deal with such specifications that well.公理化需要更多的工作,特别是你需要量化,而 SMT 求解器不能很好地处理这些规范。 But it all depends on what you're trying to achieve: For specific problems you can get away with a simpler model.但这完全取决于您要实现的目标:对于特定问题,您可以使用更简单的 model 来解决问题。 Without knowing your actual application, the only thing we can say is that this axiomatization is too weak for your use case.在不了解您的实际应用程序的情况下,我们只能说这种公理化对于您的用例来说太弱了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM