Cannot add equality constraint to Gurobi

656 Views Asked by At

Python code:

from gurobipy import *
m = Model("mip1")
x0 = m.addVar(vtype=GRB.CONTINUOUS, name="x0")
x1 = m.addVar(vtype=GRB.CONTINUOUS, name="x1")
m.setObjective(1 * x0 + 2 * x1, GRB.MAXIMIZE)
m.addConstr(x0 + x1 <= 0)
m.addConstr(x0 + x1 >= 0)
m.addConstr(x0 <= 1)
m.addConstr(x1 <= 1)
m.addConstr(x0 >= -1)
m.addConstr(x1 >= -1)
m.optimize()
for v in m.getVars():
    print(v.varName, v.x)
print('Obj:', m.objVal)

This task have solution x0 = -1, x1 = 1 with objecitve equal to 1, but Gurobi prints:

x0 0.0
x1 0.0
Obj: -0.0

What I'm doing wrong?

1

There are 1 best solutions below

0
On BEST ANSWER

Variables are nonnegative by default, addVar method have lb (lower bound) and ub (upper bound) arguments. Found here