Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. my $file = $ARGV[0] or die "No file specified";
  6. my %scores = ();
  7. my %power = ();
  8. my %weird = ();
  9. my %teamCnt = ();
  10. my %volatile = ();
  11.  
  12. sub read_csv($) {
  13. my $file = shift;
  14. open(DATA, '<', $file) or die "could not open $file";
  15. my $header = <DATA>;
  16. chomp $header;
  17. my ($homeTeam, $homeScore, $awayTeam, $awayScore) = (0, 0, 0, 0);
  18. my @fields = split(/,\s*/, $header);
  19. for my $i (0 .. $#fields) {
  20. if ($fields[$i] =~ /Home team/i) {
  21. $homeTeam = $i;
  22. }
  23. if ($fields[$i] =~ /Vis team/i) {
  24. $awayTeam = $i;
  25. }
  26. if ($fields[$i] =~ /score/i) {
  27. ($homeTeam > $awayTeam) ? $homeScore = $i : $awayScore = $i;
  28. }
  29. }
  30. my $i = 0;
  31. while (my $line = <DATA>){
  32. chomp $line;
  33. @fields = split(/,\s*/, $line);
  34. $scores{$i}{'t1'} = $fields[$homeTeam];
  35. $scores{$i}{'t1s'} = $fields[$homeScore];
  36. $teamCnt{$fields[$homeTeam]} = (exists $teamCnt{$fields[$homeTeam]}) ? $teamCnt{$fields[$homeTeam]} + 1 : 1;
  37.  
  38. $scores{$i}{'t2'} = $fields[$awayTeam];
  39. $scores{$i}{'t2s'} = $fields[$awayScore];
  40. $teamCnt{$fields[$awayTeam]} = (exists $teamCnt{$fields[$awayTeam]}) ? $teamCnt{$fields[$awayTeam]} + 1 : 1;
  41.  
  42. $i++;
  43. }
  44.  
  45. # Remove teams who have a single connection to the dataset.
  46. my $removed;
  47. do {
  48. $removed = 0;
  49. foreach my $team (keys %teamCnt) {
  50. if ($teamCnt{$team} le 1) {
  51. foreach my $matchup (keys %scores) {
  52. if ($scores{$matchup}{'t1'} eq $team) {
  53. $teamCnt{$scores{$matchup}{'t2'}}--;
  54. delete $scores{$matchup};
  55. delete $teamCnt{$team};
  56. print "Removed $team\n";
  57. $removed++;
  58. }
  59. if ($scores{$matchup}{'t2'} eq $team) {
  60. $teamCnt{$scores{$matchup}{'t1'}}--;
  61. delete $scores{$matchup};
  62. delete $teamCnt{$team};
  63. print "Removed $team\n";
  64. $removed++;
  65. }
  66. }
  67. }
  68. }
  69. } while ($removed gt 0);
  70. }
  71.  
  72. sub calculate_deltas($$$$){
  73. my ($t1Power, $t2Power, $scoreDiff, $velocity) = @_;
  74. # scoreDiff = t1 - t2, so t1 should go up if they win big
  75. my $t1Target = $t2Power + $scoreDiff;
  76. my $t2Target = $t1Power - $scoreDiff;
  77. return ((($t1Target-$t1Power)*$velocity),(($t2Target-$t2Power)*$velocity));
  78. }
  79.  
  80. sub init_power() {
  81. foreach my $i (keys %scores) {
  82. $power{$scores{$i}{'t1'}} = 100;
  83. $power{$scores{$i}{'t2'}} = 100;
  84. }
  85. }
  86.  
  87. sub run_round($) {
  88. my ($velocity) = @_;
  89. my %new_power = %power;
  90. foreach my $i (keys %scores) {
  91. my $t1p = $power{$scores{$i}{'t1'}};
  92. my $t2p = $power{$scores{$i}{'t2'}};
  93. my $diff = $scores{$i}{'t1s'} - $scores{$i}{'t2s'};
  94. my ($d1, $d2) = calculate_deltas($t1p, $t2p, $diff, $velocity);
  95. $new_power{$scores{$i}{'t1'}} += $d1;
  96. $new_power{$scores{$i}{'t2'}} += $d2;
  97. }
  98. %power = %new_power;
  99. }
  100.  
  101. sub print_sorted_power(%){
  102. my %pwr = @_;
  103. my @sorted = sort {$pwr{$b} <=> $pwr{$a}} (keys %pwr);
  104. foreach my $team (@sorted) {
  105. print "$team: $pwr{$team}\n";
  106. }
  107. }
  108.  
  109. read_csv($file);
  110. init_power();
  111. my $roundMax = 2000;
  112. my $velConst = 0.1;
  113. for (my $r = 0; $r < $roundMax; $r++) {
  114. # my $velocity = $velConst - $velConst*($r/$roundMax);
  115. # run_round($velocity);
  116. run_round($velConst);
  117. }
  118.  
  119.  
  120. print "\n\n";
  121. print_sorted_power(%power);
  122.  
  123. foreach my $tm (keys %teamCnt) {
  124. $volatile{$tm} = 0;
  125. }
  126.  
  127. print "\n\n";
  128. foreach my $i (keys %scores) {
  129. my $t1p = $power{$scores{$i}{'t1'}};
  130. my $t2p = $power{$scores{$i}{'t2'}};
  131. my $diff = ($scores{$i}{'t1s'} - $scores{$i}{'t2s'}) - ($t1p -$t2p) ;
  132. my $name = $scores{$i}{'t1'} . '-' . $scores{$i}{'t2'};
  133. $weird{$name} = $diff;
  134. $volatile{$scores{$i}{'t1'}} += abs($diff);
  135. $volatile{$scores{$i}{'t2'}} += abs($diff);
  136. }
  137. foreach my $wtf (sort {abs($weird{$b}) <=> abs($weird{$a})} (keys %weird)) {
  138. print "$wtf $weird{$wtf}\n";
  139. }
  140.  
  141. print "\n\n";
  142.  
  143. foreach my $v (sort {$volatile{$b} / $teamCnt{$b} <=> $volatile{$a} / $teamCnt{$a}} (keys %volatile)) {
  144. my $volAve = $volatile{$v} / $teamCnt{$v};
  145. print "$v $volAve\n";
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement