Create a variable with sparse index in pyomo

空扰寡人 提交于 2020-01-06 08:50:00

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/54926298/create-a-variable-with-sparse-index-in-pyomo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!