Using the Callable Library

Here is a C program using the CPLEX Callable Library to solve the example model. An expanded version of this example is discussed in detail in Chapter 5, Callable Library Tutorial.

  #include <ilcplex/cplex.h>
  #include <stdlib.h>
  #include <string.h>
  
  #define NUMROWS    2
  #define NUMCOLS    3
  #define NUMNZ      6
  
  int
  main (int argc, char **argv)
  {
     int       status = 0;
     CPXENVptr env = NULL;
     CPXLPptr  lp  = NULL;
  
     double   obj[NUMCOLS];
     double   lb[NUMCOLS];
     double   ub[NUMCOLS];
     double   x[NUMCOLS];
     int      rmatbeg[NUMROWS];
     int      rmatind[NUMNZ];
     double   rmatval[NUMNZ];
     double   rhs[NUMROWS];
     char     sense[NUMROWS];
  
     int      solstat;
     double   objval;
  
     env = CPXopenCPLEX (&status);
     if ( env == NULL ) {
        char  errmsg[1024];
        fprintf (stderr, "Could not open CPLEX environment.\n");
        CPXgeterrorstring (env, status, errmsg);
        fprintf (stderr, "%s", errmsg);
        goto TERMINATE;
     }
  
     lp = CPXcreateprob (env, &status, "lpex1");
     if ( lp == NULL ) {
        fprintf (stderr, "Failed to create LP.\n");
        goto TERMINATE;
     }
  
     CPXchgobjsen (env, lp, CPX_MAX);
  
         obj[0] = 1.0;      obj[1] = 2.0;           obj[2] = 3.0;
          lb[0] = 0.0;       lb[1] = 0.0;           lb[2]  = 0.0;
          ub[0] = 40.0;      ub[1] = CPX_INFBOUND;  ub[2]  = CPX_INFBOUND;
  
     status = CPXnewcols (env, lp, NUMCOLS, obj, lb, ub, NULL, NULL);
     if ( status ) {
        fprintf (stderr, "Failed to populate problem.\n");
        goto TERMINATE;
     }
  
     rmatbeg[0] = 0;
     rmatind[0] = 0;     rmatind[1] = 1;    rmatind[2] = 2;  sense[0] = 'L';
     rmatval[0] = -1.0;  rmatval[1] = 1.0;  rmatval[2] = 1.0;  rhs[0] = 20.0;
  
     rmatbeg[1] = 3;
     rmatind[3] = 0;    rmatind[4] = 1;    rmatind[5] = 2;     sense[1] = 'L';
     rmatval[3] = 1.0;  rmatval[4] = -3.0; rmatval[5] = 1.0;   rhs[1]   = 30.0;
  
     status = CPXaddrows (env, lp, 0, NUMROWS, NUMNZ, rhs, sense, rmatbeg,
                          rmatind, rmatval, NULL, NULL);
     if ( status ) {
        fprintf (stderr, "Failed to populate problem.\n");
        goto TERMINATE;
     }
  
     status = CPXlpopt (env, lp);
     if ( status ) {
        fprintf (stderr, "Failed to optimize LP.\n");
        goto TERMINATE;
     }
  
     status = CPXsolution (env, lp, &solstat, &objval, x, NULL, NULL, NULL);
     if ( status ) {
        fprintf (stderr, "Failed to obtain solution.\n");
        goto TERMINATE;
     }
     printf ("\nSolution status = %d\n", solstat);
     printf ("Solution value  = %f\n", objval);
     printf ("Solution        = [%f, %f, %f]\n\n", x[0], x[1], x[2]);
  
  TERMINATE:
  
     if ( lp != NULL ) {
        status = CPXfreeprob (env, &lp);
        if ( status ) {
           fprintf (stderr, "CPXfreeprob failed, error code %d.\n", status);
        }
     }
  
     if ( env != NULL ) {
        status = CPXcloseCPLEX (&env);
        if ( status ) {
           char  errmsg[1024];
           fprintf (stderr, "Could not close CPLEX environment.\n");
           CPXgeterrorstring (env, status, errmsg);
           fprintf (stderr, "%s", errmsg);
        }
     }
  
     return (status);
  
  }  /* END main */


Previous Page: Concert Technology for Java Users  Return to Top Next Page: What You Need to Know