Gurobi Python:如何在约束中编写嵌套总和 [英] Gurobi Python: how to write nested sum in a constraint

查看:108
本文介绍了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.

这篇关于Gurobi Python:如何在约束中编写嵌套总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