Implementing lazy constrain using constraint attribute Lazy

592 views
Skip to first unread message

Fa Kho

unread,
Apr 19, 2017, 10:27:14 AM4/19/17
to Gurobi Optimization

I am using Java, Gurobi and I would like to implement some constraints as lazy. As written in the FAQ in the website "There are two ways to implement lazy constraints for a MIP model. If the constraints can be enumerated up front, simply set the Lazy attribute on the constraints that should be treated as lazy. If the constraints must be generated during the MIP search, you need to write a callback function."

1) I have tried both methods. using the first method I get this error "Error code: 20001. null" which means "Tried to use a constraint or variable that is not in the model, either because it was removed or because it has not yet been added." I have also made sure that I have set LazyConstraints to 1. (model.getEnv().set(GRB.IntParam.LazyConstraints,1);). 
How can I fix this error? aren't lazy constraints supposed to come late?

one example: 

  I define expr and expr2 and then:
   
                        GRBConstr lazy = model.addConstr(expr,GRB.LESS_EQUAL,expr2);
  lazy.set(GRB.IntAttr.Lazy, 2);

2) Using the second method (callback), only one set of the lazy constraints (the first one) is read and the rest of them are ignore. Why is it like that? do I need to change any option to make the rest of the lazy constraints readable? 


Thanks in advance!
Fa


Sonja Mars

unread,
Apr 20, 2017, 4:07:42 AM4/20/17
to gur...@googlegroups.com
Hi Fa,

What is your Gurobi version?
>
> I define expr and expr2 and then:
>
> GRBConstr lazy = model.addConstr(expr,GRB.LESS_EQUAL,expr2);
> lazy.set(GRB.IntAttr.Lazy, 2);
I just tried this and it is working fine for me with Gurobi 7.0.2.

If you are using an older Gurobi version, you might have to call model.update() before referring to constraint objects and changing attributes. Another option would be to set the parameter UpdateMode to 1: http://www.gurobi.com/documentation/7.0/refman/updatemode.html#parameter:UpdateMode


> 2) Using the second method (callback), only one set of the lazy constraints (the first one) is read and the rest of them are ignore. Why is it like that? do I need to change any option to make the rest of the lazy constraints readable?
What do you mean by "one set of lazy constraints"? In your callback you will have add all the violated constraints all the time. This can mean that you have to add one lazy constraint more than once. Details can be found here: http://www.gurobi.com/documentation/7.0/refman/java_grbcallback_addlazy.html#javamethod:GRBCallback.addLazy

Best regards,
Sonja


----
Dr. Sonja Mars
Gurobi Optimization - Technical Support



Fa Kho

unread,
Apr 20, 2017, 11:20:26 AM4/20/17
to Gurobi Optimization
Hi Sonja, 

Thanks for your reply and checking. 

I have version 6.5.2. For some reasons I do not want to upgrade to the newer version. 

I made sure that I update the model, perhaps it is a version problem. 

About the second part, you are right. I don't want to add the violated constraints as lazy. I want to add valid inequalities as lazy, they are not violated constraints. I use general MIP callback where GRB.CB_MIP is active. Perhaps the first method is better to use in my case. I try to get it run. 


Best regards,
Fa

Sonja Mars

unread,
Apr 21, 2017, 5:10:32 AM4/21/17
to gur...@googlegroups.com
Hi Fa,

I have version 6.5.2. For some reasons I do not want to upgrade to the newer version. 

I made sure that I update the model, perhaps it is a version problem. 
I don't think so, I just tested version 6.5.2 and it is working fine for me. Can you post your code? If you don't want to post it, can you send your code to support[at]gurobi[dot]com.

About the second part, you are right. I don't want to add the violated constraints as lazy. I want to add valid inequalities as lazy, they are not violated constraints. I use general MIP callback where GRB.CB_MIP is active. Perhaps the first method is better to use in my case. I try to get it run. 
So these are user cuts and no lazy constraints? Lazy constraints are really designed for cutting of feasible solutions. And you should add them in GRB.Callback.MIPSOL and GRB.Callback.MIPNODE.

For adding user cuts you need a different routine and you can only add them in GRB.Callback.MIPNODE. Please see here for more details: http://www.gurobi.com/documentation/7.0/refman/py_model_cbcut.html

Fa Kho

unread,
Apr 21, 2017, 7:39:06 AM4/21/17
to Gurobi Optimization
Thank you very much! 
Yes they should be defined as user defined cuts. I will try it. 

Best regards,
Fa
Reply all
Reply to author
Forward
0 new messages