- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试根据 x a1+y a2+a3 z >= 1 形式的一系列 (N=20) 不等式约束来最小化函数 0.5*(x^2+y^2)。解决方案应在 x=0.50251、y=-0.5846、z=0.36787 左右。该例程以“优化成功终止”消息终止,但超过一半的约束未得到遵守。我也尝试了不同的求解器,结果相同。
缩放目标函数会改变解决方案,但不会收敛到预期的解决方案。
from scipy.optimize import minimize
import numpy as np
Pct=np.array([[-0.664, 3.179],[ 0.231, -2.044],[-2.493, 3.25 ],[ 0.497, -0.654],[-1.27, 1.248],[-1.185, 1.814],[-1.843, 4.386],[-1.616, 1.401],[ 0.052, -1.232],[-3.145, 0.404],[ 0.672, -1.655],[ 2.202, -1.888],[ 4.084, -1.067],[ 1.006, -1.671],[-2.255, 1.51 ],[-1.264, 1.663],[ 1.897, -2.217],[ 1.843, -1.276],[-1.693, 1.623],[ 2.297, -1.709]])
Sid=np.array([-1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, 1, -1, 1])
# func to be minimized
def OptFunc(x):
return 0.5*(x[0]**2+x[1]**2)
def JacOptFunc(x):
return np.array([x[0],x[1],0.0])
# Constraints
c=[]
for i in range(len(Sid)):
c+=[{'type': 'ineq', 'fun': lambda x: Sid[i]*(x[0]*Pct[i,0]+x[1]*Pct[i,1]+x[2])-1 }]
cons=tuple(c)
# start optimization
res = minimize(OptFunc,(0.3,-0.2,0.1),constraints=cons,method='SLSQP',jac=JacOptFunc)
#expected solution should be around
# [0.5025062702615434, -0.584685257866671, 0.36787016514022236]
print("-->",res.message)
print("solution ",res.x,flush=True)
print("Check Constraints")
cons=list(cons)
for i in range(len(cons)):
lokfun=c[i]['fun']
print("Constraint # ",i," value: ",lokfun(res.x))
预期结果就在附近x=0.50251, y=-0.5846, z=0.36787但我得到以下输出:
--> Optimization terminated successfully.
solution [-1.14580677e-04 -1.16285140e-04 1.00006446e+00]
Check Constraints
Constraint # 0 value: -1.9997708716077622
Constraint # 1 value: 0.0002756791862408292
Constraint # 2 value: -1.999972183420499
Constraint # 3 value: 8.356438220613605e-05
Constraint # 4 value: -2.0000648541023893
Constraint # 5 value: -1.9999892973558606
Constraint # 6 value: -1.9997656060620763
Constraint # 7 value: -2.000086707390163
Constraint # 8 value: 0.00020176559401496874
Constraint # 9 value: -2.0003778375289833
Constraint # 10 value: 0.00017991418852214558
Constraint # 11 value: 3.1700190727068644e-05
Constraint # 12 value: -0.0002794107423930159
Constraint # 13 value: 0.00014350480474445426
Constraint # 14 value: -2.000147249362345
Constraint # 15 value: -2.0000159082853974
Constraint # 16 value: 0.00010490510804150865
Constraint # 17 value: 1.6681482228886324e-06
Constraint # 18 value: -2.0000697148012767
Constraint # 19 value: -1.354516498963676e-11
最佳答案
我对 scipy.optimize
知之甚少,但我可以发现问题
for i in range(len(Sid)):
c+=[{'type': 'ineq', 'fun': lambda x: Sid[i]*(x[0]*Pct[i,0]+x[1]*Pct[i,1]+x[2])-1 }]
问题在于 Python 闭包是后期绑定(bind)的,这意味着每个约束中 i
的值实际上是在循环完成后计算的。实际上,您实际上是在施加相同的(最后一个)约束 20 次。见 https://docs.python-guide.org/writing/gotchas/#late-binding-closures
可能的修复:
from scipy.optimize import minimize
import numpy as np
Pct=np.array([[-0.664, 3.179],[ 0.231, -2.044],[-2.493, 3.25 ],[ 0.497, -0.654],[-1.27, 1.248],[-1.185, 1.814],[-1.843, 4.386],[-1.616, 1.401],[ 0.052, -1.232],[-3.145, 0.404],[ 0.672, -1.655],[ 2.202, -1.888],[ 4.084, -1.067],[ 1.006, -1.671],[-2.255, 1.51 ],[-1.264, 1.663],[ 1.897, -2.217],[ 1.843, -1.276],[-1.693, 1.623],[ 2.297, -1.709]])
Sid=np.array([-1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, 1, -1, 1])
# func to be minimized
def OptFunc(x):
return 0.5*(x[0]**2+x[1]**2)
def JacOptFunc(x):
return np.array([x[0],x[1],0.0])
# Constraints
def constraint_maker(i=0): # i MUST be an optional keyword argument, else it will not work
def constraint(x):
return Sid[i]*(x[0]*Pct[i,0]+x[1]*Pct[i,1]+x[2])-1
return constraint
c=[]
for i in range(len(Sid)):
c+=[{'type': 'ineq', 'fun': constraint_maker(i)}]
cons=tuple(c)
# start optimization
res = minimize(OptFunc,(0.3,-0.2,0.1),constraints=cons,method='SLSQP',jac=JacOptFunc)
#expected solution should be around
# [0.5025062702615434, -0.584685257866671, 0.36787016514022236]
print("-->",res.message)
print("solution ",res.x)
print("Check Constraints")
cons=list(cons)
for i in range(len(cons)):
lokfun=c[i]['fun']
print("Constraint # ",i," value: ",lokfun(res.x))
结果
--> Optimization terminated successfully.
solution [ 0.52374351 -0.56495542 0.37021863]
Check Constraints
Constraint # 0 value: 0.7735403550593944
Constraint # 1 value: 0.6459722649608017
Constraint # 2 value: 1.7715790719554194
Constraint # 3 value: 8.137268636687622e-11
Constraint # 4 value: -2.2235047136831554e-10
Constraint # 5 value: 0.27524657110337936
Constraint # 6 value: 2.0729351509689136
Constraint # 7 value: 0.2676534344356165
Constraint # 8 value: 0.09347837249122604
Constraint # 9 value: 0.5051967055706261
Constraint # 10 value: 0.6571754935710583
Constraint # 11 value: 1.5901376792721638
Constraint # 12 value: 2.1119945643862095
Constraint # 13 value: 0.8411451130595076
Constraint # 14 value: 0.6639056792092357
Constraint # 15 value: 0.23131403951409935
Constraint # 16 value: 1.6162662427554526
Constraint # 17 value: 1.0563610395273058
Constraint # 18 value: 0.43340178883510116
Constraint # 19 value: 1.5387662919992176
关于python - Scipy 最小化成功终止,但不满足不等式约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56444759/
我可以添加一个检查约束来确保所有值都是唯一的,但允许默认值重复吗? 最佳答案 您可以使用基于函数的索引 (FBI) 来实现此目的: create unique index idx on my_tabl
嗨,我在让我的约束在grails项目中工作时遇到了一些麻烦。我试图确保Site_ID的字段不留为空白,但仍接受空白输入。另外,我尝试设置字段显示的顺序,但即使尝试时也无法反射(reflect)在页面上
我似乎做错了,我正在尝试将一个字段修改为外键,并使用级联删除...我做错了什么? ALTER TABLE my_table ADD CONSTRAINT $4 FOREIGN KEY my_field
阅读目录 1、约束的基本概念 2、约束的案例实践 3、外键约束介绍 4、外键约束展示 5、删除
SQLite 约束 约束是在表的数据列上强制执行的规则。这些是用来限制可以插入到表中的数据类型。这确保了数据库中数据的准确性和可靠性。 约束可以是列级或表级。列级约束仅适用于列,表级约束被应用到整
我在 SerenityOS project 中偶然发现了这段代码: template void dbgln(CheckedFormatString&& fmtstr, const Parameters
我有表 tariffs,有两列:(tariff_id, reception) 我有表 users,有两列:(user_id, reception) 我的表 users_tariffs 有两列:(use
在 Derby 服务器中,如何使用模式的系统表中的信息来创建选择语句以检索每个表的约束名称? 最佳答案 相关手册是Derby Reference Manual .有许多可用版本:10.13 是 201
我正在使用 z3py 进行编码。请参阅以下示例。 from z3 import * x = Int('x') y = Int('y') s = Solver() s.add(x+y>3) if s.c
非常快速和简单的问题。我正在运行一个脚本来导入数据并声明了一个临时表并将检查约束应用于该表。显然,如果脚本运行不止一次,我会检查临时表是否已经存在,如果存在,我会删除并重新创建临时表。这也会删除并重新
我有一个浮点变量 x在一个线性程序中,它应该是 0或两个常量之间 CONSTANT_A和 CONSTANT_B : LP.addConstraint(x == 0 OR CONSTANT_A <= x
我在使用grails的spring-data-neo4j获得唯一约束时遇到了一些麻烦。 我怀疑这是因为我没有正确连接它,但是存储库正在扫描和连接,并且CRUD正在工作,所以我不确定我做错了什么。 我正
这个问题在这里已经有了答案: Is there a constraint that restricts my generic method to numeric types? (24 个回答) 7年前
我有一个浮点变量 x在一个线性程序中,它应该是 0或两个常量之间 CONSTANT_A和 CONSTANT_B : LP.addConstraint(x == 0 OR CONSTANT_A <= x
在iOS的 ScrollView 中将图像和带有动态文本(动态高度)的标签居中的最佳方法是什么? 我必须添加哪些约束?我真的无法弄清楚它是如何工作的,也许我无法处理它,因为我是一名 Android 开
考虑以下代码: class Foo f class Bar b newtype D d = D call :: Proxy c -> (forall a . c a => a -> Bool) ->
我有一个类型类,它强加了 KnownNat约束: class KnownNat (Card a) => HasFin a where type Card a :: Nat ... 而且,我有几
我知道REST原则上与HTTP无关。 HTTP是协议,REST是用于通过Web传输hypermedia的体系结构样式。 REST可以使用诸如HTTP,FTP等的任何应用程序层协议。关于REST的讨论很
我有这样的情况,我必须在数据库中存储复杂的数据编号。类似于 21/2011,其中 21 是文件编号,但 2011 是文件年份。所以我需要一些约束来处理唯一性,因为有编号为 21/2010 和 21/2
我有一个 MySql (InnoDb) 表,表示对许多类型的对象之一所做的评论。因为我正在使用 Concrete Table Inheritance ,对于下面显示的每种类型的对象(商店、类别、项目)
我是一名优秀的程序员,十分优秀!