]> git.sesse.net Git - foosball/blobdiff - foosball.pm
Adjusted initial parameters for maximum prediction power; in particular,
[foosball] / foosball.pm
index 164b3e3d4c6381cb308b583e9a129c502b8f12ad..38722422caea742ec265373305a913c3e342b451 100644 (file)
@@ -1,28 +1,53 @@
 use strict;
 use warnings;
 use DBI;
+use POSIX;
 
 package foosball;
 
+our $initial_rating = 1500.0;
+our $initial_rd = 400.0;
+our $c = 22.5;
+
 sub db_connect {
        my $dbh = DBI->connect("dbi:Pg:dbname=foosball;host=127.0.0.1", "foosball", "cleanrun", {AutoCommit => 0});
        $dbh->{RaiseError} = 1;
        return $dbh;
 }
 
+sub round {
+       my $x = shift;
+       return -round(-$x) if ($x < 0.0);
+       return POSIX::floor($x + 0.5);
+}
+
 sub find_single_rating {
        my ($dbh, $username, $limit) = @_;
+       $limit = "" if (!defined($limit));
        my ($age, $rating, $rd) = $dbh->selectrow_array('SELECT EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP-ratetime)), rating, rd FROM single_rating WHERE username=? '.$limit.' ORDER BY ratetime DESC LIMIT 1',
                undef, $username);
        $rd = apply_aging($rd, $age / 86400.0);
+
+       if (!defined($rating)) {
+               $rating = $initial_rating;
+               $rd = $initial_rd;
+       }
+
        return ($rating, $rd);
 }
 
 sub find_double_rating {
        my ($dbh, $username, $limit) = @_;
+       $limit = "" if (!defined($limit));
        my ($age, $rating, $rd) = $dbh->selectrow_array('SELECT EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP-ratetime)), rating, rd FROM double_rating WHERE username=? '.$limit.'ORDER BY ratetime DESC LIMIT 1',
                undef, $username);
        $rd = apply_aging($rd, $age / 86400.0);
+       
+       if (!defined($rating)) {
+               $rating = $initial_rating;
+               $rd = $initial_rd;
+       }
+
        return ($rating, $rd);
 }
 
@@ -33,74 +58,42 @@ sub create_user_if_not_exists {
        return if ($count > 0);
        $dbh->do('INSERT INTO users (username) VALUES (?)',
                undef, $username);
-       $dbh->do('INSERT INTO single_rating (username,ratetime,rating,rd) VALUES (?,CURRENT_TIMESTAMP,1500.0,350.0)',
-               undef, $username);
-       $dbh->do('INSERT INTO double_rating (username,ratetime,rating,rd) VALUES (?,CURRENT_TIMESTAMP,1500.0,350.0)',
-               undef, $username);
+       $dbh->do('INSERT INTO single_rating (username,ratetime,rating,rd) VALUES (?,CURRENT_TIMESTAMP,?,?)',
+               undef, $username, $initial_rating, $initial_rd);
+       $dbh->do('INSERT INTO double_rating (username,ratetime,rating,rd) VALUES (?,CURRENT_TIMESTAMP,?,?)',
+               undef, $username, $initial_rating, $initial_rd);
        return $dbh;
 }
 
-# 10-9 is 0.60
-# 10-0 is 1.00
-sub find_score {
-       my ($score1, $score2) = @_;
-       if ($score1 == 10) {
-               # yay, a win
-               return 0.60 + 0.40 * (9.0-$score2)/9.0;
-       }
-       if ($score2 == 10) {
-               # a loss
-               return 0.40 - 0.40 * (9.0-$score1)/9.0;
-       }
-       die "Nobody won?";
-}
-
-# c=8 => RD=50 moves to RD=350 over approx. five years
-our $c = 8;
-
+# $age is in days
 sub apply_aging {
        my ($rd, $age) = @_;
-       $rd = sqrt($rd*$rd + $c * $c * ($age / 86400.0));
-       $rd = 350.0 if ($rd > 350.0);
+       $rd = sqrt($rd*$rd + $c * $c * $age);
+       $rd = $initial_rd if ($rd > $initial_rd);
        return $rd;
 }
 
-our $q = log(10)/400.0;
-our $pi = 3.1415926535897932384626433832795;
-
-# computes a^b
-sub pow {
-       my ($a, $b) = @_;
-
-       # a^b = exp(log(a^b)) = exp(b log a)
-       return exp($b * log($a));
-}
-
-sub g {
-       my $rd = shift;
-       return 1.0 / sqrt(1.0 + 3.0 * $q * $q * $rd * $rd / ($pi*$pi));
-}
+sub calc_rating {
+       my ($rating1, $rd1, $rating2, $rd2, $score1, $score2) = @_;
+       my $result = `/srv/foosball.sesse.net/foosrank $rating1 $rd1 $rating2 $rd2 $score1 $score2`;
+       chomp $result;
+       my ($newr1, $newrd1, $likelihood) = split / /, $result;
 
-sub E {
-       my ($r, $rating, $rd) = @_;
-       return 1.0 / (1.0 + pow(10.0, - g($rd) * ($r - $rating) / 400.0));
-}
+       $newrd1 = 30.0 if ($newrd1 < 30.0);
 
-sub dsq {
-       my ($r, $rating, $rd) = @_;
-       return 1.0 / ($q*$q * g($rd) * g($rd) * E($r, $rating, $rd) * (1 - E($r, $rating, $rd)));
+       return ($newr1, $newrd1, $likelihood);
 }
 
-sub calc_rating {
-       my ($rating1, $rd1, $rating2, $rd2, $score1, $score2) = @_;
-       my $s1 = find_score($score1, $score2);
-       my $d1sq = dsq($rating1, $rating2, $rd2);
-       my $newr1 = $rating1 + ($q / (1.0/($rd1*$rd1) + 1.0 / $d1sq)) * g($rd2) * ($s1 - E($rating1, $rating2, $rd2));
-       my $newrd1 = sqrt(1.0 / (1.0 / ($rd1*$rd1) + 1.0 / $d1sq));
+sub calc_rating_double {
+       my ($rating1, $rd1, $rating2, $rd2, $rating3, $rd3, $rating4, $rd4, $score1, $score2) = @_;
+       my $result = `/srv/foosball.sesse.net/foosrank $rating1 $rd1 $rating2 $rd2 $rating3 $rd3 $rating4 $rd4 $score1 $score2`;
+       chomp $result;
+       my ($newr1, $newrd1) = split / /, $result;
 
        $newrd1 = 30.0 if ($newrd1 < 30.0);
 
        return ($newr1, $newrd1);
 }
 
+
 1;