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'] = 150def 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 innerackley_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)[<matplotlib.lines.Line2D at 0x7fb3345a8a30>]
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()<matplotlib.image.AxesImage at 0x7fb32a3046a0>
<matplotlib.colorbar.Colorbar at 0x7fb3221cf7c0>
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)