Simplex(3pm)

πŸ’– Simplex β€” the PDL::Opt::Simplex era πŸ’–

okay bestie, we're diving into Simplex(3pm) from the User Contributed Perl Documentation, and ngl this one's a lil gem. it's the simplex optimization routines for PDL, and it's giving "math but make it cute". let's go πŸ¦‹


✨ NAME ✨

PDL::Opt::Simplex β€” Simplex optimization routines πŸ’…


🌸 SYNOPSIS 🌸

lowkey just copy-paste energy, here's the vibe:

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 minimum 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 ($optimum, $ssize, $optval) = simplex(pdl(30), 3, 1e-6, 100, \&f, \&log);
print "ssize=$ssize  opt=$optimum -> minimum=$optval\n";

πŸŽ€ DESCRIPTION πŸŽ€

so this package implements the commonly used simplex optimization algorithm. the basic idea? you take a "simplex" of N+1 points and you move it around your N-dimensional search space according to certain rules. the main benefit β€” and this is the iconic part β€” is that you do not need to calculate the derivatives of your function. periodt. no derivative drama. 🫢

$init is a 1D vector holding the initial values of the N fitted parameters. $optimum is a vector holding the final values. and $optval? that's the evaluation of the final values. πŸ’«

$initsize is the size of $init. it's only used if your supplied $init is a single point in your search space β€” in that case it constructs the simplex ("cloud") of N+1 points that the algorithm uses, being the distance away from your single $init point along each dimension. this is done by the exportable function make_simplex($init, $initsize), e.g.:

pdl> use PDL::Opt::Simplex
pdl> p $t = make_simplex(pdl(0,0,0), pdl(0.12,0.12,0.12))
[
 [    0 -0.06 -0.08]
 [ 0.12 -0.06 -0.08]
 [    0  0.06 -0.08]
 [    0     0  0.04]
]
pdl> use PDL::Graphics::TriD
pdl> spheres3d $t # spheres not points so can easily see

(they're spheres not points so you can easily see them, iykyk 🧚)

$minsize is the convergence criterion, e.g. $minsize = 1e-6; β€” the algorithm will terminate when all the values of $ssize are less than $minsize. πŸ’€ yes it's a whole vibe check.

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, $x2) = $xv->using(0,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's the largest Euclidean distance between the first vertex and any other. if it is not very small, the algorithm has not converged. 😭 do not trust it blindly bestie.


πŸ‘‘ FUNCTIONS πŸ‘‘

✨ simplex ✨

Simplex optimization routine πŸ’…

Mutates its $init input if given as a full simplex (dims n,n+1). (yes, it will literally change your input, be aware πŸ’€)

 ($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. 🚫

ngl... 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). πŸ’€ iconic honestly.


πŸ”₯ SEE ALSO πŸ”₯

  • PDL::Opt::Simplex::Simple β€” Use names for Simplex-optimized values
  • PDL::Opt::ParticleSwarm β€” A PDL implementation of Particle Swarm
  • PDL::Opt::ParticleSwarm::Simple β€” Use names for Particle Swarm-optimized values
  • <https://web.archive.org/web/19981206200518/http://chem1.nrl.navy.mil/~shaffer/chemoweb.html> β€” Ron Shaffer's chemometrics web page and references therein (archive from 1998) πŸ¦‹
  • 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.


okay bestie that's the whole thing, no cap. πŸ’– this page was served up by perl v5.42.3, dated 2026-08-19, and yes it really is Simplex(3pm). slay, go optimize something. πŸ’…βœ¨πŸ«Ά

πŸ“¦ where this page comes from