Please correct my code
回答済みDear,
I tried to code P-median problem in gurobi by using python, but my code did not show any results. So, please tell me what is wrong with my code. Below is find my code.
Thank you for your quick response.
# Problem data
n=22
p=8
# make a list to creat the number of clients
V = [i for i in range(0,n)]
d={};h={};
for k in range(0,6):
sheet =book.sheet_by_index(k)
for i in range(0,n):
for j in range(0,n):
d[i,j,k]=sheet.cell(i,j).value
a={};W={}
sheet1=book.sheet_by_index(6)
for k in range(0,6):
W[k]=sheet1.cell(22,k).value
for i in range(0,n):
for k in range(0,6):
a[i,k]=sheet1.cell(i,k).value
K = [k for k in range(0,6)]
#K = {i for i in range(3)}
#beta = [0.25, 0.20, 0.20, 0.20, 0.20]
model = Model('P-Median Model')
x,w = {},{}
D ={}
M = {}
# Add variables
for k in K:
for i in V:
for j in V:
x[i,j,k] = model.addVar(vtype="B", name="x(%s,%s,%s)"%(i,j,k))
model.update()
M=model.addVar(vtype="C",name="M-value")
for j in V:
w[j] = model.addVar(vtype="B", name="w(%s)"%j)
# Constraints
# (2)
for k in K:
model.addConstr(quicksum(a[i,k]*d[i,j,k]*x[i,j,k]/W[k] for i in V for j in V)<=M)
model.update()
# (3) district i can be served by relief facility j if relief facility j
# is opened
for i in V:
model.addConstr(quicksum(x[i,j,k] for j in V ) == 1, "Assign(%s)"%i)
model.update()
# (4)
for i in V:
for j in V:
for k in K:
model.addConstr(w[j]>=x[i,j,k], name="x(%s,%s,%s)"%(i,j,k))
# (5) total number of facilities to be opened is only P
model.addConstr(quicksum(w[j] for j in V) == p, "Facilities")
model.update()
# Set Parameter for output
model.setParam('OutputFlag',0)
# Compute optimal solution
model.write("2024A.mps")
model.setObjective(M,GRB.MINIMIZE)
-
正式なコメント
This post is more than two 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 try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Wichitsawat,
It looks like there might be a couple of issues in your P-median model implementation.
1. Model Optimization Call: You have correctly set up the model's objective and variables but have not called the model.optimize() function to start the optimization process. After setting your objective and before writing the model to a file, insert:
model.optimize()
2. OutputFlag Set to 0: You have set model.setParam('OutputFlag', 0) which disables Gurobi's logging output. This might be why you see no output even if the model runs. To see Gurobi logs in your console, you can set this parameter to 1.
- Bot
0 -
Wow. It works.
Thank you so much for your quick response.
P.S. I am an old man who forgot simple rules to make Gurobi code run.1
投稿コメントは受け付けていません。
コメント
3件のコメント