Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  cplex concert c++ memory leakage

    Posted 03/10/15 10:37 PM

    Originally posted by: wzyryan


    platform vs2010+cplex12.5+qt4.8.1

    When I run my code, there is a probem. It says Unhandled exception at 0x7531b727 in ImageLoading.exe: Microsoft C++ exception: IloCplex::Exception at memory location 0x0043c62c.. I've been stuck here for a day. I list a part of my code below.

    IloEnv env;
    typedef IloArray<IloIntArray>    IntMatrix;
    IntMatrix mat(env, w);
    for(IloInt i=0;i<w;i++){                           //w=256,h=256
    mat[i]=IloIntArray(env,h);
    }
    
        for(IloInt j=0;j<h;j++){
            for(IloInt i=0;i<w;i++){
                QRgb pixel=image->pixel(i,j);
                IloInt r=qRed(pixel)*0.3;
                IloInt g=qGreen(pixel)*0.59;
                IloInt b=qBlue(pixel)*0.11;
                mat[i][j]=r+g+b;   
            }
        }
    
    typedef IloArray<IloIntVarArray> IntVarMatrix;   
                          
    IloInt i,j;
    IloInt n=4;
    IloInt width=64;
    IloInt height=64;
    IntVarMatrix hmat(env, width); 
    for(i=0;i<width;i++){
    hmat[i]=IloIntVarArray(env,height,0,255);
    }
    
    typedef IloArray<IloNumExprArray> NumExprMatrix;
    NumExprMatrix fmat(env, w);
    for(i=0;i<w;i++){
    fmat[i]=IloNumExprArray(env,h);
    }
    for(i=0;i<w;i++){                                //initializing fmat[][]
    for(j=0;j<h;j++){
    fmat[i][j]=IloNumExpr(env);
    }
    }
    
    IloModel model(env);              //to construct a modeling object named model, within the environment env
    IloIntVarArray t(env);
    IloRangeArray c(env);
    
    IloAnd and1(env);
    IloAnd and2(env);
    IloOr or(env);
    t.add(IloIntVar(env,0,255));
    t.add(IloIntVar(env,0,255));
    c.add( t[0]-t[1]>=0 );
    
    for(j=0;j<height;j++){
                for(i=0;i<width;i++){    //big pixel
                    //IloInt count=0;
                    for(IloInt q=0;q<n;q++){
                        for(IloInt p=0;p<n;p++){    //small pixel
                           //for computing the light intensity
    
                           IloInt x,y,startx,starty,boundryx,boundryy; 
                           if(j>=3){
                               starty=j-3;
                           }else{
                               starty=0;
                           }
                           if(i>=3){
                               startx=i-3;
                           }else{
                               startx=0;
                           }
                           if(j+3>height){
                               boundryy=height;
                           }else{
                               boundryy=j+3;
                           }
                           if(i+3>width){
                               boundryx=width;
                           }else{
                               boundryx=i+3;
                           }
    
                           for(y=starty;y<boundryy;y++){
                               for(x=startx;x<boundryx;x++){     //convolution range
                                   if(((n*x+n/2-n*i-p)*(n*x+n/2-i*n-p)+(n*y+n/2-j*n-q)*(n*y+n/2-j*n-q))<=9*n*n){
                                   
                  fmat[n*i+p][n*j+q] += (hmat[x][y])*(qExp(-((x+1/2-i-(IloNum)p/n)*(x+1/2-i-(IloNum)p/n)+(y+1/2-j-(IloNum)q/n)*(y+1/2-j-(IloNum)q/n))/2))/6.28;     //小像素点的光强的叠加计算
                                   }
                               }
                           }
    
                        }
                    }
                   
                }
            }
    
        
    for(int p=0;p<256;p++){
    for(int q=0;q<256;q++){
    
    and1.add(mat[p][q] >= t[0]);
    and1.add(fmat[p][q] >= t[0]);
    
    and2.add(mat[p][q] <= t[1]);
    and2.add(fmat[p][q] <= t[1]);
    
    or.add(and1);
    or.add(and2);
    
    model.add(or);
    }
    }
    model.add(c);
    
    //IloObjective obj = IloMaximize(env,t[0]-t[1]);
    model.add(IloMaximize(env, t[0]-t[1]));
    //model.add(obj);
    IloCplex cplex(model);
    cplex.solve();
    
    
    cout<<"Max="<<cplex.getObjValue();<<endl;   //Here occurs the exception
    
    //cplex.getStatus();
    for(i=0;i<width;i++){
    for(j=0;j<height;j++){
    cout<<cplex.getValue(hmat[i][j])<<endl;
    }
    }
    env.end();
    
     

    I know that if I define an IloExpr expr(env), I have to end it using expr.end(). In my code, I define an IloNumExprArray fmat[][]. I wonder if it is because that I did not end this array.


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: cplex concert c++ memory leakage

    Posted 03/12/15 03:19 AM

    How about figuring out what exactly that exception is and at which line of your code it is thrown? Any debugger should allow you to find the line that throws the exception.

    You could also wrap your code into a try/catch block, catch the exception and print its message:

    try {
        // Your code here
    }
    catch (IloException e) {
        std::cout << "Exception: " << e << std::endl;
    }
    

    The exception message will probably give a good idea about what is going wrong.


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: cplex concert c++ memory leakage

    Posted 03/12/15 03:52 AM

    Originally posted by: wzyryan


    Hi Daniel,

            Thank you very much for your reply! I guess it is because there are two many constraints. Now, I use 128*128 instead of 256*256, and no exception occurs. However, I have another question. As you can see, my objective is "IloMaximize(env, t[0]-t[1])". I can get the value of maximization result by calling cplex.getObjValue(). Do you know how can I get the exact value of t[0] and t[1] in the maximization status of "t[0]-t[1]"? 

    Regards,

    Ryan


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: cplex concert c++ memory leakage

    Posted 03/12/15 04:34 AM

    You should take a look at the various examples that are shipped with CPLEX (see the cplex/examples/src/cpp folder in your installation). They illustrate how to do things like that.

    To get the values of t[0] and t[1] just do

    std::cout << "t[0] = " << cplex.getValue(t[0]) << std::endl
              << "t[1] = " << cplex.getValue(t[1]) << std::endl;
    

    Also see the reference documentation.


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: cplex concert c++ memory leakage

    Posted 03/12/15 04:58 AM

    Originally posted by: wzyryan


    Thanks for your reply and advice. I will study those examples. :-)


    #CPLEXOptimizers
    #DecisionOptimization