frogfish 发表于 2007-6-25 03:28

一个基于Matlab的人工免疫算法

%Immune Algorithm based on the immune network model for function f(x1,x2) optimum
%copy right SCUT Guangxing Tan 2005.02.18
clear all;

%Parameters
Size=120;   
G=200;   
CodeL=15;

E=round(rand(Size,2*CodeL));    %Initial Code

%Main Program
for k=1:1:G
time(k)=k;

for s=1:1:Size
m=E(s,:);
y1=0;y2=0;

%Uncoding
m1=m(1:1:CodeL);
for i=1:1:CodeL
   y1=y1+m1(i)*2^(i-1);
end
x1=10.24*y1/65535.0-5.12;
m2=m(CodeL+1:1:2*CodeL);
for i=1:1:CodeL
   y2=y2+m2(i)*2^(i-1);
end
x2=10.24*y2/65535.0-5.12;
%f(X1,X2)=(a/(b+(x1*x1+x2*x2)))*(a/(b+(x1*x1+x2*x2)))+(x1*x1+x2*x2)*(x1*x1+x2*x2)
%here -5.12=<x1<-2,-5.12=<x2<=5.12, a=3.0,b=0.05;

% f(X1,X2)=90+(a/(b+(x1*x1+x2*x2)))*(a/(b+(x1*x1+x2*x2)))+(x1*x1+x2*x2)*(x1*x1+x2*x2)
% here -2=<x1<=5.12,-5.12=<x2<=5.12

% f(X)=(-x+3*M_PI-0.5)(1+sinx*sinx)   if 1.5M_PI<x<=2M_PI;

   ifx1<-2
    F(s)=(3.0/(0.05+(x1*x1+x2*x2)))*(3.0/(0.05+(x1*x1+x2*x2)))+(x1*x1+x2*x2)*(x1*x1+x2*x2);
   else
    F(s)=90+(3.0/(0.05+(x1*x1+x2*x2)))*(3.0/(0.05+(x1*x1+x2*x2)))+(x1*x1+x2*x2)*(x1*x1+x2*x2);
   end
end

sumf=0;
for s=1:1:Size;
   sumf = sumf+F(s);
end
favg=sumf./Size;

%initiate density
   x=F./sumf;

% This function updates the concentrations of the population antibodies
% according to the algorithm update rule:
%UpdateConcentrations
%Hamming distance
for i=1:1:Size
   for j=1:1:Size
      H(i,j)=0;
      for Q=1:1:20
         H(i,j)=H(i,j)+E(i,Q).*(1-E(j,Q))+E(j,Q).*(1-E(i,Q));
       end
      affinity(i,j)=F(i)*H(i,j);
    end
end
% 亲密度计算完毕
%**************以上是人工免疫的,以下是遗传算法的交叉、突变代码*****************
%Euclider distance         
%for i=1:1:Size;
   %for j=1:1:Size;
       %H(i,j)=0;
            %H(i,j)=sqrt((x1(i)-x1(j)).^2+(x2(i)-x2(j)).^2);
            %affinity(i,j)=F(i)./(H(i,j)+1);
            %end
            %end   

% This function normalizes the values of the concentrations of the
% population. If the concentrations are perceived as percentages of
% the total concentration of the system then their sum must be 1.0.
% This function recalculates the concentrations so that this rule
% is followed.
c =0.8;
k1 =0.1;
k2 =0.3;
%x = popv(:,fitness);
for i = 1:1:Size
      xsum = 0.0;   
for j = 1:1:Size
    xsum= abs(xsum+(F(j).*affinity(i,j)-k1.*F(j).*affinity(j,i)-k2/(c*Size)).*F(i));
       %m(j,i)=popv(i,fitness).*affinity(i,j);
       %m(i,j)=popv(j,fitness).*affinity(j,i);
       %n(i,j)= m(j,i)-k1.*m(i,j)-k2/(c*popsize);
       %sum= sum+n(i,j).*popv(j,fitness);   
   end
   x=x+x.*c.*xsum;
end
   
   Fitness=x./sum(x);

Ji=1./Fitness;
%****** Step 1 : Evaluate BestJ ******


fi=Fitness;                        %Fitness Function
=sort(fi);   %Arranging fi small to bigger
Bestfi=Oderfi(Size);         %Let Bestfi=max(fi)
BestS=E(Indexfi(Size),:);      %Let BestS=E(m), m is the Indexfi belong to max(fi)
bfi(k)=Bestfi;

%****** Step 2 : Select and Reproduct Operation******
   fi_sum=sum(fi);
   fi_Size=(Oderfi/fi_sum)*Size;
   
   fi_S=floor(fi_Size);      %Selecting Bigger fi value
   
   kk=1;
   for i=1:1:Size
      for j=1:1:fi_S(i)      %Select and Reproduce
       TempE(kk,:)=E(Indexfi(i),:);
         kk=kk+1;            %kk is used to reproduce
      end
   end
   
%************ Step 3 : Crossover Operation ************
pc=0.80;
n=ceil(20*rand);
for i=1:2:(Size-1)
    temp=rand;
    if pc>temp                  %Crossover Condition
    for j=n:1:20
      TempE(i,j)=E(i+1,j);
      TempE(i+1,j)=E(i,j);
    end
    end
end
TempE(Size,:)=BestS;
E=TempE;
   
%************ Step 4: Mutation Operation **************
%pm=0.001;
%pm=0.001-*(0.001)/Size; %Bigger fi, smaller Pm
%pm=0.0;    %No mutation
pm=0.1;   %Big mutation

   for i=1:1:Size
      for j=1:1:2*CodeL
         temp=rand;
         if pm>temp                %Mutation Condition (varify incident)
            if TempE(i,j)==0
               TempE(i,j)=1;
            else
               TempE(i,j)=0;
            end
      end
      end
   end
   
%Guarantee TempPop(30,:) is the code belong to the best individual(max(fi))
TempE(Size,:)=BestS;
E=TempE;
M(k)=F(Size);
end   % Is special for if k

Max_Value=F(Size)
BestS
x1
x2
figure(1);
plot(time,M);
xlabel('Times');ylabel('Best F');
figure(2);
plot(time,bfi);
xlabel('times');ylabel('Best Fitness');

yanzi621 发表于 2007-11-21 14:49

:lol不错的

wangahu2008 发表于 2008-4-3 11:45

能用吗??发表者

chg555 发表于 2008-5-7 10:52

在其他地方看到过,好象还缺函数..

oobb1314 发表于 2008-6-22 18:59

确实牛逼!!!!!!!!!!!!!顶
页: [1]
查看完整版本: 一个基于Matlab的人工免疫算法