gpt4 book ai didi

python - 古罗比 python : how to write nested sum in a constraint

转载 作者:太空狗 更新时间:2023-10-30 00:53:46 27 4
gpt4 key购买 nike

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

constraint

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

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

最佳答案

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

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 对象。

关于python - 古罗比 python : how to write nested sum in a constraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43806800/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com