UtilsLite
Utilities for C++ programming
Loading...
Searching...
No Matches
Utils_HJPatternSearch.hh
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*\
2 | |
3 | Copyright (C) 2022 |
4 | |
5 | , __ , __ |
6 | /|/ \ /|/ \ |
7 | | __/ _ ,_ | __/ _ ,_ |
8 | | \|/ / | | | | \|/ / | | | |
9 | |(__/|__/ |_/ \_/|/|(__/|__/ |_/ \_/|/ |
10 | /| /| |
11 | \| \| |
12 | |
13 | Enrico Bertolazzi |
14 | Dipartimento di Ingegneria Industriale |
15 | Università degli Studi di Trento |
16 | email: enrico.bertolazzi\unitn.it |
17 | |
18\*--------------------------------------------------------------------------*/
19
20//
21// file: Utils_HJPatternSearch.hh
22//
23
24#pragma once
25
26#ifndef UTILS_HJ_PATTERN_SEARCH_dot_HH
27#define UTILS_HJ_PATTERN_SEARCH_dot_HH
28
29#include <iostream>
30#include <sstream>
31#include <string>
32#include <algorithm>
33#include <functional>
34#include <cmath>
35
36#include "Utils.hh"
37#include "Utils_eigen.hh"
38
39namespace Utils {
40
45
169 template <typename Real>
171 using Vec_t = Eigen::Matrix<Real,Eigen::Dynamic,1>;
172 using MapVec = Eigen::Map<Vec_t>;
173 using integer = int;
174 using HJFunc = std::function<Real(Real const[])>;
175
176 private:
177 string const m_name;
178
179 Malloc<Real> m_base_value;
180
181 HJFunc m_fun;
182 Console const * m_console{nullptr};
183
184 Real m_rho{Real(0.9)};
185 Real m_h{Real(0.1)};
186 bool m_stencil_failure{false}; // stencil failure flag - used to shrink h,
187 // stencil_failure = true means failure
188
189 integer m_dim{0};
190 MapVec m_x_old{nullptr,0};
191 MapVec m_x_best{nullptr,0};
192 Real m_f_old;
193 Real m_f_best;
194
195 MapVec m_dir{nullptr,0};
196 MapVec m_search_sign{nullptr,0};
197
198 MapVec m_p{nullptr,0};
199 MapVec m_p1{nullptr,0};
200 MapVec m_new_x{nullptr,0};
201
202 Real m_tolerance{Real(1e-8)};
203 integer m_max_iteration{500};
204 integer m_max_fun_evaluation{1000};
205 integer m_max_num_stagnation{10};
206 integer m_verbose{1};
207
208 integer m_iteration_count;
209 integer m_fun_evaluation_count;
210
211 void allocate( integer n );
212
214 Real
215 eval_function( MapVec const & x ) const
216 { ++m_fun_evaluation; return m_fun( x.data() ); }
217
218 mutable integer m_fun_evaluation;
219
220 public:
221
227 explicit
228 HJPatternSearch( string_view name )
229 : m_name(name)
230 , m_base_value( string("HJ_")+string(name) )
231 {}
232
238 string_view name(void) const { return m_name; }
239
247 void setup( integer dim, HJFunc & fun, Console const * console );
248
254 void change_console( Console const * console ) { m_console = console; }
255
261 void set_verbose( integer level ) { m_verbose = level; }
262
268 void set_tolerance( Real tol );
269
275 void set_max_iterations( integer mit );
276
282 void set_max_fun_evaluation( integer mfev );
283
289 void set_max_num_stagnation( integer nstg );
290
296 string info() const;
297
303 void print_info( ostream_type & stream ) const { stream << info(); }
304
310 void best_nearby();
311
317 void search();
318
325 void run( Real const x_sol[], Real h );
326
333 Real
334 get_last_solution( Real x[] ) const
335 { std::copy_n( m_x_best.data(), m_dim, x ); return m_f_best; }
336
337 };
338
340
341}
342
343#endif
344
345//
346// eof: Utils_HJPatternSearch.hh
347//
Class to handle console output with different styles and levels.
Definition Console.hxx:52
void set_tolerance(Real tol)
Sets the convergence tolerance.
Definition Utils_HJPatternSearch.cc:34
void set_max_fun_evaluation(integer mfev)
Sets the maximum number of function evaluations.
Definition Utils_HJPatternSearch.cc:58
void change_console(Console const *console)
Changes the console used for output messages.
Definition Utils_HJPatternSearch.hh:254
HJPatternSearch(string_view name)
Constructs a Hooke-Jeeves Pattern Search instance.
Definition Utils_HJPatternSearch.hh:228
void setup(integer dim, HJFunc &fun, Console const *console)
Sets up the pattern search with the specified dimension and objective function.
Definition Utils_HJPatternSearch.cc:102
Real get_last_solution(Real x[]) const
Retrieves the last solution found by the algorithm.
Definition Utils_HJPatternSearch.hh:334
string_view name(void) const
Retrieves the name of the pattern search instance.
Definition Utils_HJPatternSearch.hh:238
void run(Real const x_sol[], Real h)
Runs the optimization algorithm starting from the provided solution.
Definition Utils_HJPatternSearch.cc:254
void set_max_iterations(integer mit)
Sets the maximum number of iterations.
Definition Utils_HJPatternSearch.cc:46
string info() const
Provides information about the current state of the algorithm.
Definition Utils_HJPatternSearch.cc:116
void search()
Performs the main search algorithm.
Definition Utils_HJPatternSearch.cc:147
void best_nearby()
Searches for the best nearby solution.
Definition Utils_HJPatternSearch.cc:211
void set_verbose(integer level)
Sets the verbosity level for output messages.
Definition Utils_HJPatternSearch.hh:261
void print_info(ostream_type &stream) const
Prints the current state information to the specified output stream.
Definition Utils_HJPatternSearch.hh:303
void set_max_num_stagnation(integer nstg)
Sets the maximum number of allowed stagnation iterations.
Definition Utils_HJPatternSearch.cc:70
Class for dynamic memory allocation of objects.
Definition Malloc.hxx:79
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