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

📄 ainet_sig2.m

📁 This manual describes how to run the Matlab&reg Artificial Immune Systems tutorial presentation deve
💻 M
字号:
function [M,S,sig] = ainet_sig2(Ag,ts,n,N,gen,qi,tp,mi,sc)

%
% Ph.D. Thesis
% Leandro Nunes de Castro
% February, 2000
% Artificial Immune Network (aiNet) - Description in aiNet.doc
% Secondary Functions: RUN_INET, DIST, CENTROID
% Internal functions: CLONE, SUPPRESS, VER_EQ, EXTRACT, PLOTVET1, DRAW_NET, NORMA
%
% function [M,D] = ainet(Ag,ts,n,N,gen,qi,tp,mi,sc)
% M     -> memory cells matrix
% D     -> distance matrix for M
% Ag    -> antigens (training patterns)
% ts    -> suppression threshold (default: 0.001)
% n     -> no. of best-matching cells taken for each Ag (Selection)
% N     -> clone number multiplier
% gen   -> maximum number of generations
% qi    -> percentile amount of clones to be Re-selected
% tp    -> pruning threshold
% mi    -> learning (hypermutation) rate (default: 4.0)
% sc	  -> stopping criterion
%
% L     -> Ag and Ab length
% N1    -> no. of antibodies (constructive)
% N2    -> no. of antigens
% Nc    -> no. of clones to be generated
% D     -> Ag-Ab affinity vector
% Do    -> D sorted in ascending order
% D1    -> idiotypic affinity matrix [N1,N1]
% vbD   -> vector best affinity for each Ag
% nR    -> no. of Ab to be re-selected
%

[N2,L] = size(Ag);
if nargin < 2,
   n = 4; N = 5; gen = 5; mi = 4.0; 
   qi = 0.2; tp = 1.0;
   ts = 0.05; sc = 0.01;
end;
N1 = 10; Ab = rand(N1,L); Agt = rand(N1,L); 

disp(sprintf('Limiar de supress鉶: %.2f',ts));
disp(sprintf('Limiar de poda: %.2f',tp/100));
disp(sprintf('N鷐ero n de melhores candidatos a serem selecionados: %d',n));
disp(sprintf('N鷐ero percentual de candidatos a serem re-selecionados: %d%%',100*qi));
disp(sprintf('N鷐ero de gera珲es: %d',gen));
disp(sprintf('Tamanho M da popula玢o a ser reconhecida: [%d,%d]',N2,L));
%disp('Pressione qualquer tecla para continuar...'); pause;
%plot(Ag,'+');

it = 0; avD = 1000; alfa = 1;
RUNNET = 0; sig = []; M = [];
disp(sprintf('Aguarde... Posicionando os Centros'));
while it < gen & avD > sc,
   vbD = []; i = 1;
   M = []; sig = [];
   vet = randperm(N2);
   while i <= N2,
      % Ag-Ab Affinity (Match-Function: Euclidian distance)
      D = dist(Ab,Ag(vet(i),:)');
      [Dn,I] = sort(norma(D));
      Nc = floor(N-Dn(1:n,:)*N);
      
      % Clone & Affinity Maturation
      [C,Cag,Cmi] = clone(Ab,Ag(vet(i),:),mi,D,I,Nc);
      C = C - Cmi.*(C-Cag);
      
      % Re-Selection
      D = dist(C,Ag(vet(i),:)');
      [Dn,I] = sort(D);
      nR = round(qi*size(C,1));
      m = C(I(1:nR),:); % 1 clone for each Ag
      D = D(I(1:nR));   % new affinities
      
      % Network pruning (Natural Death)
      Ip = find(D > tp);
      m = extract(m,Ip); D = extract(D,Ip);
      
      % Suppression (Idiotypic Network)
      [m,D1,D] = suppress(m,ts,D);
      
      % General parameters
      minD = min(D); [vbD] = [vbD; minD];
      Cs = size(m,1);
      M = [M;m];  % memory matrix
      sig = [sig;D];				% RBF dispersion vector
      i = i + 1;
   end;
   % Search for similaritites among clusters
   [M,S,sig] = suppress(M,ts,sig); 
   Ab = M;
   avD = [avD;mean(vbD)];
   it = it + 1;
   disp(sprintf('It: %d	avD: %f	alfa: %.2f	N: %d',it,avD(end),alfa,size(M,1)));
end;
disp(sprintf('Aguarde... Auto-Organizando as Dispers鮡s'));
i = 0; gama = 0.9; lambda = 1;	% Self-Organizing dispersions rate
while gama > 1e-3,
   vet = randperm(N2);
   for j = 1:N2
      d = dist(M,Ag(vet(j),:)');
      [a,Ia] = min(d);
      if d(Ia) < sig(Ia),
         sig(Ia) = sig(Ia) - gama .* d(Ia);
      elseif d(Ia) > sig(Ia),
         lambda = 1/(1000*gama);
         sig(Ia) = sig(Ia) + lambda .* d(Ia);
      end;
   end;
   if rem(i,5) == 0,
      gama = 0.95 * gama; %lambda = 0.9 * lambda; 
   end;
   i = i + 1;
   if rem(i,40) == 0,
      disp(sprintf('It: %d	gama: %.4f	lambda: %.4f',i,gama,lambda));
   end;
end;
dsig = std(sig); msig = mean(sig); I = find(sig < (msig-dsig/2)); if ~isempty(I),sig(I) = sig(I)+dsig;end; %% GOOD RESULT
sig = ones(N2,1)*sig';

% Drawing Network & Plotting Results
% figure(2); plot(avD(2:end)); title('Average Affinities'); xlabel('Generations');
% hold on; plot(M,'r*'); title('+: Ag	*: Ab'); hold off;

% SECONDARY FUNCTIONS %

function [C,Cag,Cmi] = clone(Ab,ag,mi,D,I,Nc);
% C   -> clones (from greater to smaller affinity)
% Cag -> clones of Ag
% Cmi -> clones of mi
% S   -> selected antibodies
% Obs.: Cag and Cmi are necessary for the updating procedure
%       The original cell is mantained
[N1,L] = size(Ab); [n,N2] = size(Nc);
% C = []; Cmi = []; Cag = [];
C = Ab(I(1),:); Cmi = ones(1,L); Cag = C; % Maintenance of the fittest cell before maturation
for i=1:n,
   vones = ones(Nc(i),1);
   C = [C; vones * Ab(I(i),:)];
   Cag = [Cag; vones * ag];
   Cmi = [Cmi; rand(Nc(i),L) .* D(I(i)) .* mi];
end;

% Function suppress self-recognizing and non-stimulated Ab from Memory (M)
function [M,D1,sig] = suppress(M,ts,sig);
% 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);
   sig = extract(sig,Is);
end;
D1 = dist(M,M');

% 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

⌨️ 快捷键说明

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