Reputation: 179
I built a very simple optimization model in Gurobi-Python as follows:
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()
But I get a KeyError for the constraint.
KeyError: (('Detroit', 'Boston'), 'Pencils')
I cannot see what is the problem with this constraint. Any comment is greatly appreciated!
Upvotes: 0
Views: 577
Reputation: 7157
If you take a look at your variables:
{('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>}
you'll see that you need to unpack every tuple of arcs
:
m.addConstrs( flow[e1, e2, c] == 1 for e1, e2 in arcs for c in commodities)
Upvotes: 1