千家信息网

如何使用Python和Matla实现模拟退火法

发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,这篇文章主要为大家展示了"如何使用Python和Matla实现模拟退火法",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"如何使用Python和Matla实现
千家信息网最后更新 2025年11月07日如何使用Python和Matla实现模拟退火法

这篇文章主要为大家展示了"如何使用Python和Matla实现模拟退火法",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"如何使用Python和Matla实现模拟退火法"这篇文章吧。

1 Python实现

1.1 源码实现

我在前面已经给出了模拟退火法的完整知识点和源码实现:智能优化算法—蚁群算法(Python实现)

模拟退火蒙特卡洛实验一样,全局随机,由于没有自适应的过程(例如向最优靠近、权重梯度下降等),对于复杂函数寻优,很难会找到最优解,都是近似最优解;然而像蝙蝠算法粒子群算法等有向最优逼近且通过最优最差调整参数的步骤,虽然对于下图函数易陷入局部最优,但是寻优精度相对较高。如果理解这段话应该就明白了为什么神经网络训练前如果初步寻优一组较好的网络参数,会使训练效果提高很多,也会更快达到误差精度。

1.2 sko.SA 实现

#===========1导包================import matplotlib.pyplot as pltimport pandas as pdfrom sko.SA import SA #============2定义问题===============fun = lambda x: x[0] ** 2 + (x[1] - 0.05) ** 2 + x[2] ** 2 #=========3运行模拟退火算法===========sa = SA(func=fun, x0=[1, 1, 1], T_max=1, T_min=1e-9, L=300, max_stay_counter=150)best_x, best_y = sa.run()print('best_x:', best_x, 'best_y', best_y) #=======4画出结果=======plt.plot(pd.DataFrame(sa.best_y_history).cummin(axis=0))plt.show()    #scikit-opt 还提供了三种模拟退火流派: Fast, Boltzmann, Cauchy. #===========1.1 Fast Simulated Annealing=====================from sko.SA import SAFast sa_fast = SAFast(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, q=0.99, L=300, max_stay_counter=150)sa_fast.run()print('Fast Simulated Annealing: best_x is ', sa_fast.best_x, 'best_y is ', sa_fast.best_y) #===========1.2 Fast Simulated Annealing with bounds=====================from sko.SA import SAFast sa_fast = SAFast(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, q=0.99, L=300, max_stay_counter=150,                 lb=[-1, 1, -1], ub=[2, 3, 4])sa_fast.run()print('Fast Simulated Annealing with bounds: best_x is ', sa_fast.best_x, 'best_y is ', sa_fast.best_y) #===========2.1 Boltzmann Simulated Annealing====================from sko.SA import SABoltzmann sa_boltzmann = SABoltzmann(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, q=0.99, L=300, max_stay_counter=150)sa_boltzmann.run()print('Boltzmann Simulated Annealing: best_x is ', sa_boltzmann.best_x, 'best_y is ', sa_fast.best_y) #===========2.2 Boltzmann Simulated Annealing with bounds====================from sko.SA import SABoltzmann sa_boltzmann = SABoltzmann(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, q=0.99, L=300, max_stay_counter=150,                           lb=-1, ub=[2, 3, 4])sa_boltzmann.run()print('Boltzmann Simulated Annealing with bounds: best_x is ', sa_boltzmann.best_x, 'best_y is ', sa_fast.best_y) #==================3.1 Cauchy Simulated Annealing==================from sko.SA import SACauchy sa_cauchy = SACauchy(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, q=0.99, L=300, max_stay_counter=150)sa_cauchy.run()print('Cauchy Simulated Annealing: best_x is ', sa_cauchy.best_x, 'best_y is ', sa_cauchy.best_y) #==================3.2 Cauchy Simulated Annealing with bounds==================from sko.SA import SACauchy sa_cauchy = SACauchy(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, q=0.99, L=300, max_stay_counter=150,                     lb=[-1, 1, -1], ub=[2, 3, 4])sa_cauchy.run()print('Cauchy Simulated Annealing with bounds: best_x is ', sa_cauchy.best_x, 'best_y is ', sa_cauchy.best_y)

2 Matlab实现

2.1 模拟退火法

clearclcT=1000; %初始化温度值T_min=1; %设置温度下界alpha=0.99; %温度的下降率num=1000; %颗粒总数n=2; %自变量个数sub=[-5,-5]; %自变量下限up=[5,5]; %自变量上限tufor i=1:numfor j=1:nx(i,j)=(up(j)-sub(j))*rand+sub(j);    end    fx(i,1)=fun(x(i,1),x(i,2));end %以最小化为例[bestf,a]=min(fx);bestx=x(a,:);trace(1)=bestf;while(T>T_min)for i=1:numfor j=1:n            xx(i,j)=(up(j)-sub(j))*rand+sub(j);        end        ff(i,1)=fun(xx(i,1),xx(i,2));        delta=ff(i,1)-fx(i,1);if delta<0            fx(i,1)=ff(i,1);x(i,:)=xx(i,:);else            P=exp(-delta/T);if P>rand                fx(i,1)=ff(i,1);x(i,:)=xx(i,:);            end        end      endif min(fx)
function z=fun(x,y)z = x.^2 + y.^2 - 10*cos(2*pi*x) - 10*cos(2*pi*y) + 20;
function tu[x,y] = meshgrid(-5:0.1:5,-5:0.1:5);z = x.^2 + y.^2 - 10*cos(2*pi*x) - 10*cos(2*pi*y) + 20;figuremesh(x,y,z)%建一个网格图,该网格图为三维曲面,有实色边颜色,无面颜色hold onxlabel('x')ylabel('y')zlabel('z')title('z =  x^2 + y^2 - 10*cos(2*pi*x) - 10*cos(2*pi*y) + 20')

这里有一个待尝试的想法,先用蒙特卡洛/模拟退火迭代几次全局去找最优的区域,再通过其他有向最优逼近过程的算法再进一步寻优,或许会很大程度降低产生局部最优解的概率。

下面是模拟退火和蒙特卡洛对上述函数寻优的程序,迭代次数已设为一致,可以思考下两种程序写法的效率、共同点、缺点。理论研究讲究结果好,实际应用既要保证结果好也要保证程序运算效率。

2.2 蒙特卡诺法

clearclcnum=689000; %颗粒总数n=2; %自变量个数sub=[-5,-5]; %自变量下限up=[5,5]; %自变量上限tux=zeros(num,n);fx=zeros(num,1);for i=1:numfor j=1:nx(i,j)=(up(j)-sub(j))*rand+sub(j);    end    fx(i,1)=fun(x(i,1),x(i,2));end [bestf,a]=min(fx);bestx=x(a,:); disp('最优解为:')disp(bestx)disp('最优值为:')disp(bestf)hold onplot3(bestx(1),bestx(2),bestf,'ro','LineWidth',5)

效果确实值得商榷。

以上是"如何使用Python和Matla实现模拟退火法"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

0