Optimization algorithms play an important role in various fields, including machine learning, engineering, and finance. Differential Evolution (DE) is a powerful optimization algorithm designed for global optimization of real-valued functions.
In this blog post, we'll explore the basics of Differential Evolution and demonstrate its application on a specific function using the SciPy differential_evolution() function in Python.
The tutorial covers:
- Understanding the problem
- Differential Evolution implementation
- Conclusion
- Source code listing
We'll start by loading the required libraries.
import numpy as np
from scipy.optimize import differential_evolution
import matplotlib.pyplot as plt
from matplotlib import cm
# define ranges
x_range = np.arange(-4, 4, 0.1)
y_range = np.arange(-4, 4, 0.1)
# create meshgrid
x, y = np.meshgrid(x_range, y_range)
# define the function
z = np.sqrt(np.sqrt(x**2+y**2))
# Plot the surface.
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
surf = ax.plot_surface(x, y, z, cmap=cm.jet,
linewidth=0, antialiased=False)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
# define function
def func(p):
x, y = p
r = np.sqrt(x**2+y**2)
return np.sqrt(r)
We'll set variable bounds that can be specified by defining the max and min values.
bounds = [[-4, 4], [-4, 4]]
Next, execute the differential evolution function with the given objective function and bounds.
# execute differential evolution search
result = differential_evolution(func, bounds)
print(result)
fun: 0.0
message: 'Optimization terminated successfully.'
nfev: 3093
nit: 100
success: True
x: array([0., 0.])
The result shows that minimum of the function is located in a point of (0, 0).
Here, result contains the following attributes:
To print evaluated function at every iteration, we'll set true to the 'disp' parameter.
result = differential_evolution(func, bounds, disp=True)
differential_evolution step 1: f(x)= 0.681272
differential_evolution step 2: f(x)= 0.457103
differential_evolution step 3: f(x)= 0.457103
differential_evolution step 4: f(x)= 0.301956
differential_evolution step 5: f(x)= 0.301956
...
differential_evolution step 83: f(x)= 5.01213e-08
differential_evolution step 84: f(x)= 3.54411e-08
differential_evolution step 85: f(x)= 2.98023e-08
differential_evolution step 86: f(x)= 0
differential_evolution step 87: f(x)= 0
differential_evolution step 88: f(x)= 0
differential_evolution step 89: f(x)= 0
differential_evolution step 90: f(x)= 0
differential_evolution step 91: f(x)= 0
differential_evolution step 92: f(x)= 0
differential_evolution step 93: f(x)= 0
# Scatter plot the optimal point
optimal_point = result.x
z_value = func(optimal_point)
ax.scatter(optimal_point[0], optimal_point[1], z_value,
color='red', marker='o', s=70, label='Optimal Point')
fig.colorbar(surf, shrink=0.5, aspect=5)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.legend()
plt.show()
import numpy as np from scipy.optimize import differential_evolution import matplotlib.pyplot as plt from matplotlib import cm # define ranges x_range = np.arange(-4, 4, 0.1) y_range = np.arange(-4, 4, 0.1) # create meshgrid x, y = np.meshgrid(x_range, y_range) # define the function z = np.sqrt(np.sqrt(x**2 + y**2)) # plot the surface fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) surf = ax.plot_surface(x, y, z, cmap=cm.jet, linewidth=0, antialiased=False) # set bounds bounds = [[-4, 4], [-4, 4]] # define function def func(p): x, y = p r = np.sqrt(x**2 + y**2) return r # execute differential evolution search result = differential_evolution(func, bounds, disp=True) print(result) # Scatter plot the optimal point optimal_point = result.x z_value = func(optimal_point) ax.scatter(optimal_point[0], optimal_point[1], z_value, color='red',
marker='o', s=70, label='Optimal Point')
fig.colorbar(surf, shrink=0.5, aspect=5)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.legend()
plt.show()
No comments:
Post a Comment