]> git.sesse.net Git - wloh/blob - train.pl
Add a friendly header to the scenario analysis.
[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 my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password)
10         or die "connect: " . $DBI::errstr;
11 $dbh->{AutoCommit} = 0;
12 $dbh->{RaiseError} = 1;
13
14 $dbh->do('SET client_min_messages TO WARNING');
15
16 # Find last completely done season 
17 my $ref = $dbh->selectrow_hashref('SELECT sesong FROM fotballserier GROUP BY sesong HAVING COUNT(*)=COUNT(avgjort=1 OR NULL) ORDER BY sesong DESC LIMIT 1');
18 my $last_season = $ref->{'sesong'};
19
20 # Fetch games
21 my $q = $dbh->prepare('
22 SELECT
23   deltager1.id as p1, deltager2.id as p2, maalfor, maalmot, least(pow(2.0, (sesong - ? + 3) / 3.0), 1.0) AS vekt
24 FROM
25   Fotballresultater resultater
26   JOIN Fotballdeltagere deltager1 ON resultater.Lagrecno=deltager1.Nr AND resultater.Serie=deltager1.Serie
27   JOIN Fotballdeltagere deltager2 ON resultater.Motstander=deltager2.Nr AND resultater.Serie=deltager2.Serie
28   JOIN Fotballserier serier on resultater.Serie=serier.Nr
29 WHERE deltager1.Nr > deltager2.nr
30 ');
31 $q->execute($last_season);
32
33 my @games = ();
34 my %ids = ();
35
36 while (my $ref = $q->fetchrow_hashref) {
37         next if ($ref->{'maalfor'} == 150 && $ref->{'maalmot'} == 0);
38         next if ($ref->{'maalfor'} == 0 && $ref->{'maalmot'} == 150);
39         next if ($ref->{'maalfor'} == 150 && $ref->{'maalmot'} == 150);
40         push @games, { %$ref };
41         $ids{$ref->{'p1'}} = 1;
42         $ids{$ref->{'p2'}} = 1;
43 }
44
45 # Output to file
46 my $tmpnam = POSIX::tmpnam();
47 open DATA, ">", $tmpnam
48         or die "$tmpnam: $!";
49
50 printf DATA "%d\n", scalar keys %ids;
51 for my $id (keys %ids) {
52         printf DATA "%d\n", $id;
53 }
54 for my $ref (@games) {
55         printf DATA "%d %d %d %d %f\n", $ref->{'p1'}, $ref->{'p2'}, $ref->{'maalfor'}, $ref->{'maalmot'}, $ref->{'vekt'};
56 }
57 close DATA;
58
59 my @ratings = ();
60 my @covariances = ();
61
62 open RATINGS, "$config::base_dir/bayeswf < $tmpnam |"
63         or die "bayeswf: $!";
64 while (<RATINGS>) {
65         chomp;
66         my @x = split;
67         if ($x[0] eq 'covariance') {
68                 push @covariances, (join("\t", @x[1..3]));
69         } else {
70                 push @ratings, ($x[2] . "\t" . $x[0] . "\t" . $x[1]);
71         }
72 }
73
74 $dbh->do('TRUNCATE ratings');
75 $dbh->do('COPY ratings ( id, rating, rating_stddev ) FROM STDIN');
76 $dbh->pg_putcopydata(join("\n", @ratings));
77 $dbh->pg_putcopyend();
78
79 $dbh->do('CREATE TABLE new_covariance ( player1 smallint NOT NULL, player2 smallint NOT NULL, cov float NOT NULL )');
80 $dbh->do('COPY new_covariance ( player1, player2, cov ) FROM STDIN');
81 $dbh->pg_putcopydata(join("\n", @covariances));
82 $dbh->pg_putcopyend();
83 $dbh->do('ALTER TABLE new_covariance ADD PRIMARY KEY ( player1, player2 );');
84 $dbh->do('DROP TABLE IF EXISTS covariance');
85 $dbh->do('ALTER TABLE new_covariance RENAME TO covariance');
86
87 $dbh->commit;
88 unlink($tmpnam);