2016-03-04 24 views
0

我試圖模擬一個網球調度問題,因爲我在這個post解釋。我很幸運地得到了方程式的答案,這些方程式描述了允許我在Choco中實現它的問題,並且它看起來很好。如何在喬科(CSP)定義產品

那麼我將要解釋的是關於前一後的回答實施的產品。

基本上我將最終具有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或以類似的方式它被現在進行。

所以我的問題是,因爲是在喬科約束稱爲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] 

回答

1

從你的代碼中的註釋,x是一組二元變量(值是0或1),所以你應該使用BoolVar宣佈它的(延伸IntVar)。

乘以二進制變量賦予1,如果所有的二進制文件被設置爲1,否則爲0。換句話說,您可以使用「LogicalConstraintFactory.and」或「IntConstraintFactory.minimum」約束來獲得該乘法。看看IntConstraintFactory,你也有隱含的約束,可能是有用的。

它有幫助嗎?

讓 - 紀堯姆 https://www.cosling.com/

+0

你能告訴我在一個小樣本代碼怎麼做我想用'和'和'minimum'?我只是不知道如何。此外,BoolVars的問題在於,在另一部分問題中,我需要將這些矩陣變成IntVars,因爲我需要計算每個球員進場的球場數量,並添加一個約束條件以匹配每個球員所需的比賽數量必須發揮。那麼我怎麼能做到這兩件事?無論如何,我仍然想知道如何做產品。 – dabadaba

+0

爲了得到BX等於B1 * B2 * B3一個BoolVar: solver.post(最小(BX,新BoolVar [] {B1,B2,B3})); –

+0

如果域爲0/1,那麼無論您將它們存儲到BoolVar矩陣還是IntVar矩陣中,都將是BoolVar對象,因此我建議將矩陣的類型設置爲BoolVar [] []。由於任何BoolVar都是IntVar,你不應該有問題。如果是這樣,請確定哪個指令不起作用。 –

相關問題