| 1 | package PDL::Opt::Simplex; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use PDL; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use PDL::Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | our @ISA = qw/PDL::Exporter/; | |||||||||||||||||||||||||||
| 8 | 1 | our @EXPORT_OK = qw/simplex make_simplex/; | |||||||||||||||||||||||||||
| 9 | 1 | our %EXPORT_TAGS = ( Func => [@EXPORT_OK] ); | |||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '2.098'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | *simplex = \&PDL::simplex; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 7 | ▶ | sub make_simplex { | ||||||||||||||||||||||||||
|
Subroutine
make_simplex: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | 7 | my ($init, $initsize) = @_; | |||||||||||||||||||||||||||
| 16 | 7 | my ( $nd, $nd2, @otherdims ) = ( $init->dims, 1 ); | |||||||||||||||||||||||||||
| 17 | 7 | ▶ | pop @otherdims if @otherdims; # drop the spurious "1" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 7 | ▶ | return unless $nd2 == $nd + 1 or $nd2 == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$nd2 == $nd + 1 or $nd2 == 1
MC/DC0%0/2
$nd2 == $nd + 1 or $nd2 == 1
$nd2 == $nd + 1
$nd2 == 1
Truth table66%2/3
A or B
A= $nd2 == $nd + 1B= $nd2 == 1
| |||||||||||||||||||||||||||||
| 19 | 7 | ▶ | return $init if $nd2 == $nd + 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 5 | my $simp = PDL->zeroes( $nd, $nd + 1, @otherdims ); | |||||||||||||||||||||||||||
| 21 | # Constructing a tetrahedron: | ||||||||||||||||||||||||||||
| 22 | # At step n (starting from zero) | ||||||||||||||||||||||||||||
| 23 | # take vertices 0..n and move them 1/(n+1) to negative dir on axis n. | ||||||||||||||||||||||||||||
| 24 | # Take vertex n+1 and move it n/(n+1) to positive dir on axis n | ||||||||||||||||||||||||||||
| 25 | 5 | my $uppertri = $simp->copy; | |||||||||||||||||||||||||||
| 26 | 5 | ones($nd * ($nd+1) / 2)->tritosquare($uppertri->slice(",:-2")->t); | |||||||||||||||||||||||||||
| 27 | 5 | my $iseq = sequence($nd); | |||||||||||||||||||||||||||
| 28 | 5 | my $pjseq = $iseq / ( $iseq + 1 ); | |||||||||||||||||||||||||||
| 29 | 5 | $simp .= $init - $initsize * $pjseq * $uppertri; | |||||||||||||||||||||||||||
| 30 | 5 | $simp->slice(",1:")->diagonal(0,1) += $initsize * (1-$pjseq); | |||||||||||||||||||||||||||
| 31 | 5 | $simp; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 6 | ▶ | sub PDL::simplex { | ||||||||||||||||||||||||||
|
Subroutine
simplex: called CC=12 SCAR=25.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | 6 | my ( $init, $initsize, $minsize, $maxiter, $sub, $logsub, $t ) = @_; | |||||||||||||||||||||||||||
| 36 | 6 | ▶ | my $simp = make_simplex($init, $initsize) // return; | ||||||||||||||||||||||||||
|
Condition50%1/2
make_simplex($init, $initsize) // (return)
MC/DC0%0/1
make_simplex($init, $initsize) // (return)
make_simplex($init, $initsize)
Truth table50%1/2
A
A= make_simplex($init, $initsize)
| |||||||||||||||||||||||||||||
| 37 | 6 | my $nd = $simp->dim(0); | |||||||||||||||||||||||||||
| 38 | 6 | my $vals = $sub->($simp); | |||||||||||||||||||||||||||
| 39 | 6 | my $ssize; | |||||||||||||||||||||||||||
| 40 | 6 | ▶ | while ($maxiter--) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 354 | $ssize = ( $simp - $simp->slice(":,0") )->magnover->maxover; | |||||||||||||||||||||||||||
| 42 | 354 | ▶ | $logsub->( $simp, $vals, $ssize ) if $logsub; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 354 | ▶ | last unless $ssize > $minsize; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 348 | ▶ | my $valsn = !$t ? $vals : $vals - $t * log( $vals->random + 0.00001 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 348 | my $minind = $valsn->minimum_ind; | |||||||||||||||||||||||||||
| 46 | 348 | my $maxind = $valsn->maxover_n_ind(2); | |||||||||||||||||||||||||||
| 47 | 348 | my $maxind0 = $maxind->at(0); | |||||||||||||||||||||||||||
| 48 | 348 | my @worstvals = $valsn->index($maxind)->list; | |||||||||||||||||||||||||||
| 49 | 348 | my $bestval = $valsn->at($minind); | |||||||||||||||||||||||||||
| 50 | 348 | my $ssum = ($simp->t->sumover - (my $worst = $simp->slice(":,($maxind0)"))) / $nd; | |||||||||||||||||||||||||||
| 51 | 348 | my $new = 2 * $ssum - $worst; | |||||||||||||||||||||||||||
| 52 | 348 | my $val = $sub->($new)->at(0); | |||||||||||||||||||||||||||
| 53 | 348 | ▶ | $val += $t * log( rand() + 0.00001 ) if $t; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 348 | my $removetop = 0; | |||||||||||||||||||||||||||
| 55 | 348 | ▶ | if ( $val < $bestval ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 56 | 115 | my $newnew = $new + $ssum - $worst; | |||||||||||||||||||||||||||
| 57 | 115 | my $val2 = $sub->($newnew); | |||||||||||||||||||||||||||
| 58 | 115 | ▶ | if ( $val2->at(0) < $val ) { # CASE1 Reflection and Expansion | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 80 | $new = $newnew; | |||||||||||||||||||||||||||
| 60 | 80 | $val = $val2; | |||||||||||||||||||||||||||
| 61 | } # else CASE2 Reflection | ||||||||||||||||||||||||||||
| 62 | 115 | $removetop = 1; | |||||||||||||||||||||||||||
| 63 | } elsif ( $val < $worstvals[1] ) { # CASE3 Reflection | ||||||||||||||||||||||||||||
| 64 | 39 | $removetop = 1; | |||||||||||||||||||||||||||
| 65 | } else { | ||||||||||||||||||||||||||||
| 66 | 194 | my $newnew = 0.5 * ($ssum + $worst); | |||||||||||||||||||||||||||
| 67 | 194 | my $val2 = $sub->($newnew); | |||||||||||||||||||||||||||
| 68 | 194 | ▶ | if ( $val2->at(0) < $worstvals[0] ) { # CASE4 Contraction | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 194 | $new = $newnew; | |||||||||||||||||||||||||||
| 70 | 194 | $val = $val2; | |||||||||||||||||||||||||||
| 71 | 194 | $removetop = 1; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 348 | ▶ | if ($removetop) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 348 | $worst .= $new; | |||||||||||||||||||||||||||
| 76 | 348 | $vals->slice( "($maxind0)" ) .= $val; | |||||||||||||||||||||||||||
| 77 | } else { # CASE5 Multiple Contraction | ||||||||||||||||||||||||||||
| 78 | 0 | $simp .= 0.5 * ($simp->slice(":,$minind") + $simp); | |||||||||||||||||||||||||||
| 79 | 0 | my $idx = which( sequence($nd+1) != $minind ); | |||||||||||||||||||||||||||
| 80 | 0 | $vals->index($idx) .= $sub->($simp->dice_axis(1,$idx)); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 6 | my $mmind = $vals->minimum_ind; | |||||||||||||||||||||||||||
| 84 | 6 | return ( $simp->slice(":,$mmind"), $ssize, $vals->index($mmind) ); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1 | 1; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | __END__ | ||||||||||||||||||||||||||||