0

i want to model the following constraint in CHOCO. Any help please

    //cSelected[i]=1 ==> ∀ j∈{1,…,i-1} xCategory[i]!= xCategory[j] 
      cSelected[i]=0, otherwise

this is what i am trying to do

        int[] x  =      new int[]{0,  1, 0,  0, 1, 0};
        int[] xCategory  = new int[]{3,  1, 2,  2, 3, 0};
        int[] xCategorySize  = new int[]{0,  100, 200,  300};
        IntVar[] cSelected = model.intVarArray("d", 6, 0, 1);

    // 3. Post constraints
        // i want to add and post this constraint:
        //cSelected [i]=1 ==> ∀ j∈{1,…,i-1} xCategory[i]!= xCategory[j] 
        //cSelected [i]=0, otherwise

        for (int i=0; i<x.length;i++)
        sum += (1-x[i]) * cSelected[i].getValue() * xCategorySize[xCategory[i]];
letraitre
  • 3
  • 2

1 Answers1

0

You'll need to reify the constraint for each test xCategory[i]!= xCategory[j] and put the result of the reification in a BoolVar contained in a BoolVar[][] cMatrix. Then you post an or constraint of every vector BoolVar[] in that matrix, saving it in your cSelected (which also is a BoolVarArray, not an IntVarArray):

for (int i=0; i<x.length;i++){
    for (int j=0; j<x.length;j++){
        xCategory[i].neq(xCategory[j]).reifyWith(cMatrix[i][j]);
    }
    cSelected[i] = model.or(cMatrix[i]).reify();
}
Philip Harding
  • 392
  • 1
  • 10
  • Thank you! I am new to constraint programming and choco solver. Still trying to figure out the doc and examples. However, your solution looks like an "imperative paradigm" solution to the problem. I thought that in constraint programming there might be specific constraints to solve the problem (S[i] = 1 <=> ∀ j∈{1,…,i-1} T[i]≠ T[j]) just by specifying it without precising how it will be solved. In any case, thank you very much – letraitre Dec 07 '18 at 10:28