1Simplex(3) User Contributed Perl Documentation Simplex(3)
2
3
4
6 PDL::Opt::Simplex -- Simplex optimization routines
7
9 use PDL::Opt::Simplex;
10
11 ($optimum,$ssize,$optval) = simplex($init,$initsize,$minsize,
12 $maxiter,
13 sub {evaluate_func_at($_[0])},
14 sub {display_simplex($_[0])}
15 );
16
17 # more involved:
18 use PDL;
19 use PDL::Opt::Simplex;
20
21 my $count = 0;
22 # find value of $x that returns a minimum
23 sub f {
24 my ($vec) = @_;
25 $count++;
26 my $x = $vec->slice('(0)');
27 # The parabola (x+3)^2 - 5 has a minima at x=-3:
28 return (($x+3)**2 - 5);
29 }
30
31 sub log {
32 my ($vec, $vals, $ssize) = @_;
33 # $vec is the array of values being optimized
34 # $vals is f($vec)
35 # $ssize is the simplex size, or roughly, how close to being converged.
36 my $x = $vec->slice('(0)');
37 # each vector element passed to log() has a min and max value.
38 # ie: x=[6 0] -> vals=[76 4]
39 # so, from above: f(6) == 76 and f(0) == 4
40 print "$count [$ssize]: $x -> $vals\n";
41 }
42
43 my $vec_initial = pdl [30];
44 my ( $vec_optimal, $ssize, $optval ) = simplex($vec_initial, 3, 1e-6, 100, \&f, \&log);
45 my $x = $vec_optimal->slice('(0)');
46 print "ssize=$ssize opt=$x -> minima=$optval\n";
47
49 This package implements the commonly used simplex optimization
50 algorithm. The basic idea of the algorithm is to move a "simplex" of
51 N+1 points in the N-dimensional search space according to certain
52 rules. The main benefit of the algorithm is that you do not need to
53 calculate the derivatives of your function.
54
55 $init is a 1D vector holding the initial values of the N fitted
56 parameters, $optimum is a vector holding the final solution. $optval
57 is the evaluation of the final solution.
58
59 $initsize is the size of $init (more...)
60
61 $minsize is some sort of convergence criterion (more...) - e.g.
62 $minsize = 1e-6
63
64 The sub is assumed to understand more than 1 dimensions and
65 broadcasting. Its signature is 'inp(nparams); [ret]out()'. An example
66 would be
67
68 sub evaluate_func_at {
69 my($xv) = @_;
70 my $x1 = $xv->slice("(0)");
71 my $x2 = $xv->slice("(1)");
72 return $x1**4 + ($x2-5)**4 + $x1*$x2;
73 }
74
75 Here $xv is a vector holding the current values of the parameters being
76 fitted which are then sliced out explicitly as $x1 and $x2.
77
78 $ssize gives a very very approximate estimate of how close we might be
79 - it might be miles wrong. It is the euclidean distance between the
80 best and the worst vertices. If it is not very small, the algorithm has
81 not converged.
82
84 simplex
85 Simplex optimization routine
86
87 ($optimum,$ssize,$optval) = simplex($init,$initsize,$minsize,
88 $maxiter,
89 sub {evaluate_func_at($_[0])},
90 sub {display_simplex($_[0])}
91 );
92
93 See module "PDL::Opt::Simplex" for more information.
94
96 Do not use the simplex method if your function has local minima. It
97 will not work. Use genetic algorithms or simulated annealing or
98 conjugate gradient or momentum gradient descent.
99
100 They will not really work either but they are not guaranteed not to
101 work ;) (if you have infinite time, simulated annealing is guaranteed
102 to work but only after it has visited every point in your space).
103
105 Ron Shaffer's chemometrics web page and references therein:
106 "http://chem1.nrl.navy.mil/~shaffer/chemoweb.html".
107
108 Numerical Recipes (bla bla bla XXX ref).
109
110 The demonstration (Examples/Simplex/tsimp.pl and tsimp2.pl).
111
113 Copyright(C) 1997 Tuomas J. Lukka. All rights reserved. There is no
114 warranty. You are allowed to redistribute this software / documentation
115 under certain conditions. For details, see the file COPYING in the PDL
116 distribution. If this file is separated from the PDL distribution, the
117 copyright notice should be included in the file.
118
119
120
121perl v5.36.0 2023-01-20 Simplex(3)