Demos/Simplex.pm untested

filter: Statementstmt 0.0% 0 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 14 CC 1 SCAR 6.9
CC
1
Cov
0%
init
2.0
done
2.0
info
2.0
CRAP 2.0
1 package PDL::Demos::Simplex;
2  
3 sub init {'
4 use PDL::Opt::Simplex;
5 '}
6 sub done {'
7 undef $w;
8 '}
9  
10 sub info {('simplex','Simplex optimisation (Req.: PDL::Graphics::Simple)')}
11  
12 my @demos = (
13 [comment => q|
14 This demo illustrates the PDL::Opt::Simplex module.
15 You must have PDL::Graphics::Simple installed to run it.
16  
17 The simplex algorithm finds the optimum "point" (coordinates) in a space
18 you define, which can have any number (called "n" here) of dimensions.
19  
20 The algorithm takes either a fully-formed cloud of n+1 points, or a
21 single starting point, in which case it constructs the cloud for you
22 using the "initsize" parameter. It also takes a function that will take a
23 series of points in your space, and returns the "value" at each of those
24 points. From that, it works out which point of the simplex to move to be
25 closer to the optimum point, which has the lowest value of your function.
26  
27 It also takes other, less important parameters, which you'll see,
28 including a "logging" function which you can use to report progress,
29 or plot data.
30 |],
31  
32 [act => q|
33 # Load the necessary modules, set up a plotting window.
34 use PDL::Graphics::Simple;
35 use PDL::Opt::Simplex;
36 $w = pgswin();
37  
38 # Try a simple ellipsoid; the multiplier makes the algorithm prioritise
39 # the first (X) dimension, as you'll see on the plot.
40 $w->plot(with=>'lines', [0], [1], {xrange=>[-15,5],yrange=>[-15,5]});
41 my $mult = pdl 4,1;
42 sub func { (($mult * $_[0]) ** 2)->sumover }
43 sub logs {
44 $w->oplot(with=>'lines', $_[0]->glue(1,$_[0]->slice(",0"))->using(0,1));
45 }
46 simplex(pdl(-10,-10), 0.5, 0.01, 30,
47 \&func,
48 \&logs
49 );
50 |],
51  
52 [act => q|
53 # Now the first of two examples contributed by Alison Offer.
54 # These values are for both.
55  
56 $minsize = 1.e-6; # convergence: if simplex points are this far apart, stop
57 $maxiter = 100; # max number of iterations: if done these many, stop
58  
59 # Now we minimise polynomial: (x-3)^2 + 2*(x-3)*(y-2.5) + 3*(y-2.5)^2
60  
61 $w->plot(with=>'lines', [0], [1], {xrange=>[-1,5],yrange=>[-1,4]}); # reset
62 $init = pdl [ 0 , 1 ];
63 $initsize = 2;
64 ($optimum,$ssize,$optval) = simplex($init,$initsize,$minsize,$maxiter,
65 sub {
66 my ($x, $y) = $_[0]->using(0,1);
67 ($x-3)**2 + 2*($x-3)*($y-2.5) + 3*($y-2.5)**2;
68 },
69 \&logs
70 );
71 |],
72  
73 [act => q|
74 # Now to minimise least squares Gaussian fit to data + noise:
75 # 32 *exp (-((x-10)/6)^2) + noise
76 $factor = 3; # noise factor
77 # data : gaussian + noise
78 $j = sequence(20); srandom(5);
79 $data = 32*exp(-(($j-10)/6)**2) + $factor * (random(20) - 0.5);
80 $init = pdl [ 33, 9, 12 ];
81 $initsize = 2;
82  
83 # The plotting will flatten, i.e. ignore, the third dimension in the vectors.
84  
85 $w->plot(with=>'lines', [0], [1], {xrange=>[30,36],yrange=>[7,12]}); # reset
86 ($optimum,$ssize,$optval) = simplex($init,$initsize,$minsize,$maxiter,
87 sub {
88 my ($x, $y, $z) = map $_[0]->slice($_), 0..2;
89 (($data - $x*exp(-(($j-$y)/$z)**2))**2)->sumover;
90 },
91 \&logs
92 );
93 |],
94  
95 [comment => q|
96 That concludes the demo of PDL::Opt::Simplex. There are other optimisation
97 modules for PDL, including PDL::Opt::GLPK, PDL::Opt::NonLinear,
98 PDL::Opt::QP, PDL::Opt::ParticleSwarm.
99 |],
100 );
101  
102 sub demo { @demos }
103  
104 1;