我做了正确的设置,但在 gurobipy 中有关键错误
i made right set, but there is key error in gurobipy
我在gurobipy中做了这样的线性规划模型
def cal_gl_1():
m = gp.Model('Inventory')
s=['AUC','ADU','CF','LT'] #AUC: average unit cost, ADU: annual dollar usage, CF: critical factor, LT: lead time
item_number=list(range(1,11))
def cal_s_1():
s_auc=[71.21, 58.45, 40.82, 19.86, 86.50, 71.2, 78.4, 51.68, 14.66, 72]
list_key = [('AUC',1),('AUC',2),('AUC',3),('AUC',4),('AUC',5),('AUC',6),('AUC',7),
('AUC',8),('AUC',9),('AUC',10)]
auc=dict(zip(list_key,s_auc))
return auc
Lj = m.addVars(item_number,lb=0, name ="lamda" )
ACU=cal_s_1()
for r in s[0]:
m.addConstrs(gp.quicksum(Lj[j]*ACU[r,j])>=ACU[r,j] for j in item_number)
obj=(Lj[j].sum(j,'*') for j in item_number)
m.setObjective(obj, GRB.MINIMIZE)
m.optimize()
data_all.append(m.objVal)
return data_all
但是出现按键错误
KeyError: ('A', 1)
我认为有右键 ('AUC',1)...('AUC',10)。不知道哪里错了
问题大概出在这里:
for r in s[0]:
尝试:
for r in s:
否则 r
将是 ['A', 'U', 'C']
而不是 ['AUC','ADU','CF','LT']
。
但也许您只想对 'AUC'
进行约束。在这种情况下,使用:
for r in ['AUC']:
或r = 'AUC'
没有循环。
我在gurobipy中做了这样的线性规划模型
def cal_gl_1():
m = gp.Model('Inventory')
s=['AUC','ADU','CF','LT'] #AUC: average unit cost, ADU: annual dollar usage, CF: critical factor, LT: lead time
item_number=list(range(1,11))
def cal_s_1():
s_auc=[71.21, 58.45, 40.82, 19.86, 86.50, 71.2, 78.4, 51.68, 14.66, 72]
list_key = [('AUC',1),('AUC',2),('AUC',3),('AUC',4),('AUC',5),('AUC',6),('AUC',7),
('AUC',8),('AUC',9),('AUC',10)]
auc=dict(zip(list_key,s_auc))
return auc
Lj = m.addVars(item_number,lb=0, name ="lamda" )
ACU=cal_s_1()
for r in s[0]:
m.addConstrs(gp.quicksum(Lj[j]*ACU[r,j])>=ACU[r,j] for j in item_number)
obj=(Lj[j].sum(j,'*') for j in item_number)
m.setObjective(obj, GRB.MINIMIZE)
m.optimize()
data_all.append(m.objVal)
return data_all
但是出现按键错误
KeyError: ('A', 1)
我认为有右键 ('AUC',1)...('AUC',10)。不知道哪里错了
问题大概出在这里:
for r in s[0]:
尝试:
for r in s:
否则 r
将是 ['A', 'U', 'C']
而不是 ['AUC','ADU','CF','LT']
。
但也许您只想对 'AUC'
进行约束。在这种情况下,使用:
for r in ['AUC']:
或r = 'AUC'
没有循环。