模擬退火算法求函數極值(含MATLAB代碼實現)

二、模擬退火算法
1. 簡介
     模擬退火算法的思想借鑑於固體的退火過程,當固體的溫度很高時,內能比較大,固體內的粒子處於快速無序運動狀態,當溫度慢慢降低,固體的內能減小,粒子逐漸趨於有序,最終固體處於常溫狀態,內能達到最小,此時粒子最爲穩定。

     白話理解:一開始爲算法設定一個較高的值T(模擬溫度),算法不穩定,選擇當前較差解的概率很大;隨着T的減小,算法趨於穩定,選擇較差解的概率減小,最後,T降至終止迭代的條件,得到近似最優解。

2.算法思想及步驟

   (1)設置算法的參數:初始溫度,結束溫度,溫度衰減係數,每個溫度下的擾動次數,初始狀態,初始解

   (2)對狀態產生擾動,計算新狀態下的解,比較兩個解的大小,判斷是否接受新的狀態

   (3)在此溫度下,對步驟(2)按設置的擾動次數重複進行擾動

   (4)對溫度進行衰減,並在新的溫度下重複(2)(3),直到結束溫度

   (5)輸出記錄最優狀態和最優解,算法結束

                                                

      

其中,P爲算法選擇較差解的概率;T 爲溫度的模擬參數;。

當T很大時,,此時算法以較大概率選擇非當前最優解;
P的值隨着T的減小而減小;
當時,,此時算法幾乎只選擇最優解,等同於貪心算法。
 

%VELASCO, Gimel David F.
%2012-58922
%Cmsc 191
%Simulated Annealing
%Final Exam
%for runs=1:3
clear;
tic;

%%%%%%%%%%%%%%%%%%%%%%%%%%%INPUT ARGUMENTS%%%%%%%Sir Joel, dito po%%%%%%%%%
CostF = 2; % | 1 - DE JONGS | 2 - AXIS PARALLEL HYPER-ELLIPSOID | 3 - ROTATED HYPER-ELLIPSOID | 4 - RASTRIGINS | ow - ACKLEYS |
nVar = 3; %染色體的等位基因數目,染色體長度
VarSize = zeros(nVar);
VarMin = -5.12; %upper bound of variable value
VarMax = 5.12; %lower bound of variable value
MaxIt = 100000;%最大迭代次數
T0 = 100;%初始溫度
Tf = 0.000000000000001;%最終溫度
alpha = 0.7;%溫度下降率
%nPop = 10000;
nMove = 10000;
mu = 0.1;
%sigma = 0.25;
%%
%初始化
test_func = CostF;  %sets the number of w/c test function to be solved
ub = VarMax;
lb = VarMin;
ulb = ub;        %upper and lower bound
tpl = nVar;      %dimensions
x_sol = 2*ulb*(rand(1,tpl)-0.5);               %initial guess of the solution x
cooling_ratio = alpha;                    %sets the cooling ratio to 0.8  i.e. 0.7 < 0.8 < 0.9
num_neigh = nMove;                      %initializes the size of the random neighbors
cooling_sched = zeros(1);               %pre-allocation for speed
cooling_sched(1) = T0;                 %initializes the cooling schedule T0
iteration_array = zeros(1);
fittest_array = zeros(1);
solution_array = VarSize;

%%%%%%%%%%%%%%%%%%%%%%%%%SIMULATED ANNEALING%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sched = 1;                                  %index
while cooling_sched(sched) > Tf     %終止條件
    T = cooling_sched(sched);               %sets the value of the temperature T
    for j=1:num_neigh
        r  = (cooling_ratio)^sched;             %is used so that the randomness of selecting a neighbor becomes narrower
        x_tmp = 2*ulb*r*(rand(1,tpl)-0.5);    %隨機選擇一個鄰居進行比較
        if OBJFUNC(x_tmp,tpl,test_func) < OBJFUNC(x_sol,tpl,test_func)  %比較找最優的值
            x_sol = x_tmp;
        elseif OBJFUNC(x_tmp,tpl,test_func) > OBJFUNC(x_sol,tpl,test_func)  %if not, change the solution if it is lucky
            delta = OBJFUNC(x_tmp,tpl,test_func) - OBJFUNC(x_sol,tpl,test_func);
            p = P(delta,T);
            q = rand(1);
            if q <= p
                x_sol = x_tmp; 
            end
        end
    end

	fittest_array(sched) = OBJFUNC(x_sol,tpl,test_func);
	iteration_array(sched) = sched;
	solution_array(sched,:) = x_sol;

    cooling_sched(sched+1) = T*(cooling_ratio)^sched;
    sched = sched+1;
    if sched > MaxIt
    	break;
    end
end
%SOLUTION
if test_func == 1
fprintf('====================DE JONGS FUNCTION=============================\n');
elseif test_func == 2
fprintf('==============AXIS PARALLEL HYPER-ELLIPSOID FUNCTION==============\n');
elseif test_func == 3
fprintf('===============ROTATED HYPER-ELLIPSOID FUNCTION====================\n');
elseif test_func == 4
fprintf('====================RASTRIGINS FUNCTION===========================\n');
else
fprintf('=====================ACKLEYS FUNCTION=============================\n');
end
fprintf('==================SIMULATED ANNEALING=============================\n');
fprintf('With the Objective Function Value of %.16f\nTotal Runtime of %f seconds\nAnd Final Cooling Temperature of %.16f\n',OBJFUNC(x_sol,tpl,test_func),toc,cooling_sched(sched));
fprintf('The Root for Test Function %d is\n',test_func);
disp(x_sol)
fprintf('==================================================================\n');

figure
subplot(2,1,1);
plot(iteration_array,fittest_array);
legend('Cost Function Value');
xlabel('Generation');
ylabel('Fitness of fittest Chromosome');

solution_array = transpose(solution_array);
subplot(2,1,2);
plot(iteration_array,solution_array);
xlabel('Generation');
ylabel('Solution of fittest Chromosome');
%end

結果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章