scipy.optimize.minimize中的逐元素约束 [英] Element-wise constraints in scipy.optimize.minimize
问题描述
我正在使用scipy.optimize.minimize
的COBYLA方法来查找用于分类分布的参数矩阵.我需要施加一个约束,即每个参数都大于零,并且参数矩阵的行之和是一列.
I'm using scipy.optimize.minimize
's COBYLA method to find a matrix of parameters for a categorical distribution. I need to impose the constraint that each parameter is greater than zero, and that the sum of the rows of the parameter matrix is a column of ones.
我不清楚如何在scipy.minimize
中实现这一点,因为检查了约束的非负性而不是真实性.如果仅将数组作为约束,则最小化会引发异常.
It's not clear to me how to implement this in scipy.minimize
, because the constraints are checked for non-negativity rather than truth. The minimization raises an exception if I just pass the arrays as the constraint.
有人知道如何实施这些约束吗?
Does anyone know how to go about implementing these kinds of constraints?
推荐答案
第一个约束x > 0
可以非常简单地表示:
The first constraint x > 0
can be expressed very simply:
{'type':'ineq', 'fun': lambda x: x}
第二个约束是等式约束,COBYLA本身不支持该约束.但是,您可以将其表示为两个单独的不等式约束:
The second constraint is an equality constraint, which COBYLA doesn't natively support. However, you could express it as two separate inequality constraints instead:
{'type':'ineq', 'fun': lambda x: np.sum(x, 0) - 1} # row sum >= 1
{'type':'ineq', 'fun': lambda x: 1 - np.sum(x, 0)} # row sum <= 1
否则,您可以尝试使用SLSQP,它确实支持相等性约束.
Otherwise you could try SLSQP instead, which does support equality constraints.
这篇关于scipy.optimize.minimize中的逐元素约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!