首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Choco (CSP)中定义产品

如何在Choco (CSP)中定义产品
EN

Stack Overflow用户
提问于 2016-03-04 16:43:31
回答 1查看 513关注 0票数 0

我正在尝试模拟一个网球调度问题,正如我在这个post中所解释的那样。我很幸运地得到了描述这个问题的方程式的答案,这个问题允许我在Choco中实现,而且它看起来工作得很好。

因此,我将要解释的是关于执行上一篇文章的答案的产品。

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

代码语言:javascript
复制
// 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;

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

代码语言:javascript
复制
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的约束,您可以确保一组值的求和等于一个值,是否有一个可以定义这组值的?如果没有,我该怎么做呢?

基本上,我要做的是:

代码语言:javascript
复制
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]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-04 19:20:39

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

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

有用吗?

Jean,https://www.cosling.com/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35801461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档