After using .feasRelax(), what should I do?
AnsweredFor example I have the following linear programming problem with an infeasible area. Then I use m.feasRelax() to get a new model with some new variables. But the original objective also changed to the all panalty factors of the new vairbles. What does it do? Then how can I get an estimation of the original objective function?
m = gurobipy.Model("lp1")
m.setParam("OutputFlag", False)
m.setParam("LogToConsole", 0)
x = m.addVar(vtype=GRB.CONTINUOUS, name="x")
y = m.addVar(vtype=GRB.CONTINUOUS, name="y")
m.addConstr(x >= 0, "c0")
m.addConstr(y >= 0, "c1")
m.addConstr(x + y <= 1, "c2")
c = m.addConstr(x >= 1.5, "c3")
kact_constrs = [c]
m.setObjective(x, GRB.MAXIMIZE)
m.feasRelax(relaxobjtype=1, minrelax=False, vars=None, lbpen=None, ubpen=None,
constrs=kact_constrs, rhspen=[1] * len(kact_constrs))
m.optimize()
print(m.display())
print('Status:', m.status)
for v in m.getVars():
print('%s %g' % (v.varName, v.x))
print('Obj: %g' % m.objVal)

When you call feasRelax, Gurobi modifies the model so that it computes the minimal violations necessary to make the model feasible. The new variables denote those violations, and the optimal objective value of the feasRelax model represents the total violation (sum of squares in your example). You can find more information about how to interpret the solution values in this article.
Now, if you just want to know the value of your original objective in this solution (with the computed violations), you can save the objective to a variable (before calling feasRelax) and then call the getValue method after optimizing. E.g.:
my_obj = m.getObjective()
m.feasRelax(...)
m.optimize()
print(my_obj.getValue())However, it may also be interesting to look into the minrelax setting of the feasRelax function. If you set this to True, Gurobi will perform two steps:
 First, it will compute the minimal violation. (The same as in your example code, but it will happen immediately after calling feasRelax.)
 Gurobi will then restore your original objective. When you optimize, it will compute the optimal solution with respect to your original objective but only among those that minimize the cost of the violation.
1
Please sign in to leave a comment.
Comments
1 comment