メインコンテンツへスキップ

how to use the C api "GRBsetlogcallbackfuncenv" correctly

回答済み

コメント

4件のコメント

  • Riley Clement
    • Gurobi Staff

    Hi Qi,

    When I compile and run the following code (with v11 Gurobi):

    #include "gurobi_c.h"
    
    int callback(char *msg, void *logdata) {
        fprintf(stderr, "Callback: %s\n", msg);
        fflush(stderr);
        return 0;
    }
    
    int main() {
        int error = 0;
        GRBenv *env = NULL;
        GRBmodel *model = NULL;
    
        error = GRBemptyenv(&env);
        if (error) goto QUIT;
    
        error = GRBsetstrparam(env, "LogFile", "mip1.log");
        if (error) goto QUIT;
    
        error = GRBstartenv(env);
        if (error) goto QUIT;
    
        error = GRBsetlogcallbackfuncenv(env, callback, NULL);
        if (error) goto QUIT;
    
        GRBmsg(env, "---------------- Hello World ---------------- \n");
        
        error = GRBsetlogcallbackfuncenv(env, NULL, NULL);
        if (error) goto QUIT;
    
    QUIT:
        if (error) {
            printf( "ERROR: %s\n", GRBgeterrormsg(env));
            return 1;
        }
        GRBfreeenv(env);
        return 0;
    }

    I get the following output to console:

    Set parameter LicenseID to value xxxxxx
    Set parameter LogFile to value "mip1.log"
    ---------------- Hello World ---------------- 
    Callback: ---------------- Hello World ---------------- 

    You don't see similar output?

    - Riley

    0
  • ZHANG Qi
    • First Comment
    • First Question

    Thanks!
    Only when I add a ‘\n’ at the end of the string passed to GRBmsg, I can see the same output.

    0
  • Riley Clement
    • Gurobi Staff

    Ok, is your problem now solved then?  Or is there still an issue?

    Don't forget you will also need to use GRBsetlogcallbackfunc in a similar way so you can redirect output from the model.

    0
  • ZHANG Qi
    • First Comment
    • First Question

    yes,  after i add a ‘\n’, no problem anymore. Thanks again!

    0

サインインしてコメントを残してください。