Debian Codegraph @

src/bnetd/ladder_calc.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1999 Rob Crittenden ([email protected])
3 * Copyright (C) 1999,2000 Ross Combs ([email protected])
4 * Copyright (C) 1999,2000 D.Moreaux ([email protected])
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20 #include "common/setup_before.h"
21 #include "ladder_calc.h"
22
23 #include <cmath>
24
25 #include "common/eventlog.h"
26
27 #include "account.h"
28 #include "account_wrap.h"
29 #include "common/setup_after.h"
30
31
32 namespace pvpgn
33 {
34
35 namespace bnetd
36 {
37
38 static double probability(unsigned int a, unsigned int b);
39 static int coefficient(t_account * account, t_clienttag clienttag, t_ladder_id id);
40
41 static double two_player(unsigned int *rating);
42
43 static double three_player(unsigned int *rating);
44
45 static double four_player(unsigned int *rating);
46
47 static double five_player(unsigned int *rating);
48 static double five_f1(int a, int b, int c, int d, int e);
49 static double five_f2(int a, int b, int c);
50
51 static double six_player(unsigned int *rating);
52 static double six_f1(int a, int b, int c, int d, int e, int f);
53 static double six_f2(int a, int b, int c, int d, int e, int f);
54 static double six_f3(int a, int b, int c, int d);
55
56 static double seven_player(unsigned int *rating);
57 static double seven_f1(int a, int b, int c, int d, int e, int f, int g);
58 static double seven_f2(int a, int b, int c, int d, int e, int f, int g);
59
60 static double eight_player(unsigned int *rating);
61 static double eight_f1(int a, int b, int c, int d, int e, int f, int g);
62 static double eight_f2(int a, int b, int c, int d, int e, int f, int g);
63 static double eight_f3(int a, int b, int c, int d, int e);
64
65 /*
66 * Compute probability of winning using the Elo system
67 *
68 * The formula is:
69 *
70 * D = rating(player a) - rating(player b)
71 *
72 * 1
73 * Pwin(D) = ------------------
74 * -(D / 400)
75 * 1 + 10
76 */
77 static double probability(unsigned int a, unsigned int b)
78 {
79 double i, j;
80
81 i = (((double)a) - ((double)b)) / 400.0;
82
83 j = std::pow(10.0, -i);
84
85 return (1.0 / (1.0 + j));
86 }
87
88
89 /*
90 * This is the coefficient k which is meant to enhance the
91 * effect of the Elo std::system where more experienced players
92 * will gain fewer points when playing against newbies, and
93 * newbies will gain massive points if they win against an
94 * experienced player. It also helps stabilize a player's
95 * rating after they have played 30 games or so.
96 *
97 * K=50 for new players
98 * K=30 for players who have played 30 or more ladder games
99 * K=20 for players who have attained a rating of 2400 or higher
100 */
101 static int coefficient(t_account * account, t_clienttag clienttag, t_ladder_id id)
102 {
103 int const total_ladder_games = account_get_ladder_wins(account, clienttag, id) +
104 account_get_ladder_losses(account, clienttag, id) +
105 account_get_ladder_disconnects(account, clienttag, id);
106
107 if (total_ladder_games < 30)
108 return 50;
109
110 if (account_get_ladder_rating(account, clienttag, id) < 2400)
111 return 30;
112
113 return 20;
114 }
115
116
117 /*
118 * The Elo std::system only handles 2 players, these functions extend
119 * the calculation to different numbers of players as if they were
120 * in a tournament. It turns out the math for this is really ugly,
121 * so we have hardcoded the equations for every number of players.
122 */
123 static double two_player(unsigned int *rating)
124 {
125 unsigned int a, b;
126 double ab;
127
128 a = rating[0];
129 b = rating[1];
130
131 ab = probability(a, b);
132
133 return ab;
134 }
135
136 static double three_player(unsigned int *rating)
137 {
138 unsigned int a, b, c;
139 double ab, ac, bc, cb;
140
141 a = rating[0];
142 b = rating[1];
143 c = rating[2];
144
145 ab = probability(a, b);
146 ac = probability(a, c);
147 bc = probability(b, c);
148 cb = 1.0 - bc;
149
150 return (2 * (ab*ac) + (bc*ab) + (cb*ac)) / 3;
151 }
152
153 static double four_player(unsigned int *rating)
154 {
155 unsigned int a, b, c, d;
156 double ab, ac, ad, bc, bd, cb, cd, db, dc;
157
158 a = rating[0];
159 b = rating[1];
160 c = rating[2];
161 d = rating[3];
162
163
164 ab = probability(a, b);
165 ac = probability(a, c);
166 ad = probability(a, d);
167 bc = probability(b, c);
168 bd = probability(b, d);
169 cd = probability(c, d);
170 cb = 1.0 - bc;
171 db = 1.0 - bd;
172 dc = 1.0 - cd;
173
174 return (ab*ac*(cd + bd) + ac*ad*(db + cb) + ab*ad*(dc + bc)) / 3;
175 }
176
177
178 /* [Denis MOREAUX <[email protected]>, 10 Apr 2000]
179 *
180 * C D E A D E The winner may be in the
181 * A B C E B C A E 2 players or the 3 players
182 * A C B A group. In either case, a
183 * A A second player must be choosen
184 * to be either the one playing
185 * against A in the 2-players subtree or being the winner
186 * of the 2-player subtree if A is in the 3-players subtree.
187 */
188
189 static double five_player(unsigned int *rating)
190 {
191 unsigned int a, b, c, d, e;
192
193 a = rating[0];
194 b = rating[1];
195 c = rating[2];
196 d = rating[3];
197 e = rating[4];
198
199 return (five_f1(a, b, c, d, e) + five_f1(a, c, d, e, b) +
200 five_f1(a, d, e, b, c) + five_f1(a, e, b, c, d)) / 30;
201 }
202
203 /* [Denis MOREAUX <[email protected]>, 10 Apr 2000
204 *
205 * Two cases to treat : AB-CDE and BC-ADE.
206 * in both cases, A win against B.
207 * In the first case, A win over the winner of a 3-players game
208 * (3 possible winners).
209 * In the second case, B win over one of the three other and A is in
210 * the 3-players game.
211 */
212
213 static double five_f1(int a, int b, int c, int d, int e)
214 {
215 double ab, ac, ad, ae, bc, bd, be;
216
217 ab = probability(a, b);
218 ac = probability(a, c);
219 ad = probability(a, d);
220 ae = probability(a, e);
221 bc = probability(b, c);
222 bd = probability(b, d);
223 be = probability(b, e);
224
225 return ab*(ac*five_f2(c, d, e) + ad*five_f2(d, e, c) + ae*five_f2(e, c, d) +
226 bc*five_f2(a, d, e) + bd*five_f2(a, c, e) + be*five_f2(a, c, d));
227 }
228
229 static double five_f2(int a, int b, int c)
230 {
231 double ab, ac, bc, cb;
232
233 ab = probability(a, b);
234 ac = probability(a, c);
235 bc = probability(b, c);
236 cb = 1.0 - bc;
237
238 return (2 * (ab*ac) + bc*ab + cb*ac);
239 }
240
241
242 static double six_player(unsigned int *rating)
243 {
244 unsigned int a, b, c, d, e, f;
245
246 a = rating[0];
247 b = rating[1];
248 c = rating[2];
249 d = rating[3];
250 e = rating[4];
251 f = rating[5];
252
253 /* A B C D
254 * A C E F
255 * A E
256 * A
257 */
258
259 return (six_f1(a, b, c, d, e, f) + /* A is in group of 4 */
260 six_f1(a, b, c, e, d, f) +
261 six_f1(a, b, e, d, c, f) +
262 six_f1(a, e, c, d, b, f) +
263 six_f1(a, b, c, f, d, e) +
264 six_f1(a, b, f, d, c, e) +
265 six_f1(a, f, c, d, b, e) +
266 six_f1(a, e, f, b, c, d) +
267 six_f1(a, e, f, c, b, d) +
268 six_f1(a, e, f, d, b, c) +
269 six_f2(a, b, c, d, e, f) + /* A is in group of 2 */
270 six_f2(a, c, b, d, e, f) +
271 six_f2(a, d, b, c, e, f) +
272 six_f2(a, e, b, c, d, f) +
273 six_f2(a, f, b, c, d, e)) / 45;
274 }
275
276
277 /* ABCD = group of 4, EF = group of 2, A must win */
278 /* D.Moreaux, 10 Apr 2000: changed double to int for the parameters */
279
280 static double six_f1(int a, int b, int c, int d, int e, int f)
281 {
282 double ab, ac, ad, bc, bd, cb, cd, db, dc, ef, fe, ae, af;
283
284 ab = probability(a, b);
285 ac = probability(a, c);
286 ad = probability(a, d);
287 ae = probability(a, e);
288 af = probability(a, f);
289 bc = probability(b, c);
290 bd = probability(b, d);
291 cd = probability(c, d);
292 ef = probability(e, f);
293 cb = 1.0 - bc;
294 db = 1.0 - bd;
295 dc = 1.0 - cd;
296 fe = 1.0 - ef;
297 return (ab*ac*(cd + bd) + ac*ad*(db + cb) + ab*ad*(dc + bc))*(ef*ae + fe*af);
298 }
299
300
301 /* AB is group of 2, CDEF is group of 4, A must win */
302
303 static double six_f2(int a, int b, int c, int d, int e, int f)
304 {
305 double ab, ac, ad, ae, af;
306
307 ab = probability(a, b);
308 ac = probability(a, c);
309 ad = probability(a, d);
310 ae = probability(a, e);
311 af = probability(a, f);
312
313 return (six_f3(c, d, e, f)*ab*ac +
314 six_f3(d, c, e, f)*ab*ad +
315 six_f3(e, c, d, f)*ab*ae +
316 six_f3(f, c, d, e)*ab*af);
317 }
318
319
320 /* ABCD is group of 4, A win */
321
322 static double six_f3(int a, int b, int c, int d)
323 {
324 double ab, ac, ad, bc, bd, cb, cd, db, dc;
325
326 ab = probability(a, b);
327 ac = probability(a, c);
328 ad = probability(a, d);
329 bc = probability(b, c);
330 bd = probability(b, d);
331 cd = probability(c, d);
332 cb = 1.0 - bc;
333 db = 1.0 - bd;
334 dc = 1.0 - cd;
335
336 return (ab*ac*(cd + bd) + ac*ad*(db + cb) + ab*ad*(dc + bc));
337 }
338
339
340 static double seven_player(unsigned int *rating)
341 {
342 unsigned int a, b, c, d, e, f, g;
343
344 a = rating[0];
345 b = rating[1];
346 c = rating[2];
347 d = rating[3];
348 e = rating[4];
349 f = rating[5];
350 g = rating[6];
351
352 return (seven_f1(a, b, c, d, e, f, g) + seven_f1(a, c, b, d, e, f, g) +
353 seven_f1(a, d, c, b, e, f, g) + seven_f1(a, e, c, d, b, f, g) +
354 seven_f1(a, f, c, d, e, b, g) + seven_f1(a, g, c, d, e, f, b)) / 45;
355 }
356
357 static double seven_f1(int a, int b, int c, int d, int e, int f, int g)
358 {
359
360 return seven_f2(a, b, c, d, e, f, g) + seven_f2(a, b, d, c, e, f, g) +
361 seven_f2(a, b, e, d, c, f, g) + seven_f2(a, b, f, d, e, c, g) +
362 seven_f2(a, b, g, d, e, f, c);
363 }
364
365 static double seven_f2(int a, int b, int c, int d, int e, int f, int g)
366 {
367 double ab, ac, ad, ae, af, ag, bc, bd, be, bf, bg, cd, ce, cf, cg;
368 double de, df, dg, ed, ef, eg, fd, fe, fg, gd, ge, gf;
369 ab = probability(a, b);
370 ac = probability(a, c);
371 ad = probability(a, d);
372 ae = probability(a, e);
373 af = probability(a, f);
374 ag = probability(a, g);
375 bc = probability(b, c);
376 bd = probability(b, d);
377 be = probability(b, e);
378 bf = probability(b, f);
379 bg = probability(b, g);
380 cd = probability(c, d);
381 ce = probability(c, e);
382 cf = probability(c, f);
383 cg = probability(c, g);
384 de = probability(d, e);
385 df = probability(d, f);
386 dg = probability(d, g);
387 ef = probability(e, f);
388 eg = probability(e, g);
389 fg = probability(f, g);
390 ed = 1.0 - de;
391 fd = 1.0 - df;
392 gd = 1.0 - dg;
393 fe = 1.0 - ef;
394 ge = 1.0 - eg;
395 gf = 1.0 - fg;
396
397 return
398 ab*(
399 (ac + bc)*
400 (ad*(de*df*(fg + eg) + df*dg*(ge + fe) + de*dg*(gf + ef)) + /* 4:d win */
401 ae*(ed*ef*(fg + dg) + ef*eg*(gd + fd) + ed*eg*(gf + df)) + /* 4:e win */
402 af*(fe*fd*(dg + eg) + fd*fg*(ge + de) + fe*fg*(gd + ed)) + /* 4:f win */
403 ag*(ge*gf*(fd + ed) + gf*gd*(de + fe) + ge*gd*(df + ef))) + /* 4:g win */
404 bc*
405 ((bd + cd)*(ae*af*(fg + eg) + af*ag*(ge + fe) + ae*ag*(gf + ef)) + /* 3:d */
406 (be + ce)*(ad*af*(fg + dg) + af*ag*(ad + ad) + ad*ag*(gf + df)) + /* 3:e */
407 (bf + cf)*(ae*ad*(dg + eg) + ad*ag*(ge + de) + ae*ag*(gd + ed)) + /* 3:f */
408 (bg + cg)*(ae*af*(fd + ed) + af*ad*(de + fe) + ae*ad*(df + ef)))); /* 3:g */
409
410 }
411
412 static double eight_player(unsigned int *rating)
413 {
414 unsigned int a, b, c, d, e, f, g, h;
415 double ab, ac, ad, ae, af, ag, ah;
416
417 a = rating[0];
418 b = rating[1];
419 c = rating[2];
420 d = rating[3];
421 e = rating[4];
422 f = rating[5];
423 g = rating[6];
424 h = rating[7];
425
426 ab = probability(a, b);
427 ac = probability(a, c);
428 ad = probability(a, d);
429 ae = probability(a, e);
430 af = probability(a, f);
431 ag = probability(a, g);
432 ah = probability(a, h);
433
434 /* First against A may be one from seven */
435
436 return (eight_f1(a, c, d, e, f, g, h)*ab +
437 eight_f1(a, b, d, e, f, g, h)*ac +
438 eight_f1(a, b, c, e, f, g, h)*ad +
439 eight_f1(a, b, c, d, f, g, h)*ae +
440 eight_f1(a, b, c, d, e, g, h)*af +
441 eight_f1(a, b, c, d, e, f, h)*ag +
442 eight_f1(a, b, c, d, e, f, g)*ah) / 315;
443
444 }
445
446
447 static double eight_f1(int a, int b, int c, int d, int e, int f, int g)
448 {
449 /* The winner of the second group, who'll then play against A, may be one
450 from six possible players */
451
452 return eight_f2(a, b, c, d, e, f, g) +
453 eight_f2(a, c, b, d, e, f, g) +
454 eight_f2(a, d, b, c, e, f, g) +
455 eight_f2(a, e, b, c, d, f, g) +
456 eight_f2(a, f, b, c, d, e, g) +
457 eight_f2(a, g, b, c, d, e, f);
458 }
459
460 static double eight_f2(int a, int b, int c, int d, int e, int f, int g)
461 {
462 double ab, bc, bd, be, bf, bg;
463
464 ab = probability(a, b);
465 bc = probability(b, c);
466 bd = probability(b, d);
467 be = probability(b, e);
468 bf = probability(b, f);
469 bg = probability(b, g);
470
471 /* There are 5 player who may play against the 3rd. The third (b) will win
472 over them and lose against a */
473
474 return ab*(eight_f3(a, d, e, f, g)*bc +
475 eight_f3(a, c, e, f, g)*bd +
476 eight_f3(a, c, d, f, g)*be +
477 eight_f3(a, c, d, e, g)*bf +
478 eight_f3(a, c, d, e, f)*bg);
479
480 }
481
482 /* D.Moreaux, 10 Apr 2000: changed double to int for the parameters */
483
484 static double eight_f3(int a, int b, int c, int d, int e)
485 {
486 double ab, ac, ad, ae, bc, bd, be, cb, cd, ce, db, dc, de, eb, ec, ed;
487
488 ab = probability(a, b);
489 ac = probability(a, c);
490 ad = probability(a, d);
491 ae = probability(a, e);
492 bc = probability(b, c);
493 bd = probability(b, d);
494 be = probability(b, e);
495 cd = probability(c, d);
496 ce = probability(c, e);
497 de = probability(d, e);
498 cb = 1.0 - bc;
499 db = 1.0 - bd;
500 dc = 1.0 - cd;
501 eb = 1.0 - be;
502 ec = 1.0 - ce;
503 ed = 1.0 - de;
504
505 /* expansion then factorisation (this function is called 210 times)
506 * gain 4 func_call
507 * 24 *
508 * 30 probability
509 */
510 return (bc*de + be*dc)*((ab - ad)*bd + ad) +
511 (bd*ce + be*cd)*((ab - ac)*bc + ac) +
512 (cb*de + db*ce)*((ac - ad)*cd + ad) +
513 (cd*eb + cb*ed)*((ac - ae)*ce + ae) +
514 (dc*eb + db*ec)*((ad - ae)*de + ae) +
515 (bd*ec + bc*ed)*((ae - ab)*eb + ab);
516 }
517
518 /* Determine changes in ratings due to game results. */
519 extern int ladder_calc_info(t_clienttag clienttag, t_ladder_id id, unsigned int count, t_account * * players, t_game_result * results, t_ladder_info * info)
520 {
521 unsigned int curr;
522 unsigned int *rating;
523 unsigned int *sorted;
524
525 if (!players)
526 {
527 eventlog(eventlog_level_error, __FUNCTION__, "got NULL players");
528 return -1;
529 }
530 if (!results)
531 {
532 eventlog(eventlog_level_error, __FUNCTION__, "got NULL results");
533 return -1;
534 }
535 if (!clienttag)
536 {
537 eventlog(eventlog_level_error, __FUNCTION__, "got bad clienttag");
538 return -1;
539 }
540 if (!info)
541 {
542 eventlog(eventlog_level_error, __FUNCTION__, "got NULL info");
543 return -1;
544 }
545
546 rating = (unsigned int*)xmalloc(sizeof(unsigned int)*count);
547 sorted = (unsigned int*)xmalloc(sizeof(unsigned int)*count);
548
549 for (curr = 0; curr < count; curr++)
550 rating[curr] = account_get_ladder_rating(players[curr], clienttag, id);
551
552 for (curr = 0; curr < count; curr++)
553 {
554 double k;
555 double prob;
556 double delta;
557 t_game_result myresult;
558 unsigned int opponent_count;
559 unsigned int team_members;
560
561 k = coefficient(players[curr], clienttag, id);
562 opponent_count = 0;
563 myresult = results[curr];
564
565 {
566 unsigned int i, j;
567
568 /* Put the current user into slot 0, his opponents into other slots
569 order is not important for the other players */
570 for (i = 0, j = 1; i < count; i++)
571 if (i == curr)
572 sorted[0] = rating[i];
573 else
574 {
575 if (results[i] != myresult)
576 {
577 sorted[j++] = rating[i];
578 opponent_count++;
579 }
580 }
581 }
582
583 team_members = count - opponent_count;
584
585 switch (opponent_count)
586 {
587 case 1:
588 prob = two_player(sorted);
589 break;
590 case 2:
591 prob = three_player(sorted);
592 break;
593 case 3:
594 prob = four_player(sorted);
595 break;
596 case 4:
597 prob = five_player(sorted);
598 break;
599 case 5:
600 prob = six_player(sorted);
601 break;
602 case 6:
603 prob = seven_player(sorted);
604 break;
605 case 7:
606 prob = eight_player(sorted);
607 break;
608 default:
609 eventlog(eventlog_level_error, __FUNCTION__, "sorry, unsupported number of ladder opponents ({})", opponent_count);
610 xfree((void *)rating);
611 xfree((void *)sorted);
612 return -1;
613 }
614
615 if (results[curr] == game_result_win)
616 delta = std::fabs(k * (1.0 - prob) / team_members); /* better the chance of winning -> fewer points added */
617 else
618 delta = -std::fabs(k * prob); /* better the chance of winning -> more points subtracted */
619
620 eventlog(eventlog_level_debug, __FUNCTION__, "computed probability={}, k={}, deltar={:+g}", prob, k, delta);
621
622 info[curr].prob = prob;
623 info[curr].k = (unsigned int)k;
624 info[curr].adj = (int)delta;
625 info[curr].oldrating = rating[curr];
626 info[curr].oldrank = account_get_ladder_rank(players[curr], clienttag, id);
627 }
628
629 xfree((void *)rating);
630 xfree((void *)sorted);
631
632 return 0;
633 }
634
635 }
636
637 }