Example #1
0
void GenModelCplex::AttachCallback(bool(*callbackFunction) (double obj, double bestBound, bool feasibleSolution))
{
    CplexData* d = (CplexData*)solverdata;
    d->callbackFunction = callbackFunction;

    CPXsetinfocallbackfunc(d->env, SetInfoCallbackFunc, d);
}
Example #2
0
int
main (int argc, char *argv[])
{
    int     uselogcallback = 0;
    LOGINFO myloginfo;

    int         usetimelimcallback = 0;
    TIMELIMINFO mytimeliminfo;

    int          useterminate = 0;
    volatile int terminator;

    CPXENVptr env = NULL;
    CPXLPptr  lp = NULL;
    int       solstat;
    int       status = 0;

    /* Check the command line arguments */

    if (( argc != 3 )                                         ||
            ( strchr ("lta", argv[2][0]) == NULL )  ) {
        usage (argv[0]);
        goto TERMINATE;
    }

    switch (argv[2][0]) {
    case 'l':
        uselogcallback = 1;
        break;
    case 't':
        usetimelimcallback = 1;
        break;
    case 'a':
        useterminate = 1;
        break;
    default:
        break;
    }

    /* Initialize the CPLEX environment */

    env = CPXopenCPLEX (&status);

    /* If an error occurs, the status value indicates the reason for
       failure.  A call to CPXgeterrorstring will produce the text of
       the error message.  Note that CPXopenCPLEX produces no output,
       so the only way to see the cause of the error is to use
       CPXgeterrorstring.  For other CPLEX routines, the errors will
       be seen if the CPXPARAM_ScreenOutput indicator is set to CPX_ON.  */

    if ( env == NULL ) {
        char  errmsg[CPXMESSAGEBUFSIZE];
        fprintf (stderr, "Could not open CPLEX environment.\n");
        CPXgeterrorstring (env, status, errmsg);
        fprintf (stderr, "%s", errmsg);
        goto TERMINATE;
    }

    /* Turn on output to the screen */

    status = CPXsetintparam (env, CPXPARAM_ScreenOutput, CPX_ON);
    if ( status ) {
        fprintf (stderr,
                 "Failure to turn on screen indicator, error %d.\n", status);
        goto TERMINATE;
    }

    /* Create the problem, using the filename as the problem name */

    lp = CPXcreateprob (env, &status, argv[1]);

    /* A returned pointer of NULL may mean that not enough memory
       was available or there was some other problem.  In the case of
       failure, an error message will have been written to the error
       channel from inside CPLEX.  In this example, the setting of
       the parameter CPXPARAM_ScreenOutput causes the error message to
       appear on stdout.  Note that most CPLEX routines return
       an error code to indicate the reason for failure.   */

    if ( lp == NULL ) {
        fprintf (stderr, "Failed to create LP.\n");
        goto TERMINATE;
    }

    /* Now read the file, and copy the data into the created lp */

    status = CPXreadcopyprob (env, lp, argv[1], NULL);
    if ( status ) {
        fprintf (stderr, "Failed to read and copy the problem data.\n");
        goto TERMINATE;
    }

    if ( usetimelimcallback ) {
        double t;
        status = CPXgettime (env, &t);
        if ( status ) {
            fprintf (stderr, "Failed to initialize timer.\n");
            goto TERMINATE;
        }
        mytimeliminfo.acceptablegap = 10.0;
        mytimeliminfo.aborted       = 0;
        mytimeliminfo.timestart     = t;
        mytimeliminfo.timelim       = 1.0;

        status = CPXsetinfocallbackfunc (env, timelimcallback, &mytimeliminfo);
        if ( status ) {
            fprintf (stderr, "Failed to set time limit callback function.\n");
            goto TERMINATE;
        }
    }
    else if ( uselogcallback ) {
        /* Set overall node limit in case callback conditions are not met */
        status = CPXsetintparam (env, CPXPARAM_MIP_Limits_Nodes, 5000);
        if ( status ) goto TERMINATE;

        status = CPXgettime (env, &myloginfo.timestart);
        if ( status ) {
            fprintf (stderr, "Failed to query time.\n");
            goto TERMINATE;
        }
        status = CPXgetdettime (env, &myloginfo.dettimestart);
        if ( status ) {
            fprintf (stderr, "Failed to query deterministic time.\n");
            goto TERMINATE;
        }
        myloginfo.numcols       = CPXgetnumcols (env, lp);
        myloginfo.lastincumbent = CPXgetobjsen (env, lp) * 1e+35;
        myloginfo.lastlog       = -10000;
        status = CPXsetinfocallbackfunc (env, logcallback, &myloginfo);
        if ( status ) {
            fprintf (stderr, "Failed to set logging callback function.\n");
            goto TERMINATE;
        }
        /* Turn off CPLEX logging */
        status = CPXsetintparam (env, CPXPARAM_MIP_Display, 0);
        if ( status )  goto TERMINATE;
    }
    else if ( useterminate) {
        status = CPXsetterminate (env, &terminator);
        if ( status ) {
            fprintf (stderr, "Failed to set terminator.\n");
            goto TERMINATE;
        }
        /* Typically, you would pass the terminator variable to
           another thread or pass it to an interrupt handler,
           and  monitor for some event to occur.  When it does,
           set terminator to a non-zero value.

           To illustrate its use without creating a thread or
           an interrupt handler, terminate immediately by setting
           terminator before the solve.
        */
        terminator = 1;
    }

    /* Optimize the problem and obtain solution. */

    status = CPXmipopt (env, lp);

    if ( status ) {
        fprintf (stderr, "Failed to optimize MIP.\n");
        goto TERMINATE;
    }

    solstat = CPXgetstat (env, lp);
    printf ("Solution status %d.\n", solstat);

TERMINATE:

    /* Free up the problem as allocated by CPXcreateprob, if necessary */

    if ( lp != NULL ) {
        int xstatus = CPXfreeprob (env, &lp);
        if ( xstatus ) {
            fprintf (stderr, "CPXfreeprob failed, error code %d.\n", xstatus);
            status = xstatus;
        }
    }

    /* Free up the CPLEX environment, if necessary */

    if ( env != NULL ) {
        int xstatus = CPXcloseCPLEX (&env);

        /* Note that CPXcloseCPLEX produces no output,
           so the only way to see the cause of the error is to use
           CPXgeterrorstring.  For other CPLEX routines, the errors will
           be seen if the CPXPARAM_ScreenOutput indicator is set to CPX_ON. */

        if ( status ) {
            char  errmsg[CPXMESSAGEBUFSIZE];
            fprintf (stderr, "Could not close CPLEX environment.\n");
            CPXgeterrorstring (env, status, errmsg);
            fprintf (stderr, "%s", errmsg);
            status = xstatus;
        }
    }

    return (status);

}  /* END main */