blib/lib/PDL/Opt/Simplex.pm

filter: Statementstmt 87.5% 84 / 96 Branchbran 70.0% 21 / 30 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 1 totaltotal 82.4% 113 / 137 CC 17 SCAR 27.6
CC
17
Cov
83%
simplex
19.2
BEGIN
1.0
BEGIN
1.0
CRAP 15.7
« blib/lib/PDL/Image2D.pm ↑ Summary blib/lib/PDL/Reduce.pm »
1  
2 =head1 NAME
3  
4 PDL::Opt::Simplex -- Simplex optimization routines
5  
6 =head1 SYNOPSIS
7  
8 use PDL::Opt::Simplex;
9  
10 ($optimum,$ssize,$optval) = simplex($init,$initsize,$minsize,
11 $maxiter,
12 sub {evaluate_func_at($_[0])},
13 sub {display_simplex($_[0])}
14 );
15  
16 =head1 DESCRIPTION
17  
18 This package implements the commonly used simplex optimization
19 algorithm. The basic idea of the algorithm is to move
20 a "simplex" of N+1 points in the N-dimensional search space
21 according to certain rules. The main
22 benefit of the algorithm is that you do not need to calculate
23 the derivatives of your function.
24  
25 $init is a 1D vector holding the initial values of the N fitted
26 parameters, $optimum is a vector holding the final solution.
27 $optval is the evaluation of the final solution.
28  
29 $initsize is the size of $init (more...)
30  
31 $minsize is some sort of convergence criterion (more...)
32 - e.g. $minsize = 1e-6
33  
34 The sub is assumed to understand more than 1 dimensions and threading.
35 Its signature is 'inp(nparams); [ret]out()'. An example would be
36  
37 sub evaluate_func_at {
38 my($xv) = @_;
39 my $x1 = $xv->slice("(0)");
40 my $x2 = $xv->slice("(1)");
41 return $x1**4 + ($x2-5)**4 + $x1*$x2;
42 }
43  
44 Here $xv is a vector holding the current values of the parameters
45 being fitted which are then sliced out explicitly as $x1 and $x2.
46  
47 $ssize gives a very very approximate estimate of how close we might
48 be - it might be miles wrong. It is the euclidean distance between
49 the best and the worst vertices. If it is not very small, the algorithm
50 has not converged.
51  
52 =head1 FUNCTIONS
53  
54 =head2 simplex
55  
56 =for ref
57  
58 Simplex optimization routine
59  
60 =for usage
61  
62 ($optimum,$ssize,$optval) = simplex($init,$initsize,$minsize,
63 $maxiter,
64 sub {evaluate_func_at($_[0])},
65 sub {display_simplex($_[0])}
66 );
67  
68 See module C<PDL::Opt::Simplex> for more information.
69  
70 =head1 CAVEATS
71  
72 Do not use the simplex method if your function has local minima.
73 It will not work. Use genetic algorithms or simulated annealing
74 or conjugate gradient or momentum gradient descent.
75  
76 They will not really work either but they are not guaranteed not to work ;)
77 (if you have infinite time, simulated annealing is guaranteed to work
78 but only after it has visited every point in your space).
79  
80 =head1 SEE ALSO
81  
82 Ron Shaffer's chemometrics web page and references therein:
83 C<http://chem1.nrl.navy.mil/~shaffer/chemoweb.html>.
84  
85 Numerical Recipes (bla bla bla XXX ref).
86  
87 The demonstration (Examples/Simplex/tsimp.pl and tsimp2.pl).
88  
89 =head1 AUTHOR
90  
91 Copyright(C) 1997 Tuomas J. Lukka.
92 All rights reserved. There is no warranty. You are allowed
93 to redistribute this software / documentation under certain
94 conditions. For details, see the file COPYING in the PDL
95 distribution. If this file is separated from the PDL distribution,
96 the copyright notice should be included in the file.
97  
98  
99  
100 =cut
101  
102 package PDL::Opt::Simplex;
103 1 ▶use PDL;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 1 ▶use PDL::Primitive;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
106 1 ▶use PDL::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
107  
108 # use AutoLoader;
109  
110 1 @PDL::Opt::Simplex::ISA = qw/PDL::Exporter/;
111  
112 1 @PDL::Opt::Simplex::EXPORT_OK = qw/simplex/;
113 1 %PDL::Opt::Simplex::EXPORT_TAGS = ( Func => [@PDL::Opt::Simplex::EXPORT_OK] );
114  
115 1 *simplex = \&PDL::simplex;
116  
117 sub PDL::simplex {
118 2 ▶ my ( $init, $initsize, $minsize, $maxiter, $sub, $logsub, $t ) = @_;
Subroutine simplex: called CC=17 SCAR=29.5
Pod undocumented
119 2 ▶ if ( !defined $t ) { $t = 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $t) 2 0 2
120 2 my ( $i, $j );
121 2 my ( $nd, $nd2 ) = ( dims($init), 1 );
122 2 my $simp;
123 2 ▶ if ( $nd2 == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($nd2 == 1) { } 1 1 2
elsif ($nd2 == $nd + 1) { } 1 0 1
124 1 $simp = PDL->zeroes( $nd, $nd + 1 );
125 1 $simp .= $init;
126  
127 # Constructing a tetrahedron:
128 # At step n (starting from zero)
129 # take vertices 0..n and move them 1/(n+1) to negative dir on axis n.
130 # Take vertex n+1 and move it n/(n+1) to positive dir on axis n
131 1 ▶ if ( !ref $initsize ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $initsize) 0 1 1
132 0 $initsize = PDL->pdl($initsize)->dummy( 0, $nd );
133 }
134 1 ▶ for ( $i = 0 ; $i < $nd ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $nd) 2 1 3
135 2 my $pj = $i / ( $i + 1 );
136 2 ( my $stoopid = $simp->slice("$i,0:$i") ) -=
137 $initsize->at($i) * $pj;
138 2 ( my $stoopid1 = $simp->slice( "$i," . ( $i + 1 ) ) ) +=
139 $initsize->at($i) * ( 1 - $pj );
140 }
141 }
142 elsif ( $nd2 == $nd + 1 ) {
143 1 $simp = $init;
144 }
145 else {
146 0 return;
147 }
148 2 my $maxind = PDL->zeroes(2);
149 2 my $minind = PDL->null;
150 2 my $ssum = PDL->null;
151 2 my $worst;
152 my $new;
153 2 my $vals = &{$sub}($simp);
154 2 my $ss1 = ( $simp - $simp->slice(":,0") )**2;
155 2 sumover( $ss1, ( my $ss2 = PDL->null ) );
156 2 my $ssize = PDL::max( sqrt($ss2) );
157 2 ▶ &{$logsub}( $simp, $vals, $ssize )
Branch: 1 branch
BranchTrueFalseTotal
if $logsub 0 2 2
158 if $logsub;
159  
160 2 ▶ while ( $maxiter-- and max( PDL->topdl($ssize) ) > $minsize ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxiter-- and max("PDL"->topdl($ssize)) > $minsize) 101 2 103
Condition66%2/3
$maxiter-- and max("PDL"->topdl($ssize)) > $minsize
!ll&&!rl&&r
02101
MC/DC50%1/2
$maxiter-- and max("PDL"->topdl($ssize)) > $minsize
$maxiter-- max("PDL"->topdl($ssize)) > $minsize
Truth table66%2/3
A and B
A= $maxiter--B= max("PDL"->topdl($ssize)) > $minsize
ABresult
0X0
100
111
161 101 my $valsn = $vals;
162 101 ▶ if ($t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t) 0 101 101
163 0 my $noise = $vals->random();
164 0 $noise->random;
165 0 $valsn = $vals + $t * ( -log( $noise + 0.00001 ) );
166 }
167 101 maximum_n_ind( $valsn, $maxind );
168 101 minimum_ind( $valsn, $minind );
169 101 my @worstvals = map { $valsn->at( $maxind->at($_) ) } 0 .. 1;
170 101 my $bestval = $valsn->at($minind);
171  
172 101 sumover( $simp->xchg( 0, 1 ), $ssum );
173 101 $ssum -= ( $worst = $simp->slice( ":,(" . $maxind->at(0) . ")" ) );
174 101 $ssum /= $nd;
175 101 $new = 2 * $ssum - $worst;
176 101 my $val = ( &{$sub}($new) )->at(0);
177 101 ▶ if ($t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t) 0 101 101
178 0 $val = $val - $t * ( -log( rand() + 0.00001 ) );
179 }
180 101 my $removetop = 0;
181 101 ▶ if ( $val < $bestval ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($val < $bestval) { } 30 71 101
elsif ($val < $worstvals[1]) { } 12 59 71
182 30 my $newnew = $new + $ssum - $worst;
183 30 my $val2 = &{$sub}($newnew);
184 30 ▶ if ( $val2->at(0) < $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val2->at(0) < $val) { } 22 8 30
185 # print "CASE1 Reflection and Expansion\n";
186 22 $worst .= $newnew;
187 22 $val = $val2;
188 }
189 else {
190 # print "CASE2 Reflection, $newnew, $val, $val2\n";
191 8 $worst .= $new;
192 }
193 30 $removetop = 1;
194 }
195 elsif ( $val < $worstvals[1] ) {
196 # print "CASE3 Reflection\n";
197 12 $worst .= $new;
198 12 $removetop = 1;
199 }
200 else {
201 59 my $newnew = 0.5 * $ssum + 0.5 * $worst;
202 59 my $val2 = &{$sub}($newnew);
203 59 ▶ if ( $val2->at(0) < $worstvals[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val2->at(0) < $worstvals[0]) 59 0 59
204 # print "CASE4 Contraction, $newnew, $val, $val2\n";
205 59 $worst .= $newnew;
206 59 $val = $val2;
207 59 $removetop = 1;
208 }
209 }
210 101 ▶ if ($removetop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($removetop) { } 101 0 101
211 101 ( my $stoopid = $vals->slice( "(" . $maxind->at(0) . ")" ) ) .= $val;
212 }
213 else {
214 # print "CASE5 Multiple Contraction\n";
215 0 $simp = 0.5 * $simp->slice(":,$minind") + 0.5 * $simp;
216 0 my $idx = which( sequence($nd+1) != $minind );
217 0 ( my $stoopid = $vals->index($idx) ) .= &{$sub}($simp->dice_axis(1,$idx));
218 }
219 101 my $ss1 = ( $simp - $simp->slice(":,0") )**2;
220 101 sumover( $ss1, ( $ss2 = PDL->null ) );
221 101 $ssize = PDL::max( sqrt($ss2) );
222 101 ▶ &{$logsub}( $simp, $vals, $ssize )
Branch: 1 branch
BranchTrueFalseTotal
if $logsub 0 101 101
223 if $logsub;
224 }
225 2 minimum_ind( $vals, ( my $mmind = PDL->null ) );
226 2 return ( $simp->slice(":,$mmind"), $ssize, $vals->index($mmind) );
227 }
228  
229 1 1;
« blib/lib/PDL/Image2D.pm ↑ Summary blib/lib/PDL/Reduce.pm »