Model is infeasible or unbounded
Answered親愛的 Gurobi 社區。對於一項教育任務,我製作了一個數學模型並用 python 編寫,並嘗試使用 Gurobi 作為求解器。
儘管我認為我已經正確地實現了決策變量、約束和目標函數,但 Gurobi 似乎不同意這一點,因為它指出“模型不可行或無界”。
我已經嘗試了很多東西,並試圖通過閱讀文檔來找出問題,但到目前為止沒有成功。請注意,我是初學者,請原諒我的代碼中可能存在的愚蠢錯誤。
![]() ![]() ![]() ![]()
|
----------------------------------------------
ori_center(xorii,yorii) = [[104.5, 43.0], [18.5, 15.5], [103.5, 65.0], [17.5, 100.5], [75.0, 43.0], [49.0, 43.0], [52.0, 100.5], [48.0, 15.5], [77.0, 66.5], [109.5, 100.0]]
ori_ap(ai 和 bi) = [[31, 24], [37, 31], [33, 20], [35, 39], [28, 24], [24, 24], [34, 39], [22, 31], [20, 23], [21, 31]]
sta_p(xp,yp) = [[-20, 100] , [-20, 20], [140, 100], [140, 20]]
sta_m(xm,ym) = [[20, 140], [-20, 60], [20, -20], [100 , -20], [140, 60], [100, 140]]f_w = 120
f_l(L) = 120
f_w(W) = 120
seq2(fim) = [[0, 0, 0, 0, 0, 0 ],
[1, 1, 0, 1, 0, 1],
[1, 1, 1, 1, 0, 1],
[1, 1, 0, 0, 1, 1],
[1, 1, 1 ] , 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 1, 0],
[0, 0, 1, 0, 0, 1],
[1, 1, 1, 0, 0, 0],
[1, 0, 1, 0, 1, 0]]
seq3(fip) = [[1, 1, 1, 1],
[0, 1, 1, 0],
[1, 0, 0, 0],
[0, 1, 0, 1],
[0, 1, 1, 1],
[1, 0, 1, 1],
[1, 0, 0, 0],
[0, 1, 0, 1],
[0, 0, 1, 0],
[0, 0, 1, 0]]
m=Model("final_project")
fim = []
for i in range(len(ori_center)):
fim.append([])
for j in range(len(sta_m)):
fim[i].append(m. addVar(vtype = GRB.CONTINUOUS,name="fim_%d_%d"%(i,j)))
fip = []
for i in range(len(ori_center)):
fip.append([])
for j in範圍(len(sta_p)):
fip[i].append(m.addVar(vtype = GRB.CONTINUOUS,name="fip_%d_%d"%(i,j)))
L = m.addVar(vtype = GRB.CONTINUOUS,name="L")
W = m.addVar(vtype = GRB.CONTINUOUS,name="W")
ai = [m.addVar(vtype = GRB.CONTINUOUS,name="ai_%d"%i ) 對於範圍內的我 (len(ori_center))]
bi = [m.addVar(vtype = GRB.CONTINUOUS,name="bi_%d"%i) 對於範圍內的我(len(ori_center))]
Vi = m.addVar(vtype = GRB.CONTINUOUS,name="Vi")
Vp = m.addVar(vtype = GRB.CONTINUOUS,name="Vp")
Ti = m.addVar(vtype = GRB.CONTINUOUS,name= "Ti")
xorii = [m.addVar(vtype = GRB.CONTINUOUS,name="yorii_%d"%i) for i in range(len(ori_center))]
yorii = [m.addVar(vtype = GRB.CONTINUOUS ,name="yorii_%d"%i) for i in range(len(ori_center))]
xm = [m.addVar(vtype = GRB.CONTINUOUS,name="xm_%d"%i) for i in range( len(sta_m))]
ym = [m.addVar(vtype = GRB.CONTINUOUS,name="ym_%d"%i) for i in range(len(sta_m))]
xp = [m.addVar(vtype = GRB .CONTINUOUS,name="xp_%d"%i) for i in range(len(sta_p))]
yp = [m.addVar(vtype = GRB.CONTINUOUS,name="yp_%d"%i) for i in 範圍(len(sta_p))]
xnewi = [m.addVar(vtype = GRB.CONTINUOUS,name="xnewi_%d"%i) for i in range(len(ori_center))]
ynewi = [m.addVar(vtype = GRB.CONTINUOUS,name="ynewi_%d"%i) for i in range(len(ori_center))]
li = [m.addVar(vtype = GRB.CONTINUOUS,name="li_%d"%i) for i in range(len(ori_center))]
wi = [m.addVar(vtype = GRB.CONTINUOUS,name="wi_%d"%i) for i in range(len(ori_center))]
DXi = [m.addVar(vtype = GRB.CONTINUOUS,name="DXi_%d"%i) for i in range(len(ori_center))]
DYi = [m.addVar(vtype = GRB.CONTINUOUS,name="DYi_%d"%i) for i in range(len(ori_center))]
DXim = []
for i in range(len(ori_center)):
DXim.append([])
for j in range(len(sta_m)):
DXim[i].append(m.addVar(vtype = GRB.CONTINUOUS,name="DXim_%d_%d"%(i,j)))
DYim = []
for i in range(len(ori_center)):
DYim.append([])
for j in range(len(sta_m)):
DYim[i].append(m.addVar(vtype = GRB.CONTINUOUS,name="DYim_%d_%d"%(i,j)))
DXip = []
for i in range(len(ori_center)):
DXip.append([])
for j in range(len(sta_p)):
DXip[i].append(m.addVar(vtype = GRB.CONTINUOUS,name="DXip_%d_%d"%(i,j)))
DYip = []
for i in range(len(ori_center)):
DYip.append([])
for j in range(len(sta_p)):
DYip[i].append(m.addVar(vtype = GRB.CONTINUOUS,name="DYip_%d_%d"%(i,j)))
DXij = []
for i in range(len(ori_center)):
DXij.append([])
for j in range(len(ori_center)):
DXij[i].append(m.addVar(vtype = GRB.CONTINUOUS,name="DXij_%d_%d"%(i,j)))
DYij = []
for i in range(len(ori_center)):
DYij.append([])
for j in range(len(ori_center)):
DYij[i].append(m.addVar(vtype = GRB.CONTINUOUS,name="DYij_%d_%d"%(i,j)))
oi = [m.addVar(vtype = GRB.BINARY,name="oi_%d"%i) for i in range(len(ori_center))]
leftij = []
for i in range(len(ori_center)):
leftij.append([])
for j in range(len(ori_center)):
leftij[i].append(m.addVar(vtype = GRB.BINARY,name="leftij_%d_%d"%(i,j)))
belowij = []
for i in range(len(ori_center)):
belowij.append([])
for j in range(len(ori_center)):
belowij[i].append(m.addVar(vtype = GRB.BINARY,name="belowij_%d_%d"%(i,j)))
m.addConstrs((fim[i][j] == seq2[i][j] for i in range(len(ori_center)) for j in range(len(sta_m))), name = "")
m.addConstrs((fip[i][j] == seq3[i][j] for i in range(len(ori_center)) for j in range(len(sta_p))), name = "")
m.addConstr(L == f_l, name = "")
m.addConstr(W == f_w, name = "")
m.addConstrs((ai[i] == min_(ori_ap[0],ori_ap[1]) for i in range(len(ori_center))), name = "")
m.addConstrs((bi[i] == max_(ori_ap[0],ori_ap[1]) for i in range(len(ori_center))), name = "")
m.addConstr(Vi == 1/10, name = "")
m.addConstr(Vp == 1/5, name = "")
m.addConstr(Ti == 20, name = "")
m.addConstrs((xorii[i] == ori_center[i][0] for i in range(len(ori_center))), name = "")
m.addConstrs((yorii[i] == ori_center[i][1] for i in range(len(ori_center))), name = "")
m.addConstrs((xm[i] == sta_m[i][0] for i in range(len(sta_m))), name = "")
m.addConstrs((ym[i] == sta_m[i][1] for i in range(len(sta_m))), name = "")
m.addConstrs((xp[i] == sta_p[i][0] for i in range(len(sta_p))), name = "")
m.addConstrs((yp[i] == sta_p[i][1] for i in range(len(sta_p))), name = "")
m.addConstrs((xnewi[i] >= 0.5*li[i] for i in range(len(ori_center))), name = "")
m.addConstrs((xnewi[i] <= L - 0.5*li[i] for i in range(len(ori_center))), name = "")
m.addConstrs((ynewi[i] >= 0.5*wi[i] for i in range(len(ori_center))), name = "")
m.addConstrs((ynewi[i] <= W - 0.5*wi[i] for i in range(len(ori_center))), name = "")
m.addConstrs((li[i] == ai[i]*oi[i] + bi[i]*(1 - oi[i]) for i in range(len(ori_center))), name = "")
m.addConstrs((wi[i] == bi[i]*oi[i] + ai[i]*(1 - oi[i]) for i in range(len(ori_center))), name = "")
m.addConstrs((DXi[i] >= xnewi[i] - xorii[i] for i in range(len(ori_center))), name = "")
m.addConstrs((DXi[i] >= xorii[i] - xnewi[i] for i in range(len(ori_center))), name = "")
m.addConstrs((DYi[i] >= ynewi[i] - yorii[i] for i in range(len(ori_center))), name = "")
m.addConstrs((DYi[i] >= yorii[i] - ynewi[i] for i in range(len(ori_center))), name = "")
m.addConstrs((DXim[i][j] >= xnewi[i] - xm[j] for i in range(len(ori_center)) for j in range(len(sta_m))), name = "")
m.addConstrs((DXim[i][j] >= xm[j] - xnewi[i] for i in range(len(ori_center)) for j in range(len(sta_m))), name = "")
m.addConstrs((DYim[i][j] >= ynewi[i] - ym[j] for i in range(len(ori_center)) for j in range(len(sta_m))), name = "")
m.addConstrs((DYim[i][j] >= ym[j] - ynewi[i] for i in range(len(ori_center)) for j in range(len(sta_m))), name = "")
m.addConstrs((DXip[i][j] >= xnewi[i] - xp[j] for i in range(len(ori_center)) for j in range(len(sta_p))), name = "")
m.addConstrs((DXip[i][j] >= xp[j] - xnewi[i] for i in range(len(ori_center)) for j in range(len(sta_p))), name = "")
m.addConstrs((DYip[i][j] >= ynewi[i] - yp[j] for i in range(len(ori_center)) for j in range(len(sta_p))), name = "")
m.addConstrs((DYip[i][j] >= yp[j] - ynewi[i] for i in range(len(ori_center)) for j in range(len(sta_p))), name = "")
m.addConstrs((DXij[i][j] >= xnewi[i] - xnewi[j] for i in range(len(ori_center))
for j in range(i+1,len(ori_center))), name = "")
m.addConstrs((DXij[i][j] >= xnewi[j] - xnewi[i] for i in range(len(ori_center))
for j in range(i+1,len(ori_center))), name = "")
m.addConstrs((DYij[i][j] >= ynewi[i] - ynewi[j] for i in range(len(ori_center))
for j in range(i+1,len(ori_center))), name = "")
m.addConstrs((DYij[i][j] >= ynewi[j] - ynewi[i] for i in range(len(ori_center))
for j in range(i+1,len(ori_center))), name = "")
m.addConstrs((xnewi[i] + 0.5*li[i] - (xnewi[j] - 0.5*li[j]) <= L*(1 - leftij[i][j])
for i in range(len(ori_center)) for j in range(len(ori_center)) if j != i),name = "")
m.addConstrs((ynewi[i] + 0.5*wi[i] - (ynewi[j] - 0.5*wi[j]) <= W*(1 - belowij[i][j])
for i in range(len(ori_center)) for j in range(len(ori_center)) if j != i),name = "")
m.addConstrs((leftij[i][j] + leftij[j][i] + belowij[i][j] + belowij[j][i] == 1
for i in range(len(ori_center)) for j in range(len(ori_center)) if j != i),name = "")
m.addConstrs((xnewi[i] >=0 for i in range(len(ori_center))), name = "")
m.addConstrs((ynewi[i] >=0 for i in range(len(ori_center))), name = "")
m.addConstrs((li[i] >=0 for i in range(len(ori_center))), name = "")
m.addConstrs((wi[i] >=0 for i in range(len(ori_center))), name = "")
m.addConstrs((DXim[i][j] >=0 for i in range(len(ori_center)) for j in range(len(sta_m))), name = "")
m.addConstrs((DYim[i][j] >=0 for i in range(len(ori_center)) for j in range(len(sta_m))), name = "")
m.addConstrs((DXip[i][j] >=0 for i in range(len(ori_center)) for j in range(len(sta_p))), name = "")
m.addConstrs((DYip[i][j] >=0 for i in range(len(ori_center)) for j in range(len(sta_p))), name = "")
m.addConstrs((DXij[i][j] >= 0 for i in range(len(ori_center)) for j in range(len(ori_center)) if j != i),name = "")
m.addConstrs((DYij[i][j] >= 0 for i in range(len(ori_center)) for j in range(len(ori_center)) if j != i),name = "")
dis_xy_p = []
for i in range(len(ori_center)):
dis_xy_p.append([])
for j in range(len(sta_p)):
dis_xy_p[i].append(m.addVar(vtype = GRB.CONTINUOUS,name="dis_xy_p_%d_%d"%(i,j)))
m.addConstrs((dis_xy_p[i][j] == 2*(DXip[i][j] + DYip[i][j])
for i in range(len(ori_center)) for j in range(len(sta_p))), name = "")
flow_dis_xy_p = []
for i in range(len(ori_center)):
flow_dis_xy_p.append([])
for j in range(len(sta_p)):
flow_dis_xy_p[i].append(m.addVar(vtype = GRB.CONTINUOUS,name="flow_dis_xy_p_%d_%d"%(i,j)))
m.addConstrs((flow_dis_xy_p[i][j] == fip[i][j]*dis_xy_p[i][j]
for i in range(len(ori_center)) for j in range(len(sta_p))), name = "")
dis_xy_m = []
for i in range(len(ori_center)):
dis_xy_m.append([])
for j in range(len(sta_m)):
dis_xy_m[i].append(m.addVar(vtype = GRB.CONTINUOUS,name="dis_xy_m_%d_%d"%(i,j)))
m.addConstrs((dis_xy_m[i][j] == 2*(DXim[i][j] + DYim[i][j])
for i in range(len(ori_center)) for j in range(len(sta_m))), name = "")
flow_dis_xy_m = []
for i in range(len(ori_center)):
flow_dis_xy_m.append([])
for j in range(len(sta_m)):
flow_dis_xy_m[i].append(m.addVar(vtype = GRB.CONTINUOUS,name="flow_dis_xy_m_%d_%d"%(i,j)))
m.addConstrs((flow_dis_xy_m[i][j] == fim[i][j]*dis_xy_m[i][j]
for i in range(len(ori_center)) for j in range(len(sta_m))), name = "")
m.update()
obj_p = quicksum((flow_dis_xy_p[i][j]*Vp) for i in range(len(ori_center)) for j in range(len(sta_p)))
obj_m = quicksum((flow_dis_xy_m[i][j]*Vi + fim[i][j]*2*Ti) for i in range(len(ori_center)) for j in range(len(sta_m)))
obj_new = quicksum((DXi[i] + DYi[i])*Vi for i in range(len(ori_center)))
objective = obj_p + obj_m + obj_new
m.setObjective(objective,GRB.MINIMIZE)
m.update()
m.optimize()
-------------------
Gurobi creates the following output:
Gurobi Optimizer version 9.5.0 build v9.5.0rc5 (mac64[x86])
Thread count: 2 physical cores, 4 logical processors, using up to 4 threads
Optimize a model with 1730 rows, 1035 columns and 3500 nonzeros
Model fingerprint: 0x9287f22a
Model has 180 quadratic objective terms
Model has 300 quadratic constraints
Model has 20 general constraints
Variable types: 825 continuous, 210 integer (210 binary)
Coefficient statistics:
Matrix range [5e-01, 2e+00]
QMatrix range [1e+00, 1e+00]
QLMatrix range [5e-01, 1e+00]
Objective range [0e+00, 0e+00]
QObjective range [2e+00, 4e+00]
Bounds range [1e+00, 1e+00]
RHS range [1e-01, 1e+02]
GenCon const rng [2e+01, 4e+01]
Presolve removed 915 rows and 220 columns
Presolve time: 0.00s
Explored 0 nodes (0 simplex iterations) in 0.04 seconds (0.00 work units)
Thread count was 1 (of 4 available processors)
Solution count 0
Model is infeasible or unbounded
Best objective -, best bound -, gap -
-
Official comment
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. -
Please refer to the Knowledge Base articles How do I resolve the error "Model is infeasible or unbounded"? and How do I determine why my model is infeasible?
0
Post is closed for comments.




Comments
2 comments