【问题标题】:How to define a product in Choco (CSP)如何在 Choco (CSP) 中定义产品
【发布时间】:2016-03-04 16:43:31
【问题描述】:

正如我在post 中解释的那样,我正在尝试对网球调度问题进行建模。我很幸运地得到了描述问题的方程式的答案,这使我能够在 Choco 中实现它,而且看起来它工作得很好。

所以我要解释的是关于上一篇回答的实现的产物。

基本上我会得到 2 个三维矩阵和 1 个二维矩阵,描述如下:

// Matches schedule
// i -> players, j-> courts, k -> timeslots
// x[i][j][k] holds a value 0..1 where 0 means the player doesn't play in court_j in the timeslot_k, and 1 the opposite
IntVar[][][] x;

// Beginning of all matches
// i -> players, j-> courts, k -> timeslots
// g[i][j][k] holds a value 0..1 where 0 means the player doesn't play in court_j in the timeslot_k, and 1 the opposite
// Basically the same matrix as the previous one but it only holds the first timeslot of a match
IntVar[][][] g;

// Occupied courts
// i-> courts, j-> timeslots
// crt[i][j] holds a value 0..1 where 0 means the court_i is occupied in the timeslot_j, and 1 means the opposite
IntVar[][] crt;

使用这种方法,将时间表矩阵映射到游戏开始矩阵的约束如下所示:

for (int p = 0; p < nPlayers; p++) {
    for (int c = 0; c < nCourts; c++) {
        for (int t = 0; t < nTimeslots - nTimeslotsPerMatch; t++) {
            if (nTimeslotsPerMatch == 1)
                solver.post(IntConstraintFactory.arithm(g[p][c][t], "=", x[p][c][t]));
            else
                solver.post(IntConstraintFactory.times(x[p][c][t], x[p][c][t + 1], g[p][c][t]));
        }               

        if (nTimeslotsPerMatch == 1)
            solver.post(IntConstraintFactory.arithm(g[p][c][nTimeslots - 1], "=", x[p][c][nTimeslots - 1]));
        else
            for (int i = 0; i < nTimeslotsPerMatch - 1; i++)
                solver.post(IntConstraintFactory.arithm(g[p][c][nTimeslots - i - 1], "=", 0));
    }
}

这使用times 约束技巧将x[p][c][t]x[p][c][t + 1] 映射到g[p][c][t]

但是,该定义认为每场比赛都有 2 个时隙的固定持续时间。我想改变它,使持续时间可变。

但是如果我想要一个可变的匹配持续时间,我必须在x 中映射两个以上的值来定义g 中的值。例如,如果比赛持续时间是 3 个空位,对于 map g[p][c][t],我需要使用 x[p][c][t] * x[p][c][t + 1] * x[p][c][t + 2],但我无法使用 times 或以类似的方式这样做。

所以我的问题是,由于 Choco 中有一个名为 sum 的约束,您可以确保一组值的总和等于一个值,是否有一个定义 产品这组值?如果没有,我该怎么做?

基本上我要做的是:

g[i][j][k] = x[i][j][k] + x[i][j][k + 1] * x[i][j][k + 2] * x[i][j][k + 3] * ... * x[i][j][nTimeslotsPerMatch - 1]

【问题讨论】:

    标签: java constraints constraint-programming choco


    【解决方案1】:

    从您的代码 cmets 中,x 是一组二进制变量(值是 0 或 1),因此您应该使用 BoolVar(它扩展 IntVar)来声明它。

    如果所有二进制变量都设置为 1,则乘以二进制变量为 1,否则为 0。换句话说,您可以使用“LogicalConstraintFactory.and”或“IntConstraintFactory.minimum”约束来获得乘法。看看 IntConstraintFactory,你也有可能有用的隐含约束。

    有帮助吗?

    让-纪尧姆, https://www.cosling.com/

    【讨论】:

    • 你能用一个小示例代码告诉我如何使用andminimum 来做我正在尝试的事情吗?我只是不明白怎么做。此外,BoolVars 的问题在于,在问题的另一部分中,我需要这些矩阵是 IntVars,因为我需要计算每个球员参加的球场数量并添加一个约束以将其与每个球员所需的比赛数量相匹配必须玩。那么我该如何做这两件事呢?反正我还是想知道怎么做产品。
    • 获得 BoolVar bx 等于 b1*b2*b3 :solver.post(minimum(bx,new BoolVar[]{b1,b2,b3}));
    • 如果域是0/1,不管你把它们存入BoolVar矩阵还是IntVar矩阵,都是BoolVar对象,所以我推荐矩阵的类型为BoolVar[][]。由于任何 BoolVar 都是 IntVar,因此您应该没有问题。如果是这样,请准确说明哪条指令不起作用。
    • 是的,我现在完全明白了。我看到它乘以布尔变量的方式与对它们应用 AND 操作相同,因此与使用 minimum 约束相同。现在我需要对一组变量应用 OR 操作,我猜使用 maximum 会以类似的方式工作吗?你能解释一下使用LogicalConstraintFactory.andor 对我的问题有什么帮助吗?据我所知,确保约束应用于一组变量的力量,但我无法将“结果”绑定到 var 上,就像使用 sumtimes 等约束一样, minimummaximum.
    • 尽管这完全解决了我无法回答主要问题的特定问题:如何在 Choco 中定义产品?想象一下,我有一组IntVars,我想对它们定义一个约束,以确保x1 * x2 * x3 * ... * xn OP y。基本上是为了达到与IntConstraintFactory.sum 相同的效果,只是它应该是乘积而不是总和。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 2018-01-24
    • 1970-01-01
    相关资源
    最近更新 更多