1 模拟退火算法简介
模拟退火算法是一种启发式算法。 通过不断的迭代当前解在周围找到问题的最优解。( 个人理解是在简单的爬坡法加上了概率的因素,防止陷入局部最优解。) 完整的模拟退火过程是模拟热力学的退火过程,随着温度的不断下降(本质上是随着迭代次数的增多,发生跳转的概率越来越小的过程),算法将在多项式的时间内找到问题的近似全局最优解(有可能还是局部最优解)。
2 算法流程
- 初始化温度
和每个
时,进行迭代次数的L, 时间衰减率
, 初始化一个解
;
-
-
- 计算函数值
;
- 在
周围随机试探产生新的
,
;
-
与
作比较
-
比
更接近目标函数值
-
-
- 以一定的概率接受
, 条件式
- t = t *
这里面与爬坡法最大的区别是在算法步骤10中, 当不满足爬坡条件的时候,仍然以一定的概率接受接受到新值;当温度较高的时候概率越大,也就更容易跳出局部极值点,而随着温度慢慢下降,慢慢的也收敛到全局极值点。
3 python代码实现
import numpy as np
import matplotlib.pyplot as plt
import math
def function(x):
return -(10*math.sin(5*x)+7*math.cos(4*x))
initT = 1000
minT = 1
iterL = 1000
eta = 0.95
k = 1
x0 = 1.5#10*(2*np.random.rand()-1)
t = initT
print("init solution :",x0)
yy=[]
xx = np.linspace(0,10,300)
for i in range(len(xx)):
yy.append(function(xx[i]))
plt.figure()
plt.plot(xx,yy)
plt.plot(x0,function(x0),'o')
x_old = x0
while t > minT:
for i in range(iterL):#MonteCarlo method reject propblity
value_old = function(x_old)
x_new = x_old+np.random.rand()-0.5
if x_new>=0 and x_new<=10:
value_new = function(x_new)
res = value_new-value_old
if res<0 or np.exp(-res/(k*t))>np.random.rand():
x_old = x_new
t = t*eta
print("最优解: ",x_old)
print("最优值:",function(x_old))
plt.plot(x_old,function(x_old),'or')
plt.show()