0

I have an MILP model that I am trying to solve. I have a new constriant that I explained before on this question My new constraint is :

if: y[(i,j,k)]==1

then : y[(j,i,k+1)] ,y[(j,i,k+2)],y[(j,i,k+3)] ,y[(j,i,k+4)],y[(j,i,k+5)],y[(j,i,k+6)],y[(j,i,k+7)],y[(j,i,k+8)==0 .

I put this constraint in this way on my mode:

        mdl.add_constraints((y[(i,j,k)]+y[(j,i,k+1)] +y[(j,i,k+2)]+y[(j,i,k+3)]
       +y[(j,i,k+4)]+y[(j,i,k+5)]+y[(j,i,k+6)]+y[(j,i,k+7)]+y[(j,i,k+8)]  )<=1 for k in K4 for i in T for j in T ) 

But running my model with this new constriant makes my model to be solved very slow. Is there something wrong with my constraint or is there a way to change it in a way that my model can be solved faster?

Edit: When I put my condition in this way the run time is fast but the model does not respect the if then constraint in solutions . my code:

        for i in T:
            for j in T:
                if i!=j:
                    for k in K4:
                        mdl.add( mdl.if_then( y[(i,j,k)]==1 , y[(j,i,k+1)]==0))
                        mdl.add( mdl.if_then( y[(i,j,k)]==1 , y[(j,i,k+2)]==0))
                        mdl.add( mdl.if_then( y[(i,j,k)]==1 , y[(j,i,k+3)]==0))
                        mdl.add( mdl.if_then( y[(i,j,k)]==1 , y[(j,i,k+4)]==0))
                        mdl.add( mdl.if_then( y[(i,j,k)]==1 , y[(j,i,k+5)]==0))
                        mdl.add( mdl.if_then( y[(i,j,k)]==1 , y[(j,i,k+6)]==0))
                        mdl.add( mdl.if_then( y[(i,j,k)]==1 , y[(j,i,k+7)]==0))
                        mdl.add( mdl.if_then( y[(i,j,k)]==1 , y[(j,i,k+8)]==0))
       
Sana.Nz
  • 81
  • 11
  • CPLEX will not give a solution that does not respect *all* of the constraints. If your second formulation is giving wrong answers (it does not respect the constraints that you *think* that you added) then that second formulation is wrong. If it solves much faster than your 'correct' formulation, that is another big clue that your implementation of the constraints is wrong. Dump out your model from CPLEX as an LP file and look at what constraints are actually in the model, as there must be a flaw in your logic somewhere. – TimChippingtonDerrick Jul 14 '20 at 06:56
  • I know that my seconds formulation is wrong but I can't understand which part I'm doing wrong. – Sana.Nz Jul 14 '20 at 08:11
  • As Philippe says, dump out your model as an LP file. It is a quite readable format and you should see from that what variables and constraints are in your model and hence where the code that builds that model is going wrong. – TimChippingtonDerrick Jul 15 '20 at 09:18

1 Answers1

0

regarding the second formulation: do I understand well in that Cplex gives you a solution in which the if_then is violated, that is y[i,j,k] is 1 and any other y[j,i,k+..] is not 0 ? If so this is strange and deserves investigation. As Tim said, dump the LP using Model.export_as_lp() and we'll take a look.

In addition, I have two remarks on this alternate formulation:

  • your 'if constraint' in if_then is actually some variable being equal to 1, so this can be rewritten using indicator constraint.

  • Assuming your y variables are binary, then forcing all 8 of them to be zero can be done in one constraint, stating their sum is 0. Thus I would rewrite the inner statement in the alternative formulation as:

    mdl.add_indicator(y[i,j,k],
                      mdl.sum(y[j,i,k+d] for d in range(9)) ==0))
    

Which means that whenever the variable y[i,j,k] is equal to 1, then all y[j,i,k+d] variables are zero (assuming they are binary)

Unlike if_then, indicator constraints do not generate intermediate binary variables, so this formulation generates a smaller model. Again, if this formulation generate a solution which violates it, then forward us the LP file for investigation.

Philippe Couronne
  • 826
  • 1
  • 5
  • 6
  • Thank you Philippe. How can I share mu model.lp here? it hase lots of lines – Sana.Nz Jul 16 '20 at 09:22
  • Not possible here. Open an issue on https://community.ibm.com/community/user/datascience/, section decision optimization. You need to point out which constraint you think is violated, or better replicate on a smaller model. – Philippe Couronne Jul 16 '20 at 10:43