具有各种索引的总和内的 Pyomo sum [英] Pyomo sum inside sum with various index
问题描述
我想这样做:
但我不知道具体如何,这是我尝试过的:
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)
推荐答案
我不是 100% 理解你的数学公式.
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.
如果你想表达的是一个约束系统,即DA
、GR
、CU
的每个元素一个约束,并且集合 Pd
和 A
在 DA
、GR
、CU
上被索引:
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
:
那你应该写
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.
这篇关于具有各种索引的总和内的 Pyomo sum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!