|
| HJPatternSearch (string_view name) |
| Constructs a Hooke-Jeeves Pattern Search instance.
|
|
string_view | name (void) const |
| Retrieves the name of the pattern search instance.
|
|
void | setup (integer dim, HJFunc &fun, Console const *console) |
| Sets up the pattern search with the specified dimension and objective function.
|
|
void | change_console (Console const *console) |
| Changes the console used for output messages.
|
|
void | set_verbose (integer level) |
| Sets the verbosity level for output messages.
|
|
void | set_tolerance (Real tol) |
| Sets the convergence tolerance.
|
|
void | set_max_iterations (integer mit) |
| Sets the maximum number of iterations.
|
|
void | set_max_fun_evaluation (integer mfev) |
| Sets the maximum number of function evaluations.
|
|
void | set_max_num_stagnation (integer nstg) |
| Sets the maximum number of allowed stagnation iterations.
|
|
string | info () const |
| Provides information about the current state of the algorithm.
|
|
void | print_info (ostream_type &stream) const |
| Prints the current state information to the specified output stream.
|
|
void | best_nearby () |
| Searches for the best nearby solution.
|
|
void | search () |
| Performs the main search algorithm.
|
|
void | run (Real const x_sol[], Real h) |
| Runs the optimization algorithm starting from the provided solution.
|
|
Real | get_last_solution (Real x[]) const |
| Retrieves the last solution found by the algorithm.
|
|
template<typename Real>
class Utils::HJPatternSearch< Real >
Class for implementing the Hooke-Jeeves Pattern Search Algorithm.
The HJPatternSearch
class provides an implementation of the Hooke-Jeeves Pattern Search Algorithm, which is a direct search method for optimization problems. This method does not require the computation of gradients and is suitable for minimizing functions in arbitrary dimensions.
- Template Parameters
-
Real | The data type used for the optimization (e.g., float, double). |
References
The implementation is based on the following references:
- R. Hooke, T. A. Jeeves, "Direct Search" Solution of Numerical and Statistical Problems, Westinghouse Research Laboratories, Pittsburgh, Pennsylvania.
- Arthur Kaupe, "Algorithm 178: Direct Search," Communications of the ACM, Volume 6, Number 6, June 1963, page 313.
- M. Bell, Malcolm Pike, "Remark on Algorithm 178: Direct Search," Communications of the ACM, Volume 9, Number 9, September 1966, page 684.
- F.K. Tomlin, L.B. Smith, "Remark on Algorithm 178: Direct Search," Communications of the ACM, Volume 12, Number 11, November 1969, pages 637-638.
Using the HJPatternSearch Class for Function Minimization
The HJPatternSearch
class is a powerful tool for finding the minimum of a function in an arbitrary number of dimensions using the Hooke-Jeeves Pattern Search algorithm. This tutorial will guide you through the process of using this class effectively.
Prerequisites
Before you begin, ensure you have the following:
- C++ Compiler: A C++ compiler that supports C++11 or later.
- Eigen Library: Make sure the Eigen library is included in your project for matrix operations.
- Your Implementation of
HJPatternSearch
: Ensure you have the implementation of the HJPatternSearch
class available in your project.
Step 1: Include Necessary Headers
Start by including the headers for the HJPatternSearch
class and any necessary libraries.
#include <cmath>
#include <iostream>
#include <functional>
using namespace std;
using real_type = double;
Class for implementing the Hooke-Jeeves Pattern Search Algorithm.
Definition Utils_HJPatternSearch.hh:170
Step 2: Define the Objective Function
You need to define the function you want to minimize. The function should take an array of doubles as input and return a double representing the function value.
Here’s an example of a simple quadratic function:
static real_type fun3(real_type const X[]) {
real_type x = X[0];
real_type y = X[1];
return 100 * pow(y - x * x, 2) + pow(1 - x, 2);
}
Step 3: Set Up the Solver
Create a function to set up and run the HJPatternSearch solver.
template <typename FUN>
void do_solve(FUN f, real_type const X0[], real_type delta) {
solver.setup(2, f, &console);
solver.set_tolerance(1e-20);
solver.run(X0, delta);
real_type X[2];
solver.get_last_solution(X);
fmt::print("X={}, Y={}\n", X[0], X[1]);
}
Class to handle console output with different styles and levels.
Definition Console.hxx:52
HJPatternSearch(string_view name)
Constructs a Hooke-Jeeves Pattern Search instance.
Definition Utils_HJPatternSearch.hh:228
Step 4: Main Function to Execute the Solver
In your main function, define your initial guess and the step size for the search. Then call the do_solve function with your defined objective function.
int main() {
real_type X0[2]{-1, 1};
real_type delta = 0.1;
std::function<real_type(real_type const[])> F(fun3);
do_solve(F, X0, delta);
cout << "\nAll Done Folks!\n";
return 0;
}
Step 5: Compile and Run
Compile your program using a suitable C++ compiler. For example, using g++ you can compile with:
g++ -std=c++11 -o HJPatternSearchExample your_file.cpp
Run the executable: