[英]How to establish conditions for indexes in pulp constraints
I am using PuLP in Python-3 to solve a MIP problem. 我正在Python-3中使用PuLP解决MIP问题。
I want to create a constraint that sums only the variables in which the index i is different from the index j, but I cannot find the right syntax to do it. 我想创建一个约束,仅对索引i与索引j不同的变量求和,但是我找不到正确的语法来做到这一点。
for j in Are:
for t in Per_fl:
prob += pulp.lpSum([X[i][j][t] for i != j in Are]) <= 1
The code above (for i != j in Are) does not work. 上面的代码(对于Are中的i!= j)无效。 Is there a way to build this constraint?
有没有办法建立这种约束?
Your problem is with how you are using list comprehension, you need to put the conditional bit last. 您的问题在于如何使用列表理解,您需要将条件位放在最后。
There are three indices: i, j, t
. 有三个索引:
i, j, t
。 Given the way you have laid out your for loops I'm assuming your want a constraint for each j
in Are
and each t
in Per_fl
. 鉴于你已经布局方式的for循环我假设你想为每一个约束
j
的Are
每个t
的Per_fl
。
I'm then assuming you want to sum over all of the i
indexes in Are
, excluding the one where i==j
. 然后,假设您要对
Are
所有i
索引求和,但不包括i==j
索引。 You would do this as follows: 您可以按照以下步骤进行操作:
for j in Are:
for t in Per_fl:
prob += pulp.lpSum([X[i][j][t] for i in Are if i != j]) <= 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.