Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Cplex in vs2012: constraints not satisfied

    Posted 12/07/14 05:17 PM

    Originally posted by: Ghizlane


    Hello,

    I am traying to code a mixed integer lineair mathematical program in Cplex using C++ in VS2012. The problem is that the programm give me a solution that do not satisfy certain constraints.

    The C++ code is:

    #include <ilcplex\ilocplex.h>
    #include <stdlib.h>
    #include <time.h >
    #include <stdio.h>
    #include <iostream>
    #include <vector>
    #include <string>
    #include <fstream>
    #include <math.h>
    using namespace std ;


    typedef IloArray<IloNumVarArray> NumVarMatrix;
    typedef IloArray<IloNumArray> NumMatrix;
    const int max_entier = std::numeric_limits<int>::max();
     int m=4,n=4,M=max_entier;
    int P[16]={54,34,61,2,9,15,89,70,38,19,28,87,95,34,7,29};
     
    ILOSTLBEGIN
    int main(int argc, char** argv) {
         IloEnv env;
    srand( time( NULL ) );
    double debut, fin;  
    debut = clock();    
          
        IloInt nbjobs=n;
        IloInt nbmachines=m;
       try {
      IloModel model(env);
     
    // creation of variables
    IloNumVarArray  D(env,n*n,0,1,ILOINT);
    IloNumVarArray  S(env,m*n+1,0,max_entier,ILOFLOAT);
    NumVarMatrix  q(env, nbmachines);

    // variable q
      for (int j = 0; j <nbmachines; j++)
        {
           q[j]=IloNumVarArray(env, nbjobs*nbjobs,0,max_entier,ILOFLOAT);     
        }
         for (int r = 0; r < nbmachines; r++)
        {
            for(int k=0;k<nbjobs;++k)
            {
                for(int i=0;i<k;++i)
                {
          q[r][k*n+i] = IloNumVar(env,0,M-P[r*n+i]-P[r*n+k],ILOFLOAT);
                }
            }
         }
    //contrainte 1:
    for (int r=0;r<nbmachines-1;++r)
    {
        for(int i=0;i<nbjobs;++i)
        {
        IloExpr expr1(env);
        IloExpr expr2(env);
        expr1=S[r*n+i]+P[r*n+i];
        expr2=S[(r+1)*n+i];
    model.add(expr1<=expr2);
        expr1.end();
        expr2.end();
        }
    }
    //contrainte 2:
    for (int r=0;r<nbmachines;++r)
    {
        for(int k=0;k<nbjobs;++k)
            {
                for(int i=0;i<k;++i)
                {
        IloExpr expr1(env);
        IloExpr expr2(env);
        expr1=S[r*n+i]-S[r*n+k]-P[r*n+k]+M*D[k*n+i];
        expr2=q[r][k*n+i];    
        model.add(expr1==expr2);
        expr1.end();
        expr2.end();
                }
        }
    }
    //constraint 3:
    for (int i=0;i<nbjobs;++i)
    {  
        IloExpr expr1(env);
       IloExpr expr2(env);
    expr1=S[(m-1)*n+n];
    expr2=S[(m-1)*n+i]+P[(m-1)*n+i];
    model.add(expr1>=expr2);
        expr1.end();
        expr2.end();
    }
    //objective function :
    IloExpr expr1(env);
        expr1=S[(m-1)*nbjobs+n];

    model.add(IloMinimize(env, expr1));
    expr1.end();
    ////////solvind the problem

     
    IloCplex cplex(model);

    cplex.solve ();

    IloNum objval = cplex.getObjValue ();
    cout <<"objective value"<< objval << "\n" ;
    // Values of variables :
    IloArray<IloNum> D1(env ,nbjobs*nbjobs) ;
    for(int k=0;k<nbjobs;++k)
            {
                for(int i=0;i<k;++i)
                {
         D1[k*nbjobs+i]=cplex.getValue(D[k*nbjobs+i]);
     }
        
     }
     cout << D1 << "\n" ;
     IloArray<IloNum> S1(env ,m*n+1) ;
    for (int r=0;r<nbmachines;++r)
    {
        for(int i=0;i<nbjobs;++i)
        {
         S1[r*nbjobs+i]= cplex.getValue(S[r*nbjobs+i]);
     }
        
     }
     cout << S1 << "\n" ;
      NumMatrix q1(env,nbmachines);
      for (int j = 0; j <nbmachines; j++)
        {
           q1[j]=IloNumArray(env, nbjobs*nbjobs);     
        }
     for (int r = 0; r < nbmachines; r++)
        {
            for(int k=0;k<nbjobs;++k)
            {
                for(int i=0;i<k;++i)
                {
          q1[r][k*n+i] = cplex.getValue(q[r][k*n+i]);
                }
            }
         }
     cout << q1 << "\n" ;

    //verify the values of constraints 2
     for (int r=0;r<nbmachines;++r)
    {
        for(int k=0;k<nbjobs;++k)
            {
                for(int i=0;i<k;++i)
                {
        cout<<" Constraints2: expr1= "<<S1[r*n+i]-S1[r*n+k]-P[r*n+k]+M*D1[k*n+i];
        cout<<"--------- expr2= "<<q1[r][k*n+i]<<endl;    
                }
        }
    }
    fin = clock();
    double temps_cpu = (fin-debut) / CLOCKS_PER_SEC ;


    }
         catch (IloException& e) {
    cerr << "Concert Exception: " << e << endl;
    } catch (...) {
    cerr << "Other Exception" << endl;
    }
        


    env.end();


     system ("pause");
     return 0 ;
    } // END main

    So when we solve this problem, we get solutions that dont verify the constraints 2. I cannot find where the error is exactelly, could you help me please.

    Best regards,


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Cplex in vs2012: constraints not satisfied

    Posted 12/07/14 06:27 PM

    Originally posted by: Laci Ladanyi


    You are using a big-M formulation with M being INT_MAX, which is ~10^9. While that works in theory, in practice it leads to all sort of numerical troubles. Try to chose as small as possible M that woul still guarantee correctness. Or even better, try to use indicator constraint.


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Cplex in vs2012: constraints not satisfied

    Posted 12/08/14 06:01 AM

    Originally posted by: Ghizlane


    I have changed the value of M and it works very well, thank you so much @Laci Ladanyi. I dont have any idea about the use of "indicator constraint.", i will try to use that.

    Best regards


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Cplex in vs2012: constraints not satisfied

    Posted 12/08/14 08:23 AM

    Originally posted by: Laci Ladanyi


    I just noticed something... In your code you have "model.add(expr1==expr2)" where expr1 contains the big-M. Are you sure that the equality requirement is correct? That would be highly unusual for a big-M formulation. 

    In any case you can read about indicator constraints here: http://www-01.ibm.com/support/knowledgecenter/SS9UKU_12.6.0/com.ibm.cplex.zos.help/CPLEX/UsrMan/topics/discr_optim/indicator_constr/02_indicators_defn.html?lang=en


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Cplex in vs2012: constraints not satisfied

    Posted 12/08/14 09:16 AM

    Originally posted by: Ghizlane


    Yeah, because in this constraint, we have modeled  two disjoint constraints, this is why expr1 contains the big-M. Thank you for the link.

    Best regards,


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Cplex in vs2012: constraints not satisfied

    Posted 12/08/14 03:32 PM

    Originally posted by: Ghizlane


    I am traying to use indecators to model two disjoint constraints instead  using big M but I have problem in the syntax.

    so I have these to constraints witch are disjoint: S_rk-S_ri-T_ri>=0 if Dik=1

                                                                                            S_ri-S_rk-T_rk>=0 if Dik=0

    the code in C++:

    IloNumVarArray  D(env,n*n,0,1,ILOINT);
    IloNumVarArray  S(env,m*n+1,0,max_entier,ILOFLOAT);

    //contrainte 2:
    for (int r=0;r<nbmachines;++r)
    {
        for(int k=0;k<nbjobs;++k)
            {
                for(int i=0;i<k;++i)
                {
                  if(D[k*n+i]==0)
                  {
        IloExpr expr1(env);
        expr1=S[r*n+i]-S[r*n+k]-P[r*n+k];
        model.add(expr1>=0);
        expr1.end();
                  }
                  else
                  {
        IloExpr expr1(env);
        expr1=S[r*n+k]-S[r*n+i]-P[r*n+i];
        model.add(expr1>=0);
        expr1.end();
                  }

                }
        }
    }

     

    and I get this error:  error C2451: conditional expression of type 'IloRange' is illegal
                 No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Cplex in vs2012: constraints not satisfied

    Posted 12/12/14 04:17 AM

    You should use instances of IloIfThen to model this constraint. Use the Dik==0/1 condition as left expression and the linear constraints as right expressions.


    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: Cplex in vs2012: constraints not satisfied

    Posted 12/13/14 12:37 PM

    Originally posted by: Ghizlane


    and we will declare 'Dik' as a variable?


    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: Cplex in vs2012: constraints not satisfied

    Posted 01/06/15 03:48 AM

    Yes, 'Dik' should be boolean variables.


    #CPLEXOptimizers
    #DecisionOptimization