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

Class for implementing the Hooke-Jeeves Pattern Search Algorithm. More...

#include <Utils_HJPatternSearch.hh>

Public Member Functions

 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.
 

Detailed Description

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
RealThe 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:

  1. C++ Compiler: A C++ compiler that supports C++11 or later.
  2. Eigen Library: Make sure the Eigen library is included in your project for matrix operations.
  3. 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 "Utils_HJPatternSearch.hh" // Adjust the path according to your project structure
#include "Utils_fmt.hh" // For formatted output (optional)
#include <cmath>
#include <iostream>
#include <functional>
using namespace std;
using Utils::HJPatternSearch; // Adjust according to your namespace
using real_type = double; // Define the real type as double for precision
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); // Rosenbrock function
}

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) {
Utils::Console console(&cout, 4); // Create a console for logging
HJPatternSearch<real_type> solver("HJPatternSearch"); // Create the solver
solver.setup(2, f, &console); // Setup for 2D optimization
solver.set_tolerance(1e-20); // Set the desired tolerance for convergence
solver.run(X0, delta); // Run the optimization from initial guess X0 with step size delta
real_type X[2]; // Array to store the result
solver.get_last_solution(X); // Retrieve the best solution found
fmt::print("X={}, Y={}\n", X[0], X[1]); // Print the results
}
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}; // Initial guess for [x, y]
real_type delta = 0.1; // Initial step size for search
std::function<real_type(real_type const[])> F(fun3); // Create a function wrapper
do_solve(F, X0, delta); // Execute the optimization
cout << "\nAll Done Folks!\n"; // Indicate completion
return 0; // Return success
}

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:

./HJPatternSearchExample

Constructor & Destructor Documentation

◆ HJPatternSearch()

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

Constructs a Hooke-Jeeves Pattern Search instance.

Parameters
nameThe name of this instance for identification.

Member Function Documentation

◆ best_nearby()

template<typename Real>
void Utils::HJPatternSearch< Real >::best_nearby ( )

Searches for the best nearby solution.

This method performs a search around the current best solution to find a nearby minimum.

◆ change_console()

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

Changes the console used for output messages.

Parameters
consolePointer to the new console for output.

◆ get_last_solution()

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

Retrieves the last solution found by the algorithm.

Parameters
xArray to store the last solution.
Returns
The objective function value at the last solution.

◆ info()

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

Provides information about the current state of the algorithm.

Returns
A string containing the current state information.

◆ name()

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

Retrieves the name of the pattern search instance.

Returns
The name of the instance.

◆ print_info()

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

Prints the current state information to the specified output stream.

Parameters
streamThe output stream to which the information will be printed.

◆ run()

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

Runs the optimization algorithm starting from the provided solution.

Parameters
x_solInitial solution to start the optimization.
hInitial step size for the search.

◆ search()

template<typename Real>
void Utils::HJPatternSearch< Real >::search ( )

Performs the main search algorithm.

This method executes the Hooke-Jeeves pattern search algorithm iteratively.

◆ set_max_fun_evaluation()

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

Sets the maximum number of function evaluations.

Parameters
mfevThe maximum number of function evaluations allowed.

◆ set_max_iterations()

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

Sets the maximum number of iterations.

Parameters
mitThe maximum iterations allowed.

◆ set_max_num_stagnation()

template<typename Real>
void Utils::HJPatternSearch< Real >::set_max_num_stagnation ( integer nstg)

Sets the maximum number of allowed stagnation iterations.

Parameters
nstgThe maximum number of allowed stagnation iterations.

◆ set_tolerance()

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

Sets the convergence tolerance.

Parameters
tolThe new tolerance value.

◆ set_verbose()

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

Sets the verbosity level for output messages.

Parameters
levelThe verbosity level (0: none, 1: basic info, 2: detailed info).

◆ setup()

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

Sets up the pattern search with the specified dimension and objective function.

Parameters
dimThe dimension of the problem.
funA reference to the objective function to minimize.
consoleOptional pointer to a console for output messages.

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