了解CPLEX输出

时间:2016-08-18 01:50:42

标签: optimization linear-programming cplex

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

我在C中使用它,并且我在示例" populate.c"上编写了基于A LOT的主文件。在CPLEX分发中作为示例提供的文件。

这是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

我确实理解现任值是我的变量/目标的值。 但是我对一些输出有一些问题:

- MIP重点:平衡最优性和可行性。我能否专注于最优性?

- MIP搜索方法:我该如何更改?

- 最重要的是,阶段I 阶段II 是什么?在我更大的情况下,第一阶段比第二阶段(例如20年代)采取更多(例如700s)。这些阶段在做什么?如果我理解正确,第一阶段正在寻找一个可行的解决方案,第二阶段正在寻找优化,但正如您在日志中所看到的,它报告了第一阶段的第一个解决方案(即行&#34; * 3 4积分0 0.9091 47 ---&#34;)但在第一阶段继续。所以我一定明白这个错误...

- 我可以阅读一本书或一些资源,以便自己回答任何未来的问题吗?我发现的只是来自IBM的130页教程,让我淹没了&#34;无关紧要&#34;事情,我找不到我想要的东西。

感谢。

1 个答案:

答案 0 :(得分:1)

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

    这与Cplex参数 MipEmphasis 有关。此选项&#34;控制MIP&#34;中速度,可行性,最优性和移动界限之间的权衡。通常可以将其保留为默认值。您可以告诉Cplex更多地强调最优性,但这并不一定导致更快的解决方案时间。对于大型复杂模型,这是一个有用的选项。

  • MIP搜索方法

    这与Cplex参数 MipSearch 有关。此选项&#34;设置混合整数程序(MIP)的搜索策略&#34;。我几乎没有使用过这个选项,我相信它最好保留默认值。

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

    这与解决方案池算法有关。 (不是线性规划中阶段1和阶段2的概念)。请参阅填充的文档。

我通常会将大部分或甚至所有选项保留为默认值,除非有充分的理由进行更改。 Cplex旨在通过默认设置做得很好。