Mathematical Definition

\[f(x, y)=0.5 + \frac{sin^2(x^2-y^2)-0.5}{(1+0.001(x^2+y^2))^2}\]

Plots

Schaffer N. 2 Function

Schaffer N. 2 Function

Schaffer N. 2 Function

Schaffer N. 2 Function

Schaffer N. 2 Function

Two contours of the function are as presented below:

Schaffer N. 2 Function

Schaffer N. 2 Function

Description and Features

  • The function is continuous.
  • The function is not convex.
  • The function is defined on 2-dimensional space.
  • The function is unimodal.
  • The function is differentiable.
  • The function is non-separable.
  • The function is .

Input Domain

The function can be defined on any input domain but it is usually evaluated on $x_i \in [-100, 100]$ for $i=1, 2$.

Global Minima

The function has one global minimum $f(\textbf{x}^{\ast})=0.0$ at $\textbf{x}^{\ast} = (0, 0)$.

Implementation

Python

For Python, the function is implemented in the benchmarkfcns package, which can be installed from command line with pip install benchmarkfcns.

from benchmarkfcns import schaffer2

print(schaffer2([[0, 0],
              [1, 1]]))

MATLAB

An implementation of the Schaffer N. 2 Function with MATLAB is provided below.

% Computes the value of the Schaffer N. 2 benchmark function.
% SCORES = SCHAFFERN2FCN(X) computes the value of the Schaffer N. 2 function 
% at point X. SCHAFFERN2FCN accepts a matrix of size M-by-2 and returns a 
% vetor SCORES of size M-by-1 in which each row contains the function value 
% for the corresponding row of X. For more information please visit: 
% https://en.wikipedia.org/wiki/Test_functions_for_optimization
% 
% Author: Mazhar Ansari Ardeh
% Please forward any comments or bug reports to mazhar.ansari.ardeh at
% Google's e-mail service or feel free to kindly modify the repository.
function scores = schaffern2fcn(x)
    
    n = size(x, 2);
    assert(n == 2, 'The Schaffer N. 2 function is only defined on a 2D space.')
    X = x(:, 1);
    Y = x(:, 2);
    
    sincomponent = sin( (X .^ 2) - (Y .^ 2) ).^2;
    
    scores = 0.5 + ((sincomponent - 0.5) ./ (1 + 0.001 * (X .^2 + Y .^2)) .^2 ) ;
end

The function can be represented in Latex as follows:

f(x, y)=0.5 + \frac{sin^2(x^2-y^2)-0.5}{(1+0.001(x^2+y^2))^2}

References: