Question

When solving optimisation problems in cvxpy, is there a nice way to check that the constraints are valid by substituing in actual values for the optimisation variables?

I have a complicated optimisation problem (100+ constraints), but I know what the optimal solution should be. However, cvxpy fails with error message ValueError: Rank(A) < p or Rank([G; A]) < n I think this is because I have a typo in one of the constraints, making them inconsistent. Is there a nice way to substitute actual values for the variables, to see which constraints are violated (since they probably have typos)?

My actual problem is complicated, so I've made a simple example:

from cvxpy import *

x = variable(name='x')
y = variable(name='y')

c1 = greater_equals(x, 1.)
c2 = greater_equals(y, 1.)
c3 = less_equals(x + y, -4.) # typo: -4 should be +4

p = program(maximize(2. * x + y), [c1, c2, c3])

p.solve()

The -4 in constraint c3 should be +4. This fails with error message: Certificate of primal infeasibility found. If I enter p.show() I get:

maximize 2.0*x + y
subject to
x >= 1.0
y >= 1.0
x + y <= -4.0

Is there a value to substitue the correct solution (x == 3., y == 1.) so see that 3rd constraint is violated? I've tried messing around with x.value etc. but haven't found a way

Was it helpful?

Solution

I've found an ok way to do it, using the left attribute of the constraint, which does have a value attribute:

x.value = 3.
y.value = 1.
for c in [c1, c2, c3]:
    constraint_text = '%s %s %s' % (c.left.value, c.type, c.right)
    print '%s becomes %s which is %s' % (c, constraint_text, eval(constraint_text))

which prints:

x >= 1.0 becomes 3.0 >= 1.0 which is True 
y >= 1.0 becomes 1.0 >= 1.0 which is True
x + y <= -4.0 becomes 4.0 <= -4.0 which is False

If anyone knows a better way, feel free to share.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top