2017-12-18 87 views
-2

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

我注册了一个回调函数(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); 

} 
+0

你的文章有太多错误...没有代码,没有实际的解释,我们甚至不知道你是否在发布之前尝试过任何东西。 – schaiba

+0

你说的很对,我正在用更详细的信息更新这篇文章。 – acco93

回答

1

我发现了问题,调用回调前的子问题已解决,使CPXsolution返回无解的存在。

+1

很高兴听到您找到问题的原因。你可以通过解释你如何诊断它,特别是当别处看到这个问题时能够找出这个问题,从而使你的答案对未来的读者更有帮助。 –