- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
考虑以下(凸)优化问题:
minimize 0.5 * y.T * y
s.t. A*x - b == y
其中优化(向量)变量是x
和y
和A
,b
是一个矩阵和向量,分别具有适当的维度。
下面的代码使用 Scipy 中的 SLSQP
方法轻松找到了解决方案:
import numpy as np
from scipy.optimize import minimize
# problem dimensions:
n = 10 # arbitrary integer set by user
m = 2 * n
# generate parameters A, b:
np.random.seed(123) # for reproducibility of results
A = np.random.randn(m,n)
b = np.random.randn(m)
# objective function:
def obj(z):
vy = z[n:]
return 0.5 * vy.dot(vy)
# constraint function:
def cons(z):
vx = z[:n]
vy = z[n:]
return A.dot(vx) - b - vy
# constraints input for SLSQP:
cons = ({'type': 'eq','fun': cons})
# generate a random initial estimate:
z0 = np.random.randn(n+m)
sol = minimize(obj, x0 = z0, constraints = cons, method = 'SLSQP', options={'disp': True})
Optimization terminated successfully. (Exit mode 0)
Current function value: 2.12236220865
Iterations: 6
Function evaluations: 192
Gradient evaluations: 6
请注意,约束函数是一个方便的“数组输出”函数。
现在,原则上可以使用一组等效的“标量输出”约束函数,而不是约束的数组输出函数(实际上,scipy.optimize 文档仅讨论这种类型的约束函数作为输入最小化
).
这是等效的约束集,后面是 minimize
的输出(与上面的列表相同的 A
、b
和初始值) :
# this is the i-th element of cons(z):
def cons_i(z, i):
vx = z[:n]
vy = z[n:]
return A[i].dot(vx) - b[i] - vy[i]
# listable of scalar-output constraints input for SLSQP:
cons_per_i = [{'type':'eq', 'fun': lambda z: cons_i(z, i)} for i in np.arange(m)]
sol2 = minimize(obj, x0 = z0, constraints = cons_per_i, method = 'SLSQP', options={'disp': True})
Singular matrix C in LSQ subproblem (Exit mode 6)
Current function value: 6.87999270692
Iterations: 1
Function evaluations: 32
Gradient evaluations: 1
显然,算法失败了(返回的目标值实际上是给定初始化的目标值),我觉得有点奇怪。请注意,运行 [cons_per_i[i]['fun'](sol.x) for i in np.arange(m)]
显示 sol.x
,使用数组输出约束公式满足 cons_per_i
的所有标量输出约束(在数值公差范围内)。
如果有人对此问题有一些解释,我将不胜感激。
最佳答案
您遇到了 "late binding closures" gotcha .所有对 cons_i
的调用都是用等于 19 的第二个参数进行的。
解决方法是在定义约束的字典中使用 args
字典元素而不是 lambda 函数闭包:
cons_per_i = [{'type':'eq', 'fun': cons_i, 'args': (i,)} for i in np.arange(m)]
有了这个,最小化工作:
In [417]: sol2 = minimize(obj, x0 = z0, constraints = cons_per_i, method = 'SLSQP', options={'disp': True})
Optimization terminated successfully. (Exit mode 0)
Current function value: 2.1223622086
Iterations: 6
Function evaluations: 192
Gradient evaluations: 6
您还可以使用链接文章中的建议,即使用带有第二个参数的 lambda 表达式,该参数具有所需的默认值:
cons_per_i = [{'type':'eq', 'fun': lambda z, i=i: cons_i(z, i)} for i in np.arange(m)]
关于python - 具有线性约束的 Scipy.optimize.minimize SLSQP 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37791680/
在我的 previous question ,已经确定,当纹理四边形时,面部被分解为三角形,纹理坐标以仿射方式插值。 不幸的是,我不知道如何解决这个问题。 provided link很有用,但没有达到
是否有简单的解决方案可以在 Qt 中为图像添加运动模糊?还没有找到任何关于模糊的好教程。我需要一些非常简单的东西,我可以理解,如果我可以改变模糊角度,那就太好了。 最佳答案 Qt 没有运动模糊过滤器。
我想构建一个有点复杂的轴,它可以处理线性数据到像素位置,直到某个值,在该值中所有内容都被归入一个类别,因此具有相同的数据到像素值。例如,考虑具有以下刻度线的 y 轴: 0%, 10%, 20%, 30
我需要确保两个 View 元素彼此相邻且垂直高度相同。我会使用基线约束来做到这一点,但目前我正在使用线性、可滚动的布局( ScrollView 中的线性布局),当我点击一个元素时,它不允许我从中获取基
考虑正则表达式 ".*?\s*$" 和一个不以空格结尾的字符串。 示例 " a" .最后\s永远无法匹配 a这就是为什么 匹配器迭代: \s\s\s\s\s - fails .\s\s\
Closed. This question needs to be more focused。它当前不接受答案。 想要改善这个问题吗?更新问题,使它仅关注editing this post的一个问题。
我正在尝试阅读英特尔软件开发人员手册以了解操作系统的工作原理,这四个寻址术语让我感到困惑。以上是我的理解,如有不对请指正。 线性地址 : 对一个孤立的程序来说,似乎是一长串以地址0开头的内存。该程序的
有很多方法可以使用正则表达式并相应地使用匹配/测试匹配来检查字符串是否有效。我正在检查包含字母(a-b)、运算符(+、-、/、*)、仅特殊字符(如(')'、'(')和数字(0-9)的表达式是否有效 我
我正在使用 iris 数据集在 R 中练习 SVM,我想从我的模型中获取特征权重/系数,但我想我可能误解了一些东西,因为我的输出给了我 32 个支持向量。假设我要分析四个变量,我会得到四个。我知道在使
我正在使用 iris 数据集在 R 中练习 SVM,我想从我的模型中获取特征权重/系数,但我想我可能误解了一些东西,因为我的输出给了我 32 个支持向量。假设我要分析四个变量,我会得到四个。我知道在使
如何向左或向右滑动线性布局。在该线性布局中,默认情况下我有一个不可见的删除按钮,还有一些其他小部件,它们都是可见状态,当向左滑动线性布局时,我需要使其可见的删除按钮,当向右滑动时,我需要隐藏该删除按钮
我正在编写一个 R 脚本,运行时会给出因变量的预测值。我的所有变量都被分类(如图所示)并分配了一个编号,总类数为101。(每个类是歌曲名称)。 所以我有一个训练数据集,其中包含 {(2,5,6,1)8
如果源栅格位于 linear RGB color space使用以下 Java 代码进行转换,应用过滤器时(最后一行)会引发 java.awt.image.ImagingOpException: Un
我想为我的多个 UIImageView 设置动画,使其从 A 点线性移动到 B 点。 我正在使用 options:UIViewAnimationOptionCurveLinear - Apple 文档
我第一次无法使用 CSS3 创建好看的渐变效果。右侧应该有从黑色到透明的渐变透明渐变。底部是页脚,所以它需要在底部另外淡化为透明。 如果可能的话,一个例子: 页面的背景是一张图片,所以不可能有非透明淡
我有一组线性代数方程,Ax=By。其中A是36x20的矩阵,x是20x1的 vector ,B是36x13,y是13x1。 排名(A)=20。因为系统是超定的,所以最小二乘解是可能的,即; x = (
我有一个带有年月数据列(yyyymm)的 Pandas 数据框。我计划将数据插入每日和每周值。下面是我的 df。 df: 201301 201302 201303
假设我想找到2条任意高维直线的“交点”。这两条线实际上不会相交,但我仍然想找到最相交的点(即尽可能靠近所有线的点)。 假设这些线有方向向量A、B和初始点C、D,我可以通过简单地设置一个线性最小二乘问题
如果我想编写一个函数(可能也是一个类),它从不可变的查找表(调用构造函数时固定)返回线性“平滑”数据,如下所示: 例如func(5.0) == 0.5。 存储查找表的最佳方式是什么? 我正在考虑使用两
给定一条线 X像素长如: 0-------|---V---|-------|-------|-------max 如果0 <= V <= max , 线性刻度 V位置将是 X/max*V像素。 如何计
我是一名优秀的程序员,十分优秀!