How to obtain a feasible solution within constraints?
AnsweredHere's my model, and the output x has no assigned value, how could I obtain a feasible solution?
def Optimisation(distance, P, log_file=None):
setParam("LogToConsole", 0)
R = [i for i in range(distance.shape[0])]
J = [i for i in range(distance.shape[1])]
A = [[i, j] for i in range(distance.shape[0]) for j in range(distance.shape[1])]
d = {(i, j):
distance[i,j].
tolist()
for i in range(distance.shape[0])
for j in range(distance.shape[1])}
combinations, ms = multidict(d)
# Create a new model: resource assignment problem
m = gp.Model()
# Create variables
x = m.addVars(combinations, name="assign")
# Add constraint: x + 2 y + 3 z <= 4
jobs = m.addConstrs((x.sum('*', j) <= 1 for j in J), "job")
# Add constraint: x + y >= 1
resources = m.addConstrs((x.sum(r, '*')<=1 for r in R), "resource")
# add constraints: 不可行区域sum为零
m.addConstrs((x[j, r]<=ms[j, r] for j in J for r in R))
# m.addConstr(x.sum("*", "*")>=P)
m.addConstr(quicksum([x[j,r] for j,r in A])>=P, 'total num')#(x[j,r]) for j in J for r in R)
# m.addConstr(sum(x.prod(ms))>P)
# Set objective
m.setObjective(x[0,1], GRB.MAXIMIZE)
# SAVE MODEL FOR INSPECTION
m.write('RAP.lp')
# Optimize model
m.optimize()
m.terminate()
return x

Could you please elaborate a bit more on what exactly is your issue? Gurobi can solve models with constant objective functions. After a call to optimize() and a successful optimization run, you can access the feasible solution point via the X attribute.
0 
I upload some details. Actually, after optimization, the var in m.getVars() has no accessible X attribute. I tried several ways with different objective functions, but they weren't working at all. Does this mean the optimization didn't run properly? but I got no error or warning. My test is
Optimisation(np.array([[1,1],[1,1]]), N)
0 
OK, I got it. The problem is my bound N, I got no assigned value because I have no feasible solution at all. Very negligent. Thanks for your time!
0 
This is a scoping issue. The model you define lives only within the \(\texttt{Optimisation}\) function. There are two ways to deal with this. The first option is to gather all solution information you need at the end of the \(\texttt{Optimization}\) function and return this information
def Optimisation(...)
[...]
m.optimize()
result = []
for x in m.getVars():
result.append((x.VarName,x.X))
return result
# outside of the Optimisation function
res = Optimisation(np.array([[1,1],[1,1]]), 1)
print(res)An alternative would be to generate the model before the function call and pass it as an argument
def Optimisation(m, distance, P, log_file=None):
[...]
m.optimize()
# outside of the Optimisation function
# Create a new model: resource assignment problem
m = gp.Model()
Optimisation(m, np.array([[1,1],[1,1]]), 1)
for v in m.getVars():
print("%s: %f"%(v.VarName,v.X))Any of the two works. Note that I removed the call to the terminate method, because it only interrupts a currently running optimization process and does nothing when called after optimization has already been performed. Moreover, you should always check for a model's Status attribute before accessing the solution point, because a solution point will not be available if the model is infeasible.
Best regards,
Jaromił0
Please sign in to leave a comment.
Comments
4 comments