The complete program, qpex2.c
, appears here or online in the standard distribution.
#include <ilcplex/cplex.h> /* Bring in the declarations for the string and character functions and malloc */ #include <ctype.h> #include <stdlib.h> #include <string.h> /* Include declarations for functions in this program */ #ifndef CPX_PROTOTYPE_MIN static void free_and_null (char **ptr), usage (char *progname); #else static void free_and_null (), usage (); #endif #ifndef CPX_PROTOTYPE_MIN int main (int argc, char *argv[]) #else int main (argc, argv) int argc; char *argv[]; #endif { /* Declare and allocate space for the variables and arrays where we will store the optimization results including the status, objective value and variable values. */ int solstat; double objval; double *x = NULL; CPXENVptr env = NULL; CPXLPptr lp = NULL; int status; int j; int cur_numcols; /* Check the command line arguments */ if ( argc != 2 ) { usage (argv[0]); goto TERMINATE; } /* 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 CPX_PARAM_SCRIND indicator is set to CPX_ON. */ if ( env == NULL ) { char errmsg[1024]; 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, CPX_PARAM_SCRIND, CPX_ON); if ( status != 0 ) { 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 CPX_PARAM_SCRIND 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 ( CPXgetprobtype (env, lp) != CPXPROB_QP ) { fprintf (stderr, "File does not contain quadratic data. Exiting.\n"); goto TERMINATE; } /* Optimize the problem and obtain solution. */ status = CPXbaropt (env, lp); if ( status ) { fprintf (stderr, "Failed to optimize QP.\n"); goto TERMINATE; } solstat = CPXgetstat (env, lp); printf ("Solution status %d.\n", solstat); status = CPXgetobjval (env, lp, &objval); if ( status ) { fprintf (stderr,"Failed to obtain objective value.\n"); goto TERMINATE; } printf ("Objective value %.10g\n", objval); /* The size of the problem should be obtained by asking CPLEX what the actual size is. cur_numcols stores the current number of columns. */ cur_numcols = CPXgetnumcols (env, lp); /* Allocate space for solution */ x = (double *) malloc (cur_numcols*sizeof(double)); if ( x == NULL ) { fprintf (stderr,"No memory for basis statuses.\n"); goto TERMINATE; } status = CPXgetx (env, lp, x, 0, cur_numcols-1); if ( status ) { fprintf (stderr, "Failed to obtain solution.\n"); goto TERMINATE; } /* Write out the solution */ for (j = 0; j < cur_numcols; j++) { printf ( "Column %d: Value = %17.10g\n", j, x[j]); } TERMINATE: /* Free up the basis and solution */ free_and_null ((char **) &x); /* Free up the problem as allocated by CPXcreateprob, if necessary */ if ( lp != NULL ) { status = CPXfreeprob (env, &lp); if ( status ) { fprintf (stderr, "CPXfreeprob failed, error code %d.\n", status); } } /* Free up the CPLEX environment, if necessary */ if ( env != NULL ) { status = 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 CPX_PARAM_SCRIND indicator is set to CPX_ON. */ 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 */ /* This simple routine frees up the pointer *ptr, and sets *ptr to NULL */ #ifndef CPX_PROTOTYPE_MIN static void free_and_null (char **ptr) #else static void free_and_null (ptr) char **ptr; #endif { if ( *ptr != NULL ) { free (*ptr); *ptr = NULL; } } /* END free_and_null */ #ifndef CPX_PROTOTYPE_MIN static void usage (char *progname) #else static void usage (progname) char *progname; #endif { fprintf (stderr,"Usage: %s filename\n", progname); fprintf (stderr," where filename is a file with extension \n"); fprintf (stderr," MPS, SAV, or LP (lower case is allowed)\n"); fprintf (stderr," This program uses the CPLEX Barrier optimizer\n"); fprintf (stderr," to optimize quadratic programs.\n"); fprintf (stderr," Exiting...\n"); } /* END usage */ |