]> git.sesse.net Git - wloh/blob - train.pl
Increase the expiry time for the ratings page, and rely on clearing it explicitly...
[wloh] / train.pl
1 #! /usr/bin/perl
2 use DBI;
3 use strict;
4 use warnings;
5 no warnings qw(once);
6 use POSIX;
7 use lib qw( include );
8 require 'config.pm';
9 require 'common.pm';
10
11 # Find last completely done season 
12 sub find_last_season {
13         my ($dbh, $locale) = @_;
14         my $ref = $dbh->selectrow_hashref('SELECT sesong FROM fotballserier se JOIN fotballspraak sp ON se.spraak=sp.id GROUP BY kultur,sesong HAVING COUNT(*)=COUNT(avgjort=1 OR NULL) AND kultur=? ORDER BY kultur,sesong DESC LIMIT 1', undef, $locale);
15         return $ref->{'sesong'};
16 }
17
18 sub fetch_games {
19         my ($dbh, $locale, $last_season, $games, $ids) = @_;
20         my $q = $dbh->prepare('
21 SELECT
22   deltager1.id as p1, deltager2.id as p2, maalfor, maalmot, least(pow(2.0, (sesong - ? + 3) / 3.0), 1.0) AS vekt
23 FROM
24   ( SELECT * FROM fotballresultater UNION ALL SELECT * FROM fotballresultater_2123 ) resultater
25   JOIN Fotballdeltagere deltager1 ON resultater.Lagrecno=deltager1.Nr AND resultater.Serie=deltager1.Serie
26   JOIN Fotballdeltagere deltager2 ON resultater.Motstander=deltager2.Nr AND resultater.Serie=deltager2.Serie
27   JOIN Fotballserier serier ON resultater.Serie=serier.Nr
28   JOIN Fotballspraak spraak ON serier.Spraak=spraak.Id
29 WHERE deltager1.Nr > deltager2.nr AND kultur=?
30         ');
31         $q->execute($last_season, $locale);
32
33         while (my $ref = $q->fetchrow_hashref) {
34                 next if ($ref->{'maalfor'} == 150 && $ref->{'maalmot'} == 0);
35                 next if ($ref->{'maalfor'} == 0 && $ref->{'maalmot'} == 150);
36                 next if ($ref->{'maalfor'} == 150 && $ref->{'maalmot'} == 150);
37                 next if ($ref->{'maalfor'} == 0 && $ref->{'maalmot'} == 0);
38                 push @$games, { %$ref };
39                 $ids->{$ref->{'p1'}} = 1;
40                 $ids->{$ref->{'p2'}} = 1;
41         }
42 }
43
44 sub output_to_file {
45         my ($games, $ids) = @_;
46
47         my $tmpnam = POSIX::tmpnam();
48         open DATA, ">", $tmpnam
49                 or die "$tmpnam: $!";
50
51         printf DATA "%d\n", scalar keys %$ids;
52         for my $id (keys %$ids) {
53                 printf DATA "%d\n", $id;
54         }
55         for my $ref (@$games) {
56                 printf DATA "%d %d %d %d %f\n", $ref->{'p1'}, $ref->{'p2'}, $ref->{'maalfor'}, $ref->{'maalmot'}, $ref->{'vekt'};
57         }
58         close DATA;
59
60         return $tmpnam;
61 }
62
63 sub train_model {
64         my ($filename, $locale, $ratings, $covariances, $aux_params) = @_;
65
66         open RATINGS, "$config::base_dir/bayeswf < $filename |"
67                 or die "bayeswf: $!";
68         while (<RATINGS>) {
69                 chomp;
70                 my @x = split;
71                 if ($x[0] eq 'covariance') {
72                         push @$covariances, (join("\t", @x[1..3]));
73                 } elsif ($x[0] eq 'aux_param') {
74                         push @$aux_params, ($locale .  "\t" . $x[1] . "\t" . $x[2]);
75                 } else {
76                         push @$ratings, ($x[2] . "\t" . $x[0] . "\t" . $x[1]);
77                 }
78         }
79
80         close RATINGS;
81 }
82
83 my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password)
84         or die "connect: " . $DBI::errstr;
85 $dbh->{AutoCommit} = 0;
86 $dbh->{RaiseError} = 1;
87
88 $dbh->do('SET client_min_messages TO WARNING');
89
90 my @locales = wloh_common::find_all_locales($dbh);
91
92 my @ratings = ();
93 my @covariances = ();
94 my @aux_params = ();
95
96 for my $locale (@locales) {
97         my $last_season = find_last_season($dbh, $locale);
98         my @games = ();
99         my %ids = ();
100         fetch_games($dbh, $locale, $last_season, \@games, \%ids);
101         my $tmpnam = output_to_file(\@games, \%ids);
102
103         train_model($tmpnam, $locale, \@ratings, \@covariances, \@aux_params);
104         unlink($tmpnam);
105 }
106
107 $dbh->do('CREATE TABLE new_covariance ( player1 smallint NOT NULL, player2 smallint NOT NULL, cov float NOT NULL )');
108 $dbh->do('COPY new_covariance ( player1, player2, cov ) FROM STDIN');
109 $dbh->pg_putcopydata(join("\n", @covariances));
110 $dbh->pg_putcopyend();
111 $dbh->do('ALTER TABLE new_covariance ADD PRIMARY KEY ( player1, player2 );');
112 $dbh->do('DROP TABLE IF EXISTS covariance');
113 $dbh->do('ALTER TABLE new_covariance RENAME TO covariance');
114
115 $dbh->do('TRUNCATE aux_params');
116 $dbh->do('COPY aux_params ( kultur, id, value ) FROM STDIN');
117 $dbh->pg_putcopydata(join("\n", @aux_params));
118 $dbh->pg_putcopyend();
119
120 $dbh->do('TRUNCATE ratings');
121 $dbh->do('COPY ratings ( id, rating, rating_stddev ) FROM STDIN');
122 $dbh->pg_putcopydata(join("\n", @ratings));
123 $dbh->pg_putcopyend();
124
125 $dbh->commit;