Adding lazy constraint in python-Gurobi interface

995 views
Skip to first unread message

sina.fa...@gmail.com

unread,
Sep 28, 2016, 4:00:07 PM9/28/16
to Gurobi Optimization
Hi,

I have used Python-Gurobi for a while but this is my first time that I want to use callback function to add lazy constraints. 

I am trying to add some lazy constraints to the first stage of a stochastic programming problem. For example, the optimal solution shows me that locations 16 and 20 are chosen together which I don't want to so I want to add a lazy constraint as below. But after running this function, locations 16 and 20 are still in the optimal solution. Could you please let me know how should I attack this issue?

   First Stage
       x1 + x2 + ... + x40 = 5
       z_i,l <= x_i   i=1,..,40  and l=1,2
   Second Stage
       ....

   def mycallback(model,where):
       if where == GRB.Callback.MIPSOL:
          sol = model.cbGetSolution([model._vars[s] for s in range(1,40)])
          if sol[16] + sol[20] == 2:
             Exp = LinExpr([(1,model._vars[16]),(1,model._vars[20])])
             model.cbLazy(Exp <= 1)

  model._vars = x
  model.optimize(mycallback)

Renan Garcia

unread,
Sep 28, 2016, 4:03:56 PM9/28/16
to gur...@googlegroups.com
Couple of things:

1) sol[16] + sol[20] == 2 is a dangerous check because these are floating point values. For example, 0.999999999 + 1 != 2. Are you sure the cut is being added?


--

---
You received this message because you are subscribed to the Google Groups "Gurobi Optimization" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gurobi+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

sina.fa...@gmail.com

unread,
Sep 28, 2016, 5:03:58 PM9/28/16
to Gurobi Optimization
I can change the condition to greater than 1 and yes, I am running "m.params.LazyConstraints = 1" to the model as well but it is not still working.

Renan Garcia

unread,
Sep 28, 2016, 5:54:29 PM9/28/16
to gur...@googlegroups.com
Note that Python lists are indexed from 0 to n-1, and iterating over range(1,40) will start at the second element in model._vars instead of the first. Therefore, sol[16] and sol[20] represent the solution values for model._vars[17] and model._vars[21], respectively. As a result, your cut is expressed with different variables than those used in the condition.
Reply all
Reply to author
Forward
0 new messages