3

我有一个有时可能不可行的问题。期望的行为是放松被违反的约束并继续,但警告用户违反了约束。

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

使用首页上的示例:

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.

您如何获得残差或检查违反了哪个约束?

4

0 回答 0