簡體   English   中英

Choco Solver ICF約束定義數組中最小和最大變量之間的距離

[英]Choco Solver ICF constraint to define the distance between min and max variable in an array

我有一個Choco求解器IntVar變量數組,例如X1,X2,...,Xn。 我需要定義一個強制規則的約束-最小值和最大值之間的距離(絕對差)值應小於固定值,例如100,即| max(X1,.. Xn)-min(X1, ..,Xn)| <100。

有人可以幫忙嗎?

您可以使用IntConstraintFactory.distance()方法來精確地做到這一點。 參見docs

這是執行您所要求的實際代碼:

Solver solver = new Solver();

int n = 10;
IntVar[] x = VariableFactory.boundedArray("x", n, 0, 500, solver);

IntVar min = VariableFactory.bounded("min", 0, 500, solver);
IntVar max = VariableFactory.bounded("max", 0, 500, solver);

solver.post(IntConstraintFactory.minimum(min, x));
solver.post(IntConstraintFactory.maximum(max, x));

solver.post(IntConstraintFactory.distance(min, max, "<", 100));

if (solver.findSolution()) {
    int solutions = 5;
    int nSol = 0;
    do {
        System.out.print("x: ");
        for (int i = 0; i < n; i++)
            System.out.print(x[i].getValue() + " ");
        System.out.println("\nmin = " + min.getValue() + ", max = " + max.getValue() + ", |min - max| = " + Math.abs(min.getValue() - max.getValue()) + "\n");

        nSol++;

        if (solutions > 0 && nSol >= solutions)
            break;
    } while (solver.nextSolution());

    System.out.println(nSol + " solutions found.");
} else {
    System.out.println("No solution found.");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM