26#ifndef UTILS_NELDER_MEAD_dot_HH
27#define UTILS_NELDER_MEAD_dot_HH
64 template <
typename Real>
68 using Vec_t = Eigen::Matrix<Real,Eigen::Dynamic,1>;
69 using Mat_t = Eigen::Matrix<Real,Eigen::Dynamic,Eigen::Dynamic>;
73 using NMFunc = std::function<Real(Real
const[])>;
89 case NM_move::INIT: res =
"INIT";
break;
90 case NM_move::REFLECT: res =
"REFLECT";
break;
91 case NM_move::EXPAND_FE: res =
"EXPAND_FE";
break;
92 case NM_move::EXPAND_FR: res =
"EXPAND_FR";
break;
93 case NM_move::CONTRACT_O: res =
"CONTRACT_O";
break;
94 case NM_move::CONTRACT_I: res =
"CONTRACT_I";
break;
95 case NM_move::SHRINK: res =
"SHRINK";
break;
96 case NM_move::RESTART: res =
"RESTART";
break;
97 case NM_move::WORSE: res =
"WORSE";
break;
109 Console const * m_console{
nullptr};
113 Real m_dim_factorial{0};
114 Real m_dim_regular_simplex_volume{0};
115 Real m_tolerance{Real(1e-8)};
124 MapMat m_dist{
nullptr,0,0};
126 MapMat m_p_work{
nullptr,0,0};
127 MapVec m_f_work{
nullptr,0};
129 Eigen::PartialPivLU<Mat_t> m_lu;
137 Real m_simplex_volume{0};
139 Real m_rho{Real(1.0)};
140 Real m_chi{Real(2.0)};
141 Real m_gamma{Real(0.5)};
142 Real m_sigma{Real(0.25)};
143 Real m_prob{Real(0.3)};
144 Real m_volume_tolerance{Real(0.01)};
146 integer m_max_fun_evaluation{5000};
149 mutable integer m_iteration_count{0};
150 mutable integer m_fun_evaluation_count{0};
152 NM_move m_which_step{NM_move::INIT};
157 Real extrapolate( Real alpha,
integer j,
MapVec & pe )
const;
162 void dist_update(
integer jpos );
163 void grad_update(
integer jpos );
164 Real reflect(
MapVec & pr ) {
return this->extrapolate( m_rho, m_high, pr ); }
165 Real expand (
MapVec & pe ) {
return this->extrapolate( m_rho*m_chi, m_high, pe ); }
166 Real outside(
MapVec & po ) {
return this->extrapolate( m_rho*m_gamma, m_high, po ); }
167 Real inside (
MapVec & pi ) {
return this->extrapolate( -m_gamma, m_high, pi ); }
169 void replace_point( Real fj,
MapVec const & pj,
integer jpos );
170 void spendley( Real
const X0[], Real delta );
171 void diamond( Real
const X0[], Real delta );
174 eval_function( Real
const x[] )
const {
175 ++m_fun_evaluation_count;
197 string_view
name(
void)
const {
return m_name; }
248 void message( Real tol )
const;
281 bool run( Real
const x_sol[], Real h );
291 std::copy_n( m_p.col(m_low).data(), m_dim, x );
292 return m_f.coeff(m_low);
Class to handle console output with different styles and levels.
Definition Console.hxx:52
Class for dynamic memory allocation of objects.
Definition Malloc.hxx:79
string info() const
Provide information about the current state of the Nelder-Mead optimization.
Definition Utils_NelderMead.cc:305
Eigen::Matrix< Real, Eigen::Dynamic, 1 > Vec_t
Definition Utils_NelderMead.hh:68
string_view name(void) const
Get the name of the class.
Definition Utils_NelderMead.hh:197
std::function< Real(Real const [])> NMFunc
Definition Utils_NelderMead.hh:73
void set_max_fun_evaluation(integer mfev)
Set the maximum number of function evaluations allowed in the optimization process.
Definition Utils_NelderMead.cc:77
Real get_last_solution(Real x[]) const
Get the solution found by the Nelder-Mead algorithm after the last run.
Definition Utils_NelderMead.hh:290
void message(Real tol) const
Display a message with information about the current tolerance level.
Definition Utils_NelderMead.cc:331
void set_tolerance(Real tol)
Set the tolerance for stopping criteria in the optimization process.
Definition Utils_NelderMead.cc:49
int integer
Definition Utils_NelderMead.hh:72
void setup(integer dim, NMFunc &fun, Console const *console)
Setup the optimizer with the function and console interface.
Definition Utils_NelderMead.cc:123
static string to_string(NM_move n)
Definition Utils_NelderMead.hh:86
bool search()
Perform the search step of the optimization process.
Definition Utils_NelderMead.cc:361
enum class NelderMead_move :integer { INIT, REFLECT, EXPAND_FE, EXPAND_FR, CONTRACT_O, CONTRACT_I, SHRINK, RESTART, WORSE } NM_move
Definition Utils_NelderMead.hh:74
bool run(Real const x_sol[], Real h)
Run the optimization process starting from an initial guess and step size.
Definition Utils_NelderMead.cc:468
Eigen::Map< Vec_t > MapVec
Definition Utils_NelderMead.hh:70
NelderMead(string_view name)
Constructor that initializes the NelderMead instance with a specific name.
Definition Utils_NelderMead.hh:187
void set_max_iterations(integer mit)
Set the maximum number of iterations allowed in the optimization process.
Definition Utils_NelderMead.cc:63
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic > Mat_t
Definition Utils_NelderMead.hh:69
Eigen::Map< Mat_t > MapMat
Definition Utils_NelderMead.hh:71
Real get_worst_value() const
Get the worst function value encountered during the optimization process.
Definition Utils_NelderMead.hh:307
void print_info(ostream_type &stream) const
Print detailed information about the current state of the Nelder-Mead optimizer.
Definition Utils_NelderMead.hh:262
void change_console(Console const *console)
Change the console output interface.
Definition Utils_NelderMead.hh:213
Real get_better_value() const
Get the best function value found during the optimization process.
Definition Utils_NelderMead.hh:300
void set_verbose(integer level)
Set the verbosity level for logging during the optimization process.
Definition Utils_NelderMead.hh:220
Definition SystemUtils.cc:39
std::basic_ostream< char > ostream_type
Type for output stream.
Definition Console.hxx:28
std::string string
Type for string.
Definition Console.hxx:29