Pyomo sum inside sum with various index

孤人 提交于 2019-12-25 03:14:48

问题


I would like to do this:

but I don't know exacly how, this is what I've tried:

def R2(model, da, gr, cu):
    return sum(sum(model.Dap[asi, pe] for asi in model.A[asi, gr, cu]) for pe in model.Pd[pe, da]) <= 1
model.R2 = Constraint(model.DA, model.GR, model.CU, rule=R2)

回答1:


I am not 100% I understand your mathematical formulation.

Based on the way you write the constraint, it seems you have multiple Indexed Sets (i.e. sets which are themselves indexed over other sets), which is often a symptom that the formulation is a bit clunky, although it is sometimes necessary.

If what you are trying to express is a system of constraints, i.e. one constraint for each element of DA, GR, CU, and the sets Pd and A are indexed over DA, GR, CU:

Then you should write

def R2(model, d, g, c):
    return sum(sum(model.Dap[p] for a in model.A[g, c]) 
               for p in model.Pd[d]) <= 1

model.R2 = Constraint(model.DA, model.GR, model.CU, rule=R2)

Otherwise, you should try to make the formulation more clear.



来源:https://stackoverflow.com/questions/53366334/pyomo-sum-inside-sum-with-various-index

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