Create a variable with sparse index in pyomo

后端 未结 1 1601
谎友^
谎友^ 2020-12-22 06:52

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=         


        
相关标签:
1条回答
  • 2020-12-22 07:52

    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)
    
    0 讨论(0)
提交回复
热议问题