Algorithm-Networksort-Chooser
view release on metacpan or search on metacpan
bin/algorithm-networksort-chooser view on Meta::CPAN
#### Output results
if ($opt->{raw}) {
print "[";
print join(',', map { "[$_->[0],$_->[1]]" } @{ $sorted_candidates[0]->{network} });
print "]\n";
exit;
}
print "Network size: $network_size\n";
if ($opt->{median}) {
print "Network type: Median network\n";
} elsif ($opt->{selection}) {
print "Network type: Selection network: $opt->{selection}\n";
} else {
print "Network type: Sorting network\n";
}
print "\n";
print "Optimisation criteria: $opt->{opt}\n";
print "\n";
print "Optimal network:\n";
output_network($sorted_candidates[0]);
if ($opt->{all}) {
print "\nAdditional candidate networks:\n";
foreach my $network (@sorted_candidates[1..$#sorted_candidates]) {
output_network($network);
}
}
sub output_network {
my $network = shift;
print " Algorithm \"$network->{algo}\":\n";
print " Comparators: $network->{comparators}\n";
print " Stages: $network->{stages}\n";
print " Avg swaps: $network->{swaps}\n" if exists $network->{swaps};
if ($opt->{show}) {
print "\n";
print Algorithm::Networksort::nw_graph($network->{network}, $network_size, graph => 'text');
}
}
__END__
=encoding utf-8
=head1 NAME
algorithm-networksort-chooser - Helper utility for Algorithm::Networksort
=head1 SYNOPSIS
The C<algorithm-networksort-chooser> script helps you find the best sorting network for your particular use-case.
$ algorithm-networksort-chooser 9 ## find best sorting network for array size 9
$ algorithm-networksort-chooser 9 --all ## show all candiate networks
$ algorithm-networksort-chooser 9 --algorithms=batcher,bitonic ## only consider batcher and bitonic algos
$ algorithm-networksort-chooser 9 --opt=comparators ## optimise for comparators (default)
$ algorithm-networksort-chooser 9 --opt=stages ## optimise for stages
$ algorithm-networksort-chooser 9 --opt=swaps ## optimise for average swaps
$ algorithm-networksort-chooser 9 --median ## best median network
$ algorithm-networksort-chooser 9 --selection=4 ## also best median network
$ algorithm-networksort-chooser 9 --selection=0,1,2 ## top-3 elements selection net
$ algorithm-networksort-chooser 9 --validate ## run 0-1 validation test
$ algorithm-networksort-chooser 9 --show ## show network as ASCII diagram
$ algorithm-networksort-chooser 9 --raw ## show network as raw comparators
=head1 DESCRIPTION
This module uses L<Algorithm::Networksort> to experiment with sorting networks.
L<Introduction To Sorting Networks|http://hoytech.github.io/sorting-networks/>
By default this script examines the output of all implemented algorithms and the currently best known special-cases, and chooses the one that best meets your specified criteria.
This module allows you to trim sorting networks into median or selection networks.
You can then choose the optimal net based on comparators (total number of operations) or on stages (number of operations considering parallelism).
Normally the output is something like this:
$ algorithm-networksort-chooser --median 22
Network size: 22
Network type: Median network
Optimisation criteria: stages
Optimal network:
Algorithm "best":
Comparators: 86
Stages: 12
For the description of the various algorithms and best-known special cases, see L<Algorithm::Networksort>'s documentation and source code.
In order to use this output in another program, there is a C<--raw> switch. Its output is C<eval>able perl and is valid JSON:
$ algorithm-networksort-chooser --median 7 --raw
[[0,4],[1,5],[2,6],[0,2],[1,3],[4,6],[2,4],[3,5],[0,1],[2,3],[4,5],[1,4],[3,6],[3,4]]
L<Algorithm::Networksort>'s ASCII output can be seen with C<--show>:
( run in 1.675 second using v1.01-cache-2.11-cpan-e1769b4cff6 )