Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
  • 1.  Error: using empty handle

    Posted Sun April 29, 2012 12:07 PM

    Originally posted by: Ximenes


    Hello,everyone,when I used C++ calling CPLEX to solve my MICQP, I got an error saying "using empty handle". I had checked the following code several times but still don't know the source error. So I want to get help from you.
    //////////////////////////////////////////////////////////////////////
    #include <ilcplex/ilocplex.h>

    ILOSTLBEGIN

    typedef IloArray<IloBoolVarArray> BoolVarMatrix;
    int main(int argc, char** argv) {
    IloEnv env;

    try {
    IloInt i, j, k;
    IloInt nOrder;

    const char* filename;
    if (argc > 1)
    filename = argv[1];
    else
    filename = "C:\\Users\\Ximenes\\Desktop\\5月31\\try.dat";
    ifstream f(filename, ios::in);
    if (!f) {
    cerr << "No such file: " << filename << endl;
    throw(1);
    }

    //C:\Users\Ximenes\Desktop\5月31

    IloNum T;
    IloNumArray P1(env);
    IloNumArray P2(env);
    IloNumArray R(env);

    f >> T;
    f >> P1;
    f >> P2;
    f >> R;

    nOrder = R.getSize();
    IloModel model(env);

    // Create start variables
    IloInt K;
    BoolVarMatrix x(env, nOrder);
    for (i = 0; i < nOrder; i++) {
    x[i] = IloBoolVarArray(env, nOrder);
    }

    BoolVarMatrix z(env, nOrder);
    for (i = 0; i < nOrder; i++) {
    z[i] = IloBoolVarArray(env, nOrder);
    }

    IloBoolVarArray y(env,nOrder);
    IloNumVarArray W(env,nOrder,0.0,T);
    IloNumVarArray I(env,nOrder,0.0,T);

    // 第1个约束
    for (i = 0; i < nOrder; i++) {
    IloExpr expr(env);
    for(j=0;j<i;j++)
    expr+=x[i][j];
    for(j=i+1;j<nOrder;j++)
    expr+=x[i][j];
    model.add(expr==y[i]);
    expr.end();
    }
    //第2个约束
    for (j = 0; j < nOrder; j++) {
    IloExpr expr(env);
    for(i=0;i<j;i++)
    expr+=x[i][j];
    for(i=j+1;i<nOrder;i++)
    expr+=x[i][j];
    model.add(expr==y[j]);
    expr.end();
    }
    //时间约束
    IloExpr expr(env);
    for(j=0;j<nOrder;j++) {
    expr+=z[j][0]*P1[j];
    expr+=P2[j]*y[j];
    expr+=I[j];
    }
    model.add(expr<=T);
    expr.end();

    //W.I约束
    for(k=0;k<nOrder-1;k++){
    IloExpr expr(env);
    for(j=0;j<nOrder;j++){
    expr+=z[j][k]*P2[j];
    expr-=z[j]k+1*P1[j];
    }
    expr+=W[k];
    expr-=Wk+1;
    expr+=I[k];
    model.add(expr==0);
    expr.end();
    }

    //Z约束
    for(j=0;j<nOrder;j++){ //订单j最多可能被安排在一个位置
    IloExpr expr(env);
    for(k=0;k<nOrder;k++)
    expr+=z[j][k];
    model.add(expr<=1);
    expr.end();
    }

    for(k=0;k<nOrder;k++){ //位置k最多被安排一个订单
    IloExpr expr(env);
    for(j=0;j<nOrder;j++)
    expr+=z[j][k];
    model.add(expr<=1);
    expr.end();
    }
    for(j=0;j<nOrder;j++){//Z_jk≤y_j,j=1,2,…,n;k=1,2,…,K
    for(k=0;k<nOrder;k++)
    model.add(z[j][k]<=y[j]);
    }

    IloExpr expr1(env);//K等于y[j]连加的和,表示选择的订单数
    for(j=0;j<nOrder;j++)
    expr1+=y[j];
    model.add(K==expr);
    expr1.end();

    // 最大化目标函数

    IloExpr profitSum(env);
    for (j = 0; j < nOrder; j++) {
    profitSum +=R[j]*y[j];
    }
    model.add(IloMaximize(env, profitSum));
    profitSum.end();

    //计算及输出
    IloCplex cplex(env);

    cplex.extract(model);

    cplex.setParam(IloCplex::MIPEmphasis, 4);

    if (cplex.solve()) {
    cout << " Optimal Value = " << cplex.getObjValue() << endl;
    }
    }
    catch (IloException& ex) {
    cerr << "Error: " << ex << endl;
    }
    catch (...) {
    cerr << "Error" << endl;
    }
    env.end();
    &IloAlgorithm::getStatus;
    system("pause");
    return 0;
    }
    /*

    10
    2, 3, 5
    1, 3, 2
    13,6, 7 */
    /////////////////////////////////////////////////////////////////////////////////////
    Thank you so much.
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Error: using empty handle

    Posted Tue May 01, 2012 06:15 AM

    Originally posted by: SystemAdmin


    That is a lot of code to look through. Could you please tell us which is the exact statement that causes the error message?
    The first thing I would try is to double check whether the values T, P1, P2, and R that your read from f are as expected. Just print out T and the arrays and double check that they contain the expected data.
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Error: using empty handle

    Posted Tue May 01, 2012 08:45 AM

    Originally posted by: Ximenes


    Thank you for your considerate reply. I've found the source error. I had not define the bound of t][. To your suggestion of input of the data, I am sure it has no problem.
    Now another problem arises. I can easily know the optimal value since the data is very simple since I take it for checking. But when I debug the revised code, the optimal is 0.
    Can you help me?
    Thank you in advance.
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    #include <ilcplex/ilocplex.h>

    ILOSTLBEGIN

    typedef IloArray<IloBoolVarArray> BoolVarMatrix;
    typedef IloArray<IloIntVarArray> IntVarMatrix;

    int main(int argc, char** argv) {
    IloEnv env;

    try {
    IloInt i, j, k;
    IloInt nOrder;

    const char* filename;
    if (argc > 1)
    filename = argv[1];
    else
    filename = "C:\\Users\\Ximenes\\Desktop\\5月31\\try.dat";
    ifstream f(filename, ios::in);
    if (!f) {
    cerr << "No such file: " << filename << endl;
    throw(1);
    }

    //C:\Users\Ximenes\Desktop\5月31

    IloNum T;
    IloNumArray P1(env);
    IloNumArray P2(env);
    IloNumArray R(env);

    f >> T;
    f >> P1;
    f >> P2;
    f >> R;

    nOrder = R.getSize();
    IloModel model(env);

    // Create start variables
    BoolVarMatrix x(env, nOrder);
    for (i = 0; i < nOrder; i++) {
    x[i] = IloBoolVarArray(env, nOrder);
    }

    BoolVarMatrix z(env, nOrder);
    for (i = 0; i < nOrder; i++) {
    z[i] = IloBoolVarArray(env, nOrder);
    }

    IntVarMatrix t(env, nOrder);
    for (i = 0; i < nOrder; i++) {
    t[i] = IloIntVarArray(env, nOrder,0,1);
    }

    IloBoolVarArray y(env,nOrder);
    IloNumVarArray W(env,nOrder,0.0,T);
    IloNumVarArray I(env,nOrder-1,0.0,T);

    // 第1个约束
    for (i = 0; i < nOrder; i++) {
    IloExpr expr(env);
    for(j=0;j<i;j++)
    expr+=x[i][j];
    for(j=i+1;j<nOrder;j++)
    expr+=x[i][j];
    model.add(expr==y[i]);
    expr.end();
    }
    //第2个约束
    for (j = 0; j < nOrder; j++) {
    IloExpr expr(env);
    for(i=0;i<j;i++)
    expr+=x[i][j];
    for(i=j+1;i<nOrder;i++)
    expr+=x[i][j];
    model.add(expr==y[j]);
    expr.end();
    }

    for(i=0;i<nOrder;i++){
    for(j=0;j<nOrder;j++)
    model.add(t[j][k]==(z[j][k]+y[j]/2));
    }
    //时间约束
    IloInt last=nOrder-1;
    IloExpr expr(env);
    for(j=0;j<last;j++) {
    expr+=t[j][0]*P1[j];
    expr+=I[j];
    expr+=P2[j]*y[j];
    }
    expr+=tlast[0]*P1last;
    expr+=P2last*ylast;
    model.add(expr<=T);
    expr.end();

    //W.I约束
    for(k=0;k<last;k++){
    IloExpr expr(env);
    for(j=0;j<nOrder;j++){
    expr+=t[j][k]*P2[j];
    expr-=t[j]k+1*P1[j];
    }
    expr+=W[k];
    expr-=Wk+1;
    expr+=I[k];
    model.add(expr==0);
    expr.end();
    }

    //Z加和等于1约束
    for(j=0;j<nOrder;j++){
    IloExpr expr(env);
    for(k=0;k<nOrder;k++)
    expr+=z[j][k];
    model.add(expr==1);
    expr.end();
    }

    for(k=0;k<nOrder;k++){
    IloExpr expr(env);
    for(j=0;j<nOrder;j++)
    expr+=z[j][k];
    model.add(expr==1);
    expr.end();
    }
    // 最大化目标函数

    IloExpr profitSum(env);
    for (j = 0; j < nOrder; j++) {
    profitSum +=R[j]*y[j];
    }
    model.add(IloMaximize(env, profitSum));
    profitSum.end();

    //计算及输出
    IloCplex cplex(env);

    cplex.extract(model);

    cplex.setParam(IloCplex::MIPEmphasis, 4);

    if (cplex.solve()) {
    cout << " Optimal Value = " << cplex.getObjValue() << endl;
    }
    }
    catch (IloException& ex) {
    cerr << "Error: " << ex << endl;
    }
    catch (...) {
    cerr << "Error" << endl;
    }
    env.end();
    system("pause");
    return 0;
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
    The data is listed as follow.
    10
    2, 3, 5
    1, 3, 2
    3,6, 7
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Error: using empty handle

    Posted Tue May 01, 2012 11:30 AM

    Originally posted by: SystemAdmin


    First thing you should do is to print out the model using one of the following lines:
    
    std::cout << model << std::endl; cplex.exportModel(model, 
    "model.lp");
    

    The second line exports the model to file model.lp which is a human readable file format and can be read using any text editor. Then check whether the model looks as expected.
    I see these lines in your code:
    
    
    
    for(i=0;i<nOrder;i++)
    { 
    
    for(j=0;j<nOrder;j++) model.add(t[j][k]==(z[j][k]+y[j]/2)); 
    }
    

    As far as I can see variable 'k' is never initialized before that block. So the value of 'k' is just random here. Doesn't your compiler warn about that? Maybe you only need to properly initialize 'k' to get the model right?
    Some technical advice (feel free to ignore):
    1. You should crank up the compiler's warning flags so that it warns you about using uninitialized values (like 'k' above).
    2. In C++ you can write
    
    
    
    for (
    
    int i = ...) 
    { ... 
    } 
    
    for (
    
    int i = ...) 
    { ... 
    }
    

    instead of
    
    
    
    int i; 
    
    for (i = ...) 
    { ... 
    } 
    
    for (i = ...) 
    { ... 
    }
    

    The first syntax is preferred as it limits the scope of variable 'i'. Had you used the first syntax you would have immediately noticed your error since there would not have been a 'k' variable defined.

    I hope this helps.
    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Error: using empty handle

    Posted Tue May 01, 2012 12:20 PM

    Originally posted by: Ximenes


    Oh,thank you so much for your considerate reply, Daniel. I find it most useful of being informed by you with the two following lines:

    std::cout << model << std::endl;
    cplex.exportModel(model, "model.lp");

    Because I didn't know that I can read the model extracted by CPLEX.

    But when I post the two lines in the following position of the codes, it underlines red wavy lines "model, "model.lp". Can you tell me why?
    Thank you so much.
    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Error: using empty handle

    Posted Thu May 03, 2012 05:38 AM

    Originally posted by: SystemAdmin


    > Oh,thank you so much for your considerate reply, Daniel. I find it most useful of being informed by you with the two following lines:
    >
    > std::cout << model << std::endl;
    > cplex.exportModel(model, "model.lp");
    >
    > Because I didn't know that I can read the model extracted by CPLEX.
    >
    > But when I post the two lines in the following position of the codes, it underlines red wavy lines "model, "model.lp". Can you tell me why?
    > Thank you so much.
    >
    Sorry, no clue. Is that considered an error? Is it possible that it performs spell checking on the string "model.lp"? That would of course fail as "model.lp" is not an English sentence.
    Is your IloModel instance called 'model' or did you use a different name for that? Then you would have no 'model' variable to print and should adapt the code appropriately.
    #CPLEXOptimizers
    #DecisionOptimization