Key error objective function
Awaiting user inputhi all, im trying to code my objective function but keep getting a key error (
KeyError: (1, 3, 'RC', 'CPM')
), dont know how to resolve it. any help would be appreciated
#Import packages
import pyomo.environ as pe
import pyomo.opt as po
from collections import defaultdict
#Dictionary - Sets
#Pallet Management System
#1 = buy/sell, 2 = rental/lease
P={1,2}
#Materials
#3 = wood, 4 = plastic
N={3,4}
#EOL Scenarios
#5 = repair, 6 = recycle, 7 = downcycle, 8 = landfill, 9 = incineration
D={5,6,7,8,9}
#Time Periods in weeks
T=5
#Nodes
V = {'G','RM', 'MP', 'CPM', 'DC', 'RET', 'RC', 'DEOL', 'LEOL', 'IEOL'}
#Arcs
A = { ('G','RM'), ('RM', 'MP'), ('MP', 'CPM'), ('CPM', 'DC'), ('DC', 'RET'),('DC', 'RM'),\
('DC', 'MP'),('DC', 'CPM'), ('DC', 'DEOL'),('DC', 'LEOL'), ('DC', 'IEOL'),\
('RET', 'RC'),('RC', 'RM'), ('RC', 'MP'), ('RC', 'CPM'), ('RC', 'DEOL'),\
('RC', 'LEOL'), ('RC', 'IEOL')}
#Parameters
#W(p,m) - Weight of pallet type p of material m - kg/PAL
W = {(1, 3): ..., (1, 4): ..., \
(2, 3): ..., (2, 4): ...}
#M(p,m) - CO2 footprint Material Primary Production per pallet type p per kg of material m - kg CO2/kg PAL
M = {(1, 3): ..., (1, 4): ..., \
(2, 3): ..., (2, 4): ...}
#MFG(p,m) - CO2 footprint Manufacturing per pallet type p per kg of material m - kg CO2/kg PAL
MFG = {(1, 3): ..., (1, 4): ..., \
(2, 3): ..., (2, 4): ...}
#ET(p,m) - CO2 footprint Transportation per pallet type p per km - kg CO2/km-kg PAL
ET = {(1, 3): ..., (1, 4): ..., \
(2, 3): ..., (2, 4): ...}
#EOL(p,m,d) - CO2 footprint EOL scenario per pallet type p of material m per EOL scenario d - kg CO2/PAL
EOL = {(1, 3, 5): ..., (2, 3, 5): ...,\
(1, 4, 6): ..., (2, 4, 6): ...,\
(1, 3, 7): ..., (2, 3, 7): ...,\
(1, 3, 8): ..., (2, 3, 8): ..., (1, 4, 8): ..., (2, 4, 8): ...,\
(1, 3, 9):..., (2, 3, 9):...}
#CO(p,n) - Purchase/leasing cost of pallet type p of material m - €/PAL
CO = {(1, 3): ..., (1, 4): ..., \
(2, 3): ..., (2, 4): ...}
#CT(p,n) - Transportation cost per pallet type p per km - €/km-PAL
CT = {(1, 3): ..., (1, 4): ..., \
(2, 3): ..., (2, 4): ...}
#S(p,n) - Credit for selling or returning pallet type p - €/PAL
S = {(1, 3): ..., (1, 4): ..., \
(2, 3): ..., (2, 4): ...}
#R(p,d) - EOL scenario fradion d per pallet type p - %
R = {(1, 3, 5): ..., (2, 3, 5): ...,\
(1, 4, 6): ..., (2, 4, 6): ...,\
(1, 3, 7): ..., (2, 3, 7): ...,\
(1, 3, 8): ..., (2, 3, 8): ..., (1, 4, 8): ..., (2, 4, 8): ...,\
(1, 3, 9):..., (2, 3, 9):...}
#FDC(p,n) - Failure fraction per pallet type p leaving DC - %
FDC = {(1, 3): ..., (1, 4): ..., \
(2, 3): ..., (2, 4): ...}
#FRC(p) - Failure fraction per pallet type p leaving RC - %
FRC = {(1, 3): ..., (1, 4): ..., \
(2, 3): ..., (2, 4): ...}
#Q(p,n,i) - Pre-positioned inventory of pallet type p of material n at DC i - #PAL
Q = {(1, 3, 'DC'): ..., (1, 4, 'DC'): ..., \
(2, 3, 'DC'): ..., (2, 4,'DC'): ...}
#L(i,j) - Distance travelled between nodes i and j - km
L = {('CPM', 'DC'): ... , ('DC', 'RET'): ... ,('DC', 'CPM'): ... ,('RET', 'RC'): ..., ('RC', 'CPM'): ... }
#DC(i,t) - Total demand at DC i at time t - #PAL
DC = {('DC', 1): ..., ('DC', 2): ..., ('DC', 3): ..., ('DC', 4): ..., ('DC', 5): ...}
#DB(i,j,t) - Total demand at retailer j from DC i at time t - #PAL
DB = {('RET', 1): ..., ('RET', 2): ..., ('RET', 3): ..., ('RET', 4): ..., ('RET', 5): ...}
#Making sets:
model = pe.ConcreteModel()
#Pallet types
model.P = pe.Set(initialize = P, ordered = False)
#Materials
model.N = pe.Set(initialize = N, ordered = False)
#EOL scenarios
model.D = pe.Set(initialize = D, ordered = False)
#Time Periods
model.T = pe.RangeSet(0, T)
#Nodes
model.V = pe.Set(initialize = V, ordered = False)
#Arcs
model.A = pe.Set(initialize = A, ordered = False)
model.AD = pe.Set(initialize = AD, ordered = False)
model.AS= pe.Set(initialize = AS, ordered = False)
#Decision Variables
model.Y = pe.Var(model.P, model.N, model.A, model.T, model.T, domain = pe.NonNegativeReals)
model.Inv = pe.Var(model.P, model.N, model.V, model.T, domain = pe.NonNegativeReals)
model.X = pe.Var(model.P, model.N, model.V, domain = pe.Binary)
#Objective funciton
obj_expr = sum(model.Y[p,n,i,j,t,t] * CO[p,n,i,j]+CT[p,n,i,j]*L[i,j]-S[p,n,i,j] for p in model.P for n in model.N for (i,j) in model.A for t in model.T if (t>0))model.obj=pe.Objective(expr=objexpr,sense=pe.minimize)
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Anne,
Your example is not reproducible. The \(\texttt{pe}\) object is not defined. Please adjust your code such that it is executable without your \(\texttt{pe}\) construct. It would be best, if you could use native Python data structures only in the reproducible example.
Best regards,
Jaromił0 -
You code is still not reproducible because values for, e.g., CO and CT, are missing.
What you could do to debug your code is to sequentially remove terms from
obj_expr = sum(model.Y[p,n,i,j,t,t] * CO[p,n,i,j]+CT[p,n,i,j]*L[i,j]
-S[p,n,i,j] for p in model.P for n in model.N for (i,j) in model.A
for t in model.T if (t>0))This means that you should remove expression C0 then try to run your code. If this does not work then remove CT and proceed. This way, you should be able to find the object responsible for the key error. Once you found the culprit, you can just print(...) it to see what the keys actually look like.
0
Post is closed for comments.
Comments
3 comments