Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Nested Callbacks ILOG Concert Technology 12.5.0.0

    Posted 04/02/15 11:39 AM

    Originally posted by: DianaHuerta


    Hi!, 

    I have doubt. Is there a possibility to call a callback inside of another callback/lazy const?. The thing is that I need one part of the code (which involves some variables of the problem) in both cases (callbacks & lazy constraints). The code in both APIs is the same, so I just want to create a function where I can keep the code and then call it every time a callback/lazy is activated.

    Any idea?,

    Best Regards.

    Diana 


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Nested Callbacks ILOG Concert Technology 12.5.0.0

    Posted 04/02/15 03:41 PM

    Why not put the common code in an external function/method (pass it as arguments any class variables it would need to access) and call that function/method from both callbacks?


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Nested Callbacks ILOG Concert Technology 12.5.0.0

    Posted 04/06/15 11:41 AM

    Originally posted by: DianaHuerta


    Thanks for your answer, Paul.

    I have done it, but the thing is that there is still part of the code that is duplicated because some functionality belongs to callbacks/lazy, ex. getValue(X) function. For example, if I need to fill a double **Matrix with values of my X decision variable during a branch and cut process, I need to repeat the same part of the code inside of a my lazyconstraint  (for integer solutions) and my callback (for fractional solutions). 

    If I type my code inside an external function I couldn't use the "getValue" method to copy values from my decision variable, could I?

    I would like to know if there is a way to reduce it doing that in just one function. 

    Regards,

    Diana.


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Nested Callbacks ILOG Concert Technology 12.5.0.0

    Posted 04/06/15 12:54 PM

    There are easy ways to do what you want but they depend on the programming language you are using. So are you using C++ or Java?


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Nested Callbacks ILOG Concert Technology 12.5.0.0

    Posted 04/06/15 01:05 PM

    Originally posted by: DianaHuerta


    I'm using C++, 

    Thanks, Daniel.

    Diana


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Nested Callbacks ILOG Concert Technology 12.5.0.0

    Posted 04/06/15 01:56 PM

    The idea is to not call getValue(X) directly. Instead call it indirectly.

    One way to do that is to create a pure virtual class (an "interface") and let your callback classes inherit from that:

    struct GetValue {
        virtual ~GetValue() {}
        virtual double get(IloNumVar x) = 0;
    };
    
    struct Callback1 : public IloCplex::LazyConstraintCallbackI, GetValue {
        double get(IloNumVar x) { return getValue(x); }
    };
    struct Callback2 : public IloCplex::UserCutCallbackI, GetValue {
        double get(IloNumVar x) { return getValue(x); }
    };
    

    Now you change your common code

    void commonFunction(...) {
        callback->getValue(x);
    }
    

    to something like this

    void commonCode(..., GetValue *getValue) {
        getValue->get(x);
    }
    

    That is, instead of calling callback->getValue() directly in your common code, you pass an instance of GetValue to your common code and get the value through that instance. From your callback's main function you can then just call commonCode(..., this).

    I hope you get the idea?

    Using templates and parametrized inheritance you can write even cleaner code. Something like this:

    template<typename T>
    struct CallbackBase : public T {
        void commonCode(...) {
            getValue(x);
        }
    };
    class Callback1 : public CallbackBase<IloCplex::LazyConstraintCallbackI> {
        void main() { commonCode(...); }
    };
    class Callback2 : public CallbackBase<IloCplex::UserCutCallbackI> {
        void main() { commonCode(...); }
    };
    

    Here the idea is to put the common code into a common base class of your callback. Using parameterized inheritance this common base class can either inherit from the lazy constraint callback or the user cut callback (or any other CPLEX callback that provides a getValue() function). Your actual callback functions then inherit from the common base class and can directly invoke the commonCode() function without having to pass additional arguments.

    If you are sufficiently familiar with templates I would use the second variant as that leads to less obscure source code. The first approach is something that would work in Java as well.


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Nested Callbacks ILOG Concert Technology 12.5.0.0

    Posted 04/06/15 02:07 PM

    Originally posted by: DianaHuerta


    Yes!, I got it!. That kind of idea is the one I was looking for.
     
    =)
     
    Thank you very much, I think it will be so useful for me.
     
    Best Regards.
     
    Diana. 

    #CPLEXOptimizers
    #DecisionOptimization