Scroll to navigation

Simplex(3pm) User Contributed Perl Documentation Simplex(3pm)

NAME

PDL::Opt::Simplex -- Simplex optimization routines

SYNOPSIS

  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('(0)');
    # 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('(0)');
    # 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\n";
  }
  my $vec_initial = pdl [30];
  my ( $vec_optimal, $ssize, $optval ) = simplex($vec_initial, 3, 1e-6, 100, \&f, \&log);
  my $x = $vec_optimal->slice('(0)');
  print "ssize=$ssize  opt=$x -> minima=$optval\n";

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.

$init is a 1D vector holding the initial values of the N fitted parameters, $optimum is a vector holding the final solution. $optval is the evaluation of the final solution.

$initsize is the size of $init (more...)

$minsize is some sort of convergence criterion (more...) - e.g. $minsize = 1e-6

The sub is assumed to understand more than 1 dimensions and broadcasting. Its signature is 'inp(nparams); [ret]out()'. An example would be

        sub evaluate_func_at {
                my($xv) = @_;
                my $x1 = $xv->slice("(0)");
                my $x2 = $xv->slice("(1)");
                return $x1**4 + ($x2-5)**4 + $x1*$x2;
        }

Here $xv is a vector holding the current values of the parameters being fitted which are then sliced out explicitly as $x1 and $x2.

$ssize 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.

FUNCTIONS

simplex

Simplex optimization routine

 ($optimum,$ssize,$optval) = simplex($init,$initsize,$minsize,
                 $maxiter,
                 sub {evaluate_func_at($_[0])},
                 sub {display_simplex($_[0])}
                 );

See module "PDL::Opt::Simplex" for more information.

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.

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).

SEE ALSO

Ron Shaffer's chemometrics web page and references therein: "http://chem1.nrl.navy.mil/~shaffer/chemoweb.html".

Numerical Recipes (bla bla bla XXX ref).

The demonstration (Examples/Simplex/tsimp.pl and tsimp2.pl).

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.

2023-06-17 perl v5.36.0