2

I am trying to solve an integer programming with CVXPY. But the solution returning by CVXPY seems to be non-integer. What's wrong with my code?

import cvxpy as cp

# Create two optimization variables of type integer.
x = cp.Variable(integer=True)
y = cp.Variable(integer=True)

# Create two constraints.
constraints = [x + y == 1,
               x - y >= 1]

# Form objective.
obj = cp.Minimize((x - y)**2)

# Form and solve problem.
prob = cp.Problem(obj, constraints)
prob.solve()  # Returns the optimal value.
print("status:", prob.status)
print("optimal value", prob.value)
print("optimal var", x.value, y.value)

The output is

status: optimal
optimal value 1.0000001874501487
optimal var 0.9999999924717351 7.528264443746919e-09
Saikat
  • 1,209
  • 3
  • 16
  • 30
  • 3
    Looks like (x,y) = (1,0) to me. Do you expect miracles from `float`s? – Rodrigo de Azevedo Mar 29 '19 at 18:36
  • But its showing (x,y)=(0.9999999924717351 7.528264443746919e-09) – Saikat Mar 29 '19 at 18:39
  • 3
    Then round the `float`s to integers (using, say, `numpy.round`) and then compare the result of the rounding with the original. If the difference exceeds a certain threshold, then something went wrong. – Rodrigo de Azevedo Mar 29 '19 at 18:42
  • Thats because an interior point solver is used without a crossover step. The solution is a very accurate approximation. Its also more a toy solver if ecosbb is used. Its recommended to use cbc or glpk (harder setup) – sascha Mar 29 '19 at 19:22

0 Answers0