声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 3486|回复: 4

[人工智能] 一个基于Matlab的人工免疫算法

[复制链接]
发表于 2007-6-25 03:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

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

  4. %Parameters
  5. Size=120;   
  6. G=200;     
  7. CodeL=15;

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

  9. %Main Program
  10. for k=1:1:G
  11. time(k)=k;

  12. for s=1:1:Size
  13. m=E(s,:);
  14. y1=0;y2=0;

  15. %Uncoding
  16. m1=m(1:1:CodeL);
  17. for i=1:1:CodeL
  18.    y1=y1+m1(i)*2^(i-1);
  19. end
  20. x1=10.24*y1/65535.0-5.12;
  21. m2=m(CodeL+1:1:2*CodeL);
  22. for i=1:1:CodeL
  23.    y2=y2+m2(i)*2^(i-1);
  24. end
  25. x2=10.24*y2/65535.0-5.12;
  26. %f(X1,X2)=(a/(b+(x1*x1+x2*x2)))*(a/(b+(x1*x1+x2*x2)))+(x1*x1+x2*x2)*(x1*x1+x2*x2)
  27. %here -5.12=<x1<-2,  -5.12=<x2<=5.12, a=3.0,  b=0.05;

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

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

  31.    if  x1<-2
  32.     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);
  33.      else
  34.     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);
  35.    end
  36. end

  37. sumf=0;
  38.   for s=1:1:Size;
  39.    sumf = sumf+F(s);
  40. end
  41.   favg=sumf./Size;
  42.   
  43. %initiate density  
  44.    x=F./sumf;

  45. % This function updates the concentrations of the population antibodies
  46. % according to the algorithm update rule:
  47. %UpdateConcentrations
  48. %Hamming distance
  49. for i=1:1:Size
  50.    for j=1:1:Size  
  51.         H(i,j)=0;
  52.       for Q=1:1:20
  53.            H(i,j)=H(i,j)+E(i,Q).*(1-E(j,Q))+E(j,Q).*(1-E(i,Q));
  54.        end
  55.         affinity(i,j)=F(i)*H(i,j);
  56.     end
  57. end
  58. % 亲密度计算完毕
  59. %**************以上是人工免疫的,以下是遗传算法的交叉、突变代码*****************
  60. %Euclider distance           
  61. %for i=1:1:Size;
  62.    %for j=1:1:Size;  
  63.        %H(i,j)=0;
  64.             %H(i,j)=sqrt((x1(i)-x1(j)).^2+(x2(i)-x2(j)).^2);
  65.             %affinity(i,j)=F(i)./(H(i,j)+1);
  66.             %end
  67.             %end   

  68. % This function normalizes the values of the concentrations of the
  69. % population. If the concentrations are perceived as percentages of
  70. % the total concentration of the system then their sum must be 1.0.
  71. % This function recalculates the concentrations so that this rule
  72. % is followed.
  73. c =0.8;
  74. k1 =0.1;
  75. k2 =0.3;
  76. %x = popv(:,fitness);
  77. for i = 1:1:Size
  78.       xsum = 0.0;   
  79.   for j = 1:1:Size
  80.     xsum= abs(xsum+(F(j).*affinity(i,j)-k1.*F(j).*affinity(j,i)-k2/(c*Size)).*F(i));
  81.        %m(j,i)=popv(i,fitness).*affinity(i,j);
  82.        %m(i,j)=popv(j,fitness).*affinity(j,i);
  83.        %n(i,j)= m(j,i)-k1.*m(i,j)-k2/(c*popsize);
  84.        %sum= sum+n(i,j).*popv(j,fitness);   
  85.    end  
  86.    x=x+x.*c.*xsum;
  87. end
  88.    
  89.    Fitness=x./sum(x);

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


  92. fi=Fitness;                          %Fitness Function
  93. [Oderfi,Indexfi]=sort(fi);     %Arranging fi small to bigger
  94. Bestfi=Oderfi(Size);           %Let Bestfi=max(fi)
  95. BestS=E(Indexfi(Size),:);      %Let BestS=E(m), m is the Indexfi belong to max(fi)
  96. bfi(k)=Bestfi;

  97. %****** Step 2 : Select and Reproduct Operation******
  98.    fi_sum=sum(fi);
  99.    fi_Size=(Oderfi/fi_sum)*Size;
  100.    
  101.    fi_S=floor(fi_Size);        %Selecting Bigger fi value
  102.    
  103.    kk=1;
  104.    for i=1:1:Size
  105.       for j=1:1:fi_S(i)        %Select and Reproduce
  106.        TempE(kk,:)=E(Indexfi(i),:);  
  107.          kk=kk+1;              %kk is used to reproduce
  108.       end
  109.    end
  110.    
  111. %************ Step 3 : Crossover Operation ************
  112. pc=0.80;
  113. n=ceil(20*rand);
  114. for i=1:2:(Size-1)
  115.     temp=rand;
  116.     if pc>temp                  %Crossover Condition
  117.     for j=n:1:20
  118.         TempE(i,j)=E(i+1,j);
  119.         TempE(i+1,j)=E(i,j);
  120.     end
  121.     end
  122. end
  123. TempE(Size,:)=BestS;
  124. E=TempE;
  125.    
  126. %************ Step 4: Mutation Operation **************
  127. %pm=0.001;
  128. %pm=0.001-[1:1:Size]*(0.001)/Size; %Bigger fi, smaller Pm
  129. %pm=0.0;    %No mutation
  130. pm=0.1;     %Big mutation

  131.    for i=1:1:Size
  132.       for j=1:1:2*CodeL
  133.          temp=rand;
  134.          if pm>temp                %Mutation Condition (varify incident)
  135.             if TempE(i,j)==0
  136.                TempE(i,j)=1;
  137.             else
  138.                TempE(i,j)=0;
  139.             end
  140.         end
  141.       end
  142.    end
  143.    
  144. %Guarantee TempPop(30,:) is the code belong to the best individual(max(fi))
  145. TempE(Size,:)=BestS;
  146. E=TempE;
  147. M(k)=F(Size);
  148. end   % Is special for if k

  149. Max_Value=F(Size)
  150. BestS
  151. x1
  152. x2
  153. figure(1);
  154. plot(time,M);
  155. xlabel('Times');ylabel('Best F');
  156. figure(2);
  157. plot(time,bfi);
  158. xlabel('times');ylabel('Best Fitness');
复制代码
回复
分享到:

使用道具 举报

发表于 2007-11-21 14:49 | 显示全部楼层
:lol不错的
发表于 2008-4-3 11:45 | 显示全部楼层

能用吗??发表者
发表于 2008-5-7 10:52 | 显示全部楼层
在其他地方看到过,好象还缺函数..
发表于 2008-6-22 18:59 | 显示全部楼层
确实牛逼!!!!!!!!!!!!!顶
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-10-2 11:14 , Processed in 0.055708 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表