UtilsLite
Utilities for C++ programming
Loading...
Searching...
No Matches
Utils::NelderMead< Real > Class Template Reference

Implements the Nelder-Mead optimization algorithm for nonlinear optimization problems. More...

#include <Utils_NelderMead.hh>

Public Types

using Vec_t = Eigen::Matrix<Real,Eigen::Dynamic,1>
 
using Mat_t = Eigen::Matrix<Real,Eigen::Dynamic,Eigen::Dynamic>
 
using MapVec = Eigen::Map<Vec_t>
 
using MapMat = Eigen::Map<Mat_t>
 
using integer = int
 
using NMFunc = std::function<Real(Real const[])>
 
using NM_move
 

Public Member Functions

 NelderMead (string_view name)
 Constructor that initializes the NelderMead instance with a specific name.
 
string_view name (void) const
 Get the name of the class.
 
void setup (integer dim, NMFunc &fun, Console const *console)
 Setup the optimizer with the function and console interface.
 
void change_console (Console const *console)
 Change the console output interface.
 
void set_verbose (integer level)
 Set the verbosity level for logging during the optimization process.
 
void set_tolerance (Real tol)
 Set the tolerance for stopping criteria in the optimization process.
 
void set_max_iterations (integer mit)
 Set the maximum number of iterations allowed in the optimization process.
 
void set_max_fun_evaluation (integer mfev)
 Set the maximum number of function evaluations allowed in the optimization process.
 
void message (Real tol) const
 Display a message with information about the current tolerance level.
 
string info () const
 Provide information about the current state of the Nelder-Mead optimization.
 
void print_info (ostream_type &stream) const
 Print detailed information about the current state of the Nelder-Mead optimizer.
 
bool search ()
 Perform the search step of the optimization process.
 
bool run (Real const x_sol[], Real h)
 Run the optimization process starting from an initial guess and step size.
 
Real get_last_solution (Real x[]) const
 Get the solution found by the Nelder-Mead algorithm after the last run.
 
Real get_better_value () const
 Get the best function value found during the optimization process.
 
Real get_worst_value () const
 Get the worst function value encountered during the optimization process.
 

Static Public Member Functions

static string to_string (NM_move n)
 

Detailed Description

template<typename Real>
class Utils::NelderMead< Real >

Implements the Nelder-Mead optimization algorithm for nonlinear optimization problems.

The Nelder-Mead algorithm is a widely used simplex-based method for minimizing an objective function in multiple dimensions. It is especially useful for problems where derivative information is not readily available or when the objective function is noisy.

Template Parameters
RealThe numeric type used for calculations, typically float or double.

This implementation of the Nelder-Mead algorithm is based on several foundational works:

  • J.A. Nelder and R. Mead, "A Simplex Method for Function Minimization," Computer Journal, vol. 7, pp. 308-313, 1965.
  • L.A. Yarbro and S.N. Deming, "Optimization by the Simplex Method," Analytica Chimica Acta, vol. 73, pp. 391-398, 1974.
  • S.L.S. Jacoby, J.S. Kowalik, and J.T. Pizzo, Iterative Methods for Nonlinear Optimization Problems (Englewood Cliffs, NJ: Prentice-Hall), 1972.
Note
The Nelder-Mead algorithm does not require derivatives, making it suitable for non-smooth or noisy functions.

Member Typedef Documentation

◆ integer

template<typename Real>
using Utils::NelderMead< Real >::integer = int

◆ MapMat

template<typename Real>
using Utils::NelderMead< Real >::MapMat = Eigen::Map<Mat_t>

◆ MapVec

template<typename Real>
using Utils::NelderMead< Real >::MapVec = Eigen::Map<Vec_t>

◆ Mat_t

template<typename Real>
using Utils::NelderMead< Real >::Mat_t = Eigen::Matrix<Real,Eigen::Dynamic,Eigen::Dynamic>

◆ NM_move

template<typename Real>
using Utils::NelderMead< Real >::NM_move
Initial value:
enum class NelderMead_move : integer {
INIT,
REFLECT,
EXPAND_FE,
EXPAND_FR,
CONTRACT_O,
CONTRACT_I,
SHRINK,
RESTART,
WORSE
}
int integer
Definition Utils_NelderMead.hh:72

◆ NMFunc

