CPXmipopt()
, and its command line does not require the user to indicate an optimizer.
CPXgetstat()
, CPXgetmipobjval()
, and CPXgetmipx()
to get a solution. It doesn't generate or print a basis.
Like other applications based on the ILOG CPLEX Callable Library, this one calls CPXopenCPLEX()
to initialize the ILOG CPLEX environment; it sets the screen-indicator parameter to direct output to the screen and calls CPXcreateprob()
to create a problem object. Before it ends, it calls CPXfreeprob()
to free the space allocated to the problem object and CPXcloseCPLEX()
to free the environment.
#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 ) { 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; } /* 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); status = CPXgetmipobjval (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 solution values.\n"); goto TERMINATE; } status = CPXgetmipx (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 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 MIP optimizer.\n"); fprintf (stderr," Exiting...\n"); } /* END usage */ |