Gurobi Python: how to write nested sum in a constraint

孤街浪徒 提交于 2019-11-30 17:05:00

问题


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.

constraint

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!


回答1:


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")

where m is your Model object.



来源:https://stackoverflow.com/questions/43806800/gurobi-python-how-to-write-nested-sum-in-a-constraint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!