CVXPY

时间:2018-06-25 03:13:30

标签: python convex-optimization cvxpy

我有一个有时可能无法实现的问题。期望的行为是放宽违反约束的行为并继续,但是警告用户违反约束的情况。

我注意到CVXPY 1.0具有一个新的violation()方法,该方法看起来可以计算constraint的残差,但是当问题不可行时,约束看起来就好像不是在计算。

>

使用首页上的示例:

In [15]:
import cvxpy as cp
import numpy as np

# Problem data.
m = 30
n = 20
np.random.seed(1)
A = np.random.randn(m, n)
b = np.random.randn(m)

# Construct the problem.
x = cp.Variable(n)
objective = cp.Minimize(cp.sum_squares(A*x - b))
constraints = [0 <= x, x <= 1, x >= 2]
prob = cp.Problem(objective, constraints)

# The optimal objective value is returned by `prob.solve()`.
result = prob.solve()
# The optimal value for x is stored in `x.value`.
print(x.value)
# The optimal Lagrange multiplier for a constraint is stored in
# `constraint.dual_value`.
print(constraints[0].dual_value)
None
None

In [16]:

In [16]: prob.status
Out[16]: 'infeasible'

In [17]: prob.constraints
Out[17]:
[NonPos(Expression(AFFINE, UNKNOWN, (20,))),
 NonPos(Expression(AFFINE, UNKNOWN, (20,))),
 NonPos(Expression(AFFINE, UNKNOWN, (20,)))]

In [18]: prob.constraints[0]
Out[18]: NonPos(Expression(AFFINE, UNKNOWN, (20,)))

In [19]: prob.constraints[0].violation()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-19-5ce1fdea8037> in <module>()
----> 1 prob.constraints[0].violation()

C:\Program Files\Anaconda3\envs\py35\lib\site-packages\cvxpy\constraints\constra
int.py in violation(self)
    137         residual = self.residual
    138         if residual is None:
--> 139             raise ValueError("Cannot compute the violation of an constra
int "
    140                              "whose expression is None-valued.")
    141         return residual

ValueError: Cannot compute the violation of an constraint whose expression is None-valued.

如何获取残差或检查违反了哪些约束?

0 个答案:

没有答案