.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Simplex 3pm" .TH Simplex 3pm 2024-04-25 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME PDL::Opt::Simplex \-\- Simplex optimization routines .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use PDL::Opt::Simplex; \& \& ($optimum,$ssize,$optval) = simplex($init,$initsize,$minsize, \& $maxiter, \& sub {evaluate_func_at($_[0])}, \& sub {display_simplex($_[0])} \& ); \& \& # more involved: \& use PDL; \& use PDL::Opt::Simplex; \& \& my $count = 0; \& # find value of $x that returns a minimum \& sub f { \& my ($vec) = @_; \& $count++; \& my $x = $vec\->slice(\*(Aq(0)\*(Aq); \& # The parabola (x+3)^2 \- 5 has a minima at x=\-3: \& return (($x+3)**2 \- 5); \& } \& \& sub log { \& my ($vec, $vals, $ssize) = @_; \& # $vec is the array of values being optimized \& # $vals is f($vec) \& # $ssize is the simplex size, or roughly, how close to being converged. \& my $x = $vec\->slice(\*(Aq(0)\*(Aq); \& # each vector element passed to log() has a min and max value. \& # ie: x=[6 0] \-> vals=[76 4] \& # so, from above: f(6) == 76 and f(0) == 4 \& print "$count [$ssize]: $x \-> $vals\en"; \& } \& \& my $vec_initial = pdl [30]; \& my ( $vec_optimal, $ssize, $optval ) = simplex($vec_initial, 3, 1e\-6, 100, \e&f, \e&log); \& my $x = $vec_optimal\->slice(\*(Aq(0)\*(Aq); \& print "ssize=$ssize opt=$x \-> minima=$optval\en"; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" This package implements the commonly used simplex optimization algorithm. The basic idea of the algorithm is to move a "simplex" of N+1 points in the N\-dimensional search space according to certain rules. The main benefit of the algorithm is that you do not need to calculate the derivatives of your function. .PP \&\f(CW$init\fR is a 1D vector holding the initial values of the N fitted parameters, \f(CW$optimum\fR is a vector holding the final solution. \&\f(CW$optval\fR is the evaluation of the final solution. .PP \&\f(CW$initsize\fR is the size of \f(CW$init\fR (more...) .PP \&\f(CW$minsize\fR is some sort of convergence criterion (more...) \&\- e.g. \f(CW$minsize\fR = 1e\-6 .PP The sub is assumed to understand more than 1 dimensions and broadcasting. Its signature is 'inp(nparams); [ret]\fBout()\fR'. An example would be .PP .Vb 6 \& sub evaluate_func_at { \& my($xv) = @_; \& my $x1 = $xv\->slice("(0)"); \& my $x2 = $xv\->slice("(1)"); \& return $x1**4 + ($x2\-5)**4 + $x1*$x2; \& } .Ve .PP Here \f(CW$xv\fR is a vector holding the current values of the parameters being fitted which are then sliced out explicitly as \f(CW$x1\fR and \f(CW$x2\fR. .PP \&\f(CW$ssize\fR gives a very very approximate estimate of how close we might be \- it might be miles wrong. It is the euclidean distance between the best and the worst vertices. If it is not very small, the algorithm has not converged. .SH FUNCTIONS .IX Header "FUNCTIONS" .SS simplex .IX Subsection "simplex" Simplex optimization routine .PP .Vb 5 \& ($optimum,$ssize,$optval) = simplex($init,$initsize,$minsize, \& $maxiter, \& sub {evaluate_func_at($_[0])}, \& sub {display_simplex($_[0])} \& ); .Ve .PP See module \f(CW\*(C`PDL::Opt::Simplex\*(C'\fR for more information. .SH CAVEATS .IX Header "CAVEATS" Do not use the simplex method if your function has local minima. It will not work. Use genetic algorithms or simulated annealing or conjugate gradient or momentum gradient descent. .PP They will not really work either but they are not guaranteed not to work ;) (if you have infinite time, simulated annealing is guaranteed to work but only after it has visited every point in your space). .SH "SEE ALSO" .IX Header "SEE ALSO" .IP "PDL::Opt::Simplex::Simple \- Use names for Simplex-optimized values" 4 .IX Item "PDL::Opt::Simplex::Simple - Use names for Simplex-optimized values" .PD 0 .IP "PDL::Opt::ParticleSwarm \- A PDL implementation of Particle Swarm" 4 .IX Item "PDL::Opt::ParticleSwarm - A PDL implementation of Particle Swarm" .IP "PDL::Opt::ParticleSwarm::Simple \- Use names for Particle Swarm-optimized values" 4 .IX Item "PDL::Opt::ParticleSwarm::Simple - Use names for Particle Swarm-optimized values" .IP " \- Ron Shaffer's chemometrics web page and references therein (archive from 1998)" 4 .IX Item " - Ron Shaffer's chemometrics web page and references therein (archive from 1998)" .PD .PP The demonstration (Examples/Simplex/tsimp.pl and tsimp2.pl). .SH AUTHOR .IX Header "AUTHOR" Copyright(C) 1997 Tuomas J. Lukka. All rights reserved. There is no warranty. You are allowed to redistribute this software / documentation under certain conditions. For details, see the file COPYING in the PDL distribution. If this file is separated from the PDL distribution, the copyright notice should be included in the file.