多维变量的 Gurobi Python 中的 KeyError

我在 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 model

m = Model('netflow')


# Create variables

flow = 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 solution

m.optimize()

但是我得到了约束的 KeyError 。


KeyError: (('底特律', '波士顿'), '铅笔')


我看不出这个约束有什么问题。任何评论都非常感谢!


HUWWW
浏览 917回答 3
3回答

牛魔王的故事

如果你看看你的变量:{('Detroit', 'Boston', 'Pencils'): <gurobi.Var C0>,&nbsp;('Detroit', 'Boston', 'Pens'): <gurobi.Var C1>,&nbsp;('Detroit', 'New York', 'Pencils'): <gurobi.Var C2>,&nbsp;('Detroit', 'New York', 'Pens'): <gurobi.Var C3>,&nbsp;('Detroit', 'Seattle', 'Pencils'): <gurobi.Var C4>,&nbsp;('Detroit', 'Seattle', 'Pens'): <gurobi.Var C5>,&nbsp;('Denver', 'Boston', 'Pencils'): <gurobi.Var C6>,&nbsp;('Denver', 'Boston', 'Pens'): <gurobi.Var C7>,&nbsp;('Denver', 'New York', 'Pencils'): <gurobi.Var C8>,&nbsp;('Denver', 'New York', 'Pens'): <gurobi.Var C9>,&nbsp;('Denver', 'Seattle', 'Pencils'): <gurobi.Var C10>,&nbsp;('Denver', 'Seattle', 'Pens'): <gurobi.Var C11>}你会看到你需要解开每个元组arcs:m.addConstrs( flow[e1, e2, c] == 1 for e1, e2 in arcs for c in commodities)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python