Skip to main content

Calback in C++ : list of arguments

Answered

Comments

10 comments

  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Could you please share a minimal reproducible example of your code describing the issue?

    Please also post the exact error you get when trying to execute your code.

    0
  • ALESSANDRA VIEIRA
    Gurobi-versary
    Curious
    Conversationalist

    Hallo Jaromil, 

     

    Please find below the instantiation of part of the callback, that will be applied at the solution node. After I commented Ecurrent expression, then the optimization runs, without running the callback.

     

    class subproblem1 : public GRBCallback {

    public:

       
        int *Pwind;
        int* t_D_f;
        GRBVar** Pbss;
        GRBVar** Pbssd;
        GRBQuadExpr** Ecurrent;

       
       
        subproblem1(int *xPwind,int *xt_D_f,GRBVar**xPbss,GRBVar**xPbssd){

           // numvars_sp1 = xnumvars;
            Pwind = xPwind;
            t_D_f = xt_D_f;
            Pbss = xPbss;
            Pbssd = xPbssd;
           //Ecurrent = xEcurrent;
        }


    protected:

        void callback() {

            try {
                if (where == GRB_CB_MIPSOL) {

                    // Query Zllower builds Zupper and compares its difference to epsilon tolerance

                    double zl = getDoubleInfo(GRB_CB_MIPSOL_OBJ);
                    int nodecnt = (int)getDoubleInfo(GRB_CB_MIPSOL_NODCNT);         // solution at node .
                    double obj = getDoubleInfo(GRB_CB_MIPSOL_OBJ);
                    int solcnt = getIntInfo(GRB_CB_MIPSOL_SOLCNT);
                    int Kb = 0;
                    int kf = 0;
                    Kb= T - 1;            //T is an extern const int variable

            //do something with the Gurobi expression to get the same Gurobi variables to count.
                            while (! Kb == 0) {
                                for (int bess = 0; bess < N_BESS; bess++) {

                                       p=Kb;

                                      while (! p==Kb-6){

                                             addCut(Ecurrent[bess][p] >= Ecurrent[bess][p-1]);

                                     p -=1;

                                    }

                                    kf = p;

                                  while (! kf==p+6){                               
                                    addCut(Ecurrent[bess][kf] .>= Ecurrent[bess][kf + 1]);
                                  kf +=1;
                                }

                                  Kb - = 1;
                            }
                        }
                    }

                }
            }   catch (GRBException e) {
                cout << "Error number: " << e.getErrorCode() << endl;
                cout << e.getMessage() << endl;
            }
            catch (...) {
                cout << "Error during callback" << endl;
            }

        }
    };

    Thanks in advance! 

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    What is the exact error you get when you run with your callback?

    0
  • ALESSANDRA VIEIRA
    Gurobi-versary
    Curious
    Conversationalist

    Hallo Jaromil,

     

    I think we mismatch answers and questions please find above the piece of my callback.

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    These conditions

    while (! Kb == 0) {
    ...
     while (! p==Kb-6){
    ...
    while (! kf==p+6){

    look strange. Did you mean

    while ( Kb != 0) {
    ...
    while (p != Kb-6){
    ...
    while (kf != p+6){

    or (equivalently)

    while (! (Kb == 0)) {
    ...
    while (! (p==Kb-6)){
    ...
    while (! (kf==p+6)){

    Your current code might be misunderstood as \(\texttt{(!Kb) == 0}\).

     

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Could you also point me to which arguments don't match according to the error you see?

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    The constructor

     subproblem1(int *xPwind,int *xt_D_f,GRBVar**xPbss,GRBVar**xPbssd)

    is missing an argument for \(\texttt{Ecurrent}\).

    0
  • ALESSANDRA VIEIRA
    Gurobi-versary
    Curious
    Conversationalist

    Hallo Jaromil,

    Yeah ... because if I join it then I get the error message about the list of arguments.

    "subproblem1 cb = subproblem1(P_wind,t_D_f,Pbss,Pbssd,Ecurrent);

    Severity    Code    Line    Description    Project    File
    Error (active)    E0289    1626    no instance of constructor "subproblem1::subproblem1" matches the argument list   "

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi Alessandra,

    The argument \(\texttt{GRBQuadExpr** Ecurrent}\) is missing in the constructor \(\texttt{subproblem1}\).

    Does the following work?

    class subproblem1 : public GRBCallback {

    public:

        int *Pwind;
        int* t_D_f;
        GRBVar** Pbss;
        GRBVar** Pbssd;
        GRBQuadExpr** Ecurrent;

      subproblem1(int *xPwind, int *xt_D_f,GRBVar **xPbss, GRBVar **xPbssd, GRBQuadExpr **xEcurrent){

           // numvars_sp1 = xnumvars;
            Pwind = xPwind;
            t_D_f = xt_D_f;
            Pbss = xPbss;
            Pbssd = xPbssd;
          Ecurrent = xEcurrent;
        }


    protected:
    ...

    Best regards, 
    Jaromił

    0
  • ALESSANDRA VIEIRA
    Gurobi-versary
    Curious
    Conversationalist

    I already had noted my mistake but Thanks a lot! 

     

    0

Please sign in to leave a comment.