flow conservation constraint

70 views
Skip to first unread message

Ahmed Al-Quzweeni

unread,
Jun 5, 2022, 10:42:52 AM6/5/22
to Python-MIP
Hello,
I'm using CPLEX in writing my models and currently have a plan to transfer to PYTHON MIP
However, I'm struggling in writing the following constraints in python

any help please,

constraints.png

thanks

Luciano Rigolin de Almeida

unread,
Jun 6, 2022, 3:26:15 AM6/6/22
to Ahmed Al-Quzweeni, Python-MIP
Hi.

Does your lambda variable have 2 or 4 dimensions?

Not taking that information into account, I would do something like this:

for s in S:
    for d in D:
        for i in N:
            if s != d and i != j:
                if i = s:
                    m.add_constr(xsum(l[s][d][i][j] - l[s][d][j][i] for j in N) - l[s][d][?][?] == 0)
                else
                    if i = d:
                        m.add_constr(xsum(l[s][d][i][j] - l[s][d][j][i] for j in N) + l[s][d][?][?] == 0)
                    else:
                        m.add_constr(xsum(l[s][d][i][j] - l[s][d][j][i] for j in N) == 0)

Att,

Luciano

--
You received this message because you are subscribed to the Google Groups "Python-MIP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-mip+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python-mip/41b3d279-1315-4268-8742-c975a584d8dbn%40googlegroups.com.

Ahmed Al-Quzweeni

unread,
Jun 6, 2022, 3:18:25 PM6/6/22
to Python-MIP
Hi Luciano,
thanks for your reply.
lambda variable has 4 dimensions.

based on your suggestion I did the following:

for s in N:
      for d in N:
          if s != d :
              for i in N:
                   if i == s:
                        model += xsum(Lsdij[s][d][i][j] for j in N if i != j) - xsum(Lsdij[s][d][j][i] for j in N if i != j) == Lsd[s][d]
                   if i == d:
                        model += xsum(Lsdij[s][d][i][j] for j in N if i != j) - xsum(Lsdij[s][d][j][i] for j in N if i != j) == -Lsd[s][d]
                   else:
                        model += xsum(Lsdij[s][d][i][j] for j in N if i != j) - xsum(Lsdij[s][d][j][i] for j in N if i != j) == 0

I got:
<OptimizationStatus.INFEASIBLE: 1>

and model.objective_value gave None

any suggestion ?

kind regards

Luciano Rigolin de Almeida

unread,
Jun 7, 2022, 3:36:49 AM6/7/22
to Ahmed Al-Quzweeni, Python-MIP
Hello.

An else is not missing before: if i == d:? If i == s, two constraints are being added. The constraint of if i == s and the constraint of else.

for s in N:
    for d in N:
        if s != d :
            for i in N:
                if i == s:
                    model += xsum(Lsdij[s][d][i][j] for j in N if i != j) - xsum(Lsdij[s][d][j][i] for j in N if i != j) == Lsd[s][d]
                else

                    if i == d:
                        model += xsum(Lsdij[s][d][i][j] for j in N if i != j) - xsum(Lsdij[s][d][j][i] for j in N if i != j) == -Lsd[s][d]
                    else:
                        model += xsum(Lsdij[s][d][i][j] for j in N if i != j) - xsum(Lsdij[s][d][j][i] for j in N if i != j) == 0

Att,

Luciano

Ahmed Al-Quzweeni

unread,
Jun 7, 2022, 3:29:29 PM6/7/22
to Python-MIP
Dear Luciano,
I really appreciate the time you have spent reading my message and writing yours. Thank you very much.

You right !  I have added "elif" and it worked and I got the same results as IBM CPLEX.

Thank you very much

Ahmed.

Luciano Rigolin de Almeida

unread,
Jun 8, 2022, 3:38:23 AM6/8/22
to Ahmed Al-Quzweeni, Python-MIP
Ahmed, that's what discussion groups are for, for people to help other people with their doubts and for everyone to evolve.

Was the performance with MIP-Python better or worse than IBM CPLEX?

I also suggest that you try using MIP-Python with the Gurobi solver (you can request a free academic license). I had better performances with Gurobi when compared to the native MIP-Python solver.

Att,

Luciano

Ahmed Al-Quzweeni

unread,
Jun 8, 2022, 3:55:07 PM6/8/22
to Python-MIP
Dear Luciano,
Thank you very much for your message.
I have tested MIP-Python on small models with few numbers of variable. It is faster than IBM-CPLEX.
Thank for your suggestion, I will try Gurobi solver and get back to you with the results.

thank you very much for your support.

kindly regards

Ahmed.

Ahmed Al-Quzweeni

unread,
Jun 9, 2022, 4:01:35 PM6/9/22
to Python-MIP
Dear Luciano,
I have tried GUROBI solver. and requested an academic license. I have got better performance than CBC and IBM CPLEX. 
Really thank you for your support.

I have one last question in my mind regarding GUROBI solver. Are there any limitation regarding the academic license such as the maximum number of variables that the solver can handle or maximum matrix size?

thank you very much.

regards,

Ahmed

On Wednesday, 8 June 2022 at 10:38:23 UTC+3 luciano...@gmail.com wrote:

Luciano Rigolin de Almeida

unread,
Jun 14, 2022, 4:06:40 AM6/14/22
to Ahmed Al-Quzweeni, Python-MIP
Hello Ahmed. 

As far as I know, Gurobi academic license is unlimited. I used it in my master's thesis. I had some models with a large number of variables and constraints. It ran smoothly.

Att,

Luciano

Reply all
Reply to author
Forward
0 new messages