pythonkeyerrorpulp

A python pulp problem about KeyError(0,0,0)


I tried to use pulp to create a decision variable x with 3 dimesions.Then I defined a objective function. However the pulp showed

P[j] * min(D[j], pl.lpSum(x[i, j, t] * Y[i, j] for i in fields)) - pl.lpSum(C[i, j, t] * x[i, j, t] for i in fields)
                          ~^^^^^^^^^
KeyError: (0, 0, 0)

And the error variable is x. The following is the whole code about pulp.

pb = pl.LpProblem("Maximize Cost", pl.LpMaximize)
x = pl.LpVariable.dicts("x", (fields, crops , times), cat=pl.LpBinary)
pb += pl.lpSum(
    P[j] * min(D[j], pl.lpSum(x[i, j, t] * Y[i, j] for i in fields)) - pl.lpSum(C[i, j, t] * x[i, j, t] for i in fields)
    for j in crops
    for t in times
)

And the 3 indices are defined by

fields = list(range(54))
crops = list(range(41))
times = list(range(6))

x(0,0,0)I tried to find if the set of x has (0,0,0), and the debugger showed it seems has (0,0,0) like the image.


Solution

  • Good news / Bad news...

    It is very easy to fix your indexing problem. I strongly recommend that you just pre-construct the list of tuples and use that to set the indices as shown below. Passing in the tuple of lists that you are doing isn't producing what you think it is. (Print the variable and see for yourself.)

    So try something like this:

    from pulp import pulp
    
    fields = [1, 2, 3]
    crops = ['corn', 'wheat']
    times = list(range(4))
    
    FCT = [(f, c, t) for f in fields for c in crops for t in times]
    
    plant = pulp.LpVariable.dicts('plant', indices=FCT, cat='Real')
    
    print(plant)
    
    print(plant[2, 'wheat', 0].varValue)  # None (no solve or initial value, but it proves the indexing)
    

    The bad news is that I see you are trying to use min() in your "linear" program and that is not a linear function and you must approach finding minimum values amongst variables in a different manner.