Gurobi python negative equality constraints and infeasible solution
AnsweredHi,
I have been trying to understand the gurobi python API. The following program outputs the maximum value, but if I change the x==1 constraint to x==-1, the program outputs, "Model is infeasible or unbounded."
Please help me to solve the issue.
import gurobipy asgp
from gurobipy import GRB
import math
m = gp.Model("model1")
# Create variables
x = m.addVar(name="x")
y = m.addVar(name="y")
z = m.addVar(name="z")
xp0 = m.addVar(name="xp0")
yp0 = m.addVar(name="yp0")
zp0 = m.addVar(name="zp0")
obj = ((xp0 - x)**2 + (yp0 - y)**2 + (zp0 - z)**2)
# m.setObjective(obj, GRB.MINIMIZE)
m.setObjective(obj, GRB.MAXIMIZE)
m.addConstr(x == 1, "c0")
m.addConstr(y == 0, "c1")
m.addConstr(z == 10, "c2")
m.addConstrs(xp0 <= 1, "qc0")
m.addConstr(xp0 >= -1, "qc1")
m.addConstr(yp0 <= 1, "qc2")
m.addConstr(yp0 >= -1, "qc3")
m.addConstr(zp0 <= 1, "qc4")
m.addConstr(zp0 >= -1, "qc5")
m.optimize()
forvinm.getVars():
print('%s%g' % (v.VarName, v.X))
print('Obj: %g' % obj.getValue())
0
-
By default, variables added to the model via Model.addVar() or Model.addVars() have a lower bound of 0. To resolve the issue, change the lower bound on \( \texttt{x} \). For example:
x = m.addVar(lb=-GRB.INFINITY, name="x")
1
Please sign in to leave a comment.
Comments
1 comment