找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1355|回复: 0
打印 上一主题 下一主题
收起左侧

赠MATLAB遗传算法代码

[复制链接]
跳转到指定楼层
楼主
ID:526755 发表于 2019-5-4 02:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
赠MATLAB代码与有缘人
  1. 遗传算法

  2. function error = fun(x,inputnum,hiddennum,hiddennum,outputnum,net,inputn,outputn)
  3. x1 = x(1:inputnum*hiddennum);
  4. B1 = x(inputnum*hiddennum+1:inputnum*hiddennum+hiddennum);
  5. w2 = x(inputnum*hiddennum+hiddennum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum);
  6. B2 = x(inputnum*hiddennum+hiddennum+hiddennum*outputnum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum);
  7. net.iw{1,1}=reshape(w1,hiddennum,inputnum);
  8. net.lw{2,1}=reshape(w2,outputnum,hiddennum);
  9. net.b{1}=reshape(B1,hiddennum,1);
  10. net.b{2}=B2;
  11. net=newff(inputn,outputn,hiddennum);
  12. net.trainParam.epochs=20;
  13. net.trainParam.lr=0.1;
  14. net.trainParam.goal=0.00001;
  15. net.trainParam.show=100;
  16. net.trainParam.showWindow=0;
  17. net=train(net,inputn,outputn);
  18. an=sim(net,inputn);
  19. error=sum(abs(an-outputn));

  20. 选择操作

  21. function ret=select(individuals,sizepop)
  22. fitness1=10./individuals.fitness;
  23. sumfitness=sum(fitness1);
  24. sumf=fitmessl./sumfitness;
  25. index=[];
  26. for i=1:sizepop
  27.     pick=rand;
  28.     while pick==0
  29.         pick=rand;
  30.     end
  31.     for i=1:sizepop
  32.         pick=pick-sumf(i);
  33.         if pick<0
  34.             index=[index i];
  35.             break;
  36.         end
  37.     end
  38. end
  39. individuals.chrom=individuals.chrom(index,:);
  40. individuals.fitness=individuals.fitness(index);
  41. ret=individuals;

  42. 交叉操作

  43. function ret=Cross(pcross,lenchrom,chrom,sizepop,bound)
  44. for i=1:sizepop
  45.     pick=rand(1,2);
  46.     while prod(pick)==0
  47.         pick=rand(1,2);
  48.     end
  49.     index=ceil(pick.*sizepop);
  50.     pick=rand;
  51.     while pick==0
  52.         pick=rand;
  53.     end
  54.     if pick>pcross
  55.         continue;
  56.     end
  57.     flag=0;
  58.     while flag==0
  59.         pick=rand;
  60.     end
  61.     pos=ceil(pick.*sum(lenchrom));
  62.     pick=rand;
  63.     v1=chrom(index(1),pos);
  64.     v2=chrom(index(2),pos);
  65.     chrom(index(1),pos)=pick*v2+(1-pick)*v1;
  66.     chrom(index(2),pos)=pick*v1+(1-pick)*v2;
  67.     flag1=test(lenchrom,bound,chrom(index(1),:));
  68.     flag2=test(lenchrom,bound,chrom(index(2),:));
  69.     if flag1*flag2==0
  70.         flag=0;
  71.     else flag=1;
  72.     end
  73. end
  74. end
  75. ret=chrom;
  76.    


  77. 变异操作

  78. function ret=Mutation(pmutation,lenchrom,chrom,sizepop,num,maxgen,bound)
  79. for i=1:sizepop
  80.     pick=rand;
  81.     while pick==0
  82.         pick=rand;
  83.     end
  84.     index=ceil(pick*sizepop);
  85.     pick=rand;
  86.     if pick>pmutation
  87.         continue;
  88.     end
  89.     flag=0;
  90.     while flag==0
  91.         pick=rand;
  92.         while pick==0
  93.             pick=rand;
  94.         end
  95.         pos=ceil(pick*sum(lenchrom));
  96.         v=chrom(i,pos);
  97.         v1=v-bound(pos,1);
  98.         v2=bound(pos,2)-v;
  99.         pick=rand;
  100.         fg=(rand*(1-num/maxgen))^2;
  101.         if pick>0.5
  102.             chrom(i,pos)=chrom(i,pos)+(bound(pos,2)-chrom(i,pos))*fg;
  103.         else
  104.            chrom(i,pos)=chrom(i,pos)-(bound(i,pos)-bound(pos,1))*fg;
  105.         end
  106.         flag=test(lenchrom,boun,chrom(i,;));
  107.     end
  108. end
  109. ret=chrom;  

  110.             
  111.         
  112. 遗传算法主函数

  113. clc
  114. clear
  115. load data input output
  116. inputnum=2;
  117. hiddennum=5;
  118. outputnum=1;
  119. input_train=input(1:1900,:)';
  120. input_test=input(1901:2000,:)';
  121. output_train=output(1:1900)';
  122. output_test=output(1901:2000)';
  123. [inputn,inputps]=mapminmax(input_train);
  124. [outputn,outputps]=mapminmax(output_train);
  125. net=newff(inputn,outputn,hiddennum);
  126. maxgen=50;
  127. sizepop=10;
  128. pcross=[0.4];
  129. pmutation=[0.2];
  130. numsum=inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum;
  131. lenchrom=ones(1,numsum);
  132. bound=[-3*ones(numsum,1)3*ones(numsum,1)];
  133. individuals=struct('fitness',zeros(1,sizepop),'chrom',[]);
  134. avgfitness=[];
  135. bestfitness=[];
  136. bestchrom=[];
  137. for i=1:sizepop
  138.     individuals.chrom(i,:)=Code(lenchrom,bound);
  139.     x=individuals.chrom(i,:);
  140.     individuals.fitness(i)=fun(x,inputnum,hiddennum,outputnum,net,inputn,outputn);
  141. end
  142. for i=1:maxgen
  143.     individuals=Select(individuals,sizepop);
  144.     individuals.chrom=Cross(pcross,lenchrom,individuals.chrom,sizepop,i,maxgen,bound);
  145.     for j=1:sizepop
  146.         x=individuals.chrom(j,:);
  147.         individuals.fitness(j)=fun(x,inputnum,hiddennum,outputnum,net,inputn,output n);
  148.     end
  149.     [newbestfitness,newbestindex]=min(individuals.fitness);
  150.     [worestfitness,worestindex]=max(individuals.fitness);
  151.     if bestfitness>newbestfitness
  152.         bestfitness=newbestfitness;
  153.         bestchrom=individuals.chrom(newbestindex,:);
  154.     end
  155.     individuals.chrom(worestindex,:)=bestchrom;
  156.     individuals.fitness(worestindex,:)=bestfitness;
  157.     avgfitness=sum(individuals.fitness)/sizepop;
  158.     trace=[trace;avgfitness bestfitness];
  159. end

  160. 遗传算法优化的BP神经网络函数拟合

  161. x=bestchrom
  162. x1=x(1:inputnum*hiddennum);
  163. B1=x(inputnum*hiddennum+1:inputnum*hiddennum+hiddennum);
  164. w2=x(inputnum*hiddennum+hiddennum+1;inputnum*hiddennum+hiddennum+hiddennum*outputnum);
  165. B2=x(inputnum*hiddennum+hiddennum+hiddennum*outputnum+1;inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum);
  166. net.iw{1,1}=reshape(w1,hiddennum,inputnum);
  167. net.lw{2,1}=reshape(w2,outputnum,hiddennum);
  168. net.b{1}=reshape(B1,hiddennum,1);
  169. net.b(2)=B2;
  170. net.trainParam.epochs=100;
  171. net.trainParam.lr=0.1;
  172. [net,per2]=trian(net,inputn,outputn);
  173. inputn_test=mapminmax('apply',input_test,inputps);
  174. an=sim(net,inputn_test);
  175. test_simu=mapminmax('reverse',an,outputps);
复制代码


遗传算法.zip

13.91 KB, 下载次数: 17, 下载积分: 黑币 -5

遗传算法

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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