Ackley Function


Reading time: about 5 minutes
In [1]:
get_ipython().ast_node_interactivity = 'all'
import os
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
import math
import numba
matplotlib.rcParams['figure.dpi'] = 150
In [2]:
def make_ackley(dimensions, a=20, b=0.2, c=2 * 3.14):
    @numba.njit
    def inner(*args):
        assert len(args) == dimensions
        x = 0
        for d in args:
            x += d ** 2
        x *= 1 / dimensions
        x = math.sqrt(x)
        x *= -b
        result = -a * math.exp(x)
        
        x = 0
        for d in args:
            x += math.cos(d * c)
        x *= 1 / dimensions
        result -= math.exp(x)
        
        result += a
        result -= math.exp(1)
        
        return result
    return inner
In [3]:
ackley_1d = make_ackley(1)

graph = [ackley_1d(x) for x in np.arange(-40, 40, 0.001)]

plt.plot(np.arange(-40, 40, 0.001), graph)
Out [3]:
[]
Out:
<Figure size 900x600 with 1 Axes>
In [9]:
ackley_2d = make_ackley(2)

RES = 10
graph = np.zeros((80 * RES, 80 * RES))

for x in range(80 * RES):
    for y in range(80 * RES):
        graph[y][x] = ackley_2d((x / RES) - 40, (y / RES) - 40)

plt.imshow(graph)
plt.colorbar()
Out [9]:
Out [9]:
Out:
<Figure size 900x600 with 2 Axes>
In [5]:
ackley_2d = make_ackley(2)

gx = []
gy = []
gz = []

for x in np.arange(-40, 40, 0.5):
    for y in np.arange(-40, 40, 0.5):
        gx.append(x)
        gy.append(y)
        gz.append(ackley_2d(x, y))

fig = plt.figure()
ax = fig.add_subplot(projection='3d')
_ = ax.plot_trisurf(gx, gy, gz)
Out:
<Figure size 900x600 with 1 Axes>

The following pages link here

Citation

If you find this work useful, please cite it as:
@article{yaltirakliwikiackleyfunction,
  title   = "Ackley Function",
  author  = "Yaltirakli, Gokberk",
  journal = "gkbrk.com",
  year    = "2024",
  url     = "https://www.gkbrk.com/wiki/ackley-function/"
}
Not using BibTeX? Click here for more citation styles.
IEEE Citation
Gokberk Yaltirakli, "Ackley Function", December, 2024. [Online]. Available: https://www.gkbrk.com/wiki/ackley-function/. [Accessed Dec. 30, 2024].
APA Style
Yaltirakli, G. (2024, December 30). Ackley Function. https://www.gkbrk.com/wiki/ackley-function/
Bluebook Style
Gokberk Yaltirakli, Ackley Function, GKBRK.COM (Dec. 30, 2024), https://www.gkbrk.com/wiki/ackley-function/

Comments

© 2024 Gokberk Yaltirakli