Hooke-Jeeves method (pattern search method)

tags: Algorithm accumulation

Algorithm purpose

   The minimum value (minimum value) for solving unconstrained optimization problems.

Algorithm features

  There is no need to calculate the derivative of the objective function in the algorithm step.

Algorithm steps

Example 1

  Solve min   f ( x ) : = ( 1 x 1 ) 2 + 5 ( x 2 x 1 2 ) 2 . \min\ f(x):=(1-x_1)^2+5{(x_2-{x_1}^2)}^2.
Solution: Set the initial point to ( 2 , 0 ) T (2,0)^T , Initial detection step δ = 1 2 \delta = \frac{1}{2} , Acceleration factor α = 1 \alpha=1 , Attenuation factor β = 1 2 \beta=\frac{1}{2} ,Allowable error ϵ = 0.2 \epsilon = 0.2
  The Python code solution is as follows:

import numpy as np


def function1(x):
    return (1 - x[0]) ** 2 + 5 * (x[1] - x[0] ** 2) ** 2


# Enter the initial detection search step delta, acceleration factor alpha(alpha>=1), reduction rate beta(0<beta<1), allowable error epsilon(epsilon>0), initial point xk
delta, alpha, beta, epsilon, xk = 0.5, 1, 0.5, 0.2, np.array([2, 0])
yk = xk.copy()

# Find the dimension of the problem
dim = len(xk)

# Initialize the number of iterations
k = 1

while delta > epsilon:
    # Output the basic information of this search
    print('Enter the first', k, 'Round iteration')
    print('Base point:', xk)
    print('Function value at base point:', function1(xk))
    print('The starting point for detection is:', yk)
    print('Detect the function value at the starting point', function1(yk))
    print('Probing search step delta:', delta)

    # Enter detection movement
    for i in range(dim):
        # Generate the coordinate direction of this detection
        e = np.zeros([1, dim])[0]
        e[i] = 1

        # Calculate the detected points
        t1, t2 = function1(yk + delta * e), function1(yk)
        if t1 < t2:
            yk = yk + delta * e
        else:
            t1, t2 = function1(yk - delta * e), function1(yk)
            if t1 < t2:
                yk = yk - delta * e
        print('Th', i + 1, 'The point obtained by this detection is', yk)
        print('Function value corresponding to this point', function1(yk))

    # Determine the new base point and calculate the new detection initial point
    t1, t2 = function1(yk), function1(xk)
    if t1 < t2:
        xk, yk = yk, yk + alpha * (yk - xk)
    else:
        delta, yk = delta * beta, xk
    k += 1

    print("\n")

  Result

Enter the 1  Round iteration
 Base point: [2 0]
 Function value at base point: 81
 The starting point for detection is: [2 0]
 Probe the function value at the starting point 81
 Probe search step delta: 0.5
 First 1  The points obtained by this detection are [1.5 0. ]
 Function value corresponding to this point 25.5625
 First 2  The points obtained by this detection are [1.5 0.5]
 Function value corresponding to this point 15.5625

 Enter the 2  Round iteration
 Base point: [1.5 0.5]
 Function value at base point: 15.5625
 The starting point for detection is: [1. 1.]
 Probe the function value at the starting point 0.0
 Probe search step delta: 0.5
 First 1  The points obtained by this detection are [1. 1.]
 Function value corresponding to this point 0.0
 First 2  The points obtained by this detection are [1. 1.]
 Function value corresponding to this point 0.0

 Enter the 3  Round iteration
 Base point: [1. 1.]
 Function value at base point: 0.0
 The starting point for detection is: [0.5 1.5]
 Probe the function value at the starting point 8.0625
 Probe search step delta: 0.5
 First 1  The points obtained by this detection are [1.  1.5]
 Function value corresponding to this point 1.25
 First 2  The points obtained by this detection are [1. 1.]
 Function value corresponding to this point 0.0

 Enter the 4  Round iteration
 Base point: [1. 1.]
 Function value at base point: 0.0
 The starting point for detection is: [1. 1.]
 Probe the function value at the starting point 0.0
 Probe search step delta: 0.25
 First 1  The points obtained by this detection are [1. 1.]
 Function value corresponding to this point 0.0
 First 2  The points obtained by this detection are [1. 1.]
 Function value corresponding to this point 0.0

So the approximate minimum point is ( 1 , 1 ) T (1,1)^T , The corresponding minimum value is 0 0

Example 2

  Solve min f ( x ) : = x 1 2 + x 2 2 4 x 1 + 2 x 2 + 7. \min f(x):={x_1}^2+{x_2}^2-4x_1+2x_2+7.
Solution: Set the initial point to ( 0 , 0 ) T (0,0)^T , The initial detection step δ = 1 \delta = 1 , Acceleration factor α = 1 \alpha = 1 , Attenuation factor β = 0.25 \beta = 0.25 ,Allowable error ϵ = 0.05 \epsilon = 0.05
  The Python code is as follows:

import numpy as np


def function1(x):
    return x[0] ** 2 + x[1] ** 2 - 4 * x[0] + 2 * x[1] + 7


# Enter the initial detection search step delta, acceleration factor alpha(alpha>=1), reduction rate beta(0<beta<1), allowable error epsilon(epsilon>0), initial point xk
delta, alpha, beta, epsilon, xk = 1, 1, 0.25, 0.05, np.array([0, 0])
yk = xk.copy()

# Find the dimension of the problem
dim = len(xk)

# Initialize the number of iterations
k = 1

