?? gafcm.asv
字號:
%該文件為gacm算法的主程序clear;clear all;clc; %初始化population=100; %種群數(shù)量 可用于外設(shè)I=imread('圖像地址');%讀圖像hist=imhist(I); %獲取圖像的灰度圖a=size(hist,1); %計算hist的第一維長度c=2; %可以做成界面的傳輸量 ,聚類數(shù)V00=zeros(population,c); %對聚類中心進行的編碼for i=1:population V00(i,:)=round(rand(1,c)*(a-1));end adapt0=zeros(population,1);adapt1=zeros(population,1);adapt2=zeros(population,1);for i=1:population %調(diào)用函數(shù),輸出適應(yīng)度 [V00(i,:),adapt0(i)]=fcm(hist,c,V00(i,:)); adapt_averge=mean(adapt0); %計算平均適應(yīng)度V1=V00; %保留初始化聚類中心adapt1=adapt0; adapt_average1=adapt_average0; generation=100; for k=1:generation s1=select(V1,adapt_value1,population); %制作屬于自己的選擇算子 s_code1=dec2bin(s1,8); s_code=mat2str(s_code1);for i=1:population %轉(zhuǎn)化為二進制編碼 s_code1(i)=strcat(s_code(2*i-1,:),s_code(2*i,:)); end c1=cross(s_code1,population); %制作屬于自己的交叉算子 v1=mutation(c1,population); %制作屬于自己的變異算子 V2=(bin2dec(v1))'; %轉(zhuǎn)化為十進制編碼 for i=1:population [V2(i,:),adapt2(i)]=fcm(hist,c,V2(i,:)); %計算經(jīng)選擇、交叉、變異后的適應(yīng)度 end adapt_average2=mean(adapt2); %計算平均適應(yīng)度 if abs(adapt_average2-adapt_average1)<=0.0001 %算法停止條件 break; else V1=V2; adapt1=adapt2; adapt_average1=adapt_average2; endend [max_adapt,i]=max(adapt2); %選取最大適應(yīng)度值 number=find(adapt2==max_adapt); %具有最大適應(yīng)度的種群所占比例 % ?如何進行劃分 V=zeros(1,c); V=V2(i,:); dist = zeros(a,c); %產(chǎn)生一個a*c矩陣 for k=1:a for i=1:c dist(k,i)=sqrt(k-V(i)); %計算歐式距離 先列后行 (8)式 end end %求隸屬度 m=2;%m為加權(quán)指數(shù) 有可能的話可用于外設(shè) u = zeros(a,c); b=0; ht=0; %ht=zeros(a,1); for k=1:a for i=1:c a_a=dist(k,i); if a_a==0 temp=0; else temp=(1/a_a)^(2/(m-1)); end ht=ht+temp; %將分母的和算出 ht(k)=ht(k)+temp; end end for k=1:a for i=1:c if dist(k,i)==0 u(k,i)=0; else u(k,i)=abs(ht/(dist(k,i)^(2/(m-1))));% u(k,i)=abs(ht(k)/(dist(k,i)^(2/(m-1)))); end end end
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -