- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我遇到了一个之前提到的问题,我没有完全得到解决方案(将替代与简化相结合)。在我的编码中,我有严格的不等式,我需要将 epsilon 设置为 0 或一个非常小的值。例如,我有以下简化的 Python 代码:
from z3 import *
p = Real('p')
q = Real('q')
s = Optimize()
s.add(p > 0, p < 1)
s.add(q > 0, q < 1)
h = s.maximize(p)
print s.check()
print s.upper(h)
print s.model()
如何让 p 被赋予最大值 1? (现在它被分配了 1/2。)非常感谢!
最佳答案
前提:
我假设您只是想要一个模型,其中p
以固定精度接近 1。
在this answer注意状态(重点是我的)
epsilon refers to a non-standard number (infinitesimal). You can set it arbitrarily small. Again the model uses only standard numbers, so it picks some number, in this case 9.
鉴于..
我在 Python API 和 smt2 选项中都找不到设置 epsilon
的选项
通过改变x
的区间大小,返回模型中x
的值与最优值有不同的距离(例如区间0, 10
给出 x=9
,而 0, 1
给出x=0.5
)
..我对前面引述的看法是,z3 选择了一些随机 可满足的值,仅此而已。
因此:
我会按照以下方式进行:
from z3 import *
epsilon = 0.0000001
p = Real('p')
q = Real('q')
s = Optimize()
s.add(p > 0, p < 1)
s.add(q > 0, q < 1)
s.push()
h = s.maximize(p)
print s.check() # Here I assume SAT
opt_value = h.value()
if epsilon in opt_value: # TODO: refine
s.pop()
opt_term = instantiate(opt_value, epsilon) # TODO: encode this function
s.add(p > opt_value)
s.check()
print s.model()
else:
print s.model()
s.pop()
其中 instantiate(str, eps)
是一个定制函数,它以 ToReal(1) + ToReal(-1) 的形式解析字符串*epsilon
并返回此类字符串的明显解释的结果。
我想提一下,另一种方法是将问题编码为 smt2 公式,并将其作为 OptiMathSAT 的输入:
(set-option:produce-models true)
(declare-fun p () Real)
(declare-fun q () Real)
(assert (and (< 0 p) (< p 1)))
(assert (and (< 0 q) (< q 1)))
(maximize p)
(check-sat)
(set-model 0)
(get-model)
OptiMathSAT 有一个命令行选项-optimization.theory.la.epsilon=N
来控制epsilon 的值
在公式的返回模型 中。默认情况下 N=6
和 epsilon
是 10^-6
。这是输出:
### MAXIMIZATION STATS ###
# objective: p (index: 0)
# interval: [ -INF , +INF ]
#
# Search terminated!
# Exact strict optimum found!
# Optimum: <1
# Search steps: 1 (sat: 1)
# - binary: 0 (sat: 0)
# - linear: 1 (sat: 1)
# Restarts: 1 [session: 1]
# Decisions: 3 (0 random) [session: 3 (0 random)]
# Propagations: 6 (0 theory) [session: 13 (0 theory)]
# Watched clauses visited: 1 (0 binary) [session: 2 (1 binary)]
# Conflicts: 3 (3 theory) [session: 3 (3 theory)]
# Error:
# - absolute: 0
# - relative: 0
# Total time: 0.000 s
# - first solution: 0.000 s
# - optimization: 0.000 s
# - certification: 0.000 s
# Memory used: 8.977 MB
sat
( (p (/ 1999999 2000000))
(q (/ 1 2000000)) )
关于python - Z3 优化,严格的不等式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37868251/
所以我正在为“石头剪刀布蜥蜴 Spock”制作一个 Javascript 程序,并且使用 Math.random()。我需要使用if/else/else if's。有人可以帮我解决不平等问题吗?我
我们正在使用一个代码分析器,它具有这样的规则“不检查浮点相等性/不等性”。下面是给出的示例。 float f = 0.100000001f; // 0.1 double d = 0.100000000
我们正在使用一个代码分析器,它具有这样的规则“不检查浮点相等性/不等性”。下面是给出的示例。 float f = 0.100000001f; // 0.1 double d = 0.100000000
C++20 为我们提供了飞船操作符,甚至允许我们 default它,使用默认语义生成所有比较,这将从我们的代码库中删除很多样板,太棒了! 但是平等和不平等呢? C++20 是否也为我们提供了一种方式
我今天在写一些 Python 时意识到可以将不等运算符写成 a!=b 或 not a==b。这让我很好奇: 这两种方式的行为是否完全相同,还是存在一些细微差别? 是否有理由使用一个而不是另一个?一种比
假设我们有几个线性不等式,比如 2x-5y=0 ,我们如何绘制这两个不等式?为了扩展这一点,如果我们有多个这样的不等式,我们如何尝试以图形方式解决这个问题? 最佳答案 你可以试试ImplicitEqu
为什么以下简化 不起作用,或者如何解决: >>> x = Symbol('x', real=True) >>> y = Symbol('y', real=True) >>> simplify(x -
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
这个问题在这里已经有了答案: Draw 3d Inequality on Matlab (1 个回答) matlab - plot inequality in 3d with surf (1 个回答
考虑使用这两个表和以下查询: SELECT Product. * FROM Product WHERE EXISTS ( SELECT * FROM Codes
我在笔记本界面中使用 Mathematica 7,我想重新排列一个不等式,以便在一侧获得某个变量。例如。 FullSimplify[x^3+L+r>3x^3+2r] 给 L > r + 2 x^3 但
我是一名优秀的程序员,十分优秀!