constraint-programmingchoco

Modeling a constraint using Choco Solver


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]];

Solution

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