variable=2; % definite the number of variables bits=[20, 20]; %the bits for every variables used %bound=[-100 100]; %pl=ones(variable,1); bounds=[-100,100;-100,100]; % definite the bounds of every variable MatrixLen=sum(bits)+1; %the length of the matrix: variables and fitness popsize=100; % the inited population number, the row of the matrix MaxGeneration=300; % run the loop MaxGeneration times StartPop=zeros(popsize,MatrixLen); Generation=1; Done = 0; scale=(bounds(:,2)-bounds(:,1))′./(2.^bits-1); % scale for the min bit 数据挖掘论坛 cs=[0 cumsum(bits)]; % cs: the start position of every variable Pc=0.6; %set the probability of crossover Pm=0.05; %set the probability of mutation hold on; axis([0 MaxGeneration 15 25]);
% 【Initialize the StartPop for SGA】 for i=1:popsize for j=1:MatrixLen-1 if rand >= 0.5 StartPop(i,j)=1; end;%endif end;%endfor j end;%endfor i
% evalue the fitness of individuals in the first generation for i = 1:popsize for j = 1:variable a=StartPop(i,cs(j)+1:cs(j+1)); % the matrix for every variable var(j)=sum(2.^(bits(j)-1:-1:0).*a)*scale(j)+bounds(j,1); % the value of every variable end; % plot(var(1),var(2),′r*′); StartPop(i,MatrixLen) = 25 - (var(1).^2+var(2).^2).^0.25.*(sin(50.*(var(1).^2+var(2).^2).^0.1).^2 + 1); end; % 【End of initialization】
% plot the points of the first generation % plot(1:popsize,StartPop(1:popsize,MatrixLen),′o′);
while(~Done) [value,posit] = max(StartPop(:,MatrixLen)); Best = StartPop(posit,:); % store the best individual TraceInfo(Generation,1) = value; % the best fitness before reproduction TraceInfo(Generation,2) = mean(StartPop(:,MatrixLen)); % average fitness before reproduction TraceInfo(Generation,3) = std(StartPop(:,MatrixLen)); % Standard Deviation TraceInfo(Generation,4) = size(unique(StartPop(:,1:MatrixLen-1),′rows′),1); %diversity % plot(Generation,TraceInfo(Generation,1),′r*′); % plot the best fitness 数据挖掘交友 % plot(Generation,TraceInfo(Generation,2),′*′); % plot the average fitness % 【Reproduction by Gambling】 EndPop=zeros(popsize,MatrixLen); fitness=cumsum(StartPop(:,MatrixLen)′./sum(StartPop(:,MatrixLen))); % fitness matrix for every individual for i = 1:popsize b = rand; j = 1; while(b > fitness(j)) j = j+1; end; %endwhile EndPop(i,:)=StartPop(j,:); 数据挖掘交友 %plot(j,EndPop(i,39),′x′) //plot the reproducted individuals end; %endfor i TraceInfo(Generation,5) = mean(EndPop(:,MatrixLen)); % average fitness after reproduction;before crossover % 【End of Reproduction】
% 【Crossover Operation】 for i = 1:round(popsize.*Pc) numA = 0; numB = 0; while(numA == numB) % numA numB stand for 2 individuals will cross 数据挖掘论坛 numA = round(rand*popsize+0.5); numB = round(rand*popsize+0.5); end;%endwhile cPoint = round(rand*(MatrixLen-3)) + 1; % cPoint is the cross position p1 = EndPop(numA,:); p2 = EndPop(numB,:); cPopA = [p1(1:cPoint) p2(cPoint+1:MatrixLen-1)]; cPopB = [p2(1:cPoint) p1(cPoint+1:MatrixLen-1)]; if cPopA(1:MatrixLen-1) == p1(1:MatrixLen-1) cPopA(MatrixLen) = p1(MatrixLen); 数据挖掘工具 cPopB(MatrixLen) = p2(MatrixLen); elseif cPopA(1:MatrixLen-1) == p2(1:MatrixLen-1) cPopA(MatrixLen) = p2(MatrixLen); cPopB(MatrixLen) = p1(MatrixLen); else for j = 1:variable a1=cPopA(cs(j)+1:cs(j+1)); a2=cPopB(cs(j)+1:cs(j+1)); var1(j)=sum(2.^(size(a1,2)-1:-1:0).*a1)*scale(j)+bounds(j,1); var2(j)=sum(2.^(size(a2,2)-1:-1:0).*a2)*scale(j)+bounds(j,1); end; cPopA(MatrixLen) = 25 - (var1(1).^2+var1(2).^2).^0.25.*(sin(50.*(var1(1).^2+var1(2).^2).^0.1).^2 + 1); cPopB(MatrixLen) = 25 - (var2(1).^2+var2(2).^2).^0.25.*(sin(50.*(var2(1).^2+var2(2).^2).^0.1).^2 + 1); end;%endif EndPop(numA,:) = cPopA; EndPop(numB,:) = cPopB; end;%endfor TraceInfo(Generation,6) = mean(EndPop(:,MatrixLen)); % average fitness after crossover;before mutation % 【End of Crossover】
% 【mutation】 for i = 1:round(Pm.*popsize) numA = round(rand*popsize+0.5); mPoint = round(rand*(MatrixLen-2)) + 1; EndPop(numA,mPoint) = 1 - EndPop(numA,mPoint); for j = 1:variable a = EndPop(numA,cs(j)+1:cs(j+1)); var(j) = sum(2.^(size(a,2)-1:-1:0).*a).*scale(j)+bounds(j,1); end;%endfor j EndPop(numA,MatrixLen) = 25 - (var(1).^2+var(2).^2).^0.25.*(sin(50.*(var(1).^2+var(2).^2).^0.1).^2 + 1); 数据挖掘交友 end;%endfor i TraceInfo(Generation,7) = mean(EndPop(:,MatrixLen)); % average fitness after mutation % 【End of mutation】 StartPop=EndPop; % 精英保留策略 [value,posit] = min(StartPop(:,MatrixLen)); StartPop(posit,:) = Best; % End of 精英保留策略 Generation = Generation + 1; if Generation == MaxGeneration; Done = 1; end;%endif end; %endwhile
% Record infomation of the last generation 数据挖掘交友 [value,posit] = max(StartPop(:,MatrixLen)); Best = StartPop(posit,:); % store the best individual TraceInfo(Generation,1) = value; % the best fitness before reproduction TraceInfo(Generation,2) = mean(StartPop(:,MatrixLen)); % average fitness before reproduction TraceInfo(Generation,3) = std(StartPop(:,MatrixLen)); % Standard Deviation TraceInfo(Generation,4) = size(unique(StartPop(:,1:MatrixLen-1),′rows′),1); %diversity
% The information of matrix TraceInfo % 1.Best fitness of per generation % 2.Average fitness before repreduction % 3.Standard Deviation % 4.Diversity % 5.Average fitness after reproduction;before crossover 数据挖掘工具 % 6.Average fitness after crossover;before mutation % 7.Average fitness after mutation plot(TraceInfo(:,1),′r*′); plot(TraceInfo(:,2),′*′); %plot(TraceInfo(:,5),′r-′); %plot(TraceInfo(:,6),′b-′); %plot(TraceInfo(:,7),′g-′);
|