Skip to main content

Key error-0

Answered

Comments

5 comments

  • Riley Clement
    • Gurobi Staff

    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
  • Milad K.E.
    • Gurobi-versary
    • First Comment
    • First Question

    Many thanks Raily!
    Do you think I should define all the variable the I define the 'Fin' to resolve it?

    0
  • Riley Clement
    • Gurobi Staff

    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
  • Milad K.E.
    • Gurobi-versary
    • First Comment
    • First Question

    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
  • Riley Clement
    • Gurobi Staff

    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,
    Riley

     

    0

Please sign in to leave a comment.