while delta > epsilon:
    # Output the basic information of this search
    print('Enter the first', k, 'Round iteration')
    print('Base point:', xk)
    print('Function value at base point:', function1(xk))
    print('The starting point for detection is:', yk)
    print('Detect the function value at the starting point', function1(yk))
    print('Probing search step delta:', delta)

    # Enter detection movement
    for i in range(dim):
        # Generate the coordinate direction of this detection
        e = np.zeros([1, dim])[0]
        e[i] = 1

        # Calculate the detected points
        t1, t2 = function1(yk + delta * e), function1(yk)
        if t1 < t2:
            yk = yk + delta * e
        else:
            t1, t2 = function1(yk - delta * e), function1(yk)
            if t1 < t2:
                yk = yk - delta * e
        print('Th', i + 1, 'The point obtained by this detection is', yk)
        print('Function value corresponding to this point', function1(yk))

    # Determine the new base point and calculate the new detection initial point
    t1, t2 = function1(yk), function1(xk)
    if t1 < t2:
        xk, yk = yk, yk + alpha * (yk - xk)
    else:
        delta, yk = delta * beta, xk
    k += 1

    print("\n")

  Result:

Enter the 1  Round iteration
 Base point: [0 0]
 Function value at base point: 7
 The starting point for detection is: [0 0]
 Probe the function value at the starting point 7
 Probe search step delta: 1
 First 1  The points obtained by this detection are [1. 0.]
 Function value corresponding to this point 4.0
 First 2  The points obtained by this detection are [ 1. -1.]
 Function value corresponding to this point 3.0

 Enter the 2  Round iteration
 Base point: [ 1. -1.]
 Function value at base point: 3.0
 The starting point for detection is: [ 2. -2.]
 Probe the function value at the starting point 3.0
 Probe search step delta: 1
 First 1  The points obtained by this detection are [ 2. -2.]
 Function value corresponding to this point 3.0
 First 2  The points obtained by this detection are [ 2. -1.]
 Function value corresponding to this point 2.0

 Enter the 3  Round iteration
 Base point: [ 2. -1.]
 Function value at base point: 2.0
 The starting point for detection is: [ 3. -1.]
 Probe the function value at the starting point 3.0
 Probe search step delta: 1
 First 1  The points obtained by this detection are [ 2. -1.]
 Function value corresponding to this point 2.0
 First 2  The points obtained by this detection are [ 2. -1.]
 Function value corresponding to this point 2.0

 Enter the 4  Round iteration
 Base point: [ 2. -1.]
 Function value at base point: 2.0
 The starting point for detection is: [ 2. -1.]
 Probe the function value at the starting point 2.0
 Probe search step delta: 0.25
 First 1  The points obtained by this detection are [ 2. -1.]
 Function value corresponding to this point 2.0
 First 2  The points obtained by this detection are [ 2. -1.]
 Function value corresponding to this point 2.0

 Enter the 5  Round iteration
 Base point: [ 2. -1.]
 Function value at base point: 2.0
 The starting point for detection is: [ 2. -1.]
 Probe the function value at the starting point 2.0
 Probe search step delta: 0.0625
 First 1  The points obtained by this detection are [ 2. -1.]
 Function value corresponding to this point 2.0
 First 2  The points obtained by this detection are [ 2. -1.]
 Function value corresponding to this point 2.0

So the approximate minimum point obtained is ( 2 , 1 ) T (2,-1)^T , The corresponding minimum value is 2 2

Intelligent Recommendation

Sorting method and search method

Sort Glossary: stable: If a is originally in front of b, and a=b, a is still in front of b after sorting. Unstable: If a is originally in front of b, and a=b, a may appear after b after sorting. time ...

Simple_Template Method Pattern (Template Method)

[img]http://dl.iteye.com/upload/attachment/0062/7369/0050e697-6914-382b-8990-95dc86b9c968.bmp[/img] To understand the template method pattern by "seeing people say ghosts and saying ghosts":...

Template method pattern - Template Method

The template method pattern refers to: in an abstract class, there is a main method (usuallyfinal), then define other abstract or concrete methods, call these methods by the main method, then define t...

Factory method factory method pattern

Demand Develop a test tool specifically for testing mobile phones. The test process is opened in the following steps: test boot speed -> test screen brightness -> test camera effect. There are m...

Template Method pattern (Template Method)

Template Method pattern is the basic technology reuse of code-based inheritance. Template Method need to develop coordination between the abstract class and a concrete subclass designers, designer in ...

More Recommendation

The method of stencil pattern (Template Method)

1, the concept of Pattern mode (Template Pattern), the definition of an abstract class is disclosed a method of performing its info / template. Its subclass can override methods as needed to achieve, ...

Factory Method Pattern (Factory Method)

1. Summary The meaning of the FactoryMethod pattern is to define a factory interface that creates product objects, deferring the actual creation to subclasses. 2. Examples in life For example, mobile ...

Factory method pattern-factory method

1. Factory method pattern definition Define an interface for creating objects, let subclasses decide which class to instantiate, and Factory Method defers an instance of a class to its subclasses. The...

Design method-template method pattern

Preface The article is taken from The template method pattern is also relatively easy to understand, for example, cooking, the same steps and different people make different tastes. Or to build a car,...

Factory Method - Factory Method Pattern

Factory Method - Factory Method Pattern Factory Method Pattern: Define an interface for creating an object, allowing subclasses to instantiate it. Factory method mode allows an instantiation of a clas...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top