I am trying to understand why GurobiPy gives me a strange result for a simple linear programming model coded as below? Why is the optimality gap is 0%? Please let me know if you spot any errors in the code. Thanks.

m = gp.Model('mip1')
x = m.addVar(vtype = GRB.BINARY, name = 'x')
y = m.addVar(vtype = GRB.BINARY, name = 'y')
m.setObjective(2 * x + 1.5 * y, GRB.MAXIMIZE)
m.addConstr(x + 0.5 * y <= 100, 'c0')
m.addConstr(0.03 * x + 0.02 * y <= 100, 'c1')

Gurobi Optimizer version 9.1.1 build v9.1.1rc0 (win64)
Thread count: 2 physical cores, 4 logical processors, using up to 4 threads
Optimize a model with 2 rows, 2 columns and 4 nonzeros
Model fingerprint: 0xd53be4d9
Variable types: 0 continuous, 2 integer (2 binary)
Coefficient statistics:
  Matrix range     [2e-02, 1e+00]
  Objective range  [2e+00, 2e+00]
  Bounds range     [1e+00, 1e+00]
  RHS range        [1e+02, 1e+02]
Found heuristic solution: objective 3.5000000

Explored 0 nodes (0 simplex iterations) in 0.01 seconds
Thread count was 1 (of 4 available processors)

Solution count 1: 3.5 

Optimal solution found (tolerance 1.00e-04)
Best objective 3.500000000000e+00, best bound 3.500000000000e+00, gap 0.0000%

Out[13]: [<gurobi.Var x (value 1.0)>, <gurobi.Var y (value 1.0)>]
  • 2
    $\begingroup$ You declared binary variables and the constraint look like they are not meant for binary variables. $\endgroup$ Mar 28 at 16:47
  • 2
    $\begingroup$ It seems perfectly fine to me. You have two binary variables, a maximization objective, and two constraints that will never bind given the small values of the parameters and the large right-hand-side values. The optimality gap is zero because the solution is optimal (gurobi.com/documentation/9.1/refman/mipgap2.html). Which are your doubts? $\endgroup$
    – Libra
    Mar 28 at 17:27
  • $\begingroup$ @Undecided, as user3680510 mentioned, your problem is a bit strange. Are you sure to define the variable type correctly? $\endgroup$
    – A.Omidi
    Mar 28 at 18:56
  • 1
    $\begingroup$ As @Libra mentioned your model found the optimal solution of $x=1$ and $y=1$. What is the problem with that? $\endgroup$ Mar 28 at 19:26
  • $\begingroup$ Sorry my bad. I was thinking x and y as integer (not binary) variables. The result is good. $\endgroup$
    – Undecided
    Mar 28 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.