Gurobi Python:如何在约束中编写嵌套总和

问题描述:

我有一个优化问题,我正在使用 Python 和 Gurobi 来优化它.在我的问题公式中,有一个具有嵌套总和的约束.

I have an optimization problem and I'm using Python and Gurobi to optimize it. In my problem formulation there is a constraint that has a nested sum.

约束

我最近开始学习 python,我在 gurobi 文档和示例代码中搜索,但找不到任何嵌套和的示例.

I've recently started learning python and I searched in gurobi documentation and example codes and I couldn't find any example of nested sum.

我想知道是否有人可以帮我解决这个问题.提前致谢!

I was wondering if anyone could help me solve this problem. Thanks in advance!

quicksum() 函数中使用两个 for 语句,在生成器表达式中使用两个 for 语句:

Use two for statements inside the quicksum() function and two for statements in the generator expression:

mycts = m.addConstrs((quicksum(x[i,f,p]*y[i,f,p,t]
                               for i in I for p in P[i,f]) <= z[f,t]
                               for f in F for t in T), "myconstraint")

其中 m 是您的 Model 对象.

where m is your Model object.