Search code examples
pythoncplexquadratic-programming

Cplex not meeting lower bound in quadratic programming (using Python API)


I am trying to solve a quadratic programming problem with k unknowns. The problem is separable, i.e. all non-diagonals are zero in the Q matrix which defines the problem. There are some linear constraints as well.

I find that Cplex gives solutions which do not meet the zero lower bound. I cannot figure out what is wrong with the way I enter the problem into Cplex which generates such solutions. Note that min v is negative, whereas I'd like all values in v to be between 0 and 1.

My code below.

from __future__ import division
import cplex
from cplex.exceptions import CplexError

k = 29

prob = cplex.Cplex()
prob.objective.set_sense(prob.objective.sense.minimize)
separable = [1.0] * k
prob.objective.set_quadratic(separable)
my_ub = [1.0] * k
my_lb = [0.0] * k
prob.variables.add(ub=my_ub, lb=my_lb)

my_rhs = [10.1, 3.1, 3.1, 3.1, 3.1, 4.1, 3.1, 3.1, 3.1, 4.1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
my_sense = 'EEEEEEEEEEEEEEEEEEEE'
prob.linear_constraints.add(rhs=my_rhs, senses=my_sense)
constraints_rowColVal = [(0, 9, 3.1), (0, 11, 3.1), (0, 13, 3.1), (0, 15, 3.1), (0, 17, 4.1), (0, 20, 3.1), (0, 22, 3.1), (0, 24, 3.1), (0, 26, 4.1), (1, 0, 10.1), (1, 14, 3.1), (1, 25, 3.1), (1, 27, 4.1), (2, 1, 10.1), (2, 10, 3.1), (2, 21, 3.1), (2, 28, 4.1), (3, 2, 10.1), (3, 12, 3.1), (4, 3, 10.1), (5, 4, 10.1), (5, 16, 3.1), (6, 5, 10.1), (6, 18, 4.1), (7, 6, 10.1), (7, 19, 4.1), (8, 7, 10.1), (8, 23, 3.1), (9, 8, 10.1), (10, 0, 1), (10, 1, 1), (10, 2, 1), (10, 3, 1), (10, 4, 1), (10, 5, 1), (10, 6, 1), (10, 7, 1), (10, 8, 1), (11, 9, 1), (11, 10, 1), (12, 11, 1), (12, 12, 1), (13, 13, 1), (13, 14, 1), (14, 15, 1), (14, 16, 1), (15, 17, 1), (15, 18, 1), (15, 19, 1), (16, 20, 1), (16, 21, 1), (17, 22, 1), (17, 23, 1), (18, 24, 1), (18, 25, 1), (19, 26, 1), (19, 27, 1), (19, 28, 1)]
prob.linear_constraints.set_coefficients(constraints_rowColVal)

prob.solve()
v = prob.solution.get_values()
print min(v)

Solution

  • The problem is that your model is infeasible. Try adding the following line of code, just after calling the solve method:

    print("Solution status: {0} ({1})".format(
        prob.solution.get_status_string(),
        prob.solution.get_status()
    ))
    

    You should see the following:

    Solution status: infeasible (3)
    

    You should always check the solution status after solving. Otherwise, as you have seen, the solution results will not make any sense.

    You can use the conflict refiner in the Python API or the interactive to understand where the problems might be.