Mathematical Modeling Learning (9): Simulated Annealing Algorithm

The idea of ​​simulated annealing algorithm (Simulated Annealing, SA) is based on the annealing principle of solid. When the temperature of the solid is high, the internal energy
is relatively . When the temperature slowly decreases In the process, the internal energy of the solid decreases, and the particles gradually become orderly. Finally
, when the solid is at room temperature, the internal energy reaches the minimum, and at this time, the particles are the most stable. The simulated annealing algorithm is designed based on this principle.

insert image description hereSimulated annealing algorithm process
(1) randomly select a unit k, and give it a random displacement, and find out the energy
change ΔEk of the system due to it.
(2) If ΔEk⩽ 0, the displacement can be adopted, and the changed system state can be used as the starting point for the next change; if
ΔEk>0, the probability that the displaced state can be adopted is the formula where
insert image description here
T is the temperature, and then from ( 0, 1) Choose a number R from the uniformly distributed random numbers in the interval. If R<Pk, the
state after the change is used as the starting point of the next time; otherwise, the state before the change is used as the starting point of the next time.
(3) Go to step (1) and continue until the balance state is reached.
insert image description here
Use Simulated Annealing Algorithm Toolbox to solve the problem:

%%
clc;clear;
%%普通的目标函数
fun = @dejong5fcn %目标函数
%[x,fval] = simulannealbnd(fun,[0,0])%[0,0]凭经验猜测的初始值,没有的话,随意写就行
options = saoptimset('PlotFcns',{
    
    @saplotbestx,@saplotbestf,@saplotx,@saplotf})
x0 = [0,0];
lb = [-64,-64];%下限
ub = [64,64];%下限
[x,fval] = simulannealbnd(fun,x0,lb,ub,options);
%%
求:
% min f(x) = (4 - 2.1*x1^2 + x1^4/3)*x1^2 + x1*x2 + (-4 + 4*x2^2)*x2^2;
% 写成函数形式
% function y = simple_objective(x)
%    y = (4 - 2.1*x(1)^2 + x(1)^4/3)*x(1)^2 + x(1)*x(2) + (-4 + 4*x(2)^2)*x(2)^2;
%%
fun = @simple_objective;%注意需要将其放在最前面
X0 = [0.5 0.5];   % 初始点
lb = [-64 -64];
ub = [64 64];
[x,fval,exitFlag,output] = simulannealbnd(fun,X0,lb,ub);
fprintf('The number of iterations was : %d\n', output.iterations);
fprintf('The number of function evaluations was : %d\n', output.funccount);
fprintf('The best function value found was : %g\n', fval);
%%

%  求:
% min f(x) = (a - b*x1^2 + x1^4/3)*x1^2 + x1*x2 + (-c + c*x2^2)*x2^2;
% 
% 写成函数形式
% function y = parameterized_objective(x,a,b,c)
%    y = (a - b*x(1)^2 + x(1)^4/3)*x(1)^2 + x(1)*x(2) + (-c + c*x(2)^2)*x(2)^2;
%%带有常数的目标函数
a = 4; b = 2.1; c = 4;    % define constant values
fun = @(x) parameterized_objective(x,a,b,c);
X0 = [0.5 0.5];
options = saoptimset('PlotFcns',{
    
    @saplotbestx,@saplotbestf,@saplotx,@saplotf})
[x,fval] = simulannealbnd(fun,X0,options)
%自定义目标函数1
function y = parameterized_objective(x,a,b,c)
   y = (a - b*x(1)^2 + x(1)^4/3)*x(1)^2 + x(1)*x(2) + (-c + c*x(2)^2)*x(2)^2;
end
%自定义目标函数2
function y = simple_objective(x)
    y = (4 - 2.1*x(1)^2 + x(1)^4/3)*x(1)^2 + x(1)*x(2) + (-4 + 4*x(2)^2)*x(2)^2;
end

running result
insert image description here

Guess you like

Origin blog.csdn.net/qq_60498436/article/details/132143719