-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweighted-pairing.ts
More file actions
343 lines (291 loc) · 9.61 KB
/
weighted-pairing.ts
File metadata and controls
343 lines (291 loc) · 9.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import { Game, Result } from "./external/game";
import { Player } from "./external/player";
import { Tournament } from "./external/tournament";
import { Graph } from "./graph";
import { Matcher } from "./matcher";
export class WeightedPairingAlgorithm {
public addGames(players: Player[], games: Game[][]) {
let playersById = new Map<Number, Player>();
for (let p of players) {
playersById.set(p.id, p);
}
for (let [roundNum, roundGames] of games.entries()) {
for (let g of roundGames) {
let black = playersById.get(g.getBlackId());
if (!black) {
throw new Error(`Couldn't find black player from game ${JSON.stringify(g)}`)
}
let white = playersById.get(g.getWhiteId());
if (!white) {
throw new Error(`Couldn't find white player in game ${JSON.stringify(g)}`)
}
black.addGame(g);
if (white != black) {
white.addGame(g);
}
switch (g.getResult()) {
case Result.BlackWin:
case Result.WhiteForfeit:
black.countWin(roundNum);
break;
case Result.WhiteWin:
case Result.BlackForfeit:
white.countWin(roundNum);
break;
case Result.Draw:
white.countDraw(roundNum);
black.countDraw(roundNum);
break;
case Result.InvoluntaryBye:
black.countWin(roundNum);
break;
case Result.DoubleForfeit:
black.countVoluntaryBye(roundNum);
white.countVoluntaryBye(roundNum);
break;
case Result.DoubleLoss:
case Result.Void:
case Result.VoluntaryBye:
black.countVoluntaryBye(roundNum);
break;
case Result.NoGame:
case Result.Unknown:
break;
}
}
}
}
public doPairing(
tournament: Tournament,
players: Player[],
games: Game[][],
newGames: Game[]
): boolean {
let roundNum = games.length;
this.addGames(players, games);
players.sort(scoreAndRatingComparator);
this.createEdges(players);
// Assign penalty for cross-band play
for (let i = 0; i < players.length; i++) {
let p = players[i];
for (let j = i + 1; j < players.length; j++) {
let p2 = players[j];
let e = this.getEdge(p, p2);
let deltaScore = Math.abs(p.getScore() - p2.getScore());
e.weight -= 10000 * deltaScore * deltaScore;
}
}
if (tournament.isBalanceColors() && roundNum > 1) {
// Assign penalty to avoid pairings that force same color three in a row
for (let i = 0; i < players.length; i++) {
let p = players[i];
let lastTwo = p.getLastGames(2);
if (lastTwo == null) continue;
let prev = lastTwo[0];
let twoBack = lastTwo[1];
let pMustBeBlack = prev.getWhiteId() == p.id && twoBack.getWhiteId() == p.id;
let pMustBeWhite = prev.getBlackId() == p.id && twoBack.getBlackId() == p.id;
for (let j = i + 1; j < players.length; j++) {
let p2 = players[j];
lastTwo = p2.getLastGames(2);
if (lastTwo == null) continue;
prev = lastTwo[0];
twoBack = lastTwo[1];
let p2MustBeBlack = prev.getWhiteId() == p2.id && twoBack.getWhiteId() == p2.id;
let p2MustBeWhite = prev.getBlackId() == p2.id && twoBack.getBlackId() == p2.id;
if (
(pMustBeBlack && p2MustBeBlack) ||
(pMustBeWhite && p2MustBeWhite)
) {
let e = this.getEdge(p, p2);
e.weight -= 15000;
}
}
}
}
// Avoid floating in same direction twice in a row
let numRounds = tournament.getTotalRounds();
if (
(numRounds > 5 && roundNum < numRounds - 2) ||
(numRounds < 6 && roundNum < numRounds - 1)
) {
for (let i = 0; i < players.length; i++) {
let p = players[i];
let prevList = p.getLastGames(1);
if (prevList == null) continue;
let prev = prevList[0];
let floatDir = prev.getFloat(p, players);
if (floatDir == 0) continue;
for (let j = 0; j < players.length; j++) {
if (j == i) continue;
let p2 = players[j];
let diff = Math.floor(p2.getScore() - p.getScore());
if (Math.sign(diff) == Math.sign(floatDir)) {
let e = this.getEdge(p, p2);
e.weight -= 17500;
}
}
}
}
if (roundNum < tournament.getTotalRounds() / 2) {
// Assign penalty for intra-state/city/club/family play
let statePenalty =
tournament.getIntraStateGap() * 10000;
let cityPenalty =
tournament.getIntraCityGap() * 10000;
let clubPenalty =
tournament.getIntraClubGap() * 10000;
let familyPenalty =
tournament.getIntraFamilyGap() * 10000;
for (let i = 0; i < players.length; i++) {
let p = players[i];
for (let j = i + 1; j < players.length; j++) {
let p2 = players[j];
let e = this.getEdge(p, p2);
let state = p.getState();
let city = p.getCity();
let club = p.getClub();
let family = p.getFamily();
let state2 = p2.getState();
let city2 = p2.getCity();
let club2 = p2.getClub();
let family2 = p2.getFamily();
if (family && family === family2) {
e.weight -= familyPenalty;
} else if (club && club.toLowerCase() !== "none" && club === club2)
e.weight -= clubPenalty;
else if (state === state2) {
if (city && city === city2) e.weight -= cityPenalty;
else e.weight -= statePenalty;
}
}
}
// Minimize Handicap
for (let i = 0; i < players.length; i++) {
let p = players[i];
for (let j = i + 1; j < players.length; j++) {
let p2 = players[j];
let e = this.getEdge(p, p2);
let handi = Math.abs(tournament.calculateHandicap(p, p2));
let penalty = Math.floor(
handi * handi * 10000 * tournament.getHandicapGap()
);
e.weight -= penalty;
}
}
}
// Group by score
let groups: Player[][] = [[]];
let curGroupScore = Number.MIN_VALUE;
let curGroup: Player[] = [];
for (let p of players) {
if (p.getScore() != curGroupScore) {
curGroup = [];
groups.push(curGroup);
curGroupScore = p.getScore();
}
curGroup.push(p);
}
this.doSlidePairing(groups);
// Make sure players don't play twice
for (let i = 0; i < players.length; i++) {
let p = players[i];
for (let j = i + 1; j < players.length; j++) {
let p2 = players[j];
if (p.hasPlayed(p2)) this.removeEdge(p, p2);
}
}
let maxId = -1;
for (let p of players) maxId = Math.max(maxId, p.getId());
let g = new Graph(maxId);
let edges = [];
for (let id in this.edges) {
let e: Edge = this.edges[id];
g.addEdge(e.srcId, e.destId, e.weight);
}
let matcher = new Matcher();
let matches = matcher.weightedMatch(g, true);
for (let i = 1; i < matches.length - 1; i++) {
if (i > matches[i]) continue;
let black = players.find((p) => p.id === i);
let white = players.find((p) => p.id === matches[i]);
newGames.push(tournament.createMatch(black, white));
}
return newGames.length == players.length / 2;
}
doSlidePairing(groups: Player[][]) {
for (let i = 0; i < groups.length; i++) {
let group = groups[i];
let split = group.length / 2;
for (let j = 0; j < group.length; j++) {
let p = group[j];
let pLeft = j < split;
let pIndex = j;
if (!pLeft) pIndex -= split;
for (let k = 0; k < group.length; k++) {
if (j == k) continue;
let p2 = group[k];
let p2Left = k < split;
let p2Index = k;
if (!p2Left) p2Index -= split;
let penalty;
if (pLeft != p2Left) {
let deltaPos = Math.abs(pIndex - p2Index);
penalty = deltaPos * deltaPos;
} else {
// Max penalty for shifts on right is less than (n/2) ^ 2
penalty = split * split;
}
let e = this.getEdge(p, p2);
e.weight -= penalty;
}
}
}
}
createEdges(players: Player[]) {
this.edges = [];
for (let i = 0; i < players.length; i++) {
let p = players[i];
for (let j = i + 1; j < players.length; j++) {
let p2 = players[j];
let e = new Edge(p, p2);
let id = makeId(p, p2);
if (this.edges[id] != null) throw "Error";
this.edges[id] = e;
}
}
}
edges: Edge[];
getEdge(p: Player, p2: Player): Edge {
if (p.getId() == p2.getId()) throw "Error";
return this.edges[makeId(p, p2)];
}
removeEdge(p: Player, p2: Player) {
let id = makeId(p, p2);
if (this.edges[id] == null) throw "Error";
delete this.edges[id];
}
}
class Edge {
public srcId: number;
public destId: number;
public weight: number = 2000000000;
public constructor(p: Player, p2: Player) {
this.srcId = p.getId();
this.destId = p2.getId();
}
}
function makeId(p: Player, p2: Player): number {
let id1 = p.getId();
let id2 = p2.getId();
return Math.min(id1, id2) + 100000 * Math.max(id1, id2);
}
function scoreAndRatingComparator(arg0: Player, arg1: Player): number {
let diff = arg1.getScore() - arg0.getScore();
if (diff < 0) return -1;
if (diff > 0) return 1;
diff = arg1.getRating() - arg0.getRating();
if (diff < 0) return -1;
if (diff > 0) return 1;
return 0;
}