问题
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