Python エラー討伐してくれませんか...
Q&A
Closed
解決したいこと
3カ所の需要地と9箇所の拠点候補で、輸送費や拠点開設費などの費用を最小化しながら、荷物を需要地へ送ることのできる拠点の位置を候補から決定するロバスト最適化問題を解くプログラムですが、拠点の荷物保管上限を減らすとエラーが出ます.
発生している問題・エラー
Model is infeasible or unbounded
Best objective -, best bound -, gap -
Traceback (most recent call last):
File "C:\Users\OneDrive\hog.py", line 98, in <module>
SP_obj = SP.ObjVal
^^^^^^^^^
File "src\gurobipy\model.pxi", line 354, in gurobipy.Model.__getattr__
File "src\gurobipy\model.pxi", line 1914, in gurobipy.Model.getAttr
File "src\gurobipy\attrutil.pxi", line 100, in gurobipy.__getattr
AttributeError: Unable to retrieve attribute 'ObjVal'. Did you mean: 'ObjNVal'?
該当するソースコード
from gurobipy import *
import numpy as np
# Parameters
Ay = np.array([
[100,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0],
[0,100,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0],
[0,0,100,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0],
[0,0,0,100,0,0,0,0,0,0,0,0,-1,0,0,0,0,0],
[0,0,0,0,100,0,0,0,0,0,0,0,0,-1,0,0,0,0],
[0,0,0,0,0,100,0,0,0,0,0,0,0,0,-1,0,0,0],
[0,0,0,0,0,0,100,0,0,0,0,0,0,0,0,-1,0,0],
[0,0,0,0,0,0,0,100,0,0,0,0,0,0,0,0,-1,0],
[0,0,0,0,0,0,0,0,100,0,0,0,0,0,0,0,0,-1],
[0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1]])
by = np.array([0,0,0,0,0,0,0,0,0,200.])
G = np.array([
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[-1., -1., -1., -1., -1., -1., -1., -1., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., -1., -1., -1., -1., -1., -1., -1., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1.,-1., -1., -1., -1., -1., -1., -1., -1.],
[ 1., 0., 0., 1., 0., 0., 1., 0., 0., 1., 0., 0., 1., 0., 0., 1., 0., 0., 1., 0., 0., 1., 0., 0., 1., 0., 0.],
[ 0., 1., 0., 0., 1., 0., 0., 1., 0., 0., 1., 0., 0., 1., 0., 0., 1., 0., 0., 1., 0., 0., 1., 0., 0., 1., 0.],
[ 0., 0., 1., 0., 0., 1., 0., 0., 1., 0., 0., 1., 0., 0., 1., 0., 0., 1., 0., 0., 1., 0., 0., 1., 0., 0., 1.],
])
E = np.array([[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
])
D = np.array([
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 40., 0., 0.],
[ 0., 40., 0.],
[ 0., 0., 40.]])
h = np.array([0., 0., 0., 0., 0., 0., 85., 65., 50.])
MP = Model('MP')
# main problem
# 主問題
c = np.array([850, 650, 500, 850, 650, 500, 850, 650, 500])
a = np.array([50, 50, 50, 50, 50, 50, 50, 50, 50])
b = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27])
bigM = 10**5
LB = -GRB.INFINITY
UB = GRB.INFINITY
epsilon = 1e-5
k = 1
y = MP.addMVar((9,), obj=c, vtype=GRB.BINARY)
z = MP.addMVar((9,), obj=a, vtype=GRB.CONTINUOUS)
d = MP.addMVar((9,), lb=0, name='d')
eta = MP.addMVar((1,), obj=1, vtype=GRB.CONTINUOUS)
# MP
MP.addConstr(Ay[:, :9]@y+Ay[:, 9:]@z >= by)
MP.optimize()
MP_obj = MP.ObjVal
LB = max(MP_obj, LB)
SP = Model('SP')
x = SP.addMVar((27,), vtype=GRB.CONTINUOUS, name='x')
pi = SP.addMVar(G.shape[0], vtype=GRB.CONTINUOUS, name='pi')
g = SP.addMVar((3,), ub=1, vtype=GRB.CONTINUOUS, name='g')
v = SP.addMVar((G.shape[0],), vtype=GRB.BINARY, name='v')
w = SP.addMVar((G.shape[1],), vtype=GRB.BINARY, name='w')
G1 = SP.addConstr(G@x >= h-D@g-E@np.concatenate([y.x, z.x]), name="G1")
SP.addConstr(G.T@pi <= b, name='pi')
SP.addConstr(pi <= bigM*v, name='v')
G2 = SP.addConstr(
G@x-h+E@np.concatenate([y.x, z.x])+D@g <= bigM*(1-v), name='G2')
SP.addConstr(x <= bigM*w, name='w1')
SP.addConstr(b-G.T@pi <= bigM*(1-w), name='w2')
SP.addConstr(g[:2].sum() <= 1, name='g1')
SP.addConstr(g.sum() <= 1.5, name='g2')
SP.setObjective(b@x, GRB.MAXIMIZE)
SP.optimize()
SP_obj = SP.ObjVal
UB = min(UB, c@y.x+a@z.x+SP_obj)
MP.reset()
while abs(UB-LB) >= epsilon:
if SP_obj < GRB.INFINITY:
MP.reset()
x_new = MP.addMVar((27,), vtype=GRB.CONTINUOUS)
MP.addConstr(eta >= b.T@x_new)
MP.addConstr(E[:, :9]@y+E[:, 9:]@z+G@x_new >= h-D@g.x)
SP.reset()
MP.optimize()
MP_obj = MP.objval
LB = max(LB, MP_obj)
else:
x_new = MP.addMVar((9,), vtype=GRB.CONTINUOUS)
MP.addConstr(E[:, :9]@y+E[:, 9:]@z+G@x_new >= h-D@g.x)
# update
SP.remove(G1)
SP.remove(G2)
G1 = SP.addConstr(G@x >= h-D@g-E@np.concatenate([y.x, z.x]), name="G1")
G2 = SP.addConstr(G@x-h+E@np.concatenate([y.x, z.x])+D@g <= bigM*(1-v), name='G2')
SP.optimize()
SP_obj = SP.ObjVal
UB = min(UB, c@y.x+a@z.x+SP_obj)
k += 1
# go back to MP
# check the solutions
print("y:", y.x)
print("z:", z.x)
print("d:", d.x)
print("輸送コスト:", eta.x)
print("x:", x.x)
print("g:", g.x)
print("繰り返し回数:"+str(k))
print("上界:{}".format(UB))
print("下界:{}".format(LB))
自分で試したこと
パラメータの次元数や計算の解析
予想されるミス箇所
Ayは拠点の上限を示しますが、200まで増やすとエラーが解消されるため、
おそらくここも問題
本来であれば上限を超えた需要はほかの拠点を開設して補うはず...
x_newが正しく生成されていない点
0