www.gusucode.com > matlab编程遗传算法计算匹配电路源码程序 > code1/code/MATLAB源代码/ga_match1.m

    function [] = ga_match1()
%UNTITLED4 此处显示有关此函数的摘要
%   此处显示详细说明
Nind=100;   %个体数目,或每个子种群的个体数目
MAXGEN=200;   %最大遗传代数
GGAP=0.9;     %选择算子的选择比例
SUBPOP=10;    %子种群数
MIGR=0.2;     %迁移率
MIGGEN=20;    %每隔多少代进行迁移
INSR=0.9;     %插入率
mortality=0.2;    %淘汰率
num_integer=8;     %整数编码的基因数
num_parameter=3;     %每个结构所含的电气参数的数量
variable_range=[0 0 0 0 0 0 0 0 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1;...
8 8 8 8 8 8 8 8 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500];
%variable_range=[0 0 0 0 0 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1;8 8 8 8 8 250 250 250 250 250 250 250 250 250 250];
Chrom=creat_chrom(SUBPOP*Nind,num_integer,variable_range);    %创建种群
%Chrom=check_match_chrom(Chrom,num_integer);
%Chrom(1,:)=[8 0 0 0 0 0 0 0 20 25 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10];
%Chrom=optimize_Chrom(Chrom,NIND,SUBPOP);      %将之前的最优解插入种群
trace=zeros(MAXGEN,2);     %用于跟踪算法的解
[ObjV,S21_passband]=match1(Chrom,num_integer,num_parameter);   %计算目标函数值
gen=0;                         %代数计数器
mutation_probability_max=0.2;      %设置变异率的最大值
mutation_probability_min=0.05;      %设置变异率的最小值
crossover_probability_max=0.8;     %设置交叉率的最大值
crossover_probability_min=0.4;     %设置交叉率的最小值
while gen<MAXGEN
      FitnV=ranking(S21_passband,[2 0],SUBPOP);      %基于排序的适应度分配,目标值最小的适应度为2,最大的适应度为0
      SelCh=select('sus',Chrom,FitnV,GGAP,SUBPOP);    %随机遍历抽样
      if gen==0
           [ObjVSel,Sel_S21_passband]=match1(SelCh,num_integer,num_parameter);
      end
      SelCh=recombin_structure(SelCh,num_integer,num_parameter,SUBPOP);      %离散重组,对结构离散重组,电气参数跟随移动
      SelCh=recombin_electric_parameter(SelCh,num_integer,crossover_probability_max,crossover_probability_min,gen,MAXGEN);    %电气参数的中间重组
      SelCh=mutate_variable(SelCh,variable_range,num_integer,mutation_probability_max,mutation_probability_min,gen,MAXGEN,Sel_S21_passband);   %变异
%      SelCh=check_match_chrom(SelCh,num_integer);
      [ObjVSel,Sel_S21_passband]=match1(SelCh,num_integer,num_parameter);          %计算子代的目标函数值
      [Chrom,S21_passband]=reins(Chrom,SelCh,SUBPOP,[1 INSR],S21_passband,Sel_S21_passband);    %重插入子代的新种群
      gen=gen+1;                               %代数计数器加1
      trace(gen,1)=min(S21_passband);                  %跟踪最优解
      trace(gen,2)=sum(S21_passband)/length(S21_passband);     %跟踪平均解
      if (rem(gen,MIGGEN)==0)                  %在子种群之间迁移个体
           [Chrom,S21_passband]=migrate(Chrom,SUBPOP,[MIGR,1,0],S21_passband);
      end
%      if gen<MAXGEN
%           [x,y]=sort(ObjV','descend');
%           Chrom(y(1:Nind*SUBPOP*mortality),:)=creat_chrom(Nind*SUBPOP*mortality,num_integer,variable_range);
%      end
%      ObjV=bandpassS21_test4(Chrom,num_integer,num_parameter);
end

plot(trace(:,1));hold on;grid;                      %最优解的曲线
%plot(trace(:,2),'-.');grid;                    %平均解的曲线
legend('解的变化','均值的变化');
[BestObjV,Location]=min(S21_passband)                  %找出最优解
BestIndividual=Chrom(Location,:)               %最优解的基因
end