]> git.sesse.net Git - foosball/blobdiff - recalc-single-result.pl
Add a SWIG interface, to be able to run recalc-single-result faster.
[foosball] / recalc-single-result.pl
index 679478791027ecb283f9ea6a88028b9ac8fc3d19..b6f8b779db39b233eff5e6054326a1c964978778 100755 (executable)
@@ -5,11 +5,20 @@ use DBI;
 use CGI;
 use CGI::Carp qw(fatalsToBrowser);
 require 'foosball.pm';
+use foosrank;
+no warnings 'once';
+
+my $dryrun = 0;
+if (defined($ARGV[0])) {
+       $dryrun = 1;
+       $foosball::initial_rd = $ARGV[0];
+       $foosball::c = $ARGV[1];
+}
 
 my $dbh = foosball::db_connect();
 $dbh->{AutoCommit} = 0;
 
-$dbh->do('delete from single_rating');
+$dbh->do('delete from single_rating') unless $dryrun;
 
 my %ratings = ();
 my $q = $dbh->prepare('select *,extract(epoch from gametime) as eptime from single_results order by gametime');
@@ -21,7 +30,7 @@ my $cll = 0.0;
 while (my $ref = $q->fetchrow_hashref) {
        for my $user (($ref->{'username1'}, $ref->{'username2'})) {
                if (!exists($ratings{$user})) {
-                       $ratings{$user} = [ 1500, 350, 0 ];
+                       $ratings{$user} = [ $foosball::initial_rating, $foosball::initial_rd, 0 ];
                }
        }
        
@@ -39,24 +48,26 @@ while (my $ref = $q->fetchrow_hashref) {
        $rd1 = foosball::apply_aging($rd1, $age1 / 86400.0);
        $rd2 = foosball::apply_aging($rd2, $age2 / 86400.0);
 
-       my ($newr1, $newrd1, $likelihood) = foosball::calc_rating($rating1, $rd1, $rating2, $rd2, $score1, $score2);
-       my ($newr2, $newrd2) = foosball::calc_rating($rating2, $rd2, $rating1, $rd1, $score2, $score1);
+       my ($newr1, $newrd1, $likelihood) = foosrank::compute_new_rating($rating1, $rd1, $rating2, $rd2, $score1, $score2);
+       my ($newr2, $newrd2) = foosrank::compute_new_rating($rating2, $rd2, $rating1, $rd1, $score2, $score1);
 
        $cll += log($likelihood);
 
-       printf("%-10s - %-10s: %u - %u, new ratings %u/%u %u/%u [$likelihood]\n",
-               $ref->{'username1'}, $ref->{'username2'}, $ref->{'score1'},
-               $ref->{'score2'}, $newr1, $newrd1, $newr2, $newrd2);
-       $dbh->do('insert into single_rating values (?,?,?,?,?)', undef,
-               $ref->{'username1'}, $ref->{'gametime'}, $newr1, $newrd1, $newr1-$rating1);
-       $dbh->do('insert into single_rating values (?,?,?,?,?)', undef,
-               $ref->{'username2'}, $ref->{'gametime'}, $newr2, $newrd2, $newr2-$rating2);
+       unless ($dryrun) {
+               printf("%-10s - %-10s: %u - %u, new ratings %u/%u %u/%u [$likelihood]\n",
+                       $ref->{'username1'}, $ref->{'username2'}, $ref->{'score1'},
+                       $ref->{'score2'}, $newr1, $newrd1, $newr2, $newrd2);
+               $dbh->do('insert into single_rating values (?,?,?,?,?)', undef,
+                       $ref->{'username1'}, $ref->{'gametime'}, $newr1, $newrd1, $newr1-$rating1);
+               $dbh->do('insert into single_rating values (?,?,?,?,?)', undef,
+                       $ref->{'username2'}, $ref->{'gametime'}, $newr2, $newrd2, $newr2-$rating2);
+       }
 
        $ratings{$ref->{'username1'}} = [ $newr1, $newrd1, $ref->{'eptime'} ];
        $ratings{$ref->{'username2'}} = [ $newr2, $newrd2, $ref->{'eptime'} ];
 }
 
-$dbh->commit;
+$dbh->commit unless $dryrun;
 $dbh->disconnect;
 
 printf "\nCombined negative log-likelihood (smaller value means a better model): %f\n",