簡體   English   中英

如何在Choco(CSP)中定義產品

[英]How to define a product in Choco (CSP)

我試圖模擬一個網球調度問題,我在此解釋 我很幸運能得到描述問題的方程式的答案,該方程式使我可以在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]

從代碼注釋中可以看出,x是一組二進制變量(值可以為0或1),因此您應該使用BoolVar(擴展IntVar)對其進行聲明。

如果所有二進制文件都設置為1,則將二進制變量相乘將得到1,否則,將得到0。 換句話說,您可以使用“ LogicalConstraintFactory.and”或“ IntConstraintFactory.minimum”約束來獲得該乘法。 看一下IntConstraintFactory,您還具有可能有用的隱含約束。

有幫助嗎?

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

暫無
暫無

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

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