X-Git-Url: https://git.sesse.net/?p=wloh;a=blobdiff_plain;f=train.pl;h=97c33d32534c0697bf451d629e7a4ded8e06e8a9;hp=19d9271455a509d7392ae70e1342bf4e4382c7f3;hb=fde909c294de9806dd6337f5acb0ed87c41557c6;hpb=5d811d8049a3f14cb586d4e9eb195de18036ded1 diff --git a/train.pl b/train.pl index 19d9271..97c33d3 100755 --- a/train.pl +++ b/train.pl @@ -4,7 +4,9 @@ use strict; use warnings; no warnings qw(once); use POSIX; -require './config.pm'; +use lib qw( include ); +require 'config.pm'; +require 'common.pm'; # Find last completely done season sub find_last_season { @@ -19,7 +21,7 @@ sub fetch_games { SELECT deltager1.id as p1, deltager2.id as p2, maalfor, maalmot, least(pow(2.0, (sesong - ? + 3) / 3.0), 1.0) AS vekt FROM - Fotballresultater resultater + ( SELECT * FROM fotballresultater UNION ALL SELECT * FROM fotballresultater_2123 ) resultater JOIN Fotballdeltagere deltager1 ON resultater.Lagrecno=deltager1.Nr AND resultater.Serie=deltager1.Serie JOIN Fotballdeltagere deltager2 ON resultater.Motstander=deltager2.Nr AND resultater.Serie=deltager2.Serie JOIN Fotballserier serier ON resultater.Serie=serier.Nr @@ -40,12 +42,13 @@ WHERE deltager1.Nr > deltager2.nr AND kultur=? } sub output_to_file { - my ($games, $ids) = @_; + my ($locale, $games, $ids) = @_; my $tmpnam = POSIX::tmpnam(); open DATA, ">", $tmpnam or die "$tmpnam: $!"; + printf DATA "%s\n", $locale; printf DATA "%d\n", scalar keys %$ids; for my $id (keys %$ids) { printf DATA "%d\n", $id; @@ -58,79 +61,22 @@ sub output_to_file { return $tmpnam; } -sub train_model { - my ($filename, $locale, $ratings, $covariances, $aux_params) = @_; - - open RATINGS, "$config::base_dir/bayeswf < $filename |" - or die "bayeswf: $!"; - while () { - chomp; - my @x = split; - if ($x[0] eq 'covariance') { - push @$covariances, (join("\t", @x[1..3])); - } elsif ($x[0] eq 'aux_param') { - push @$aux_params, ($locale . "\t" . $x[1] . "\t" . $x[2]); - } else { - push @$ratings, ($x[2] . "\t" . $x[0] . "\t" . $x[1]); - } - } - - close RATINGS; -} - -sub find_all_locales { - my $dbh = shift; - my $q = $dbh->prepare('SELECT kultur FROM fotballspraak WHERE nyestesesong<>-1'); - $q->execute; - - my @locales = (); - while (my $ref = $q->fetchrow_hashref) { - push @locales, $ref->{'kultur'}; - } - - return @locales; -} - my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password) or die "connect: " . $DBI::errstr; -$dbh->{AutoCommit} = 0; -$dbh->{RaiseError} = 1; - -$dbh->do('SET client_min_messages TO WARNING'); +$dbh->{AutoCommit} = 1; -my @locales = find_all_locales($dbh); - -my @ratings = (); -my @covariances = (); -my @aux_params = (); +my @locales = wloh_common::find_all_locales($dbh); +my @filenames = (); for my $locale (@locales) { my $last_season = find_last_season($dbh, $locale); my @games = (); my %ids = (); fetch_games($dbh, $locale, $last_season, \@games, \%ids); - my $tmpnam = output_to_file(\@games, \%ids); - - train_model($tmpnam, $locale, \@ratings, \@covariances, \@aux_params); - unlink($tmpnam); + my $tmpnam = output_to_file($locale, \@games, \%ids); + push @filenames, $tmpnam; } -$dbh->do('CREATE TABLE new_covariance ( player1 smallint NOT NULL, player2 smallint NOT NULL, cov float NOT NULL )'); -$dbh->do('COPY new_covariance ( player1, player2, cov ) FROM STDIN'); -$dbh->pg_putcopydata(join("\n", @covariances)); -$dbh->pg_putcopyend(); -$dbh->do('ALTER TABLE new_covariance ADD PRIMARY KEY ( player1, player2 );'); -$dbh->do('DROP TABLE IF EXISTS covariance'); -$dbh->do('ALTER TABLE new_covariance RENAME TO covariance'); - -$dbh->do('TRUNCATE aux_params'); -$dbh->do('COPY aux_params ( kultur, id, value ) FROM STDIN'); -$dbh->pg_putcopydata(join("\n", @aux_params)); -$dbh->pg_putcopyend(); - -$dbh->do('TRUNCATE ratings'); -$dbh->do('COPY ratings ( id, rating, rating_stddev ) FROM STDIN'); -$dbh->pg_putcopydata(join("\n", @ratings)); -$dbh->pg_putcopyend(); +$dbh->disconnect; -$dbh->commit; +system("$config::base_dir/bayeswf", @filenames);