了解 CPLEX 输出

Understanding CPLEX output

我正在使用 CPLEX 解决一个问题,但我对它非常不熟悉。我知道 Simplex 算法是如何工作的,我知道 Branch&Bound、MIP 问题等,但只是从理论的角度来看。这是我第一次真正使用CPLEX。

我在 C 中使用它,并且我根据 CPLEX 发行版中作为示例给出的示例 "populate.c" 文件编写了很多主文件。

这是 C 代码。

#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 */

现在,我生成我的 LP 文件(其中包含二进制变量和指标约束,因此它不仅仅是一个 LP)并将其提供给 CPLEX。

CPLEX 一点也不抱怨,解决得很好。但是,我真的不知道它在告诉我什么。这是一个示例输出:

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

我确实理解现任价值观是我的 variables/objective 的价值观。 但是我对一些输出有几个问题:

-MIP强调:平衡最优性和可行性。我可以让它专注于最优性吗?

-MIP 搜索方式:如何更改?

-最重要的是,第一阶段第二阶段是什么?在我更大的例子中,第一阶段比第二阶段(例如 20 秒)花费更多(例如 700 秒)。这些阶段在做什么?如果我理解正确的话,Phase I 正在寻找一个可行的解决方案,而Phase II 正在优化,但是正如您在日志中看到的那样,它报告了Phase I 的第一个解决方案(即行“* 3 4 integral 0 0.9091 47 -- -") 然后继续第一阶段。所以我一定是理解错了...

-是否有我可以阅读的书籍或资源,以便我自己回答未来的任何问题?我所找到的只是来自 IBM 的 130 页教程,它让我沉浸在 "irrelevant" 的东西中,但我找不到我想要的东西。

谢谢。

  • MIP 重点:平衡最优性和可行性

    这与 Cplex 参数 MipEmphasis 有关。这个选项"controls trade-offs between speed, feasibility, optimality, and moving bounds in MIP"。通常可以将其保留为默认值。您可以告诉 Cplex 更加强调最优性,但这不一定会导致更快的求解时间。对于大型复杂模型,这是一个有用的选项。

  • MIP 搜索方式

    这与 Cplex 参数 MipSearch 有关。这个选项 "sets the search strategy for a mixed integer program (MIP)"。我几乎从不使用这个选项,我相信最好保留它的默认值。

  • 最重要的是,第一阶段和第二阶段是什么?

    这个和解池算法有关。 (不是线性规划中阶段 1 和阶段 2 的概念)。请参阅 Populate 的文档。

我通常将大部分甚至所有选项保留为默认值,除非有充分的理由更改它们。 Cplex 旨在使用默认设置做好工作。