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

📄 ga1.asv

📁 用matlab编写的采用遗传算法进行图像分割的一个程序
💻 ASV
字号:
 function [best1]=ga1(a,b,popsize,stringlength)
  tic;
pc=0.8;pm=0.01;  %popsize只能为偶数
maxgeneration=20;
[pop]=initialise(popsize,stringlength,a,b);
for generation=0:1:maxgeneration
[bmax,bestindes]=max(pop(:,stringlength+2));
best1=pop(bestindes,:);
[bmin bminindes]=min(pop(:,stringlength+2));
pop(bminindes,:)=best1;
[rnewpop]=roulette(pop,popsize,stringlength);
[cnewpop]=crossover(rnewpop,stringlength,a,b,pc);
[mnewpop]=mutation(cnewpop,stringlength,a,b,pm);
pop=mnewpop;
end
toc

function [eval1]=fitness(x,popsize,stringlength)
     a=imread('mytu.tif');
     A=
     count1=imhist(A);
     [m,n]=size(A);
     N=m*n;      %图像总的象素数
     L=256;
     count=count1/N;
     u=0;
     for j=1:L
         u=u+count(j)*j;
     end   
     w=0;ua=0;	
     for j=1:x
         w=w+count(j);
         ua=ua+count(j)*j;
     end
     if (w==0)|(w==1)
         eval1=0;
     else
         eval1=(u*w-ua)^2/(w*(1-w));
     end
% w0=0;w1=0;u0=0;u1=0;
% for j=1:x
%          w0=w0+count(j);
%          u0=u0+count(j)*j;
%  end
%  for j=x:L
%          w1=w1+count(j);
%          u1=u1+count(j)*j;
%  end
%   if (w0~=0)&(w1~=0)
%       eval1=w0*(u-u0/w0).^2+w1*(u-u1/w1).^2;
%    else
%          eval1=0;
%    end
           
           
function [pop]=initialise(popsize,stringlength,a,b)
  pop=zeros(popsize,stringlength+2);
  i=1;  
  while i<=popsize
    pop1=round(rand(1,stringlength));
    sum1=0;
    for k=1:stringlength
        sum1=sum1+pop1(k)*2^(k-1);
    end
     x=sum1;%解码
     [eval1]=fitness(x,popsize,stringlength);
     if  (eval1==0)
         i=i;
      else
          pop(i,1:stringlength)=pop1;
          pop(i,stringlength+1)=x;
          pop(i,stringlength+2)=eval1;
          i=i+1; 
      end      
 end

 function [newpop]=roulette(oldpop,pz,stringlength)
totalfit=0;
pz=size(oldpop,1);
newpop=zeros(size(oldpop));
for i=1:pz
    totalfit=totalfit+oldpop(i,stringlength+2);
end
prob=oldpop(:,stringlength+2)/totalfit;
prob=cumsum(prob);
rns=sort(rand(pz,1)); %按元素的从小到大排成一列
fitin=1;newin=1;
while (newin<=pz)
    if rns(newin)<prob(fitin)
        newpop(newin,:)=oldpop(fitin,:);
        newin=newin+1;
    else
        fitin=fitin+1;
    end
end
 
function [newpop]=crossover(oldpop,stringlength,a,b,pc);
pops=size(oldpop,1);
newpop=zeros(size(oldpop));
for i=1:pops/2
    if(rand<pc)
          cpoint=round(rand*(stringlength-2))+1;
          newpop(i,:)=[oldpop(i,1:cpoint),oldpop(pops/2+i,cpoint+1:stringlength),0,0];
          newpop(pops/2+i,:)=[oldpop(pops/2+i,1:cpoint),oldpop(i,cpoint+1:stringlength),0,0];         
    else
          newpop(i,:)=oldpop(i,:);
          newpop(pops/2+i,:)=oldpop(pops/2+i,:);
    end
end
for i=1:pops
     sum1=0;
     for j=1:stringlength
         sum1=sum1+newpop(i,j)*2^(j-1);  
     end
         newpop(i,stringlength+1)=sum1;%解码
         x=newpop(i,stringlength+1);
         newpop(i,stringlength+2)=fitness(x,pops,stringlength);    
 end

 function [newpop]=mutation(oldpop,stringlength,a,b,pm)
pz=size(oldpop,1);
newpop=zeros(size(oldpop));
for i=1:pz
   if(rand<pm)
     mpoint=round(rand*(stringlength-1))+1;
     newpop=oldpop;
     newpop(i,mpoint)=abs(oldpop(i,mpoint)-1);
     sum1=0;
     for j=1:stringlength
        sum1=sum1+newpop(j)*2^(j-1);
     end
    newpop(i,stringlength+1)=sum1;%解码   
    x(i)=newpop(i,stringlength+1);
    newpop(i,stringlength+2)=fitness(x,pz,stringlength);
  else 
     newpop=oldpop; 
  end
end

⌨️ 快捷键说明

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