Skip to content

Instantly share code, notes, and snippets.

@fuba
Created February 7, 2011 13:29
Show Gist options
  • Save fuba/814356 to your computer and use it in GitHub Desktop.
Save fuba/814356 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use YAML::Syck;
use POSIX;
my $do_yaocho = shift;
die 'USAGE: ./sumo.pl [0/1:do_yaocho]' unless defined $do_yaocho;
my $bashosu = 6 * 10 ;
my $rikishisu = int(3497/$bashosu);
my $div_num = ceil(($rikishisu+1)/2);
my %results;
srand(time);
sub power {
my ($rikishi_l, $rikishi_r, $rikishisu) = @_;
my $sign = ($rikishi_l > $rikishi_r) ? 1 : -1;
my ($large, $small) = ($rikishi_l > $rikishi_r)
? ($rikishi_l, $rikishi_r)
: ($rikishi_r, $rikishi_l);
my $power = $large / $rikishisu;
$power *= $power;
$power *= $power;
$power *= $power;
return $sign * $power;
}
sub aite {
my ($rikishi_l, $basholog) = @_;
my $rikishi_r;
do {
$rikishi_r = floor(rand($rikishisu));
} while (
($basholog->{$rikishi_l.'_'.$rikishi_r})
|| (abs($rikishi_l - $rikishi_r) > ($rikishisu / 3)) # 番付が近い相手と当たる
);
$basholog->{$rikishi_l.'_'.$rikishi_r} = 1;
return $rikishi_r;
}
sub torikumi {
my ($rikishi_l, $rikishi_r, $day, $win_l, $win_r) = @_;
my ($torikumi, $power, $kuuki) = (0,0,0);
do {
$power = power($rikishi_l, $rikishi_r, $rikishisu);
$kuuki = rand(2)-1; # lが勝つ空気なら+, 負ける空気なら-
if (($win_l < ($day / 2)) && ($win_l > ($day / 3))) {
$kuuki += 0.05;
}
if (($win_r < ($day / 2)) && ($win_r > ($day / 3))) {
$kuuki -= 0.05;
}
$torikumi = $kuuki + $power; # 取り組み=強さ+空気
} while ($torikumi == 0);
if ($day > 12 && $win_l == ceil($day / 2) && abs($win_l - $win_r) >= 1) {
#warn "$day $win_l $win_r";
if (rand(1) > 0.5) {
$torikumi = $do_yaocho; # 八百長
}
}
#warn "$rikishi_l $rikishi_r $power $kuuki $torikumi";
return ($torikumi > 0) ? 1 : 0;
}
for my $basho (0..$bashosu) {
my %basholog;
for my $day (0..14) {
for my $rikishi_l (0..$rikishisu-1) {
$results{$rikishi_l} ||= [];
next if (defined $results{$rikishi_l}->[$basho][$day]);
my $rikishi_r = aite($rikishi_l, \%basholog);
$results{$rikishi_r} ||= [];
my $win_l = grep {$_ == 1} grep {defined $_} @{$results{$rikishi_l}->[$basho]};
my $win_r = grep {$_ == 1} grep {defined $_} @{$results{$rikishi_r}->[$basho]};
my $r = torikumi($rikishi_l, $rikishi_r, $day, $win_l, $win_r);
$results{$rikishi_l}->[$basho][$day] = $r;
$results{$rikishi_r}->[$basho][$day] = ($r == 1) ? 0 : 1;
}
}
}
my %histgram;
my $sum = 0;
for my $basho (0..$bashosu) {
for my $rikishi (0..$rikishisu-1) {
my $star = grep {$_ == 1} @{$results{$rikishi}->[$basho]};
my $label = sprintf("w%02dl%02d", $star, (15-$star));
$histgram{$label} ||= 0;
$histgram{$label}++;
$sum++;
}
}
warn $sum;
warn Dump \%histgram;
warn Dump {map {($_, ($histgram{$_}/$sum)*100)} keys %histgram};
__END__
result
3538 at sumo.pl line 109.
---
w01l14: 3
w02l13: 24
w03l12: 97
w04l11: 217
w05l10: 374
w06l09: 498
w07l08: 524
w08l07: 727
w09l06: 480
w10l05: 259
w11l04: 150
w12l03: 77
w13l02: 60
w14l01: 31
w15l00: 17
---
w01l14: 0.0847936687394008
w02l13: 0.678349349915206
w03l12: 2.74166195590729
w04l11: 6.13340870548332
w05l10: 10.5709440361786
w06l09: 14.0757490107405
w07l08: 14.8106274731487
w08l07: 20.5483323911815
w09l06: 13.5669869983041
w10l05: 7.32052006783493
w11l04: 4.23968343697004
w12l03: 2.17637083097795
w13l02: 1.69587337478802
w14l01: 0.876201243640475
w15l00: 0.480497456189938
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment