I am working on a problem using CPLEX and I am very unfamiliar with it. I know how the Simplex algorithm works, I know Branch&Bound, MIP problems etc, but only from a theory point of view. This is the first time I actually use CPLEX.
I am using it in C, and I wrote the main file based A LOT on the example "populate.c" file that is given as an example in the CPLEX distribution.
Here is the C code.
#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 <math.h>
#define EPSZERO 1.0E-10
#define BUFSIZE 16
/* Include declarations for functions in this program */
static void
free_and_null (char **ptr),
usage (char *progname);
int
main (int argc, char *argv[])
{
/* 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 incobjval;
double meanobjval;
double *x = NULL;
double *incx = NULL;
int numsol;
int numsolreplaced;
int numdiff;
CPXENVptr env = NULL;
CPXLPptr lp = NULL;
int status;
int i, 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[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, 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;
}
/* Set the solution pool relative gap parameter to obtain solutions
of objective value within 10% of the optimal */
status = CPXsetdblparam (env, CPX_PARAM_SOLNPOOLGAP, 0);
if ( status ) {
fprintf (stderr,
"Failed to set the solution pool relative gap, error %d.\n",
status);
goto TERMINATE;
}//*/
/* Optimize the problem and obtain multiple solutions. */
status = CPXpopulate (env, lp);
if ( status ) {
fprintf (stderr, "Failed to populate MIP.\n");
goto TERMINATE;
}
solstat = CPXgetstat (env, lp);
printf ("Solution status: %d.\n", solstat);
status = CPXgetobjval (env, lp, &incobjval);
if ( status ) {
fprintf (stderr,
"Failed to obtain objective value for the incumbent.\n");
goto TERMINATE;
}
printf ("Objective value of the incumbent: %.10g\n", incobjval);
/* 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 */
incx = (double *) malloc (cur_numcols*sizeof(double));
if ( incx == NULL ) {
fprintf (stderr, "No memory for solution values for the incumbent.\n");
goto TERMINATE;
}
status = CPXgetx (env, lp, incx, 0, cur_numcols-1);
if ( status ) {
fprintf (stderr, "Failed to obtain the incumbent.\n");
goto TERMINATE;
}
/* Write out the incumbent */
char **cur_colname = NULL;
char *cur_colnamestore = NULL;
int cur_colnamespace;
int surplus;
status = CPXgetcolname (env, lp, NULL, NULL, 0, &surplus, 0,
cur_numcols-1);
if (( status != CPXERR_NEGATIVE_SURPLUS ) &&
( status != 0 ) ) {
fprintf (stderr,
"Could not determine amount of space for column names.\n");
goto TERMINATE;
}
cur_colnamespace = - surplus;
if ( cur_colnamespace > 0 ) {
cur_colname = (char **) malloc (sizeof(char *)*cur_numcols);
cur_colnamestore = (char *) malloc (cur_colnamespace);
if ( cur_colname == NULL ||
cur_colnamestore == NULL ) {
fprintf (stderr, "Failed to get memory for column names.\n");
status = -1;
goto TERMINATE;
}
status = CPXgetcolname (env, lp, cur_colname, cur_colnamestore,
cur_colnamespace, &surplus, 0, cur_numcols-1);
}
for (j = 0; j < cur_numcols; j++) {
printf ("Incumbent: Column %s: Value = %17.10g\n", cur_colname[j], incx[j]);
}
printf ("\n");
/* Get the number of solutions in the solution pool */
numsol = CPXgetsolnpoolnumsolns (env, lp);
printf ("The solution pool contains %d solutions.\n", numsol);
/* Some solutions are deleted from the pool because of the solution
pool relative gap parameter */
numsolreplaced = CPXgetsolnpoolnumreplaced (env, lp);
printf (
"%d solutions were removed due to the solution pool relative gap parameter.\n",
numsolreplaced);
printf ("In total, %d solutions were generated.\n",
numsol + numsolreplaced);
/* Get the average objective value of solutions in the solution
pool */
status = CPXgetsolnpoolmeanobjval (env, lp, &meanobjval);
printf ("The average objective value of the solutions is %.10g.\n\n",
meanobjval);
/* Write out the objective value of each solution and its
difference to the incumbent */
x = (double *) malloc (cur_numcols*sizeof(double));
if ( x == NULL ) {
fprintf (stderr, "No memory for solution values.\n");
goto TERMINATE;
}
printf ("Solution Objective Number of variables\n");
printf (" value that differ compared to\n");
printf (" the incumbent\n");
for (i = 0; i < numsol; i++) {
char namei[BUFSIZE];
int surplus;
/* Write out objective value */
CPXgetsolnpoolsolnname (env, lp, namei, BUFSIZE, &surplus, i);
printf ("%-15s ", namei);
status = CPXgetsolnpoolobjval (env, lp, i, &objval);
if ( status ) {
fprintf (stderr,
"Failed to obtain objective value for solution %d.\n", i);
goto TERMINATE;
}
printf ("%.10g ", objval);
status = CPXgetsolnpoolx (env, lp, i, x, 0, cur_numcols-1);
if ( status ) {
fprintf (stderr, "Failed to obtain solution %d.\n", i);
goto TERMINATE;
}
/* Compute the number of variables that differ in the solution
and in the incumbent */
numdiff = 0;
for (j = 0; j < cur_numcols; j++) {
if ( fabs (x[j] - incx[j]) > EPSZERO )
numdiff++;
}
printf ("%d / %d\n", numdiff, cur_numcols);
}
TERMINATE:
/* Free up the solution */
free_and_null ((char **) &incx);
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[CPXMESSAGEBUFSIZE];
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 */
static void
free_and_null (char **ptr)
{
if ( *ptr != NULL ) {
free (*ptr);
*ptr = NULL;
}
} /* END free_and_null */
static void
usage (char *progname)
{
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 */
Now, I generate my LP files (which have Binary variables and indicator constraints, so it's not just an LP) and give it to CPLEX.
CPLEX does not complain at all and solves very well. But, I literally have no clue what it is telling me. Here is an example output:
Populate: phase I
Tried aggregator 2 times.
Aggregator did 14 substitutions.
Reduced MIP has 92 rows, 160 columns, and 414 nonzeros.
Reduced MIP has 24 binaries, 0 generals, 0 SOSs, and 90 indicators.
Probing time = 0.00 sec.
Tried aggregator 1 time.
Presolve time = 0.00 sec.
Probing time = 0.00 sec.
MIP emphasis: balance optimality and feasibility.
MIP search method: dynamic search.
Parallel mode: deterministic, using up to 8 threads.
Root relaxation solution time = 0.00 sec.
Nodes Cuts/
Node Left Objective IInf Best Integer Best Bound ItCnt Gap
0 0 unbounded 0
0 2 unbounded 0
Elapsed real time = 0.01 sec. (tree size = 0.01 MB, solutions = 0)
* 3 4 integral 0 0.9091 47 ---
* 7 7 integral 0 0.9005 93 ---
* 12 10 integral 0 0.7397 178 ---
Root node processing (before b&c):
Real time = 0.00
Parallel b&c, 8 threads:
Real time = 0.08
Sync time (average) = 0.00
Wait time (average) = 0.00
-------
Total (root+branch&cut) = 0.08 sec.
Populate: phase II
MIP emphasis: balance optimality and feasibility.
MIP search method: dynamic search.
Parallel mode: deterministic, using up to 8 threads.
Nodes Cuts/
Node Left Objective IInf Best Integer Best Bound ItCnt Gap
601 301 1.1727 0 0.7397 0.7397 5173 0.00%
Elapsed real time = 0.00 sec. (tree size = 0.05 MB, solutions = 1)
Root node processing (before b&c):
Real time = 0.00
Parallel b&c, 8 threads:
Real time = 0.01
Sync time (average) = 0.00
Wait time (average) = 0.00
-------
Total (root+branch&cut) = 0.01 sec.
Solution status: 130.
Objective value of the incumbent: 0.7396943877
Incumbent: Column v0: Value = 0.7396943877
Incumbent: Column i_1_0: Value = 0.7396943877
Incumbent: Column i_2_0: Value = 1.479388775
... More stuff here...
Incumbent: Column b_23: Value = 0
Incumbent: Column b_24: Value = 0
The solution pool contains 1 solutions.
0 solutions were removed due to the solution pool relative gap parameter.
In total, 1 solutions were generated.
The average objective value of the solutions is 0.7396943877.
Solution Objective Number of variables
value that differ compared to
the incumbent
p2 0.7396943877 0 / 84
I do understand that the incumbent values are the values of my variables/objective. But I have a few questions about some of the output:
-MIP emphasis: balance optimality and feasibility. Can I make it focus on optimality?
-MIP search method: how can I change this?
-Most importantly, what are Phase I and Phase II? In my bigger instances Phase I takes way more (e.g 700s) than Phase II (e.g 20s). What are these phases doing? If I understood correctly, Phase I is looking for a feasable solution, and Phase II to optimize, but as you can see in the log, it reported a first solution in Phase I (namely line "* 3 4 integral 0 0.9091 47 ---") but then continued in Phase I. So I must have understood this wrong...
-Is there a book or some resource I can read from to answer any future questions by myself? All I found was a 130pages tutorial from IBM that drowns me into "irrelevant" things and I cannot find what I am after.
Thanks.