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'};
17 my ($dbh, $locale, $last_season, $games, $ids) = @_;
18 my $q = $dbh->prepare('
20 deltager1.id as p1, deltager2.id as p2, maalfor, maalmot, least(pow(2.0, (sesong - ? + 3) / 3.0), 1.0) AS vekt
22 ( SELECT * FROM fotballresultater UNION ALL SELECT * FROM fotballresultater_2123 ) 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=?
29 $q->execute($last_season, $locale);
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;
43 my ($games, $ids) = @_;
45 my $tmpnam = POSIX::tmpnam();
46 open DATA, ">", $tmpnam
49 printf DATA "%d\n", scalar keys %$ids;
50 for my $id (keys %$ids) {
51 printf DATA "%d\n", $id;
53 for my $ref (@$games) {
54 printf DATA "%d %d %d %d %f\n", $ref->{'p1'}, $ref->{'p2'}, $ref->{'maalfor'}, $ref->{'maalmot'}, $ref->{'vekt'};
62 my ($filename, $locale, $ratings, $covariances, $aux_params) = @_;
64 open RATINGS, "$config::base_dir/bayeswf < $filename |"
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]);
74 push @$ratings, ($x[2] . "\t" . $x[0] . "\t" . $x[1]);
81 sub find_all_locales {
83 my $q = $dbh->prepare('SELECT kultur FROM fotballspraak WHERE nyestesesong<>-1');
87 while (my $ref = $q->fetchrow_hashref) {
88 push @locales, $ref->{'kultur'};
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;
99 $dbh->do('SET client_min_messages TO WARNING');
101 my @locales = find_all_locales($dbh);
104 my @covariances = ();
107 for my $locale (@locales) {
108 my $last_season = find_last_season($dbh, $locale);
111 fetch_games($dbh, $locale, $last_season, \@games, \%ids);
112 my $tmpnam = output_to_file(\@games, \%ids);
114 train_model($tmpnam, $locale, \@ratings, \@covariances, \@aux_params);
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');
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();
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();