I have a problem that can sometimes be infeasible. The desired behaviour is relax the constraint that was violated and continue, but alert the user that a constraint was violated.
I noticed that CVXPY 1.0 has a new violation()
method that looks to calculate the residual of the constraint, but when the problem was infeasible, the constraint doesn't look like it's calculated.
Using the example on the front page:
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.
How do you get the residual or check which constraint was violated?