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:
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:
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: