Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2019
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 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 $outdir = $ARGV[1] or die "no outdir specified";
  7. my $weighted = (exists $ARGV[2] && $ARGV[2]) ? 1 : 0;
  8. mkdir $outdir unless (-e $outdir);
  9.  
  10.  
  11. my %scores = ();
  12. my %power = ();
  13. my %weird = ();
  14. my %teamCnt = ();
  15. my %volatile = ();
  16.  
  17. printf "Weighted\n" if ($weighted);
  18. printf "Unweighted\n" unless ($weighted);
  19.  
  20. open(DELTAS, '>', "$outdir/deltas.txt") or die "Failed to open deltas.txt";
  21.  
  22. sub read_csv($) {
  23. my $file = shift;
  24. open(DATA, '<', $file) or die "could not open $file";
  25. my $header = <DATA>;
  26. chomp $header;
  27. my ($homeTeam, $homeScore, $awayTeam, $awayScore) = (0, 0, 0, 0);
  28. my @fields = split(/,\s*/, $header);
  29. for my $i (0 .. $#fields) {
  30. if ($fields[$i] =~ /home_team/i) {
  31. $homeTeam = $i;
  32. }
  33. if ($fields[$i] =~ /away_team/i) {
  34. $awayTeam = $i;
  35. }
  36. if ($fields[$i] =~ /home_points/i) {
  37. $homeScore = $i;
  38. }
  39. if ($fields[$i] =~ /away_points/i) {
  40. $awayScore = $i;
  41. }
  42. }
  43. my $i = 0;
  44. while (my $line = <DATA>){
  45. chomp $line;
  46. @fields = split(/,\s*/, $line);
  47. next if ($fields[$homeScore] eq '');
  48. $scores{$i}{'t1'} = $fields[$homeTeam];
  49. $scores{$i}{'t1s'} = $fields[$homeScore];
  50. $teamCnt{$fields[$homeTeam]} = (exists $teamCnt{$fields[$homeTeam]}) ? $teamCnt{$fields[$homeTeam]} + 1 : 1;
  51.  
  52. $scores{$i}{'t2'} = $fields[$awayTeam];
  53. $scores{$i}{'t2s'} = $fields[$awayScore];
  54. $teamCnt{$fields[$awayTeam]} = (exists $teamCnt{$fields[$awayTeam]}) ? $teamCnt{$fields[$awayTeam]} + 1 : 1;
  55.  
  56. $i++;
  57. }
  58.  
  59. #foreach my $tm (sort { $teamCnt{$b} <=> $teamCnt{$a} } (keys %teamCnt)){
  60. # DBG missing data issues
  61. #print "$teamCnt{$tm} $tm\n";
  62. #}
  63.  
  64. open(REMOVED, '>', "$outdir/removed.txt") or die "couldn't open removed.";
  65. # Remove teams who have too few connections to the dataset (2 or fewer)
  66. my $removed;
  67. do {
  68. $removed = 0;
  69. foreach my $team (keys %teamCnt) {
  70. if ($teamCnt{$team} <= 3) {
  71. #Debug print to let us know if any team has 3 or fewer
  72. if ($teamCnt{$team} >= 3) {
  73. print "$team\n";
  74. next;
  75. }
  76. foreach my $matchup (keys %scores) {
  77. #next unless (exists $scores{$matchup});
  78. if ($scores{$matchup}{'t1'} eq $team or $scores{$matchup}{'t2'} eq $team) {
  79. $teamCnt{$scores{$matchup}{'t2'}}--;
  80. $teamCnt{$scores{$matchup}{'t1'}}--;
  81. delete $scores{$matchup};
  82. $removed++;
  83. } }
  84. delete $teamCnt{$team} unless ($teamCnt{$team} >= 1);
  85. print REMOVED "Removed $team\n";
  86. }
  87. }
  88. } while ($removed > 0);
  89. close(REMOVED);
  90.  
  91. #foreach my $tm (sort { $teamCnt{$b} <=> $teamCnt{$a} } (keys %teamCnt)){
  92. # DBG missing data issues
  93. #print "$teamCnt{$tm} $tm\n";
  94. #}
  95.  
  96. }
  97.  
  98. sub calculate_importance($$) {
  99. my ($powerDiff, $scoreDiff) = @_;
  100. return 1 unless ($weighted);
  101. if ($scoreDiff < 0) {
  102. # It's easier to think from the perspective of the winner
  103. $powerDiff = 0 - $powerDiff;
  104. $scoreDiff = 0 - $scoreDiff;
  105. }
  106. # upsets of a certain caliber are always of normal importance.
  107. # NOTE! This can cause flip-flopping behavior, check deltas.txt to ensure it resolves to 0.
  108. #if ($powerDiff < -5) {
  109. # return 1;
  110. #}
  111. # by capping at this point value we ensure nobody will get fewer power for scoring more.
  112. if ($scoreDiff >= 50){
  113. return .5;
  114. }
  115. # Calculate importance of game which decreases as blowout status goes up.
  116. return (1 - (($scoreDiff)/100));
  117. }
  118.  
  119. sub calculate_deltas($$$$){
  120. my ($t1Power, $t2Power, $scoreDiff, $velocity) = @_;
  121. # scoreDiff = t1 - t2, so t1 should go up if they win big
  122. my $t1Target = $t2Power + $scoreDiff;
  123. my $t2Target = $t1Power - $scoreDiff;
  124. $velocity = $velocity* calculate_importance($t1Power - $t2Power, $scoreDiff);
  125. return ((($t1Target-$t1Power)*$velocity),(($t2Target-$t2Power)*$velocity));
  126. }
  127.  
  128. sub init_power() {
  129. foreach my $i (keys %scores) {
  130. $power{$scores{$i}{'t1'}} = 100;
  131. $power{$scores{$i}{'t2'}} = 100;
  132. }
  133. }
  134.  
  135. sub run_round($) {
  136. my ($velocity) = @_;
  137. my %new_power = %power;
  138. foreach my $i (keys %scores) {
  139. my $t1p = $power{$scores{$i}{'t1'}};
  140. my $t2p = $power{$scores{$i}{'t2'}};
  141. my $diff = $scores{$i}{'t1s'} - $scores{$i}{'t2s'};
  142. my ($d1, $d2) = calculate_deltas($t1p, $t2p, $diff, $velocity);
  143. $new_power{$scores{$i}{'t1'}} += $d1;
  144. $new_power{$scores{$i}{'t2'}} += $d2;
  145. }
  146. my $delta = 0;
  147. foreach my $tm (keys %power) {
  148. my $tmdelta = abs($power{$tm} - $new_power{$tm});
  149. $delta += $tmdelta;
  150. if ($tmdelta > 1) {
  151. printf DELTAS "$tm %.03f\n", $tmdelta;
  152. }
  153. }
  154. printf DELTAS "%.03f\n", $delta;
  155. %power = %new_power;
  156. }
  157.  
  158. sub print_sorted_power(%){
  159. my %pwr = @_;
  160. my @sorted = sort {$pwr{$b} <=> $pwr{$a}} (keys %pwr);
  161. open (RESULTS, '>', "$outdir/results.txt") or die "Couldn't open results";
  162. foreach my $team (@sorted) {
  163. printf RESULTS "$team: %.03f\n", $pwr{$team};
  164. }
  165. }
  166.  
  167.  
  168. read_csv($file);
  169. init_power();
  170. my $roundMax = 100;
  171. my $velConst = 0.1;
  172. for (my $r = 0; $r < $roundMax; $r++) {
  173. # my $velocity = $velConst - $velConst*($r/$roundMax);
  174. # run_round($velocity);
  175. run_round($velConst);
  176. }
  177.  
  178.  
  179. print_sorted_power(%power);
  180.  
  181. foreach my $tm (keys %teamCnt) {
  182. $volatile{$tm} = 0;
  183. }
  184.  
  185. foreach my $i (keys %scores) {
  186. my $t1p = $power{$scores{$i}{'t1'}};
  187. my $t2p = $power{$scores{$i}{'t2'}};
  188. my $diff = ($scores{$i}{'t1s'} - $scores{$i}{'t2s'}) - ($t1p -$t2p) ;
  189. my $name = $scores{$i}{'t1'} . '-' . $scores{$i}{'t2'};
  190. $weird{$name} = $diff;
  191. $volatile{$scores{$i}{'t1'}} += abs($diff);
  192. $volatile{$scores{$i}{'t2'}} += abs($diff);
  193. }
  194.  
  195. open (WEIRD_GAMES, '>', "$outdir/weird_games.txt") or die "Couldn't open weird games";
  196. foreach my $wtf (sort {abs($weird{$b}) <=> abs($weird{$a})} (keys %weird)) {
  197. print WEIRD_GAMES "$wtf $weird{$wtf}\n";
  198. }
  199. close (WEIRD_GAMES);
  200.  
  201.  
  202. open (WEIRD_TEAMS, '>', "$outdir/weird_teams.txt") or die "Couldn't open weird teams";
  203. foreach my $v (sort {$volatile{$b} / $teamCnt{$b} <=> $volatile{$a} / $teamCnt{$a}} (keys %volatile)) {
  204. my $volAve = $volatile{$v} / $teamCnt{$v};
  205. print WEIRD_TEAMS "$v $volAve\n";
  206. }
  207. close(WEIRD_TEAMS);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement