我想模擬與喬科的問題,以獲得網球賽事(或任何運動)中可能的比賽的組合。模擬與喬科(CSP)的網球比賽
我試圖做到這一點,我有以下方式:
// Set of timeslots when the event is held (i.e. 10am-10pm)
int nTimeslots = 12;
// Courts available: court #1, #2 and #3
int nCourts = 3;
String[] players = { "Novak", "Andy", "Roger", "Stan", "Rafel", "Kei", "Tomas", "David" };
int nPlayers = players.length;
// Timeslots when each player cannot play for whatever reason
int[][] unavailability = {
{ 0, 1, 5 },
{ 8, 10, 11 },
{ 1, 2, 11 },
{ 0, 1 },
{ 2, 3, 4, 5, 6 },
{ 3, 4, 9, 10, 11 },
{ 4, 5 },
{ 2, 3 }
};
// Number of timeslots each match will occupy
int matchDuration = 2;
// This will hold the final combinations
// rows -> players, columns -> timeslots, matches[i][j] -> court where the player plays at that timeslot (0 means the player does not play at that time)
IntVar[][] matches;
我的主要問題是,這種設置我想不出定義我的問題的一種方式。我一直在這方面花費很多時間,但沒有成功。我似乎有些類似的問題,但是應該合併的不同元素的數量較少,通常爲1或2,但在我的問題中有3個:玩家,時隙和法庭。
這個花費了大量的時間後,我無法進一步得到比這:
for (int player = 0; player < nPlayers; player++) {
for (int timeslot = 0; timeslot < nTimeslots; timeslot++) {
for (int playerUnavailbleTimeslot : unavailability[player]) {
if (playerUnavailbleTimeslot != timeslot) {
solver.post(IntConstraintFactory.arithm(matches[player][playerUnavailbleTimeslot], ">=", 0));
} else {
for (int i = 0; i < matchDuration; i++)
if (playerUnavailbleTimeslot - i >= 0)
solver.post(IntConstraintFactory.arithm(matches[player][playerUnavailbleTimeslot - i], "=", 0));
}
}
}
}
IntVar matchesSum = VariableFactory.enumerated("Matches sum", 1 * matchDuration, nCourts * matchDuration, solver);
for (int player = 0; player < nPlayers; player++) {
solver.post(IntConstraintFactory.sum(matches[player], matchesSum));
//solver.post(IntConstraintFactory.nvalues(matches[player], VariableFactory.fixed(2, solver)));
}
第一雙迴路只是強制0那些時隙玩家爲不可用(加上範圍基礎上比賽持續時間的價值),大於或等於他是否有空。這樣,最終的矩陣開始尋找這樣的:我只是確保高於中值的每個玩家的時隙的總和
0 0 ? ? ? 0 ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? 0 0 0 0 ?
.........................
後,與最低的數字乘以比賽期間法院和最高法院之間數乘以比賽時間。這是我想的那麼每一行看起來是這樣的,例如其中一個約束條件,玩家0次在法庭2日在時隙3和4:
0 0 0 2 2 0 0 0 0 0 0 0
我試圖定義是應該強制約束nvalues
不超過n
不同的值符合數組,但如果我使用它就像你可以看到上面的問題只是呈現一個解決方案(什麼?!)。
但是我需要定義更多的限制,我甚至不知道如何開始:
- 對於每一行,法院在那裏,如果確實是法院指定
- 對於玩家扮演必須連續編號每行我只能有0和法院編號[1 - nCourts]
- 列應該配對以創建一對玩家之間的匹配。
- 同一法院不能配對不止一次相同時隙範圍(意爲不超過一個比賽可以在同一時間發生在法庭上)
這是我能想到的約束條件,但我相信還有更多。
我很樂意提供任何建議來幫助我繼續這樣做,因爲現在我感到絕對無能爲力,並且Choco在線上幾乎沒有任何信息可以幫助我解決這個問題。
這是一個很大的幫助。儘管我試圖弄清楚一些變量和方程式的用處有些麻煩。你是如何實現它的?你能否談談如何在Choco中做到這一點?再一次,非常感謝你,你在這個答案上做了很多工作,這肯定有幫助。然而,爲什麼有些球員打一場比賽?或者那隻代表所有可能的組合? – dabadaba
我對Choco不太瞭解。多場比賽是我的發明。我的目標是安排儘可能多的比賽(呃,準確地說是爲了最大限度地發揮最小數量的比賽)。這很容易被一些更無聊的東西所取代,比如一個約束要求每個人(或者某個相關的東西)只有一個匹配。 –
我可以做到這一點嗎?它既可以滿足你的需求,也可以限制每個玩家一場比賽? (這樣它就可以用於循環賽和單淘汰賽)。我想問一下「Twoslots1」和「Twoslots2」方程。他們的目標是什麼?爲什麼有兩個?不是在我最初的問題中,matchDuration是一個變量,所以匹配並不意味着爲'matchDuration'插槽設置'2'個插槽。 – dabadaba