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

📄 de.m

📁 Differential Evolution for single variable function optimization!
💻 M
字号:
%Accepting input from user
N = input('Enter number of population(N) = ');
G = input('Ener number of Generation(iteration)(G)  = ');
CR  = input('Enter crossover probability(CR) = ');
F = input ('Enter mutuation constant(F) = ');

% Defining Optimaization function
f=inline('exp(-2*log(2)*((m-0.1)/0.8)^2)*abs(sin(5*pi*m))', 'm');

% Random generation of initial population
for k = 1:N
    x(k) = -1 + rand(1)*2;
end
% Initializing best of the group from the random generated initial population
best = [1:N+1];
gbest = [1:G];
for q = 1:N
ibest(q)= f(x(q));
end
[w,y ] = max(ibest);
best(1) = x(y);
% Iteration for optimization untill the specified generation number
 for g = 1:G     
      for i = 1:N
           % three random numbers assuring(r1,r2,r3 and i are not equal)
         r1 = floor((N-1)*rand(1) + 1);
         r2 = floor((N-1)*rand(1) + 1);
         r3 = floor((N-1)*rand(1) + 1);
        while (r1 == r2 ) | (r2 == r3) | (r1 == r3) |(r1==i) | (r2==i)|(r3==i)
         r1 = floor((N-1)*rand(1) + 1);
         r2 = floor((N-1)*rand(1) + 1);
         r3 = floor((N-1)*rand(1) + 1);
        end
       % Mutuation and Crossover also Recombination
         if rand(1)<CR 
             x(i) = x(r3) + F* (x(r1)-x(r2));
             % Penality function
             if x(i)>1
                 x(i) = x(i)-1;
             elseif x(i)<-1
                 x(i) = x(i)+1;
             end
         else
         x(i) = x(i);
         end  
      end
       
     %Selection
     for i = 1:N    
         if f(best(i))<= f(x(i)) 
             best(i+1) = x(i);
         else
             best(i+1)= best(i);
         end
         
         
     end
     gbest(g) = best(N+1);
     best(1)= best(N+1);
     % Display Result for each generation
      disp(['Genration: ', num2str(g)]);
         disp(['xbest: ', num2str(gbest(g))]);
         disp(['f(best): ', num2str(f(gbest(g)))]); 
         
 end
 hold on
 ezplot('m', 'exp(-2*log(2)*((m-0.1)/0.8)^2)*abs(sin(5*pi*m))',[-1, 1] );
 axis([-1 1 0 1])
 plot(gbest(G), f(gbest(G)), 'MarkerFaceColor','g',...
                              'MarkerSize',5)
 hold off
 
  clear

 
 %hold on

 %axis([-1 1 0 1]);
 %plot(G+1, f(xbest(G+1)));
 %hold off
 
         

⌨️ 快捷键说明

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