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