Key error-0
AnsweredI want to find an optimiser solution for financial resource allocation among 353 branches based on their performances.
Data are in a excel fime uploaded to Google drive. I am not sure after importing data to the data frame, I should define them one by one, or only the decision variable 'Fin' is enough.
Right now, I used the coefficient in objective function without defining them.
I received the 'KeyError: 0'. I would be grateful if you could share your opinion with me. Here is the code and the error text after that:
#Path to the Excel file in Google Drive
file_path = '/content/drive/My Drive/data.xlsx'
# Read data from the Excel file into a DataFrame
df = pd.read_excel(file_path) m = Model()
# I am not sure I should add all the variables from my table one by one here, or since the data frame loaded with my data, it is not necessary, I only added my decision variable 'Fin'
Fin = m.addVars(df['Fin'], name='x', vtype=GRB.CONTINUOUS)
# Number of fire departments
num_fire_departments = len(df) print("Number of Fire Departments:", num_fire_departments)
# Objective function, can I use exponencial (e^x) in the objective function?
objective_expr = quicksum( (df['Qi_PotenLoss'][i] * df['ai_VulnerCoeff'][i] * math.exp(df['Effici'][i] * Fin[i])) for i in range(num_fire_departments) ) m.setObjective(objective_expr, GRB.MINIMIZE)
System return:
Number of Fire Departments: 353
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-7-a159bed2d57e> in <cell line: 11>()
9
10 # Objective function
---> 11 objective_expr = quicksum(
12 (df['Qi_PotenLoss'][i] * df['ai_VulnerCoeff'][i] * math.exp(df['Effici'][i] * Fin[i]))
13 for i in range(num_fire_departments)
src/gurobipy/gurobi.pxi in gurobipy.quicksum()
<ipython-input-7-a159bed2d57e> in <genexpr>(.0)
10 # Objective function
11 objective_expr = quicksum(
---> 12 (df['Qi_PotenLoss'][i] * df['ai_VulnerCoeff'][i] * math.exp(df['Effici'][i] * Fin[i]))
13 for i in range(num_fire_departments)
14 )
KeyError: 0
-
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 Milad,
If we look at the error message then you know that the error is in this line:
df['Qi_PotenLoss'][i] * df['ai_VulnerCoeff'][i] * math.exp(df['Effici'][i] * Fin[i])) for i in range(num_fire_departments)
and it is a KeyError, with key = 0 (not a Gurobi error).
This means at least one of these values does not exist:
df['Qi_PotenLoss'][0]
df['ai_VulnerCoeff'][0]
df['Effici'][0]
Fin[0]It should be straightforward to discover which of these is causing an issue.
- Riley
0 -
Many thanks Raily!
Do you think I should define all the variable the I define the 'Fin' to resolve it?0 -
Hi Milad,
I'm afraid I have no intuition on how to resolve it without knowing which of the values caused an error, and why.
- Riley
0 -
Thank you very much Reily!
I printed the values and all are imported and printed, the names are same as what I used in the objective function.Would you please take look at it?
Also I need your advice on using Exp (exponencial) command in objective function.
Here are the printed data:
FDCode Fin Qi_PotenLoss qi_ActualLoss ai_VulnerCoeff Effici
0 FD101 245887.76 1.047386e+12 206115 2.141628e-07 0.34406
1 FD102 235504.29 5.361513e+11 5764620 1.170838e-05 0.36189
2 FD104 215294.51 6.495308e+11 2600340 4.355603e-06 0.39163
3 FD105 228721.26 8.127873e+11 2393325 3.160836e-06 0.30984
4 FD106 372830.97 1.099356e+12 8465055 8.469634e-06 0.25552
Index(['FDCode', 'Fin', 'Qi_PotenLoss', 'qi_ActualLoss', 'ai_VulnerCoeff',
'Effici'],The obective function (which also has the keyerror-0) is:
objective_expr = quicksum( (df['Qi_PotenLoss'][i] * df['ai_VulnerCoeff'][i] * math.exp(df['Effici'][i] * Fin[i])) for i in range(num_fire_departments) ) m.setObjective(objective_expr, GRB.MINIMIZE)
dtype='object')Apologies for the typing format I am using my mobile.
Best, Milad
0 -
Hi Milad,
Let's address the key error and then we can look at the question of exponentials in the objective function.
I can see what it going wrong here. When you declare your Fin variables:
Fin = m.addVars(df['Fin'], name='x', vtype=GRB.CONTINUOUS)
the index of these variables is the "Fin" values in your dataframe, i.e. "FD101", "FD102", ...
These are the keys in the Fin tupledict (our special Gurobi dictionary). So Fin[0] will cause an error because 0 is not a value in the "Fin" column of your dataframe. A workaround is to use Fin[df['Fin'][i]] instead of Fin[i], but now we are not being very clever with our formulation.
We also will run into a problem where we cannot simply use a Gurobi object, such as a variable or linear expression, as an argument to math.exp function. Could you please review our documentatino for the addGenConstrExp function, perhaps experiment with it on a simple dummy model, and then we can take it from there.
Cheers,
Riley0
Post is closed for comments.
Comments
6 comments