Assigning an objective value of -1000, when the model is infeasible
回答済みHi team,
I am working on an academic project and am working on python interface.
I have a following query
I require to find minimum value of a function of two variables x1, x2
x1 + 2*x2 + lam1[i]*(x1*x1+x2*x2-4) + lam2[j]*(1-x1-x2)
for varying values of lambda parameters. However, when the solution is infeasible, we need to assign -1000 to objective value in that iteration and go on.
The code I wrote is as follows
import gurobipy as gp
from gurobipy import *
import numpy as np
lam1 = np.arange(0, 2.1, 0.1).tolist()
lam2 = np.arange(0, 2.1, 0.1).tolist()
Z=[None]*420
k = 0
for i in range(21):
for j in range(21):
m = gp.Model("Prob2")
x1 = m.addVar(lb = -10000, vtype=GRB.CONTINUOUS, name="x1")
x2 = m.addVar(lb = -10000, vtype=GRB.CONTINUOUS, name="x2")
m.setObjective(x1 + 2*x2 + lam1[i]*(x1*x1+x2*x2-4) + lam2[j]*(1-x1-x2), GRB.MINIMIZE)
m.optimize()
if GRB.OPTIMAL == 5:
Z[k] = -1000
else:
X = m.getObjective()
Z[k] = X.getValue()
k = k + 1
However, if GRB.OPTIMAL == 5: doesn't seem to do the trick. Please suggest how to overcome this.
-
正式なコメント
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
\( \texttt{GRB.OPTIMAL} \) is a fixed constant representing the "optimal" status. To check if a model is infeasible, compare the Model object's Status attribute to the appropriate status code:
if m.Status == GRB.INFEASIBLE:
Also, you can retrieve the objective function value directly with the ObjVal model attribute (e.g., \( \texttt{m.ObjVal} \)).
0
投稿コメントは受け付けていません。
コメント
2件のコメント