国科大中科院算法讲义 模拟退火
- 格式:ppt
- 大小:870.50 KB
- 文档页数:45


模拟退⽕算法模拟退⽕(SA)物理过程由以下三个部分组成1.加温过程问题的初始解2.等温过程对应算法的Metropolis抽样的过程3.冷却过程控制参数的下降默认的模拟退⽕是⼀个求最⼩值的过程,其中Metropolis准则是SA算法收敛于全局最优解的关键所在,Metropolis准则以⼀定的概率接受恶化解,这样就使算法跳离局部最优的陷进1.模拟退⽕算法求解⼀元函数最值问题使⽤simulannealbnd - Simulated annealing algorithm⼯具箱求y=sin(10*pi*x)./x;在[1,2]的最值下图是⽤画图法求出最值的x=1:0.01:2;y=sin(10*pi*x)./x;figurehold onplot(x,y,'linewidth',1.5);ylim([-1.5,1.5]);xlabel('x');ylabel('y');title('y=sin(10*\pi*x)/x');[maxVal,maxIndex]=max(y);plot(x(maxIndex),maxVal,'r*');text(x(maxIndex),maxVal,{['x:' num2str(x(maxIndex))],['y:' num2str(maxVal)]});[minVal,minIndex]=min(y);plot(x(minIndex),minVal,'ro');text(x(minIndex),minVal,{['x:' num2str(x(minIndex))],['y:' num2str(minVal)]});hold off;⽤模拟退⽕⼯具箱来找最值求最⼩值function fitness=fitnessfun(x)fitness=sin(10*pi*x)./x;end求最⼤值function fitness=fitnessfun(x)fitness=-sin(10*pi*x)./x;endOptimization running.Objective function value: -0.9527670052175917Maximum number of iterations exceeded: increase options.MaxIterations.⽤⼯具箱求得的最⼤值为0.95276700521759172.⼆元函数优化[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');maxVal=max(z(:));[maxIndexX,maxIndexY]=find(z==maxVal);%返回z==maxVal时,x和y的索引for i=1:length(maxIndexX)plot3(x(maxIndexX(i),maxIndexY(i)),y(maxIndexX(i),maxIndexY(i)),maxVal,'r*');text(x(maxIndexX(i),maxIndexY(i)),y(maxIndexX(i),maxIndexY(i)),maxVal,{['x:' num2str(x(maxIndexX(i)))] ['y:' num2str(y(maxIndexY(i)))] ['z:' num2str(maxVal)] }); endhold off;function fitness=fitnessfun(x)fitness=-(x(1).^2+x(2).^2-10*cos(2*pi*x(1))-10*cos(2*pi*x(2))+20);endOptimization running.Objective function value: -80.50038894455415Maximum number of iterations exceeded: increase options.MaxIterations.找到的最⼤值:80.500388944554153.解TSP问题(⽤的数据和前⼏天⽤遗传算法写TSP问题的数据⼀致,但是结果⽐遗传算法算出来效果差很多,不知道是不是我写错了,怀疑⼈⽣_(:з」∠)_中。