CPLEX MIP当前节点LP放宽

时间:2017-12-18 11:06:09

标签: c cplex

在使用CPLEX C API进行MIP优化期间,是否可以检索当前节点(即每n个节点)的线性松弛(双变量,降低成本等)?

我已经注册了一个回调函数(CPXsetsolvecallbackfunc),以便在每个新节点可用时收到通知。在回调中,我使用CPXgetcallbackinfo来检索节点信息,使用CPXgetcallbacknodelp来检索线性松弛,但不幸的是,程序CPXsolution返回没有解决方案,并且MIP优化退出。

以下是从IBM example开始实现的示例代码,其中假定环境和问题已正确初始化。

struct noderange {
int startnode;
int endnode;
 };
typedef struct noderange NODERANGE;

NODERANGE     nodeswritten;
nodeswritten.startnode = -1;
nodeswritten.endnode = 2100000000;

status = CPXsetsolvecallbackfunc(environment, usersolve, &nodeswritten);
if(status) {goto TERMINATE;} 

status = CPXmipopt(environment, problem);
if(status) {goto TERMINATE;} 

其中usersolve过程是

static int CPXPUBLIC usersolve(CPXCENVptr env, void *cbdata, int wherefrom, void *cbhandle, int *useraction_p) {

int       status = 0;
int       nodecount;
static    int count = 0;
CPXLPptr  nodelp;
NODERANGE *nodeswritten;

*useraction_p = CPX_CALLBACK_DEFAULT;
nodeswritten = (NODERANGE *)cbhandle;

/* Find out what node is being processed */

status = CPXgetcallbackinfo(env, cbdata, wherefrom, CPX_CALLBACK_INFO_NODE_COUNT, &nodecount);
if (status)  goto TERMINATE;

if (nodecount >= nodeswritten->startnode && nodecount <= nodeswritten->endnode) {

    /* Get pointer to LP subproblem, then write a SAV file. */

    status = CPXgetcallbacknodelp(env, cbdata, wherefrom, &nodelp);
    if (status)  goto TERMINATE;

    int rows = CPXgetnumcols(env, nodelp);
    int cols = CPXgetnumrows(env, nodelp);

    int lpstat;
    double objval;
    double* x = (double*)malloc(sizeof(double) * CPXgetnumcols(env, nodelp));
    double* dj = (double*)malloc(sizeof(double) * CPXgetnumcols(env, nodelp));

    double* pi = (double*)malloc(sizeof(double) * CPXgetnumrows(env, nodelp));
    double* slack = (double*)malloc(sizeof(double) * CPXgetnumrows(env, nodelp));

    status = CPXsolution(env, nodelp, &lpstat, &objval, x, pi, slack, dj);
    printf("Solutionstatus = %d\n", lpstat);

    if (status) { goto TERMINATE; } // <--- HERE it returns no solution exists

    free(x);
    free(dj);
    free(pi);
    free(slack);

    printf("[%d]\trows = %d cols = %d\t sol stat = %d\t z = %f\n", nodecount, rows, cols, lpstat, objval);



    if (nodecount == nodeswritten->endnode)  status = 1;
    count++;
}


TERMINATE:
return (status);

} 

1 个答案:

答案 0 :(得分:0)

我发现了问题,在子问题解决之前回调被称为,因此CPXsolution返回没有解决方案。