Simulated annealing algorithm is a general optimization algorithm , In theory, the algorithm has probabilistic global optimization performance , At present, it has been widely used in engineering , Such as machine learning 、 neural network 、 Signal processing and other fields .
The idea of simulated annealing algorithm borrows from the annealing principle of solids , When the temperature of the solid is very high , Internal energy is relatively large , The internal particles of a solid are in rapid disordered motion , When the temperature decreases slowly , The internal energy of a solid decreases , The of particles slowly tends to order , Final , When the solid is at room temperature , The internal energy can reach the minimum , here , Particles are the most stable . Simulated annealing algorithm is designed based on this principle .
Simulated annealing algorithm starts from a certain high temperature , Calculate the initial solution at high temperature , Then a disturbance is generated by the preset neighborhood function , So as to get a new state , That is to simulate the disordered motion of particles , Compare the energy in the new and old States , That is, the solution of the objective function . If the energy of the new state is less than that of the old state , Then the state changes ; If the energy of the new state is greater than that of the old state , Then the transformation takes place with a certain probability criterion . When the state is stable , It can be regarded as reaching the optimal solution of the current state , You can start cooling , Continue iteration at the next temperature , Finally, it reaches a stable state of low temperature , The result of simulated annealing algorithm is obtained .
According to the above description , It can be concluded that the calculation steps of simulated annealing algorithm are as follows :
import numpy as np
import matplotlib.pyplot as plt
# Build the objective function
def aimFunc(x):
y = 11 * np.sin(x) + 7*np.cos(5*x)
return y
# Show the curve of the objective function in the interval to be solved
x = np.arange(-3, 3, 0.01)
y = aimFunc(x)
plt.plot(x, y)
plt.show()
# Build new location
def new_x(x, T, x_l, x_h):
# Calculate the new location
_x_new = x + np.random.uniform(-1, 1) * T
# If the new position exceeds the upper 、 Lower limit , Readjust the position
if(_x_new < x_l):
_x_new = x - np.random.uniform(0, 1)*(x-x_l)
if(_x_new > x_h):
_x_new = x + np.random.uniform(0, 1)*(x_h-x)
return _x_new
# Definition x The scope of the
x_l = -3
x_h = 3
# Define the initial temperature
T = 1000
# Define the number of internal cycles
N = 20
# Define the initial position
_x = np.random.uniform(low=x_l, high=x_h)
_y = aimFunc(_x)
# Define the recorder
_x_track = []
_y_track = []
while T >= 0.001:
for i in range(N):
# Get a new location
_x_new = new_x(_x, T, x_l, x_h)
# Calculate the function value corresponding to the new position
_y_new = aimFunc(_x_new)
# Decide whether to accept the new position
flag_1 = _y_new < _y
flag_2 = np.exp(-(_y_new-_y)/T) > np.random.uniform()
if(flag_1 | flag_2):
_x = _x_new
_y = _y_new
_x_track.append(_x)
_y_track.append(_y)
# Update temperature
T = T * 0.8
# Show the calculation results
print(_x)
print(_y)
plt.plot(_x_track)
plt.show()
plt.plot(_y_track)
plt.show()
plt.plot(x, y)
plt.scatter(_x, _y, c='r')
plt.show()
Simulated annealing algorithm has the following three parameter adjustment problems :