Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- my $file = $ARGV[0] or die "No file specified";
- my %scores = ();
- my %power = ();
- my %weird = ();
- my %teamCnt = ();
- my %volatile = ();
- sub read_csv($) {
- my $file = shift;
- open(DATA, '<', $file) or die "could not open $file";
- my $header = <DATA>;
- chomp $header;
- my ($homeTeam, $homeScore, $awayTeam, $awayScore) = (0, 0, 0, 0);
- my @fields = split(/,\s*/, $header);
- for my $i (0 .. $#fields) {
- if ($fields[$i] =~ /Home team/i) {
- $homeTeam = $i;
- }
- if ($fields[$i] =~ /Vis team/i) {
- $awayTeam = $i;
- }
- if ($fields[$i] =~ /score/i) {
- ($homeTeam > $awayTeam) ? $homeScore = $i : $awayScore = $i;
- }
- }
- my $i = 0;
- while (my $line = <DATA>){
- chomp $line;
- @fields = split(/,\s*/, $line);
- $scores{$i}{'t1'} = $fields[$homeTeam];
- $scores{$i}{'t1s'} = $fields[$homeScore];
- $teamCnt{$fields[$homeTeam]} = (exists $teamCnt{$fields[$homeTeam]}) ? $teamCnt{$fields[$homeTeam]} + 1 : 1;
- $scores{$i}{'t2'} = $fields[$awayTeam];
- $scores{$i}{'t2s'} = $fields[$awayScore];
- $teamCnt{$fields[$awayTeam]} = (exists $teamCnt{$fields[$awayTeam]}) ? $teamCnt{$fields[$awayTeam]} + 1 : 1;
- $i++;
- }
- # Remove teams who have a single connection to the dataset.
- my $removed;
- do {
- $removed = 0;
- foreach my $team (keys %teamCnt) {
- if ($teamCnt{$team} le 1) {
- foreach my $matchup (keys %scores) {
- if ($scores{$matchup}{'t1'} eq $team) {
- $teamCnt{$scores{$matchup}{'t2'}}--;
- delete $scores{$matchup};
- delete $teamCnt{$team};
- print "Removed $team\n";
- $removed++;
- }
- if ($scores{$matchup}{'t2'} eq $team) {
- $teamCnt{$scores{$matchup}{'t1'}}--;
- delete $scores{$matchup};
- delete $teamCnt{$team};
- print "Removed $team\n";
- $removed++;
- }
- }
- }
- }
- } while ($removed gt 0);
- }
- sub calculate_deltas($$$$){
- my ($t1Power, $t2Power, $scoreDiff, $velocity) = @_;
- # scoreDiff = t1 - t2, so t1 should go up if they win big
- my $t1Target = $t2Power + $scoreDiff;
- my $t2Target = $t1Power - $scoreDiff;
- return ((($t1Target-$t1Power)*$velocity),(($t2Target-$t2Power)*$velocity));
- }
- sub init_power() {
- foreach my $i (keys %scores) {
- $power{$scores{$i}{'t1'}} = 100;
- $power{$scores{$i}{'t2'}} = 100;
- }
- }
- sub run_round($) {
- my ($velocity) = @_;
- my %new_power = %power;
- foreach my $i (keys %scores) {
- my $t1p = $power{$scores{$i}{'t1'}};
- my $t2p = $power{$scores{$i}{'t2'}};
- my $diff = $scores{$i}{'t1s'} - $scores{$i}{'t2s'};
- my ($d1, $d2) = calculate_deltas($t1p, $t2p, $diff, $velocity);
- $new_power{$scores{$i}{'t1'}} += $d1;
- $new_power{$scores{$i}{'t2'}} += $d2;
- }
- %power = %new_power;
- }
- sub print_sorted_power(%){
- my %pwr = @_;
- my @sorted = sort {$pwr{$b} <=> $pwr{$a}} (keys %pwr);
- foreach my $team (@sorted) {
- print "$team: $pwr{$team}\n";
- }
- }
- read_csv($file);
- init_power();
- my $roundMax = 2000;
- my $velConst = 0.1;
- for (my $r = 0; $r < $roundMax; $r++) {
- # my $velocity = $velConst - $velConst*($r/$roundMax);
- # run_round($velocity);
- run_round($velConst);
- }
- print "\n\n";
- print_sorted_power(%power);
- foreach my $tm (keys %teamCnt) {
- $volatile{$tm} = 0;
- }
- print "\n\n";
- foreach my $i (keys %scores) {
- my $t1p = $power{$scores{$i}{'t1'}};
- my $t2p = $power{$scores{$i}{'t2'}};
- my $diff = ($scores{$i}{'t1s'} - $scores{$i}{'t2s'}) - ($t1p -$t2p) ;
- my $name = $scores{$i}{'t1'} . '-' . $scores{$i}{'t2'};
- $weird{$name} = $diff;
- $volatile{$scores{$i}{'t1'}} += abs($diff);
- $volatile{$scores{$i}{'t2'}} += abs($diff);
- }
- foreach my $wtf (sort {abs($weird{$b}) <=> abs($weird{$a})} (keys %weird)) {
- print "$wtf $weird{$wtf}\n";
- }
- print "\n\n";
- foreach my $v (sort {$volatile{$b} / $teamCnt{$b} <=> $volatile{$a} / $teamCnt{$a}} (keys %volatile)) {
- my $volAve = $volatile{$v} / $teamCnt{$v};
- print "$v $volAve\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement