top of page
Search

# Python Implementation of SCHAFFER FUNCTION

Updated: Jul 18, 2021   Mathematical Definition Input Domain

The function is defined on input domain i.e. x ∈ [−100, 100] and y ∈ [−100, 100].

Global Minima

The function has one global minimum f(z) = 0 at z = (0, 0).

Characteristics

This function is unimodal.

The function is continuous.

The function is not convex.

The function is differentiable.

The function is non-separable.

The function is defined on 2-dimensional space.

Python Implementation

```% Please forward any comments or bug reports in chat
Copyright 2021. INDUSMIC PRIVATE LIMITED.THERE IS NO WARRANTY, EXPRESS OR IMPLIED. WE DO NOT ASSUME ANY LIABILITY FOR THE USE OF THIS PROGRAM. If software is modified to produce derivative works, such modified software should be clearly marked. Additionally, user can redistribute it and/or modify it under the terms of the GNU General Public License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. See the GNU General Public License for more details.
% for any support connect with us on help.indusmic@gmail.com
% Author: Yamini Jain

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm

def f(x,y):                          # Defines the function
num = (np.sin((x**2 + y**2)**2)**2) - 0.5
den = (1 + 0.001*(x**2 + y**2))**2
return 0.5 + num/den

X = np.linspace(-50,50)
Y = np.linspace(-50,50)

x,y = np.meshgrid(X,Y) # Makes a rough mesh in which graph will be plotted
F = f(x,y)

fig = plt.figure(figsize=(9,9))
ax = plt.axes(projection='3d')
graph = ax.contour3D(x,y, F,450,cmap = cm.jet)
# There are many color options to choose from
# colourmaps like jet, rainbow, cube_helix and many more

ax.set_title('SCHAFFER N.1 FUNCTION')         #Title of the graph
fig.colorbar(graph, shrink=0.5, aspect=8)  #Gives a color bar
ax.set_xlabel('X')                           #Labling axes
ax.set_ylabel('Y')
ax.set_zlabel('F')
ax.set_xlim(50,-50)                         #Setting axes limit
ax.set_ylim(50,-50)

ax.view_init(4,4)                           # Viewing angle of graph
plt.show()```

References:

 Jamil, Momin, and Xin-She Yang. "A literature survey of benchmark functions for global optimization problems." International Journal of Mathematical Modelling and Numerical Optimization 4.2 (2013): 150-194.