Skip to main content

SimplexAlgorithm

Namespace: MATHLIB  ·  Library: Math Library

Python equivalent

scipy.optimize.fmin in MNE-Python.

#include <math/simplex_algorithm.h>

class UTILSLIB::SimplexAlgorithm

Header-only Nelder–Mead simplex minimiser templated on the scalar type and on the cost / report callables, implementing the Strategy pattern at zero runtime cost (the functors are inlined by the compiler).

Header-only Nelder–Mead simplex minimiser with templated cost and report functors.

src/examples/ex_math/main.cpp
// Minimise the Rosenbrock function from a 3-vertex simplex around (-1, 1); ftol is relative, so the
// minimum value is offset to 1 (a minimum of exactly 0 never satisfies a relative tolerance)
auto rosenbrock = [](const VectorXd& x) {
return 1.0 + 100.0 * std::pow(x(1) - x(0) * x(0), 2) + std::pow(1.0 - x(0), 2);
};
MatrixXd simplex(3, 2);
simplex << -1.0, 1.0, -0.9, 1.0, -1.0, 1.1;
VectorXd values(3);
for (int i = 0; i < 3; ++i) {
values(i) = rosenbrock(simplex.row(i).transpose());
}
int evaluations = 0;
const bool converged = SimplexAlgorithm::simplex_minimize<double>(simplex, values, 1e-12, 0.0, rosenbrock, 5000, evaluations);
Index best = 0;
values.minCoeff(&best); // vertices are not sorted on return

Static Methods​

simplex_minimize(p, y, ftol, stol, func, max_eval, neval, report, report_func)​

mne_simplex_fit.c Refactored: mne_simplex_minimize

Minimization with the simplex algorithm. Modified from Numerical Recipes. Supports an optional absolute spatial tolerance (stol) to detect simplex collapse.

Parameters:

  • p : Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > & The initial simplex (npar+1 x npar). On return, row 0 is the best vertex.

  • y : Eigen::Matrix< T, Eigen::Dynamic, 1 > & Function values at the vertices.

  • ftol : T Relative convergence tolerance.

  • stol : T Absolute spatial convergence tolerance (0 to disable).

  • func : CostFunc && The cost function to be evaluated.

  • max_eval : int Maximum number of function evaluations.

  • neval : int & Number of function evaluations performed.

  • report : int How often to report (-1 = no reporting).

  • report_func : ReportFunc && The function to be called when reporting (may be nullptr).

Returns:

  • bool — True when minimization succeeded, false otherwise.

simplex_minimize(p, y, ftol, stol, func, max_eval, neval)​

Overload without report function (no reporting).

Parameters:

  • p : Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > & The initial simplex (npar+1 x npar). On return, row 0 is the best vertex.

  • y : Eigen::Matrix< T, Eigen::Dynamic, 1 > & Function values at the vertices.

  • ftol : T Relative convergence tolerance.

  • stol : T Absolute spatial convergence tolerance (0 to disable).

  • func : CostFunc && The cost function to be evaluated.

  • max_eval : int Maximum number of function evaluations.

  • neval : int & Number of function evaluations performed.

Returns:

  • bool — True when minimization succeeded, false otherwise.

Example​

Used in examples​

ex_math​

Math library on inputs with known answers.

src/examples/ex_math/main.cpp, line 154
int evaluations = 0;
const bool converged = SimplexAlgorithm::simplex_minimize<double>(simplex, values, 1e-12, 0.0, rosenbrock, 5000, evaluations);
Index best = 0;
values.minCoeff(&best); // vertices are not sorted on return
ok &= expect(converged && (simplex.row(best) - RowVector2d(1.0, 1.0)).norm() < 1e-3,
QString("Rosenbrock minimum (1, 1) after %1 evaluations").arg(evaluations));

Authors of this file​