Infeasible model in python project
回答済みI try to use gurobipy to solve a linear programming problem, but when I try to run my code, no matter what input that I give, the result always be infeasible model. Here is my code
where x is numpy 2D matrix and y is a list, i transform it to a 1D vector
import numpy as np
import gurobipy as gp
from gurobipy import GRB
from scipy import sparse
def classify_feasible(x, y):
try:
# define length
y = np.array(y)
D = len(x[0, :])
N = len(x)
c = np.zeros(D)
diagy = np.diag(y)
diagy = -diagy
A = np.matmul(diagy, x)
sA = sparse.csr_matrix(A)
b = np.ones(N)
b = -b
# Create a new model
m = gp.Model("1")
# Create variables
# x = MODEL.addVars(*indexes, lb=0, ub=gurobipy.GRB.INFINITY, vtype=gurobipy.GRB.CONTINUOUS, name="")
xc = m.addMVar(D, lb=float('inf'), ub=float('inf'), vtype=GRB.CONTINUOUS, name="xc")
# Set objective function
# m.setMObjective(None, c, 0.0, None, None, xc, GRB.MINIMIZE)
m.setObjective(c @ xc, GRB.MINIMIZE)
# Add constraints
m.addConstr(sA @ xc <= b, name="c")
# m.addMConstr(A, xc,'<', b)
# Optimize model
m.optimize()
print(xc)
print("Obj:", m.objVal)
m.computeIIS()
m.write("1")
return True
except gp.GurobiError as e:
print("Errorcode" + str(e.errno) + ": " + str(e))
return False
except AttributeError:
print("Encountered an attribute error")
return False
-
If your model is infeasible and you computed an IIS, you have to actually write the IIS to a file to analyze it.
m.computeIIS()
m.write("IIS.ilp")You can find more details in How do I determine why my model is infeasible?
Best regards,
Jaromił0 -
Hi Jormil,
Many thanks for your explanations, but this cannot solve my problem, maybe I didn't explain it very well. Let me rephrase my question, my problem is not about how to detect if my model is infeasible, the problem is that, I give a input that I can guarantee it is feasible(because that I can check by using other linear programming package), but the code that I write here will always return "infeasible model".
Best,
Xi
0 -
Hi Xi,
the problem is that, I give a input that I can guarantee it is feasible(because that I can check by using other linear programming package), but the code that I write here will always return "infeasible model".
Could you then please modify your code snippet such that you provide the input which should be feasible? Did you write the model you think is feasible to an LP file and analyze it?
m.write("myLP.lp")You can check all constraints one by one to see whether there is something off.
Best regards,
Jaromił0 -
Hi Jaromił,
Here is the code that I provide the input, you can change N to get infeasible or feasible model.
import numpy as np
import gurobipy as gp
from gurobipy import GRB
from scipy import sparse
from sklearn.datasets import make_classification
N = 10
one_col = np.ones((N, 1))
X1, y = make_classification(
n_samples=N,
n_features=2,
n_redundant=0,
n_clusters_per_class=1,
random_state=2,
)
y = list(y)
for i in range(len(y)):
if y[i] == 0:
y[i] = -1
X = np.append(X1, one_col, axis=1)
def classify_feasible_gurobi(x, y):
try:
# define length
y = np.array(y)
D = len(x[0, :])
N = len(x)
c = np.zeros(D)
diagy = np.diag(y)
diagy = -diagy
A = np.matmul(diagy, x)
sA = sparse.csr_matrix(A)
b = np.ones(N)
b = -b
# Create a new model
m = gp.Model()
# Create variables
# x = MODEL.addVars(*indexes, lb=0, ub=gurobipy.GRB.INFINITY, vtype=gurobipy.GRB.CONTINUOUS, name="")
xc = m.addMVar(D, lb=float('inf'), ub=float('inf'), vtype=GRB.CONTINUOUS, name="xc")
# Set objective function
# m.setMObjective(None, c, 0.0, None, None, xc, GRB.MINIMIZE)
m.setObjective(c @ xc, GRB.MINIMIZE)
# Add constraints
m.addConstr(sA @ xc <= b, name="c")
# m.addMConstr(A, xc,'<', b)
# Optimize model
m.optimize()
print("Obj:", m.objVal)
m.computeIIS()
m.write("IIS.ilp")
return True
# Print
except gp.GurobiError as e:
print("Errorcode" + str(e.errno) + ": " + str(e))
return False
except AttributeError:
print("Encountered an attribute error")
return False
if __name__ == '__main__':
a = classify_feasible_gurobi(X,y)Best,
Xi
0 -
Hi Xi,
Thank you for the minimal reproducible example.
You are setting the lower bound of your \(\texttt{xc}\) variable to \(+\infty\).
xc = m.addMVar(D, lb=float('inf'), ub=float('inf'), vtype=GRB.CONTINUOUS, name="xc")By setting
xc = m.addMVar(D, lb=-float('inf'), ub=float('inf'), vtype=GRB.CONTINUOUS, name="xc")Your model becomes feasible.
Please note that you can compute an IIS only if the model is infeasible. Also, you can only access the objective value if there is at least 1 solution available. It is better to adjust your code to
m.optimize()
if m.status == GRB.INFEASIBLE:
m.computeIIS()
m.write("IIS.ilp")
print("Model infeasible. Inspect IIS given in IIS.ilp file.")
return False
if m.SolCount > 0:
print("Obj:", m.objVal)
return True
#[...]Best regards,
Jaromił0 -
Hi Jaromił,
Many thanks for your explanations, I am so sorry for this tiny mistake, hope you will be good and well!
Best,
Xi
0
サインインしてコメントを残してください。
コメント
6件のコメント