How to draw a 3D graph before optimization.
First of all, what kind of 3D graph you want to draw is the evaluation function in the library of genetic algorithms called DEAP. Since I want to consider multi-purpose optimization, it seems easier to understand if I draw a 3D graph, so first I will write a 3D graph.
See this area for the evaluation function of optimization. DEAP 1.3.1 documentation A super-introduction to multipurpose optimization problems that anyone can understand
To draw a three-dimensional graph, first make points in a mesh shape on the two axes X1 and x2. At that point, the value on the z-axis is calculated. Draw a 3D graph of x1, x2, Z values in Axes3D.
1: Use the numpy
ʻarangefunction to create evenly spaced numbers.
np.arange (-30,30,1)From a-30 to 30, you can create numerical values with one interval at a time. 2:
meshgrid (a, b)` gives a grid point at the intersection of a and b of the one-dimensional array, and puts it in X1 and X2.
#How to make a mesh
import numpy as np
a = np.arange(-30,30,1)
b = np.arange(-30,30,1)
print(a)
#[-30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -14 -13
# -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5
# 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 24 25 26 27 28 29]
print(b)
#[-30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -14 -13
# -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5
# 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 24 25 26 27 28 29]
X1, x2 = np.meshgrid(a, b)
print(X1)
#[[-30 -29 -28 ... 27 28 29]
# [-30 -29 -28 ... 27 28 29]
# [-30 -29 -28 ... 27 28 29]
# ...
# [-30 -29 -28 ... 27 28 29]
# [-30 -29 -28 ... 27 28 29]
# [-30 -29 -28 ... 27 28 29]]
print(X2)
#[[-30 -29 -28 ... 27 28 29]
# [-30 -29 -28 ... 27 28 29]
# [-30 -29 -28 ... 27 28 29]
# ...
# [-30 -29 -28 ... 27 28 29]
# [-30 -29 -28 ... 27 28 29]
# [-30 -29 -28 ... 27 28 29]]
import matplotlib.pyplot as plt
plt.scatter(X1,X2,s=1)
plt.show()
For confirmation, I drew a two-dimensional scatter plot of X1 and X2, and it became as follows.
It is a mesh properly. Next, calculate the Z-axis value and make a three-dimensional graph. 3: Z calculated what is called the Ackley function. Formula
{f(x_{1}, x_{2})=20-20\exp \biggl( -0.2\sqrt{\frac{1}{n}\sum_{i=1}^{n}x_{i}^2} \biggr) +e-\exp \biggl(\frac{1}{n}\sum_{i=1}^{n}\cos(2\pi x_{i}) \biggr)
}
For the time being, if you plot with X1 and Z, the graph will be as follows.
4: Draw a 3D graph.
Use from mpl_toolkits.mplot3d import Axes3D
to import a library that draws 3D graphs.
ʻAx.plot_wireframe (X1, X2, Z)` makes a three-dimensional graph.
plot_surface
: Displayed as a surface.
plot_wireframe
: Displayed in wireframe.
Z =20 - 20 * np.exp(-0.2 * np.sqrt((X1 ** 2 + X2 ** 2) / 2)) + np.exp(1) - np.exp((np.cos(2 * np.pi * X1) + np.cos(2 * np.pi * X2)) / 2)
plt.scatter(X1,Z,s=1)
plt.show()
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
#Displayed in wireframe
ax.plot_wireframe(X1, X2, Z)
#Display on the surface
#ax.plot_surface(X1, X2, Z, rstride=1, cstride=1, cmap='hsv')
plt.show()
Recommended Posts