⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sa.m

📁 书籍代码:遗传演算法原理与应用_活用MATLAB(Source Code)
💻 M
字号:
% Main Program sa.m

Tinit=120; % Initial Simulated Temperature
l=0.98;  % Temperature Decrement Parameter
Tfinal=0.00001; % Final Temperature


x=-1:0.1:1; % Computation and Display of objective Function Contour
y=-1:0.1:1;
[x1,y1]=meshgrid(x,y);
z1=x1.^2+y1.^2-0.3*cos(3*pi*x1)-0.4*cos(4*pi*y1)+0.7;
figure(1);
contour(x,y,z1);

Tcur=Tinit; % Initialize Simulated Temperature
x1=-1 + 2*rand; % Select First Solution (x1, x2)
x2=-1+2*rand;
z(1)=x1^2+x2^2 - 0.3*cos(3.*pi*x1)-0.4*cos(4*pi*x2)+0.7;


r1(1)=x1;
r2(1)=x2;
i=1;

while Tcur>Tfinal % Start of Simulated Annealing Loop
   x_1=-1 + 2*rand; % Select New Solution (x_1, x_2)
   x_2=-1 + 2*rand;
   z_1=x_1^2+x_2^2 - 0.3*cos(3.*pi*x_1)-0.4*cos(4*pi*x_2)+0.7; % Evaluate New Objective Function Value

   g=exp(-((z_1-z(i))/Tcur)); % Acceptance Probability
   if ((z_1 < z(i))| (rand < g))
      x1=x_1;
      x2=x_2;
      z(i+1)=z_1;
   else
      z(i+1) = z(i);
   end
   
   Tcur=Tcur*l; % New Simulated Temperature 
   
  r1(i+1)=x1;
  r2(i+1)=x2;
  title('Search for the global optimum point');
  xlabel('x axis');
  ylabel('y axis');
  i=i+1;
end  % End of Loop
f=find(z==min(z));
fprintf(1,'The Minimum value has of the Obj. func. been observed so far  is : %f in the %d iteration\n', min(z),f(1));
fprintf(1,'x=%f,y=%f\n',r1(f(1)),r2(f(1)));

hold;
line(r1,r2);
title('Movement of x-y parameters in the search space');
xlabel('x parameter');
ylabel('y parameter');

figure(2);
plot(z);
title('Obective Function values versus Iterations');
xlabel('Iterations');
ylabel('Objective Function');

figure(3);
plot(r1);
title('Movement of the x parameter');
ylabel('x parameter');
xlabel('Iterations');

figure(4);
plot(r2);
title('Movement of the y parameter');
ylabel('y parameter');
xlabel('Iterations');



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -