Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  question about cplex concert C++ logic or constraint

    Posted 03/06/15 03:01 AM

    Originally posted by: wzyryan


    With the following code, will I be able to add 256*256 logic or constraints to the model?

    I am a beginner, please help me.

     

    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);
    }
    }

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: question about cplex concert C++ logic or constraint

    Posted 03/10/15 07:06 PM

    Originally posted by: EdKlotz


    The code above doesn't state what the classes assocated with and1, and2 and or.   But, the nested loop you have written will execute 256*256 = 65536 times, each time creating 4 new constraints.  So, you have a total of about 260000 constraints, which Concert C++ should be able to manage.  However, I doubt you really want the or.add and model.add statements insides your loop; if I understand the code correctly by assuming that and1 and and2 are IloRangeArrays or IloConstraintArrays, then you would want to add them to the or and model objects outside the loop.   Otherwise you would be adding all sorts of duplicate constraints.    Besides wasting time, this might be enough to exhaust memory on your machine and prevent you program from running even though you could actually create the model.   


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: question about cplex concert C++ logic or constraint

    Posted 03/10/15 09:42 PM

    Originally posted by: wzyryan


    Hi EdKlotz,

           Thx for your reply!  In my code, and1 and and2 are associated with IloAnd while "or" is associated with IloOr. Together with "or",  I want to implement the following logic constraint (A&&B)||(C&&D). Assuming there are 256*256 points, what I want to do is adding the constraint of each point to the model. Will I achieve my goal with the above code? 

    regards,

    Ryan


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: question about cplex concert C++ logic or constraint

    Posted 03/12/15 02:29 AM

    If I understand correctly the description of your problem then your code is almost right. You only need to make sure to instantiate IloOr and IloAnd within the innermost loop. The code you have in your original post just adds the same constraint over and over again. Here is what I think would be the correct code:

    for(int p=0;p<256;p++){
       for(int q=0;q<256;q++){
          IloAnd and1(env);
          and1.add(mat[p][q] >= t[0]);
          and1.add(fmat[p][q] >= t[0]);
     
          IloAnd and2(env);
          and2.add(mat[p][q] <= t[1]);
          and2.add(fmat[p][q] <= t[1]);
    
          IloOr or(env);
          or.add(and1);
          or.add(and2);
     
          model.add(or); /* Adds constraint
                          *      (mat[p][q] >= t[0] AND fmat[p][q] >= t[0])
                          *                   OR
                          *      (mat[p][q] <= t[1] AND fmat[p][q] <= t[1])
                          */
       }
    }
    

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: question about cplex concert C++ logic or constraint

    Posted 03/12/15 05:50 AM

    Originally posted by: wzyryan


    Hi Daniel,

    THX for your guidance. It can be seen from the printed information in the attachments that your code works. However, while your code executes differently with mine, their results are the same (i.e. Max=1, t[0]=255, t[1]=254). Those information can be seen in the pictures attached below.

    More of my code can be seen in this post another post. It seems that the logic constraints does not work because t[0] should not reach 255 (i.e. the biggest value of mat[p][q] is 254).

    Regards,

    Ryan


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: question about cplex concert C++ logic or constraint

    Posted 03/12/15 08:27 PM

    Originally posted by: EdKlotz


    I think think the solution and objective values are consistent with the model created by your and Daniel's code.  For each (p,q) pair, you are expressing the constraint:

    *      (mat[p][q] >= t[0] AND fmat[p][q] >= t[0])
                          *                   OR
                          *      (mat[p][q] <= t[1] AND fmat[p][q] <= t[1])

     

    Furthermore, the upper bounds on t[0] and t[1] are both 255, as seen from the code you sent below:

            t.add(IloIntVar(env,0,255));
            t.add(IloIntVar(env,0,255));

    So, for each OR constraint, we can choose to satisfy the condition

    mat[p][q] <= t[1] AND fmat[p][q] <= t[1]

    while leaving the corresponding condition on t[0] unsatisfied.   This leaves us free to set t[0] to its upper bound of 255.  Since the objective is

     model.add(IloMaximize(env, t[0]-t[1]));

    we then want to set t[1] as small as possible.   If the maximum value of mat is 254, and nothing in fmat prevents us from setting t[1] to 254, then the optimal solution to this model is indeed what was reported; t[0] = 255, t[1] = 254, and an objective value of 1.0.

    So, it looks to me like CPLEX correctly solves the model given to it, but that model is not quite the one you actually want to solve.

    Regarding more general recommendations for troubleshooting this type of unexpected results, I have three recommendations:

    • If possible, try it on a much smaller data set, with the dimensions of the arrays being say 8x8 instead of 256x256.
    • Use the IloCplex::exportModel to export an LP file of the model so you can look at it.
    • Experiment with simplified versions of the model by commenting out subsets of the constraints.

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: question about cplex concert C++ logic or constraint

    Posted 03/12/15 10:41 PM

    Originally posted by: wzyryan


    Thanks for your analysis. Now I know there is some problem with my logic expression. The logic model I want to implement is :

    if (mat[p][q]>=t[0])  add constraint "fmat[p][q]>=t[0];" to model

    if(mat[p][q])<=t[1])  add constraint "fmat[p][q]<=t[1];" to model

    Do you have any idea on how can I implement this logic? Obviously, the logic constraints I used before is wrong. :-(

    I tried to add the constraint using model.add(IloIfThen(env, mat[p][q] >= t[0], fmat[p][q] >= t[0])); it didn't work

                                                                 model.add(IloIfThen(env, mat[p][q] <= t[1], fmat[p][q] <= t[1]));

    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: question about cplex concert C++ logic or constraint

    Posted 03/20/15 03:38 AM

    What exactly do you mean by "it did not work"? Did it compile? Did it not find a solution? Did it find a solution but the solution was unexpected?

    From what you wrote your usage of IloIfThen looks correct to me.


    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: question about cplex concert C++ logic or constraint

    Posted 03/12/15 05:58 AM

    Originally posted by: wzyryan


    #include <ilcplex/ilocplex.h>
    #include <ilcplex/ilocplex.h>
    #include <ilcp/cpext.h>
    #include <ilconcert/ilosys.h>
    #include <ilconcert/ilonumfunc.h>
    #include <ilcplex/ilocplexi.h>
    #include <fstream>
    
    ILOSTLBEGIN
    
    int main(){
        IloEnv env;
            
            IloInt w=128;
            IloInt h=128;
        typedef IloArray<IloIntArray>    IntMatrix;
            IntMatrix mat(env, w);
            for(IloInt i=0;i<w;i++){
                    mat[i]=IloIntArray(env,h);
            }
    
            ifstream inf("out.txt");
            
            if(!inf) cout<<"error"<<endl;
    
        for(IloInt j=0;j<h;j++){
            for(IloInt i=0;i<w;i++){
                inf>>mat[i][j];
                            //图像灰度化,储存像素点的灰度信息
            }
        }
            inf.close();
    
            typedef IloArray<IloIntVarArray> IntVarMatrix;  
            IloInt i,j;
            IloInt n=4;
            IloInt width=32;
            IloInt height=32;
            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++){
                    for(j=0;j<h;j++){
                            fmat[i][j]=IloNumExpr(env);
                    }
            }
    
            IloModel model(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;  //实现图像边缘的3个像素范围的遍历
                           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;
                           }
                                               //sum=0;
                           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){
                                   //if(((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))<9){
                                       //fmat[n*i+p][n*j+q] = IloExpr(env);
                                                                       fmat[n*i+p][n*j+q] += (hmat[x][y])*(exp(-((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;     //小像素点的光强的叠加计算
                                   }
                               }
                           }
                                               //fmat[n*i+p][n*j+q]=sum;
                          
                           //qDebug()<<tr("%1").arg(f);
                        }
                    }
                    //qDebug()<<tr("%1").arg(nb);
                   
                }
            }
    
                 
            for(int p=0;p<w;p++){
       for(int q=0;q<h;q++){
          //IloAnd and1(env);
          and1.add(mat[p][q] >= t[0]);
          and1.add(fmat[p][q] >= t[0]);
     
          //IloAnd and2(env);
          and2.add(mat[p][q] <= t[1]);
          and2.add(fmat[p][q] <= t[1]);
    
          //IloOr or(env);
          or.add(and1);
          or.add(and2);
     
          model.add(or); /* Adds constraint
                          *      (mat[p][q] >= t[0] AND fmat[p][q] >= t[0])
                          *                   OR
                          *      (mat[p][q] <= t[1] AND fmat[p][q] <= t[1])
                          */
       }
    }
            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();
    
            
    
            for(i=0;i<w;i++){
                    fmat[i].endElements();
            fmat[i].end();
            }
            fmat.end();
    
            cout<<"Max="<<cplex.getObjValue()<<endl;   
            cout<<cplex.getValue(t[0])<<" "<<cplex.getValue(t[1])<<endl;
            
            cplex.getStatus();
            env.end();
            getchar();
    }
    

     


    #CPLEXOptimizers
    #DecisionOptimization