1. 先看看对SA的语言描述
一句话来概括模拟退火(Simulated Annealing,SA)的核心思想:
适当的接受不好的解。
为什么需要”
适当的“接受坏的解?在迭代的过程中,每迭代一次,便会产生新的解,而新的解就可解的新的适应度值,若新的适应度值相比原适应度值小(以求最小值为例),则此时必定接受好的解。但是坏的解也是很多的,如果每次都完全接受坏的解,那么就是”完全随机“了,其收敛速度是及其缓慢的;那么完全的不接受坏的解也是不好的,其结果为极易陷入到局部极小点中去,那么SA就不是”全局优化算法“了,所以我们要”
适当的“接受坏的解来在两个极端之间进行平衡。
什么样的接收规则是”适当的“?这里以初始版本为例,p = exp(-δf/KT) > r ,r为0到1之间的随机数;K为Boltzmann常数;δf为前后两代的适应度值之差。这样随机的接收看起来不错。需要说明的是,此处的T是对着迭代次数呈指数下降的:T->αT,α∈(0,1)。这里还有一些常数(K,α)和初始值(T)需要确定,但是这些都是和具体问题相关的,可以看看后面的示例是如何选取的,这些个参数的初始值对待优化问题有很大的影响。
2. 看完描述再看看更接近代码形式的描述:
看完上面的伪代码:如果我们要去改进SA,可以从以下方面可以入手:
1,how to move randomly to new locations
2,p的产生策略,即更改如何的”适当的“策略
3,温度T的下降策略,即α的改变
3. 看完接近代码形式的描述再看看真代码的实现:
sa_simpledemo.m
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
|
% Find the minimum of a function by Simulated Annealing
% Programmed by X S Yang (Cambridge University) % Usage: sa_simpledemo %%%%%%%%%%%%%%%%%%%%%%显示实验信息%%%%%%%%%%%%%%%%%%%%%% disp('Simulating ... it will take a minute or so!'); %%%%%%%%%%%%%%%%%%%%%%适应度函数及其图形%%%%%%%%%%%%%%%%%%%%%% % Rosenbrock test function with f*=0 at (1,1) fstr='(1-x)^2+100*(y-x^2)^2'; % Convert into an inline function f=vectorize(inline(fstr)); % Show the topography of the objective function range=[-2 2 -2 2]; xgrid=range(1):0.1:range(2); ygrid=range(3):0.1:range(4); [x,y]=meshgrid(xgrid,ygrid); surfc(x,y,f(x,y)); %%%%%%%%%%%%%%%%%%%%%%初始化SA参数%%%%%%%%%%%%%%%%%%%%%% % Initializing parameters and settings T_init = 1.0; % initial temperature T_min = 1e-10; % finial stopping temperature % (eg., T_min=1e-10) F_min = -1e+100; % Min value of the function max_rej=500; % Maximum number of rejections max_run=100; % Maximum number of runs max_accept = 15; % Maximum number of accept k = 1; % Boltzmann constant alpha=0.9; % Cooling factor Enorm=1e-5; % Energy norm (eg, Enorm=1e-8) guess=[2 2]; % Initial guess % Initializing the counters i,j etc i= 0; j = 0; accept = 0; totaleval = 0; % Initializing various values T = T_init; E_init = f(guess(1),guess(2)); E_old = E_init; E_new=E_old; best=guess; % initially guessed values %%%%%%%%%%%%%%%%%%%%%%SA迭代过程%%%%%%%%%%%%%%%%%%%%%% % Starting the simulated annealling while ((T > T_min) & (j <= max_rej) & E_new>F_min) i = i+1; % Check if max numbers of run/accept are met if (i >= max_run) | (accept >= max_accept) % Cooling according to a cooling schedule T = alpha*T; totaleval = totaleval + i; % reset the counters i = 1; accept = 1; end % Function evaluations at new locations ns=guess+rand(1,2)*randn; E_new = f(ns(1),ns(2)); % Decide to accept the new solution DeltaE=E_new-E_old; % Accept if improved if (-DeltaE > Enorm) best = ns; E_old = E_new; accept=accept+1; j = 0; end % Accept with a small probability if not improved if (DeltaE<=Enorm & exp(-DeltaE/(k*T))>rand ); best = ns; E_old = E_new; accept=accept+1; else j=j+1; end % Update the estimated optimal solution f_opt=E_old; end %%%%%%%%%%%%%%%%%%%%%%显示实验结果%%%%%%%%%%%%%%%%%%%%%% % Display the final results disp(strcat('Obj function :',fstr)); disp(strcat('Evaluations :', num2str(totaleval))); disp(strcat('Best location:', num2str(best))); disp(strcat('Best estimate:', num2str(f_opt))); |
实验结果:
参考资料:
Introduction to Mathematical Optimization (Xin-She Yang)