Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  has a problem when I run a short program coded with C++, can you help me ?

    Posted 08/10/12 10:41 PM

    Originally posted by: Ximenes


    Hello, everyone, I am running a program with codes as follows.
    But it said "try etsp.exe 中的 0x0035af9f 处有未经处理的异常: 0xC0000005: 读取位置 0xbaadf011 时发生访问冲突".
    I just had no idea about it. Can you can help me?
    
    #include <ilcplex/ilocplex.h>   ILOSTLBEGIN     typedef IloArray<IloNumArray> NumMatrix;     
    
    int main(
    
    int argc, char** argv) 
    { IloEnv env;   
    
    try 
    { 
    
    const char* filename;    
    //data input 
    
    if (argc > 1) filename = argv[1]; 
    
    else filename = 
    "C:\\Users\\Ximenes\\Desktop\\try1.txt"; ifstream f(filename, ios::in); 
    
    if (!f) 
    { cerr << 
    "No such file: " << filename << endl; 
    
    throw(1); 
    } IloNum T; IloNumArray2 r(env); IloNumArray2 p1(env); IloNumArray2 p2(env); IloNumArray2 s(env); f >> T; f >> r; f >> p1; f >> p2; f>>s; cout<<
    "T="<<T<<endl; cout<<
    "r="<<r<<endl; cout<<
    "p1="<<p1<<endl; cout<<
    "p2="<<p2<<endl; cout<<
    "s="<<s<<endl;   
    
    int N=r.getSize(); cout<<
    "N="<<N<<endl; 
    
    for(
    
    int i=0;i<N;i++)
    { cout<<
    "r["<<i<<
    "]="<<r[i]<<endl; cout<<
    "p1["<<i<<
    "]="<<p1[i]<<endl; cout<<
    "p2["<<i<<
    "]="<<p2[i]<<endl; 
    } IloIntArray n(env,N); IloInt total=0; 
    
    for(
    
    int i=0;i<N;i++)
    { n[i]=r[i].getSize(); cout<<
    "n["<<i<<
    "]="<<n[i]<<
    ","; total+=n[i]; 
    }   cout<<
    "total="<<total<<endl;       IloModel model(env); 
    //Variables IloArray <IloArray <IloBoolVarArray > > z (env, N); 
    
    for(
    
    int i=0;i<N;i++)
    { z[i]=IloArray <IloBoolVarArray >(env,n[i]); 
    
    for(
    
    int j=0;j<n[i];j++)
    { z[i][j]=IloBoolVarArray (env,total); 
    } 
    }   IloArray <IloBoolVarArray > y(env,N); 
    
    for(
    
    int i=0;i<N;i++) y[i]=IloBoolVarArray(env,n[i]); IloArray <IloBoolVarArray > t(env,total-1); 
    
    for(
    
    int i=0;i<total-1;i++) t[i]=IloBoolVarArray(env,total-i-1);       IloArray <IloArray < IloArray <IloArray <IloArray <IloBoolVarArray > > > > > b (env, N); 
    
    for(
    
    int i=0;i<N;i++)
    { b[i]=IloArray < IloArray <IloArray <IloArray <IloBoolVarArray > > > >(env,n[i]); 
    
    for(
    
    int k=0;k<n[i];k++)
    { b[i][k]=IloArray <IloArray <IloArray <IloBoolVarArray > > >(env,total); 
    
    for(
    
    int m=0;m<total;m++)
    { b[i][k][m]=IloArray <IloArray <IloBoolVarArray > >(env,N); 
    
    for(
    
    int j=0;j<N;j++)
    { b[i][k][m][j]=IloArray <IloBoolVarArray >(env,n[j]); 
    
    for(
    
    int l=0;l<n[j];l++) b[i][k][m][j][l]=IloBoolVarArray(env,total); 
    } 
    } 
    } 
    } 
    //Constraints                          
    
    for(
    
    int i=0;i<N;i++)
    { 
    
    for(
    
    int k=0;k<n[i];k++)
    { 
    
    for(
    
    int m=0;m<total;m++)
    { 
    
    for(
    
    int j=0;j<N;j++)
    { 
    
    for(
    
    int l=0;l<n[j];l++)
    { 
    
    for(
    
    int h=0;h<total;h++)
    { IloExpr expr(env); expr=b[i][k][m][j][l][h]-t[m][h]; model.add(expr<=0); expr.end(); 
    } 
    } 
    } 
    } 
    } 
    } 
    //Objection IloExpr profitSum(env); 
    
    for(
    
    int i=0;i<N;i++)
    { 
    
    for(
    
    int j=0;j<n[i];j++)
    { profitSum+=r[i][j]*y[i][j]; 
    } 
    } model.add(IloMaximize(env, profitSum)); profitSum.end(); IloCplex cplex(env); 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; 
    }
    

    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: has a problem when I run a short program coded with C++, can you help me ?

    Posted 08/15/12 07:47 PM

    Originally posted by: EdKlotz


    > Ximenes wrote:
    > Hello, everyone, I am running a program with codes as follows.
    > But it said "try etsp.exe 中的 0x0035af9f 处有未经处理的异常: 0xC0000005: 读取位置 0xbaadf011 时发生访问冲突".
    This phrase "Try etsp.exe" does not come from any of the CPLEX libraries your
    program calls. Is the name of your compiled program try.exe, and you ran it
    with etsp.exe as an argument? If so, that would take a binary executable file
    as text input, which would result in all sorts of unexpected values for the problem
    dimensions of the model your code builds.

    Beyond that, run the program in a debugger and isolate the exact line where
    the program crashes.
    > I just had no idea about it. Can you can help me?
    >
    
    > #include <ilcplex/ilocplex.h> > > ILOSTLBEGIN > > > typedef IloArray<IloNumArray> NumMatrix; > > > 
    
    int main(
    
    int argc, char** argv) 
    { > >  IloEnv env; > >    
    
    try 
    { >       
    
    const char* filename;    
    //data input >       
    
    if (argc > 1) >          filename = argv[1]; >       
    
    else >          filename = 
    "C:\\Users\\Ximenes\\Desktop\\try1.txt"; >       ifstream f(filename, ios::in); >       
    
    if (!f) 
    { >          cerr << 
    "No such file: " << filename << endl; >          
    
    throw(1); >          
    } > >     IloNum T; >          IloNumArray2 r(env); >       IloNumArray2 p1(env); >       IloNumArray2 p2(env); >     IloNumArray2 s(env); > >          f >> T; >      f >> r; >       f >> p1; >       f >> p2; >          f>>s; > >           cout<<
    "T="<<T<<endl; > cout<<
    "r="<<r<<endl; >           cout<<
    "p1="<<p1<<endl; >        cout<<
    "p2="<<p2<<endl; >        cout<<
    "s="<<s<<endl; > > > >           
    
    int N=r.getSize(); >        cout<<
    "N="<<N<<endl; >          
    
    for(
    
    int i=0;i<N;i++)
    { >             cout<<
    "r["<<i<<
    "]="<<r[i]<<endl; >               cout<<
    "p1["<<i<<
    "]="<<p1[i]<<endl; >             cout<<
    "p2["<<i<<
    "]="<<p2[i]<<endl; >     
    } >     IloIntArray n(env,N); >      IloInt total=0; >    
    
    for(
    
    int i=0;i<N;i++)
    { >            n[i]=r[i].getSize(); >           cout<<
    "n["<<i<<
    "]="<<n[i]<<
    ","; >                 total+=n[i]; >       
    } > >       cout<<
    "total="<<total<<endl; > > > >    IloModel model(env); > > >     
    //Variables >         IloArray <IloArray <IloBoolVarArray > > z (env, N); >    
    
    for(
    
    int i=0;i<N;i++)
    { >        z[i]=IloArray <IloBoolVarArray >(env,n[i]); >           
    
    for(
    
    int j=0;j<n[i];j++)
    { >                    z[i][j]=IloBoolVarArray (env,total); >               
    } >     
    } > >        IloArray <IloBoolVarArray > y(env,N); >        
    
    for(
    
    int i=0;i<N;i++) >                  y[i]=IloBoolVarArray(env,n[i]); > >       IloArray <IloBoolVarArray > t(env,total-1); >          
    
    for(
    
    int i=0;i<total-1;i++) >            t[i]=IloBoolVarArray(env,total-i-1); > > > >        IloArray <IloArray < IloArray <IloArray <IloArray <IloBoolVarArray > > > > > b (env, N); >     
    
    for(
    
    int i=0;i<N;i++)
    { >            b[i]=IloArray < IloArray <IloArray <IloArray <IloBoolVarArray > > > >(env,n[i]); >                   
    
    for(
    
    int k=0;k<n[i];k++)
    { >                     b[i][k]=IloArray <IloArray <IloArray <IloBoolVarArray > > >(env,total); >                      
    
    for(
    
    int m=0;m<total;m++)
    { >                           b[i][k][m]=IloArray <IloArray <IloBoolVarArray > >(env,N); >                                     
    
    for(
    
    int j=0;j<N;j++)
    { >                                                b[i][k][m][j]=IloArray <IloBoolVarArray >(env,n[j]); >                                                   
    
    for(
    
    int l=0;l<n[j];l++) >                                                               b[i][k][m][j][l]=IloBoolVarArray(env,total); >                                     
    } > >                         
    } >             
    } >     
    } > > >           
    //Constraints                          >      
    
    for(
    
    int i=0;i<N;i++)
    { >            
    
    for(
    
    int k=0;k<n[i];k++)
    { >                         
    
    for(
    
    int m=0;m<total;m++)
    { >                                
    
    for(
    
    int j=0;j<N;j++)
    { >                                    
    
    for(
    
    int l=0;l<n[j];l++)
    { >                                                 
    
    for(
    
    int h=0;h<total;h++)
    { >                                                    IloExpr expr(env); >                                                     expr=b[i][k][m][j][l][h]-t[m][h]; >                                                          model.add(expr<=0); >                                                     expr.end(); >                                                
    } >                                     
    } >                             
    } >                     
    } >             
    } >     
    } > >        
    //Objection >         IloExpr profitSum(env); >    
    
    for(
    
    int i=0;i<N;i++)
    { >            
    
    for(
    
    int j=0;j<n[i];j++)
    { >                    profitSum+=r[i][j]*y[i][j]; >             
    } >     
    } >       model.add(IloMaximize(env, profitSum)); >       profitSum.end(); > >         IloCplex cplex(env); >       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; > 
    } > >
    

    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: has a problem when I run a short program coded with C++, can you help me ?

    Posted 08/15/12 10:11 PM

    Originally posted by: Ximenes


    I want to say" thank you" for your considerate reply. I have solved the problem.
    But I am facing a new problem. When I ran it, I was told an error: using empty handle. The whole revised codes(code on the front of the page is just part of my whole program and I have revised that)
    are as follows. Looking forward to your reply. Let me say a sincere "thank you" to you.
    
    #include <ilcplex/ilocplex.h>   ILOSTLBEGIN     typedef IloArray<IloNumArray> NumMatrix;     
    
    int main(
    
    int argc, char** argv) 
    { IloEnv env;   
    
    try 
    { 
    
    const char* filename;    
    //数据输入 
    
    if (argc > 1) filename = argv[1]; 
    
    else filename = 
    "C:\\Users\\Ximenes\\Desktop\\try1.txt"; ifstream f(filename, ios::in); 
    
    if (!f) 
    { cerr << 
    "No such file: " << filename << endl; 
    
    throw(1); 
    } IloNum T; IloNumArray2 r(env); IloNumArray2 p1(env); IloNumArray2 p2(env); IloNumArray2 s(env); f >> T; f >> r; f >> p1; f >> p2; f>>s; cout<<
    "T="<<T<<endl; cout<<
    "r="<<r<<endl; cout<<
    "p1="<<p1<<endl; cout<<
    "p2="<<p2<<endl; cout<<
    "s="<<s<<endl;   
    
    int N=r.getSize();
    //数据输入之后,就晓得了输入订单的类数,每类订单的个数,确定最多的订单数,接下来要做的就是补足订单 cout<<
    "N="<<N<<endl; 
    
    for(
    
    int i=0;i<N;i++)
    { cout<<
    "r["<<i<<
    "]="<<r[i]<<endl; cout<<
    "p1["<<i<<
    "]="<<p1[i]<<endl; cout<<
    "p2["<<i<<
    "]="<<p2[i]<<endl; 
    } IloIntArray n(env,N); IloInt total=0; 
    
    for(
    
    int i=0;i<N;i++)
    { n[i]=r[i].getSize(); cout<<
    "n["<<i<<
    "]="<<n[i]<<
    ","; total+=n[i]; 
    }   cout<<
    "total="<<total<<endl;       IloModel model(env); IloArray <IloArray <IloBoolVarArray > > z (env, N);  
    //z(i,j,k)将i类j订单安排在位置k 
    
    for(
    
    int i=0;i<N;i++)
    { z[i]=IloArray <IloBoolVarArray >(env,n[i]); 
    
    for(
    
    int j=0;j<n[i];j++)
    { z[i][j]=IloBoolVarArray (env,total); 
    } 
    }   IloArray <IloBoolVarArray > y(env,N);
    //定义变量y(i,j)是否选择i类j订单 
    
    for(
    
    int i=0;i<N;i++) y[i]=IloBoolVarArray(env,n[i]); IloArray <IloBoolVarArray > t(env,total);
    //定义变量t(m,i)  如t(1,3)表示是否在加工完1之后立即加工3,如果是则取1,这也意味着选择了在1号位和3号位上的订单,而没有选择2号位上的订单 
    
    for(
    
    int i=0;i<total;i++) t[i]=IloBoolVarArray(env,total);   IloArray <IloArray < IloArray <IloArray <IloArray <IloBoolVarArray > > > > > a (env, N); 
    //定义a   【a(i,k,m,j,l,h)=z(i,k,m)*z(j,l,h)】 
    
    for(
    
    int i=0;i<N;i++)
    { a[i]=IloArray < IloArray <IloArray <IloArray <IloBoolVarArray > > > >(env,n[i]); 
    
    for(
    
    int k=0;k<n[i];k++)
    { a[i][k]=IloArray <IloArray <IloArray <IloBoolVarArray > > >(env,total); 
    
    for(
    
    int m=0;m<total;m++)
    { a[i][k][m]=IloArray <IloArray <IloBoolVarArray > >(env,N); 
    
    for(
    
    int j=0;j<N;j++)
    { a[i][k][m][j]=IloArray <IloBoolVarArray >(env,n[j]); 
    
    for(
    
    int l=0;l<n[j];l++) a[i][k][m][j][l]=IloBoolVarArray(env,total); 
    } 
    } 
    } 
    }   IloArray <IloArray < IloArray <IloArray <IloArray <IloBoolVarArray > > > > > b (env, N);  
    //定义b   【b(i,k,m,j,l,h)=a(i,k,m,j,l,h)*t(m,h)】 
    
    for(
    
    int i=0;i<N;i++)
    { b[i]=IloArray < IloArray <IloArray <IloArray <IloBoolVarArray > > > >(env,n[i]); 
    
    for(
    
    int k=0;k<n[i];k++)
    { b[i][k]=IloArray <IloArray <IloArray <IloBoolVarArray > > >(env,total); 
    
    for(
    
    int m=0;m<total;m++)
    { b[i][k][m]=IloArray <IloArray <IloBoolVarArray > >(env,N); 
    
    for(
    
    int j=0;j<N;j++)
    { b[i][k][m][j]=IloArray <IloBoolVarArray >(env,n[j]); 
    
    for(
    
    int l=0;l<n[j];l++) b[i][k][m][j][l]=IloBoolVarArray(env,total); 
    } 
    } 
    } 
    } IloArray <IloArray < IloArray <IloArray <IloArray <IloBoolVarArray > > > > > c (env, N);  
    //定义c   【c(i,k,m,j,l,h)=a(i,k,m,j,l,h)*y(i,k)   】 
    
    for(
    
    int i=0;i<N;i++)
    { c[i]=IloArray < IloArray <IloArray <IloArray <IloBoolVarArray > > > >(env,n[i]); 
    
    for(
    
    int k=0;k<n[i];k++)
    { c[i][k]=IloArray <IloArray <IloArray <IloBoolVarArray > > >(env,total); 
    
    for(
    
    int m=0;m<total;m++)
    { c[i][k][m]=IloArray <IloArray <IloBoolVarArray > >(env,N); 
    
    for(
    
    int j=0;j<N;j++)
    { c[i][k][m][j]=IloArray <IloBoolVarArray >(env,n[j]); 
    
    for(
    
    int l=0;l<n[j];l++) c[i][k][m][j][l]=IloBoolVarArray(env,total); 
    } 
    } 
    } 
    }   IloArray <IloArray < IloArray <IloArray <IloArray <IloBoolVarArray > > > > > d (env, N);  
    //定义d   【d(i,k,m,j,l,h)=c(i,k,m,j,l,h)*y(j,l)   】 
    
    for(
    
    int i=0;i<N;i++)
    { d[i]=IloArray < IloArray <IloArray <IloArray <IloBoolVarArray > > > >(env,n[i]); 
    
    for(
    
    int k=0;k<n[i];k++)
    { d[i][k]=IloArray <IloArray <IloArray <IloBoolVarArray > > >(env,total); 
    
    for(
    
    int m=0;m<total;m++)
    { d[i][k][m]=IloArray <IloArray <IloBoolVarArray > >(env,N); 
    
    for(
    
    int j=0;j<N;j++)
    { d[i][k][m][j]=IloArray <IloBoolVarArray >(env,n[j]); 
    
    for(
    
    int l=0;l<n[j];l++) d[i][k][m][j][l]=IloBoolVarArray(env,total); 
    } 
    } 
    } 
    }     IloNumVarArray I(env,total-1,0,T);   
    //I,W表示类内 IloNumVarArray W(env,total,0,T); 
    //a,b,c,d约束 
    
    for(
    
    int i=0;i<N;i++)
    {                       
    //a(i,k,m,j,l,h)=z(i,k,m)*z(j,l,h)  
    
    for(
    
    int k=0;k<n[i];k++)
    { 
    
    for(
    
    int m=0;m<total;m++)
    { 
    
    for(
    
    int j=0;j<N;j++)
    { 
    
    for(
    
    int l=0;l<n[j];l++)
    { 
    
    for(
    
    int h=0;h<total;h++)
    { IloExpr expr(env); expr=a[i][k][m][j][l][h]-z[i][k][m]; model.add(expr<=0); expr.end(); 
    } 
    } 
    } 
    } 
    } 
    } 
    
    for(
    
    int i=0;i<N;i++)
    { 
    
    for(
    
    int k=0;k<n[i];k++)
    { 
    
    for(
    
    int m=0;m<total;m++)
    { 
    
    for(
    
    int j=0;j<N;j++)
    { 
    
    for(
    
    int l=0;l<n[j];l++)
    { 
    
    for(
    
    int h=0;h<total;h++)
    { IloExpr expr(env); expr=a[i][k][m][j][l][h]-z[j][l][h]; model.add(expr<=0); expr.end(); 
    } 
    } 
    } 
    } 
    } 
    } 
    
    for(
    
    int i=0;i<N;i++)
    { 
    
    for(
    
    int k=0;k<n[i];k++)
    { 
    
    for(
    
    int m=0;m<total;m++)
    { 
    
    for(
    
    int j=0;j<N;j++)
    { 
    
    for(
    
    int l=0;l<n[j];l++)
    { 
    
    for(
    
    int h=0;h<total;h++)
    { IloExpr expr(env); expr=a[i][k][m][j][l][h]-z[i][k][m]-z[j][l][h]+1; model.add(expr>=0); expr.end(); 
    } 
    } 
    } 
    } 
    } 
    }   
    
    for(
    
    int i=0;i<N;i++)
    {                    
    //b(i,k,m,j,l,h)=a(i,k,m,j,l,h)*t(m,h) 
    
    for(
    
    int k=0;k<n[i];k++)
    { 
    
    for(
    
    int m=0;m<total;m++)
    { 
    
    for(
    
    int j=0;j<N;j++)
    { 
    
    for(
    
    int l=0;l<n[j];l++)
    { 
    
    for(
    
    int h=0;h<total;h++)
    { IloExpr expr(env); expr=b[i][k][m][j][l][h]-a[i][k][m][j][l][h]; model.add(expr<=0); expr.end(); 
    } 
    } 
    } 
    } 
    } 
    } 
    
    for(
    
    int i=0;i<N;i++)
    { 
    
    for(
    
    int k=0;k<n[i];k++)
    { 
    
    for(
    
    int m=0;m<total;m++)
    { 
    
    for(
    
    int j=0;j<N;j++)
    { 
    
    for(
    
    int l=0;l<n[j];l++)
    { 
    
    for(
    
    int h=0;h<total;h++)
    { IloExpr expr(env); expr=b[i][k][m][j][l][h]-t[m][h]; model.add(expr<=0); expr.end(); 
    } 
    } 
    } 
    } 
    } 
    } 
    
    for(
    
    int i=0;i<N;i++)
    { 
    
    for(
    
    int k=0;k<n[i];k++)
    { 
    
    for(
    
    int m=0;m<total;m++)
    { 
    
    for(
    
    int j=0;j<N;j++)
    { 
    
    for(
    
    int l=0;l<n[j];l++)
    { 
    
    for(
    
    int h=0;h<total;h++)
    { IloExpr expr(env); expr=b[i][k][m][j][l][h]-a[i][k][m][j][l][h]-t[m][h]+1; model.add(expr>=0); expr.end(); 
    } 
    } 
    } 
    } 
    } 
    }   
    
    for(
    
    int i=0;i<N;i++)
    {                    
    //c(i,k,m,j,l,h)=a(i,k,m,j,l,h)*y(i,k) 
    
    for(
    
    int k=0;k<n[i];k++)
    { 
    
    for(
    
    int m=0;m<total;m++)
    { 
    
    for(
    
    int j=0;j<N;j++)
    { 
    
    for(
    
    int l=0;l<n[j];l++)
    { 
    
    for(
    
    int h=0;h<total;h++)
    { IloExpr expr(env); expr=c[i][k][m][j][l][h]-a[i][k][m][j][l][h]; model.add(expr<=0); expr.end(); 
    } 
    } 
    } 
    } 
    } 
    } 
    
    for(
    
    int i=0;i<N;i++)
    { 
    
    for(
    
    int k=0;k<n[i];k++)
    { 
    
    for(
    
    int m=0;m<total;m++)
    { 
    
    for(
    
    int j=0;j<N;j++)
    { 
    
    for(
    
    int l=0;l<n[j];l++)
    { 
    
    for(
    
    int h=0;h<total;h++)
    { IloExpr expr(env); expr=c[i][k][m][j][l][h]-y[i][k]; model.add(expr<=0); expr.end(); 
    } 
    } 
    } 
    } 
    } 
    } 
    
    for(
    
    int i=0;i<N;i++)
    { 
    
    for(
    
    int k=0;k<n[i];k++)
    { 
    
    for(
    
    int m=0;m<total;m++)
    { 
    
    for(
    
    int j=0;j<N;j++)
    { 
    
    for(
    
    int l=0;l<n[j];l++)
    { 
    
    for(
    
    int h=0;h<total;h++)
    { IloExpr expr(env); expr=c[i][k][m][j][l][h]-a[i][k][m][j][l][h]-y[i][k]+1; model.add(expr>=0); expr.end(); 
    } 
    } 
    } 
    } 
    } 
    }     
    
    for(
    
    int i=0;i<N;i++)
    {                    
    //d(i,k,m,j,l,h)=c(i,k,m,j,l,h)*y(j,l) 
    
    for(
    
    int k=0;k<n[i];k++)
    { 
    
    for(
    
    int m=0;m<total;m++)
    { 
    
    for(
    
    int j=0;j<N;j++)
    { 
    
    for(
    
    int l=0;l<n[j];l++)
    { 
    
    for(
    
    int h=0;h<total;h++)
    { IloExpr expr(env); expr=d[i][k][m][j][l][h]-c[i][k][m][j][l][h]; model.add(expr<=0); expr.end(); 
    } 
    } 
    } 
    } 
    } 
    } 
    
    for(
    
    int i=0;i<N;i++)
    { 
    
    for(
    
    int k=0;k<n[i];k++)
    { 
    
    for(
    
    int m=0;m<total;m++)
    { 
    
    for(
    
    int j=0;j<N;j++)
    { 
    
    for(
    
    int l=0;l<n[j];l++)
    { 
    
    for(
    
    int h=0;h<total;h++)
    { IloExpr expr(env); expr=d[i][k][m][j][l][h]-y[j][l]; model.add(expr<=0); expr.end(); 
    } 
    } 
    } 
    } 
    } 
    } 
    
    for(
    
    int i=0;i<N;i++)
    { 
    
    for(
    
    int k=0;k<n[i];k++)
    { 
    
    for(
    
    int m=0;m<total;m++)
    { 
    
    for(
    
    int j=0;j<N;j++)
    { 
    
    for(
    
    int l=0;l<n[j];l++)
    { 
    
    for(
    
    int h=0;h<total;h++)
    { IloExpr expr(env); expr=d[i][k][m][j][l][h]-c[i][k][m][j][l][h]-y[j][l]+1; model.add(expr>=0); expr.end(); 
    } 
    } 
    } 
    } 
    } 
    } 
    //时间约束 IloExpr expr(env); 
    
    for(
    
    int i=0;i<N;i++)
    { 
    
    for(
    
    int j=0;j<n[i];j++)
    { expr+=z[i][j][0]*y[i][j]*p1[i][j];   
    //第一项 expr+=p2[i][j]*y[i][j];              
    //第二项 
    } 
    } 
    
    for(
    
    int i=0;i<total-1;i++)
    {                       
    //第三项                     expr+=I[i]; 
    }   
    
    for(
    
    int m=0;m<total-1;m++)
    {                
    //setup 项  !!! 
    
    for(
    
    int h=m+1;h<total;h++)
    { 
    
    for(
    
    int i=0;i<N;i++)
    { 
    
    for(
    
    int k=0;k<n[i];k++)
    { 
    
    for(
    
    int j=0;j<N;j++)
    { 
    
    for(
    
    int l=0;l<n[j];l++) expr+=b[i][k][m][j][l][h]*s[i][j];
    //如:expr=z[0][0][0]*y[0][0]*z[0][0][1]*y[0][0]*s[0][0]*t[0][1] 
    } 
    } 
    } 
    } 
    } model.add(expr<=T); expr.end(); 
    //W.I.类间约束 
    
    for(
    
    int m=0;m<total-1;m++)
    { IloExpr expr(env); 
    
    for(
    
    int i=0;i<N;i++)
    { 
    
    for(
    
    int j=0;j<n[i];j++)
    { expr+=z[i][j][m+1]*y[i][j]*p1[i][j]-z[i][j][m]*y[i][j]*p2[i][j]; 
    } 
    } expr+=W[m+1]-W[m]-I[m]; model.add(expr==0); expr.end(); 
    }   
    
    for(
    
    int i=0;i<N;i++)
    {            
    //对订单i类j来说,总会被安排在某个位置k 
    
    for(
    
    int j=0;j<n[i];j++)
    { IloExpr expr(env); 
    
    for(
    
    int k=0;k<total;k++) expr+=z[i][j][k]; model.add(expr==1); expr.end(); 
    } 
    } 
    
    for(
    
    int k=0;k<total;k++)
    {       
    //对于每个位置k来说,总有一个订单被安排在了k   IloExpr expr(env); 
    
    for(
    
    int i=0;i<N;i++)
    { 
    
    for(
    
    int j=0;j<n[i];j++) expr+=z[i][j][k]; 
    } model.add(expr==1); expr.end(); 
    } 
    //与t相关的约束 
    
    for(
    
    int m=0;m<total-1;m++)
    { 
    
    for(
    
    int h=m+1;h<total;h++)
    { IloExpr expr(env); 
    
    for(
    
    int i=0;i<N;i++)
    {                    
    //d 
    
    for(
    
    int k=0;k<n[i];k++)
    { 
    
    for(
    
    int j=0;j<N;j++)
    { 
    
    for(
    
    int l=0;l<n[j];l++)
    { expr+=d[i][k][m][j][l][h]; 
    } 
    } 
    } 
    } expr-=t[m][h]; 
    } model.add(expr==0); expr.end(); 
    } 
    
    for(
    
    int m=0;m<total-1;m++)
    { IloExpr expr(env); 
    
    for(
    
    int h=m+1;h<total;h++) expr+=t[m][h]; model.add(expr<=1); expr.end(); 
    }       
    //最大化目标函数 IloExpr profitSum(env); 
    
    for(
    
    int i=0;i<N;i++)
    { 
    
    for(
    
    int j=0;j<n[i];j++)
    { profitSum+=r[i][j]*y[i][j]; 
    } 
    } model.add(IloMaximize(env, profitSum)); profitSum.end(); IloCplex cplex(env); 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; 
    }
    

    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: has a problem when I run a short program coded with C++, can you help me ?

    Posted 08/16/12 01:47 PM

    Originally posted by: EdKlotz


    > I want to say" thank you" for your considerate reply. I have solved the problem.
    > But I am facing a new problem. When I ran it, I was told an error: using empty handle.
    > The whole revised codes(code on the front of the page is just part of my whole program
    > and I have revised that)
    > are as follows. Looking forward to your reply.

    Which line of code prompts the "empty handle" exception to be thrown? You can find
    that out either with print statements or (preferably) using a debugger. Once you find
    out the particular line that caused the error message, you probably will be able to quickly
    identify the cause of the empty handle. I suspect that this involves one of the numerous
    multidimensional arrays of IloBoolVars you create. While a quick glance at some of that
    code looked OK to me, the most likely cause here would involve one of those multi dimensional arrays lacking complete initialization, which could result in such an error.

    I have one other comment about the code. While it probably is not essential to your
    program running properly, it will save you some memory, and the distinction between
    handles to an object and the object themselves is worth making. You have a lot of
    code such as

    
    IloExpr expr(env); expr=b[i][k][m][j][l][h]-t[m][h]; model.add(expr<=0); expr.end();
    


    This actually creates two separate IloExprs. The line
    
    IloExpr expr(env);
    
    creates a handle to a NULL
    expression to which you can add other IloExprs. The line
    
    expr=b[i][k][m][j][l][h]-t[m][h];
    
    then sets
    this handle to point to a new IloExpr created by the - operator
    applied to the two IloBoolVars b[i][k][m][j][l][h] and t[m][h].
    That's what you want, and your next two lines use the IloExpr
    to which the handle expr points to add a constraint to the
    model and end that IloExpr. But, you still have that NULL
    original IloExpr out there. So, the loop will either unnecessarily
    create and destroy that IloExpr, or it will actually leak some memory.
    Either way, I think you could write this loop more efficiently as follows:

    
    IloExpr expr=b[i][k][m][j][l][h]-t[m][h]; model.add(expr<=0); expr.end();
    


    This creates only one IloExpr in each loop, not two. Given that this code
    segment is embedded in nested loops of dimension 5 or 6, perhaps the savings
    will be significant. For more information on this distinction between
    handle to Null extractables versus empty handles, see the reference manual
    documentation on the IloExpr class.
    #CPLEXOptimizers
    #DecisionOptimization