Here you go:
import ilog.cplex.IloCplex;
import ilog.concert.IloException;
import ilog.concert.IloNumVar;
import ilog.concert.IloRange;
import java.util.Collection;
interface Solution {
public double getVariableValue(IloNumVar var) throws IloException;
}
interface Separator {
public Collection<IloRange> separate(Solution sol) throws IloException;
}
public class PublicLazyConstraintCallback extends IloCplex.LazyConstraintCallback implements Solution {
private Collection<Separator> separators = new java.util.Vector<Separator>();
public PublicLazyConstraintCallback(Collection<Separator> separators) {
this.separators.addAll(separators);
}
@Override
public double getVariableValue(IloNumVar var) throws IloException {
return getValue(var);
}
public void main() throws IloException {
for (Separator s : separators)
for (IloRange r : s.separate(this))
add(r);
}
}
A separator would have to implement the Separator interface. Then you create an instance of PublicLazyConstraintCallback, register the separators with it and finally register the callback with CPLEX.
------------------------------
Daniel Junglas
------------------------------
Original Message:
Sent: Mon June 22, 2020 02:25 PM
From: Pedro Henrique González Silva
Subject: Re: Multiple Lazy Constraints?
Hi Daniel, good afternoon.
Could you post a simple example of what you have proposed?
Thanks for the help.
------------------------------
Pedro Henrique González Silva
------------------------------
Original Message:
Sent: Mon June 22, 2020 02:25 PM
From: Daniel Junglas
Subject: Re: Multiple Lazy Constraints?
Like Paul said, this is intended behavior and is even explicitly stated in the reference documentation here.
When I am in your situation I usually do something slightly different than what Paul suggested:
- I create a subclass of the LazyConstraintCallback that has a public API that allows querying the current values and adding lazy constraints (basically provide public access to the protected functions getValues() and add()).
- I register an instance of this class with the IloCplex instance.
- In the callback's main() I invoke the separation algorithm of the three other classes, passing 'this' as argument.
This way it is pretty simple to write your callbacks in a way so that they can either be used individually or can be combined as a list.
If you want to do that and have trouble implementing it, I can dig out an example from my old code.
#DecisionOptimization