我正在处理使用CPLEX的问题,我对此很不熟悉。我知道Simplex算法是如何工作的,我知道Branch & Bound,MIP问题等等,但只能从理论的角度来看。这是我第一次真正使用CPLEX。了解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搜索方法:我怎样才能改变呢?
- 最重要的是什么第一期和第二期?在我更大的实例中,第一阶段比第二阶段(例如20s)更多(例如700s)。这些阶段在做什么?如果我理解正确,第一阶段正在寻找一个可行的解决方案,第二阶段将进行优化,但正如您在日志中看到的那样,它报告了第一阶段的第一个解决方案(即“* 3 4 integral 0 0.9091 47 - - “),但后来在第一阶段继续。所以我必须明白这个错误...
- 是否有一本书或一些资源,我可以阅读来回答任何未来的问题,我自己?我发现的只是IBM提供的一个130页的教程,它使我陷入了“无关紧要”的事情,并且我无法找到我所追求的。
谢谢。
好的,我相信我在第一阶段和第二阶段所遇到的问题是,我正在“填充”,当我真正需要的是最佳解决方案,而不是一堆差距中的解决方案。谢谢 – ddeunagomez