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

📄 optd1.m

📁 以Matlab为平台设计一个用于寻求多峰值函数峰值点的软件对一元及二元多峰值函数的优化;
💻 M
字号:
function [Ab] = optd1(ts,N,Nc,beta,gen)

% function [M] = optd1(ts,N,Nc,beta,gen)
% M     -> matrix of memory cells
% ts    -> suppression threshold
% N     -> clone number multiplier
% Nc    -> no. of clones to be generated
% beta  -> decay of the inverse exponential function
% gen   -> maximum number of generations
%

% Function to be Optimized 
clc;
ts = 0.1; N = 20;	Nc = 10; beta = 100;gen = 2000;
f='exp(-2.0*log(2).*(x-0.1).^2/0.64).*(sin(5.*pi*x).^6)';
%f='(sin(5*pi*(x.^0.75-0.05))).^6';
% Parameters for Ploting the Affinity Landscape
xmin = 0; xmax = 1; x=xmin:0.001:xmax;
vxp = x;
vyp = eval(f);

% Initial Random Population Within the Intervals (xmin/xmax; ymin/ymax)
Ab = xmin + rand(N,1).*(xmax - xmin);
x = Ab;
fit = eval(f);
figure(1); 
imprime(f,1,vxp,vyp,x,fit,1,1);title('Initial Population');

% CHECK POSSIBILITY OF USING THE MST TO DETERMINE THE NUMBER OF OPTIMUM ...
   
it = 0; Nold = N + 1; Nsup = N;
FLAG = 0; FLAGERROR = 0;
avfitold = mean(fit); avfit = avfitold-1;
vout = []; vavfit = []; vN = [];
% Main Loop
while it < gen & FLAG == 0,
   % Reproduction (Cloning), Affinity Maturation, and Selection Within Each Clone
     [Ab] = clone_mut_select(Ab,Nc,beta,norma(fit),xmin,xmax,f);     
   % Immune Network Interactions After a Number of Iterations
   if rem(it,5) == 0,
      if abs(1-avfitold/avfit) < .001,
         [Ab] = suppress(Ab,ts);
         FLAGERROR = 1;
         Nsupold = Nsup; Nsup = size(Ab,1); vN = [vN,Nsup];
         % Convergence Criterion
         if (Nsupold-Nsup) == 0, % & rem(it,20) == 0,
            FLAG = 1; FLAGERROR = 0;
         end;
      end;
   end;
   
   % Insert randomly generated individuals
   if FLAGERROR == 1,
      d = round(.4*N);
      Ab1 = xmin + rand(d,1).*(xmax - xmin);
      Ab=[Ab;Ab1];
      FLAGERROR = 0;
   end;
      
   % Evaluating Fitness
  
   x = Ab;
   fit = eval(f); avfitold = avfit;
   [out,I] = max(fit); avfit = mean(fit);
   
   % Ploting Results
   imprime(f,1,vxp,vyp,x,fit,it,10);
   N = size(Ab,1);

   it = it + 1; vout = [vout,out]; vavfit = [vavfit,avfit]; % vN = [vN,N];
   disp(sprintf('It: %d	Max: %f	Av: %f	Net size: %d',it,out,avfit,N));
end;
imprime2(f,1,vxp,vyp,x,fit,1,1);
figure(2); plot(vout); hold on; plot(vavfit,'-.'); title('Fitness'); hold off;
figure(3); plot(vN); title('N');

disp(sprintf('找到以下%d个极值点:',N));
ymax=-1000;imax=-1;xmax=-1000;
for i=1:N,
   x = Ab(i); 
   y = eval(f);
   if y>ymax,
       imax=i;xmax=x;ymax=y;
   end;
disp(sprintf('f(%f)=%f',x,y));
end;

disp(sprintf('其中,最大值f(%f)=%f',xmax,ymax));

% ------------------- %
% SECONDARY FUNCTIONS %
% ------------------- %

function [C] = clone_mut_select(Ab,Nc,beta,fitin,xmin,xmax,f);
% C   -> matrix of clones
% g   -> vector with Gaussian mutation
% Ab  -> matrix of antibodies
% N   -> cardinality of Ab
% Nc  -> number of clones for each candidate
[N,L] = size(Ab);
C = [];
for i=1:N,
   vones = ones(Nc,1);
   Cc = vones * Ab(i,:);
   g = (randn(Nc,L)./beta) .* exp(-fitin(i));
   g(1,:) = zeros(1,L);	% Keep one previous individual for each clone unmutated
   c = Cc + g;
   % Keeps all elements of the population within the allowed bounds
   Ixmin = find(c(:,1) < xmin); Ixmax = find(c(:,1) > xmax);
  % Iymin = find(c(:,2) < ymin); Iymax = find(c(:,2) > ymax);
   if ~isempty(Ixmin),
      c(Ixmin,1) = Cc(length(Ixmin),1);
   end;
   if ~isempty(Ixmax),
      c(Ixmax,1) = Cc(length(Ixmax),1);
   end;
  
   x=c;
   fit = eval(f);
   [out,I] = max(fit);
   C = [C;c(I,:)];  % C contains only the best individuals of each clone
end;

% Function suppress self-recognizing and non-stimulated Ab from Memory (M)
function [M] = suppress(M,ts);
% M   -> memory matrix
% D1  -> idiotypic affinity matrix
D1 = dist(M,M');
aux = triu(D1,1);
[Is,Js] = find(aux>0 & aux<ts);
if ~isempty(Is),
   Is = ver_eq(Is);
   M = extract(M,Is);
   % D1 = extract(D1,Is);
end;


% Search for repeated indexes
function [Is] = ver_eq(I);
l = length(I); Is = [];
if l > 1,
   for i=1:l-1,
      aux = I(i);
      auxI = I(i+1:end);
      el = find(auxI == aux);
      if isempty(el),
         Is = [Is,aux];
      end;
   end;
   Is = [Is,I(end)];
else,
   Is = I;
end;

% Function Extracts lines from M indexed by I
function [M] = extract(M,I);
Maux = zeros(size(M));
Maux(I,:) = M(I,:);
M = M - Maux;
[I] = find(M(:,1)~=0);
M = M(I,:);

% Function normalizes matrix over [0,1]
function [Dn] = norma(D);
% Dn  -> normalized vector over [0,1]
[np,ni] = size(D);
if ni == 1,
   Dn = (D - min(D))./(max(D)-min(D));
else,
   vmaxD = max(D); vminD = min(D);
   for i=1:ni,
      Dn(:,i) = (D(:,i) - vminD(i))./(vmaxD(i)-vminD(i));
   end;
end;
% End Function NORMA

% Print Affinity Landscape and Population of Individuals
function [] = imprime(f,PRINT,vx,vy,x,fx,it,mit);
% x,fx				-> current values
% vxplot, vplot	-> original (base) function
if PRINT == 1,
   if rem(it,mit) == 0,
      hold on; 
      fplot(f,[0 1 0 1],'k-')
      xlabel('x');  ylabel('f(x)');
      plot(x,fx,'k*'); drawnow; hold off; % pause
   end;
end;

function [] = imprime2(f,PRINT,vx,vy,x,fx,it,mit);
% x,fx				-> current values
% vxplot, vplot	-> original (base) function
if PRINT == 1,
   if rem(it,mit) == 0,
      hold on; 
      fplot(f,[0 1 0 1],'k-')
      xlabel('x');  ylabel('f(x)');
      plot(x,fx,'ro'); drawnow; hold off; % pause
   end;
end;

⌨️ 快捷键说明

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