使用 Choco Solver 对约束建模

Modeling a constraint using Choco Solver

我想在 CHOCO 中模拟以下约束。请帮忙

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

这就是我想要做的

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

您需要为每个测试 xCategory[i]!= xCategory[j] 重新定义约束,并将具体化的结果放入包含在 BoolVar[][] cMatrix 中的 BoolVar 中。然后你 post 该矩阵中每个向量 BoolVar[]or 约束,将其保存在你的 cSelected 中(这也是 BoolVarArray,而不是 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();
}