为了账号安全,请及时绑定邮箱和手机立即绑定

多维变量的 Gurobi Python 中的 KeyError

多维变量的 Gurobi Python 中的 KeyError

HUWWW 2021-09-24 16:08:56
我在 Gurobi-Python 中构建了一个非常简单的优化模型,如下所示:from gurobipy import *commodities = ['Pencils', 'Pens']arcs ={  ('Detroit', 'Boston'):   100,  ('Detroit', 'New York'):  80,  ('Detroit', 'Seattle'):  120,  ('Denver',  'Boston'):   120,  ('Denver',  'New York'): 120,  ('Denver',  'Seattle'):  120 }# Create optimization modelm = Model('netflow')# Create variablesflow = m.addVars(arcs,commodities)# THE ONLY CONSTRAINT WHICH IS THE SOURCE OF ERROR!m.addConstrs( flow[e,c] == 1 for e in arcs for c in  commodities)# Compute optimal solutionm.optimize()但是我得到了约束的 KeyError 。KeyError: (('底特律', '波士顿'), '铅笔')我看不出这个约束有什么问题。任何评论都非常感谢!
查看完整描述

3 回答

?
牛魔王的故事

TA贡献1830条经验 获得超3个赞

如果你看看你的变量:


{('Detroit', 'Boston', 'Pencils'): <gurobi.Var C0>,

 ('Detroit', 'Boston', 'Pens'): <gurobi.Var C1>,

 ('Detroit', 'New York', 'Pencils'): <gurobi.Var C2>,

 ('Detroit', 'New York', 'Pens'): <gurobi.Var C3>,

 ('Detroit', 'Seattle', 'Pencils'): <gurobi.Var C4>,

 ('Detroit', 'Seattle', 'Pens'): <gurobi.Var C5>,

 ('Denver', 'Boston', 'Pencils'): <gurobi.Var C6>,

 ('Denver', 'Boston', 'Pens'): <gurobi.Var C7>,

 ('Denver', 'New York', 'Pencils'): <gurobi.Var C8>,

 ('Denver', 'New York', 'Pens'): <gurobi.Var C9>,

 ('Denver', 'Seattle', 'Pencils'): <gurobi.Var C10>,

 ('Denver', 'Seattle', 'Pens'): <gurobi.Var C11>}

你会看到你需要解开每个元组arcs:


m.addConstrs( flow[e1, e2, c] == 1 for e1, e2 in arcs for c in commodities)


查看完整回答
反对 回复 2021-09-24
  • 3 回答
  • 0 关注
  • 916 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号