Search code examples
pythonpyomo

Create a variable with sparse index in pyomo


I need help to create a variable with sparse indices. I have something like this:

model.K = Set()
model.P = Set()
model.KP = Param(model.K, model.P, default=0)

I will load a CSV file for model.KP with value KP==1 for the combinations of K & P.

model.X = Var(model.K, model.P)

I want to create this variable only for the combinations of K and P in the model.KP because when I create the variable with all the combinations of K and P, it is producing 37 million indices with the sets I give and this is creating memory issues.


Solution

  • Make a Set containing tuples (k,p) and use it as the set that defines both your variable and your parameter.

    Define your set elements:

    kp = []
    for k in model.K:
        for p in model.P:
            foo_tuple = (k, p)
            kp.append(foo_tuple)
    

    Note: Since you will use a CSV file to load your data, populationg kp with all K and P combinations can also be done at this time.

    Then create a Set using elements in kp:

    model.S = Set(initialize=kp)
    

    I recommend not using default values in your model.KP parameter if you don't need it. Doing so will notify you of a missing value for an element where it should have one. But let's say that you still want to have all values of parameter model.PK to be 0 when no value was provided for tuple (p,k) and continue using default values, you should define your parameter like so:

    model.KP = Param(model.S, default=0)
    

    Then, defining your variable will be:

    model.X = Var(model.S)