| 1 | # Algorithm::Pair::Best2.pm | ||||||||||||||||||||
| 2 | # | ||||||||||||||||||||
| 3 | # Copyright (C) 2004-2011 Reid Augustin reid@HelloSix.com | ||||||||||||||||||||
| 4 | # | ||||||||||||||||||||
| 5 | # This library is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 6 | # under the same terms as Perl itself, either Perl version 5.8.5 or, at your | ||||||||||||||||||||
| 7 | # option, any later version of Perl 5 you may have available. | ||||||||||||||||||||
| 8 | # | ||||||||||||||||||||
| 9 | # This program is distributed in the hope that it will be useful, but | ||||||||||||||||||||
| 10 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||||||||||||||||||||
| 11 | # or FITNESS FOR A PARTICULAR PURPOSE. | ||||||||||||||||||||
| 12 | # | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 1 | ▶ | use 5.002; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | package Algorithm::Pair::Best2; | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 1 | our $VERSION = '2.040'; # VERSION | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | # ABSTRACT: select pairings (designed for Go tournaments, but can be used for anything). | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 2 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=27.2
Pod
documented
| |||||||||||||||||||||
| 27 | 2 | my ($proto, %args) = @_; | |||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 2 | my $self = {}; | |||||||||||||||||||
| 30 | $self->{scoreSub} = delete $args{scoreSub} | ||||||||||||||||||||
| 31 | 0 | ▶ | || sub { croak "No scoreSub() callback" }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
delete $args{'scoreSub'} || sub {
croak('No scoreSub() callback');
}
MC/DC0%0/2
delete $args{'scoreSub'} || sub {
croak('No scoreSub() callback');
}
delete $args{'scoreSub'}
sub {
croak('No scoreSub() callback');
}
Truth table33%1/3
A || B
A= delete $args{'scoreSub'}B= sub {
croak('No scoreSub() callback');
}
| |||||||||||||||||||||
| 32 | 2 | ▶ | $self->{items} = delete $args{items} || []; | ||||||||||||||||||
|
Condition50%1/2
delete $args{'items'} || []
MC/DC0%0/1
delete $args{'items'} || []
delete $args{'items'}
Truth table50%1/2
A
A= delete $args{'items'}
| |||||||||||||||||||||
| 33 | 2 | ▶ | $self->{progress} = delete $args{progress} || sub { }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
delete $args{'progress'} || sub {
}
MC/DC0%0/2
delete $args{'progress'} || sub {
}
delete $args{'progress'}
sub {
}
Truth table33%1/3
A || B
A= delete $args{'progress'}B= sub {
}
| |||||||||||||||||||||
| 34 | 2 | ▶ | $self->{window} = delete $args{window} || 5; | ||||||||||||||||||
|
Condition50%1/2
delete $args{'window'} || 5
MC/DC0%0/1
delete $args{'window'} || 5
delete $args{'window'}
Truth table50%1/2
A
A= delete $args{'window'}
| |||||||||||||||||||||
| 35 | 2 | ▶ | if (keys %args) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | 0 | ▶ | croak sprintf "Unknown option%s to %s->new: %s", | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | scalar(keys %args) > 1 ? 's' : '', | ||||||||||||||||||||
| 38 | __PACKAGE__, | ||||||||||||||||||||
| 39 | join(', ', keys %args); | ||||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | 2 | ▶ | return bless($self, ref($proto) || $proto); | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | ### my (%cache, %all, @head); # debug variables. | ||||||||||||||||||||
| 45 | ### | ||||||||||||||||||||
| 46 | ### | ||||||||||||||||||||
| 47 | ### sub dbg_hash { | ||||||||||||||||||||
| 48 | ### my ($self, $key, $val) = @_; | ||||||||||||||||||||
| 49 | ### | ||||||||||||||||||||
| 50 | ### my $cache = \%cache; | ||||||||||||||||||||
| 51 | ### my $array; | ||||||||||||||||||||
| 52 | ### for my $pair (split ',', $key) { | ||||||||||||||||||||
| 53 | ### $array = $cache->{$pair} ||= []; | ||||||||||||||||||||
| 54 | ### $cache = $array->[0] ||= {}; | ||||||||||||||||||||
| 55 | ### } | ||||||||||||||||||||
| 56 | ### if (@_ > 2) { | ||||||||||||||||||||
| 57 | ### $array->[1] = $val; | ||||||||||||||||||||
| 58 | ### } | ||||||||||||||||||||
| 59 | ### return $cache; | ||||||||||||||||||||
| 60 | ### } | ||||||||||||||||||||
| 61 | ### | ||||||||||||||||||||
| 62 | ### sub make_key { | ||||||||||||||||||||
| 63 | ### my ($self, @idxs) = @_; | ||||||||||||||||||||
| 64 | ### | ||||||||||||||||||||
| 65 | ### my %seen; | ||||||||||||||||||||
| 66 | ### my @pairs; | ||||||||||||||||||||
| 67 | ### my $idx = 0; | ||||||||||||||||||||
| 68 | ### while ($idx < @idxs) { | ||||||||||||||||||||
| 69 | ### if (exists $seen{$idxs[$idx]} or exists $seen{$idxs[$idx + 1]}) { | ||||||||||||||||||||
| 70 | ### croak("Duplicate index"); | ||||||||||||||||||||
| 71 | ### } | ||||||||||||||||||||
| 72 | ### $seen{$idxs[$idx]} = $seen{$idxs[$idx + 1]} = 1; | ||||||||||||||||||||
| 73 | ### push @pairs, join '-', sort $idxs[$idx], $idxs[$idx + 1]; | ||||||||||||||||||||
| 74 | ### $idx += 2; | ||||||||||||||||||||
| 75 | ### } | ||||||||||||||||||||
| 76 | ### my $key = join ',', sort @pairs; | ||||||||||||||||||||
| 77 | ### # $key = ' ' x (25 - length $key) . $key; | ||||||||||||||||||||
| 78 | ### return $key; | ||||||||||||||||||||
| 79 | ### } | ||||||||||||||||||||
| 80 | ### | ||||||||||||||||||||
| 81 | #### you might want to adjust this for your items... | ||||||||||||||||||||
| 82 | ### sub print_items { | ||||||||||||||||||||
| 83 | ### my ($self, @idxs) = @_; | ||||||||||||||||||||
| 84 | ### | ||||||||||||||||||||
| 85 | ### return join ', ', map { $self->{items}[$_]->id } @idxs; | ||||||||||||||||||||
| 86 | ### } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 9 | ▶ | sub add { | ||||||||||||||||||
|
Subroutine
add: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 9 | push @{shift->items}, @_; | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 9 | ▶ | sub items { | ||||||||||||||||||
|
Subroutine
items: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||
| 95 | 9 | my ($self) = @_; | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | return wantarray | ||||||||||||||||||||
| 98 | 0 | ? @{$self->{items}} | |||||||||||||||||||
| 99 | 9 | ▶ | : $self->{items}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 2 | ▶ | sub scores { | ||||||||||||||||||
|
Subroutine
scores: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||
| 103 | 2 | my ($self, $new) = @_; | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 2 | ▶ | $self->{scores} = $new if (@_ > 1); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | return wantarray | ||||||||||||||||||||
| 107 | 0 | ? @{$self->{scores}} | |||||||||||||||||||
| 108 | 2 | ▶ | : $self->{scores}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 325809 | ▶ | sub get_score { | ||||||||||||||||||
|
Subroutine
get_score: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 112 | 325809 | my ($self, $idx0, $idx1) = @_; | |||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 325809 | my $cache_key = join ',', sort $idx0, $idx1; # swapped order required to be the same | |||||||||||||||||||
| 115 | 325809 | ▶ | if (not exists $self->{score_cache}{$cache_key}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | 107 | my $items = $self->{items}; | |||||||||||||||||||
| 117 | 107 | my $score = &{$self->{scoreSub}}($items->[$idx0], $items->[$idx1]); | |||||||||||||||||||
| 118 | 107 | ▶ | croak "Negative score: $score" if ($score < 0); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | 107 | $self->{score_cache}{$cache_key} = $score; | |||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | ### $self->dbg_hash($self->make_key($idx0, $idx1), $score); | ||||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | 325809 | return $self->{score_cache}{$cache_key}; | |||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 2 | ▶ | sub pick { | ||||||||||||||||||
|
Subroutine
pick: called CC=11 SCAR=24.3
Pod
documented
| |||||||||||||||||||||
| 127 | 2 | my ($self, $window) = @_; | |||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 2 | ▶ | $window ||= $self->{window}; # size of sliding window | ||||||||||||||||||
|
Condition33%1/3
$window ||= $self->{"window"}
MC/DC0%0/2
$window ||= $self->{"window"}
$window
$self->{"window"}
Truth table33%1/3
A ||= B
A= $windowB= $self->{"window"}
| |||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 2 | my %paired; # for marking off pairs | |||||||||||||||||||
| 132 | my @results; | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 2 | my $items = $self->{items}; | |||||||||||||||||||
| 135 | 2 | ▶ | if (scalar(@{$items}) <= 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | 0 | croak "No items"; | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | 2 | ▶ | if (scalar(@{$items}) & 1) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 139 | 0 | croak sprintf "Odd number of items (%d)", scalar @{$items}; | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | 2 | my $progress = $self->{progress}; | |||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 2 | my @scores; | |||||||||||||||||||
| 144 | 2 | $self->scores(\@scores); | |||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | # Sliding window: | ||||||||||||||||||||
| 147 | 2 | while (1) { | |||||||||||||||||||
| 148 | # create new list containing only a windows-worth of items | ||||||||||||||||||||
| 149 | 4 | my @w_idxs; # items for this window | |||||||||||||||||||
| 150 | 4 | for my $idx (0 .. $#{$items}) { | |||||||||||||||||||
| 151 | 32 | ▶ | if (not exists $paired{$idx}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 152 | 26 | push @w_idxs, $idx; | |||||||||||||||||||
| 153 | 26 | ▶ | last if (@w_idxs >= $window * 2) # window filled | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 154 | } | ||||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | 4 | my $score = 0; # need an initial score, might as well count | |||||||||||||||||||
| 157 | # initial items as passed to us | ||||||||||||||||||||
| 158 | 4 | ▶ | for (my $idx = 0; $idx < @w_idxs; $idx += 2) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 159 | 13 | $score += $self->get_score($w_idxs[$idx], $w_idxs[$idx + 1]); | |||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | # pair this window | ||||||||||||||||||||
| 162 | 4 | ($score, @w_idxs) = $self->_r_best(0, $score, @w_idxs); | |||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | ### my $combs = 1; | ||||||||||||||||||||
| 165 | ### map { $combs *= (2 * $_ - 1) } (1 .. @w_idxs / 2); | ||||||||||||||||||||
| 166 | ### print scalar keys %all, ' combinations'; | ||||||||||||||||||||
| 167 | ### print " (should be $combs)" if ($combs != scalar keys %all); | ||||||||||||||||||||
| 168 | ### print "\n"; | ||||||||||||||||||||
| 169 | ### for my $key (keys %all) { | ||||||||||||||||||||
| 170 | ### my $score = 0; | ||||||||||||||||||||
| 171 | ### for my $pair (split ',', $key) { | ||||||||||||||||||||
| 172 | ### $score += $self->get_score(split('-', $pair)); | ||||||||||||||||||||
| 173 | ### } | ||||||||||||||||||||
| 174 | ### $all{$key} = $score; | ||||||||||||||||||||
| 175 | ### } | ||||||||||||||||||||
| 176 | ### for my $key (sort { $all{$b} <=> $all{$a} } keys %all) { | ||||||||||||||||||||
| 177 | ### print "$key = $all{$key}\n"; | ||||||||||||||||||||
| 178 | ### } | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 4 | ▶ | if (scalar keys %paired < (scalar(@{$items}) - (2 * $window))) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 181 | # keep top pair | ||||||||||||||||||||
| 182 | 2 | $paired{$w_idxs[0]} = 1; | |||||||||||||||||||
| 183 | 2 | $paired{$w_idxs[1]} = 1; | |||||||||||||||||||
| 184 | 2 | push @results, $items->[$w_idxs[0]], $items->[$w_idxs[1]]; | |||||||||||||||||||
| 185 | 2 | &$progress($items->[$w_idxs[0]], $items->[$w_idxs[1]], # the paired items | |||||||||||||||||||
| 186 | $w_idxs[0], $w_idxs[1]); # item indices | ||||||||||||||||||||
| 187 | 2 | push @scores, $self->get_score(@w_idxs[0..1]); | |||||||||||||||||||
| 188 | } | ||||||||||||||||||||
| 189 | else { | ||||||||||||||||||||
| 190 | # keep all the results, we are done | ||||||||||||||||||||
| 191 | 2 | push @results, map {$items->[$_]} @w_idxs; | |||||||||||||||||||
| 192 | 2 | ▶ | for (my $idx = 0; $idx < @w_idxs; $idx += 2) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 193 | 9 | &$progress($items->[$w_idxs[$idx]], $items->[$w_idxs[$idx + 1]], # the paired items | |||||||||||||||||||
| 194 | $w_idxs[$idx], $w_idxs[$idx + 1]); # item indices | ||||||||||||||||||||
| 195 | 9 | push @scores, $self->get_score(@w_idxs[$idx..$idx+1]); | |||||||||||||||||||
| 196 | } | ||||||||||||||||||||
| 197 | 2 | last; | |||||||||||||||||||
| 198 | } | ||||||||||||||||||||
| 199 | } | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | 2 | ▶ | return wantarray ? @results : \@results; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 202 | } | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | # find best pairing of @idxs. try the first item in @idxs against every | ||||||||||||||||||||
| 205 | # other item in the array. after picking the first and the current second | ||||||||||||||||||||
| 206 | # item, recursively find the best arrangement of all the remaining items. | ||||||||||||||||||||
| 207 | # the return values are the score followed by the new arrangment. | ||||||||||||||||||||
| 208 | 190645 | ▶ | sub _r_best { | ||||||||||||||||||
|
Subroutine
_r_best: called CC=5 SCAR=16.1
| |||||||||||||||||||||
| 209 | 190645 | my ($self, $depth, $best_score, @idxs) = @_; | |||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | 190645 | ▶ | if (@idxs <= 2) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 212 | 135144 | ▶ | croak sprintf("%d items left", scalar @idxs) if (@idxs <= 1); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 213 | 135144 | return ($self->get_score(@idxs), @idxs); | |||||||||||||||||||
| 214 | } | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | 55501 | my @best_trial = @idxs; # copy in case there is no improvement | |||||||||||||||||||
| 217 | 55501 | my ($trial_0, $trial_1, @tail) = @idxs; # working copy | |||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | ### push @head, $trial_0; | ||||||||||||||||||||
| 220 | 55501 | for my $idx (0 .. @idxs - 2) { | |||||||||||||||||||
| 221 | ### push @head, $trial_1; | ||||||||||||||||||||
| 222 | ### $all{$self->make_key(@head, @tail)} = 0 if (@tail == 2); # collect every combination | ||||||||||||||||||||
| 223 | # recursively get best pairing for tail | ||||||||||||||||||||
| 224 | 190641 | my ($trial_score, @trial_tail) = $self->_r_best($depth + 1, $best_score, @tail); | |||||||||||||||||||
| 225 | # add score for top pair | ||||||||||||||||||||
| 226 | 190641 | $trial_score += $self->get_score($trial_0, $trial_1); # first pair | |||||||||||||||||||
| 227 | ### print join(', ', $trial_0, $trial_1, @trial_tail, $self->make_key($trial_0, $trial_1, @trial_tail)), "\n" if ($depth == 0); | ||||||||||||||||||||
| 228 | ### $self->dbg_hash($self->make_key($trial_0, $trial_1, @trial_tail), $trial_score); | ||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | 190641 | ▶ | if ($trial_score < $best_score) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 231 | # aha! a potential candidate. save it | ||||||||||||||||||||
| 232 | 11321 | $best_score = $trial_score; | |||||||||||||||||||
| 233 | 11321 | @best_trial = ($trial_0, $trial_1, @trial_tail); | |||||||||||||||||||
| 234 | ## printf "%2d %2d Best %8.5f idxs %s\n", | ||||||||||||||||||||
| 235 | ## $depth, | ||||||||||||||||||||
| 236 | ## $idx, | ||||||||||||||||||||
| 237 | ## $best_score, | ||||||||||||||||||||
| 238 | ## $self->print_items(@best_trial) if ($depth < 2); | ||||||||||||||||||||
| 239 | } | ||||||||||||||||||||
| 240 | else { | ||||||||||||||||||||
| 241 | ## printf "%2d %2d Not best %8.5f idxs %s\n", | ||||||||||||||||||||
| 242 | ## $depth, | ||||||||||||||||||||
| 243 | ## $idx, | ||||||||||||||||||||
| 244 | ## $trial_score, | ||||||||||||||||||||
| 245 | ## $self->print_items($trial_0, $trial_1, @trial_tail) if ($depth < 2); | ||||||||||||||||||||
| 246 | } | ||||||||||||||||||||
| 247 | # hold $trial_0 in slot 0, rotate all items below it | ||||||||||||||||||||
| 248 | 190641 | push @tail, $trial_1; # add second item to end of tail | |||||||||||||||||||
| 249 | 190641 | $trial_1 = shift @tail; # move third item into second slot | |||||||||||||||||||
| 250 | ### pop @head; | ||||||||||||||||||||
| 251 | } | ||||||||||||||||||||
| 252 | ### pop @head; | ||||||||||||||||||||
| 253 | ### my $key = $self->make_key(@best_trial); | ||||||||||||||||||||
| 254 | ### print "best: $key = $best_score\n" if ($depth == 0); | ||||||||||||||||||||
| 255 | 55501 | return ($best_score, @best_trial); | |||||||||||||||||||
| 256 | } | ||||||||||||||||||||
| 257 | |||||||||||||||||||||
| 258 | 1 | 1; | |||||||||||||||||||
| 259 | |||||||||||||||||||||
| 260 | __END__ | ||||||||||||||||||||