template<typename Real>
using Utils::NelderMead< Real >::NMFunc = std::function<Real(Real const[])>

◆ Vec_t

template<typename Real>
using Utils::NelderMead< Real >::Vec_t = Eigen::Matrix<Real,Eigen::Dynamic,1>

Constructor & Destructor Documentation

◆ NelderMead()

template<typename Real>
Utils::NelderMead< Real >::NelderMead ( string_view name)
inlineexplicit

Constructor that initializes the NelderMead instance with a specific name.

Parameters
nameThe name to be assigned to this instance of the Nelder-Mead optimizer.

Member Function Documentation

◆ change_console()

template<typename Real>
void Utils::NelderMead< Real >::change_console ( Console const * console)
inline

Change the console output interface.

Parameters
consoleA pointer to the new console object for logging.

◆ get_better_value()

template<typename Real>
Real Utils::NelderMead< Real >::get_better_value ( ) const
inline

Get the best function value found during the optimization process.

Returns
The best (lowest) function value found.

◆ get_last_solution()

template<typename Real>
Real Utils::NelderMead< Real >::get_last_solution ( Real x[]) const
inline

Get the solution found by the Nelder-Mead algorithm after the last run.

Parameters
xAn array to store the solution values (the optimizer writes the solution here).
Returns
The function value at the solution point.

◆ get_worst_value()

template<typename Real>
Real Utils::NelderMead< Real >::get_worst_value ( ) const
inline

Get the worst function value encountered during the optimization process.

Returns
The worst (highest) function value found.

◆ info()

template<typename Real>
string Utils::NelderMead< Real >::info ( ) const

Provide information about the current state of the Nelder-Mead optimization.

Returns
A string with detailed information about the current state of the optimizer.

◆ message()

template<typename Real>
void Utils::NelderMead< Real >::message ( Real tol) const

Display a message with information about the current tolerance level.

Parameters
tolThe tolerance level for which a message should be displayed.

◆ name()

template<typename Real>
string_view Utils::NelderMead< Real >::name ( void ) const
inline

Get the name of the class.

Returns
A string representing the name of the Nelder-Mead optimizer instance.

◆ print_info()

template<typename Real>
void Utils::NelderMead< Real >::print_info ( ostream_type & stream) const
inline

Print detailed information about the current state of the Nelder-Mead optimizer.

Parameters
streamThe output stream where the information will be printed.

◆ run()

template<typename Real>
bool Utils::NelderMead< Real >::run ( Real const x_sol[],
Real h )

Run the optimization process starting from an initial guess and step size.

Parameters
x_solAn array containing the initial guess for the solution.
hThe initial step size for the simplex search.
Returns
True if the optimization converged successfully; otherwise, false.

◆ search()

template<typename Real>
bool Utils::NelderMead< Real >::search ( )

Perform the search step of the optimization process.

Returns
True if the search was successful; otherwise, false.

◆ set_max_fun_evaluation()

template<typename Real>
void Utils::NelderMead< Real >::set_max_fun_evaluation ( integer mfev)

Set the maximum number of function evaluations allowed in the optimization process.

Parameters
mfevThe maximum number of function evaluations.

◆ set_max_iterations()

template<typename Real>
void Utils::NelderMead< Real >::set_max_iterations ( integer mit)

Set the maximum number of iterations allowed in the optimization process.

Parameters
mitThe maximum number of iterations to be performed.

◆ set_tolerance()

template<typename Real>
void Utils::NelderMead< Real >::set_tolerance ( Real tol)

Set the tolerance for stopping criteria in the optimization process.

Parameters
tolThe tolerance value, typically a small positive number (e.g., 1e-6).

◆ set_verbose()

template<typename Real>
void Utils::NelderMead< Real >::set_verbose ( integer level)
inline

Set the verbosity level for logging during the optimization process.

Parameters
levelThe verbosity level (higher values produce more detailed output).

◆ setup()

template<typename Real>
void Utils::NelderMead< Real >::setup ( integer dim,
NMFunc & fun,
Console const * console )

Setup the optimizer with the function and console interface.

Parameters
dimThe dimensionality of the optimization problem.
funA reference to the function to be minimized.
consoleA pointer to the console for output logging during optimization.

◆ to_string()

template<typename Real>
static string Utils::NelderMead< Real >::to_string ( NM_move n)
inlinestatic

The documentation for this class was generated from the following files: