-
Notifications
You must be signed in to change notification settings - Fork 0
/
qualRankGen.php
78 lines (64 loc) · 2.16 KB
/
qualRankGen.php
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
<?php
/*
MySQL database handler
*/
class qualRankGen {
function __construct($rankingLists){
$this->rankingLists = $rankingLists;
$this->teamList = $this->_raw_rankings_to_team_list();
$this->teamLookup = $this->_team_list_to_lookup_dict();
}
function _raw_rankings_to_team_list(){
$setDict = array();
$teamList = array();
$row_count = sizeof($this->rankingLists);
for($i = 0; $i < $row_count; $i++){
$row_size = sizeof($this->rankingLists[$i]);
for($j = 0; $j < $row_size; $j++){
$team = $this->rankingLists[$i][$j];
if(!isset($setDict[$team])){
$setDict[$team] = 1;
array_push($teamList, $team);
}
}
}
return $teamList;
}
function _team_list_to_lookup_dict(){
$lookupDict = array();
$teamCount = sizeof($this->teamList);
for($i = 0; $i < $teamCount; $i++){
$lookupDict[$teamCount[$i]] = $i;
}
return $lookupDict;
}
function _calc_elo_probability($rating_a, $rating_b){
return 1.0 * (1.0 / (1 + 1.0 * pow(10, 1.0 + ($rating_a - $rating_b) / 400)));
}
function raw_votes_to_elo_map($K){
$elo_map = array();
foreach ($this->teamList as &$team){
$elo_map[$team] = 1600;
}
foreach ($this->rankingLists as &$vote_list){
$rowCount = sizeof($vote_list);
for ($i = 0; $i < $rowCount; $i++){
for ($j = $i; $j < $rowCount; $j++){
$team_a = $vote_list[$i];
$team_b = $vote_list[$j];
if ($team_a != $team_b){
$prob_a_win = $this->_calc_elo_probability($elo_map[$team_a], $elo_map[$team_b]);
$prob_b_win = $this->_calc_elo_probability($elo_map[$team_b], $elo_map[$team_a]);
$elo_map[$team_a] = $elo_map[$team_a] + $K * (1 - $prob_a_win);
$elo_map[$team_b] = $elo_map[$team_b] + $K * (0 - $prob_b_win);
}
}
}
}
foreach ($this->teamList as &$team){
$elo_map[$team] = round($elo_map[$team]);
}
return $elo_map;
}
}
?>