【发布时间】:2016-12-24 19:52:25
【问题描述】:
我正在使用 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
我确实了解现有值是我的变量/目标的值。 但我对一些输出有几个问题:
-MIP 强调:平衡最优性和可行性。 我可以让它专注于最优性吗?
-MIP 搜索方式:我该如何改变呢?
-最重要的是,第一阶段和第二阶段是什么?在我更大的情况下,第一阶段(例如 700 秒)比第二阶段(例如 20 秒)花费的时间更多。这些阶段在做什么?如果我理解正确的话,Phase I 正在寻找可行的解决方案,而 Phase II 正在寻找优化,但是正如您在日志中看到的那样,它报告了 Phase I 的第一个解决方案(即 line "* 3 4 integral 0 0.9091 47 -- -") 但随后在第一阶段继续。所以我一定理解错了......
-我可以阅读一本书或一些资源来自己回答未来的任何问题吗?我只找到了一份来自 IBM 的 130 页教程,它把我淹没在“不相关”的东西中,我找不到我想要的东西。
谢谢。
【问题讨论】:
标签: optimization linear-programming cplex