Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  All Diff of VarArray

    Posted 06/29/14 06:29 AM

    Originally posted by: medistif@hotmail.fr


    I want to express a constraint for that three BoolVarArray whose must be differents, these arrays are the solutions. so i search a constraint like allDiff in gecode.

    IloBoolVarArray V1(env, nbrT);

    IloBoolVarArray V2(env, nbrT);

    IloBoolVarArray V3(env, nbrT);

    constraint like this : Diff (V1, V2, V3) ??

    if someone has any idea for how can i express it, thank you.


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: All Diff of VarArray

    Posted 06/29/14 04:35 PM

    I'm not aware of any MIP solver or MIP modeling language that contains a "global" constraint like that. It can be modeled by adding three additional boolean variable arrays of dimension nbrT (or, more generally n-choose-2 arrays where n is the number of arrays in the allDiff statement).

    Let W12, W23 and W13 be boolean variables of dimension nbrT. I'll show how to code W12 (which is used to ensure that V1 and V2 differ); the other two are done identically. For j = 0, ..., nbrT-1 add the constraints

    W12[j] <= V1[j] + V2[j]

    and

    W12[j] <= 2 - V1[j] - V2[j],

    which combine to ensure that W12[j] == 1 only when V1[j] != V2[j]. Then add one more constraint:

    sum_j W12[j] >= 1

    (forcing V1 and V2 to differ in at least one entry).

    Paul


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: All Diff of VarArray

    Posted 06/30/14 07:17 AM

    Originally posted by: medistif@hotmail.fr


    Hello Paul, thank you for your answer, it's a good way to model this constraint.
    the probleme is when a used it on Cplex it doesn't work, i don't know where is the problem, i share you the code on cplex perhaps you will see where i made an error.
    IloArray < IloBoolVarArray > matrice (env);
    for ( int i = 0; i <= 2; i++ ) {
          matrice.add( IloBoolVarArray ( env , nbrT ) );
    }
    IloBoolVarArray W12 (env, nbrT);
    
    IloBoolVarArray W23 (env, nbrT);
    
    IloBoolVarArray W13 (env, nbrT);
    
    for (int i=0; i<nbrT; i++){
    
    W12[i] <= matrice[0][i] + matrice[1][i];
    W12[i] <= (2 - matrice[0][i]) - matrice[1][i];
    
    W23[i] <= matrice[1][i] + matrice[2][i];
    W23[i] <= (2 - matrice[1][i])  - matrice[2][i];
    
    W13[i] <= matrice[0][i] + matrice[2][i];
    W13[i] <= (2 - matrice[0][i]) - matrice[2][i];
    
    }
    
    model.add (IloSum(W12) >= 1 );
    model.add (IloSum(W23) >= 1 );
    model.add (IloSum(W13) >= 1 );
    

    after solving the arrays martice[0], martice[1], martice[2] have the same values.

    so if you have any suggestion, thank you

     

    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: All Diff of VarArray

    Posted 06/30/14 10:35 AM

    I don't use the C++ API, so I'm guessing here. You explicitly added the summation constraints to the model in lines 24-26. Do you not have to add the other constraints (by invoking model.add()) in 13-20?


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: All Diff of VarArray

    Posted 07/01/14 03:12 PM

    Originally posted by: medistif@hotmail.fr


    Yes Mr Paul my error is i forgot to add the other constraints (by invoking model.add()), thank you for this comment.

    i have an other question, here the principle is to treat the Arrays in pairs, but in general case where there are for example 100 arrays, this way can be used or reformulate ?

    Thank you for your answers.


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: All Diff of VarArray

    Posted 07/01/14 04:43 PM

    This method will work with 100 arrays, but you will get 100-choose-2 = 4950 W arrays and a comparable number of constraints.

    If nbrT is small (and you have 100 arrays), an alternative is to define a single integer variable z[i] for i = 0,...,99 and a single binary variable w[i][k] for each i,k=0,...99, i != k (so 4950 w variables). Add the constraint

    z[i] = sum_j 2^j * matrice[i][j]

    (summing over j = 0, ..., nbrT), which essentially treats each of your original binary vectors as a bit string and assigns to z[i] the integer that bit string represents. For each i !=k, add the constraints

    z[i] >= z[k] + 1 - M*w[i][k]

    z[k] >= z[i] + 1 - M*(1-w[i][k])

    with M = 2^nbrT.

    That only works if 2^nbrT does not exceed the maximum value of an IloInt variable, and will produce very weak relaxations for large values of nbrT.


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: All Diff of VarArray

    Posted 07/02/14 07:55 AM

    Originally posted by: medistif@hotmail.fr


    Sorry Paul but i didn't undertsand this generalization, espacialy 

    the alternative to define a single integer variable z[i] for i = 0,...,99 and a single binary variable w[i][k] for each i,k=0,...99, i != k (so 4950 w variables). Add the constraint

    z[i] = sum_j 2^j * matrice[i][j]

    Sp please can you give me an example with just 4 or 5 arrays !

    Thanks.


    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: All Diff of VarArray

    Posted 07/06/14 07:43 PM

    Suppose nbrT = 3. The constraints defining z are

    z[0] = matrice[0][0] + 2*matrice[0][1] + 4*matrice[0][2]

    z[1] = matrice[1][0] + 2*matrice[1][1] + 4*matrice[1][2]

    z[2] = matrice[2][0] + 2*matrice[2][1] + 4*matrice[2][2]

    etc., and M can be set to 2^3=8 (since 0 <= z[i] <= 7 for all i).

    To force matrice[0] and matrice[1] to be distinct, you need z[0] != z[1], which is enforced by

    z[0] >= z[1] + 1 - 8*w[0][1]

    z[1] >= z[0] + 1 - 8*(1-w[0][1]).

    If w[0][1] = 0, z[0] has to be at least 1 larger than z[1] (and the second constraint is vacuous). if w[0][1] = 1, z[1] has to be at least 1 larger than z[0] (and the first constraint is vacuous). Repeat this with z[0], z[2] and w[0][2], and again with z[1], z[2] and w[1][2].


    #CPLEXOptimizers
    #DecisionOptimization