Rafaela V
Rafaela V

Reputation: 187

How to establish conditions for indexes in pulp constraints

I am using PuLP in Python-3 to solve a MIP problem.

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.

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. Is there a way to build this constraint?

Upvotes: 0

Views: 506

Answers (1)

kabdulla
kabdulla

Reputation: 5429

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. 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.

I'm then assuming you want to sum over all of the i indexes in Are, excluding the one where 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

Upvotes: 1

Related Questions