Keyerror
AnsweredHi,
I am importing the data in my python code using excel sheet. It has been working alright until I changed some values in the excel. Everything seems to be defined and looks correct to me. I keep getting the following KeyError: (1, 'SANDY', 'PORTLAND')
I am getting the error in this line particularly:
Obj1 = gp.quicksum((t_g[p,i,j]*y_kg[p,i,j,k] + t_a[p,i,j]*y_ka[p,i,j,k]) for p in P for i in I for j in J for k in K)
Here is the excerpt of the code which uses this line:
from gurobipy import *
import os
import gurobipy as gp
from gurobipy import GRB
import numpy as np
import pandas as pd
import random
import matplotlib.pyplot as plt
ExcelFile = 'C:/Users/Admin/Desktop/Final_Code/RealTime_FinalData.xlsx'
m = gp.Model()
# Create sets and parameters
K = ["FOOD", "WATER", "MEDICINE"]
I = ["SANDY", "OREGON CITY","ROSENBURG","MADRAS","BEND"]
J = ["BEAVERTON", "PORTLAND", "ASTORIA","MCMINNVILLE","SALEM","CORVALLIS","ALBANY","EUGENE","MEDFORD"]
N = ["GROUND", "AIR"]
P = [1,2,3]
file = pd.read_excel(ExcelFile, sheet_name=0)
C = file.set_index('DC')['Cost'].to_dict()
file = pd.read_excel(ExcelFile, sheet_name=1)
C_g = file.set_index(['DC','Node'])['C_g'].to_dict()
file = pd.read_excel(ExcelFile, sheet_name=2)
C_a = file.set_index(['DC','Node'])['C_a'].to_dict()
file = pd.read_excel(ExcelFile, sheet_name=3)
t_g = file.set_index(['P','DC','Node'])['t_g'].to_dict()
file = pd.read_excel(ExcelFile, sheet_name=4)
t_a = file.set_index(['P','DC','Node'])['t_a'].to_dict()
file = pd.read_excel(ExcelFile, sheet_name=5)
d_j = file.set_index(['P','Node','Commodity'])['d_j'].to_dict()
file = pd.read_excel(ExcelFile, sheet_name=6)
L = file.set_index(['DC'])['L'].to_dict()
file = pd.read_excel(ExcelFile, sheet_name=7)
w = file.set_index('Commodity')['w'].to_dict()
file = pd.read_excel(ExcelFile, sheet_name=8)
s_k = file.set_index(['P','DC','Commodity'])['s_k'].to_dict()
# decision variables
x = m.addVars(I, vtype=GRB.BINARY, name="x")
y_kg = m.addVars(P,I, J, K, vtype=GRB.BINARY, name="y_kg")
y_ka = m.addVars(P,I, J, K, vtype=GRB.BINARY, name="y_ka")
z_kg = m.addVars(P,I, J, K, vtype=GRB.INTEGER, name="z_kg")
z_ka = m.addVars(P,I, J, K, vtype=GRB.INTEGER, name="z_ka")
u_d = m.addVars(P,J, K, vtype=GRB.INTEGER, name="u_d")
r_jk = m.addVars(P,J, K, vtype=GRB.CONTINUOUS, name="r_jk")
r_k = m.addVars(P,K, vtype=GRB.CONTINUOUS, name="r_k")
V_g = 18
V_a = 10
L_g = 2500
L_a = 800
G = 80000000
M = 10000000
# # set objective function
Obj1 = gp.quicksum((t_g[p,i,j]*y_kg[p,i,j,k] + t_a[p,i,j]*y_ka[p,i,j,k]) for p in P for i in I for j in J for k in K)
Obj2 = gp.quicksum(u_d[p,j,k] for p in P for j in J for k in K)
This is the snip of the excel data from which the code is extracting the data:

Please let me know if you can spot the mistake here. It would be really helpful.
Thanks
Purnima
-
Official comment
This post is more than two 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 Purnima,
I don't see any errors in your code.
The KeyError is raised because you are attempting to access a key that is not present in a dictionary. Please crosscheck that the data is read from the correct Excel file and verify that the dictionaries t_g and t_a are constructed as expected.
Regards,
Simran
0
Post is closed for comments.
Comments
2 comments