Code
Code
% WARNING: some of the optimization routines will not work if population size
is odd.
OPTIONS.popsize = 200; % total population size
OPTIONS.Maxgen = 100; %200; %50; % generation count limit
OPTIONS.numVar = 5; % original 20; % number of vriables in each population
member
OPTIONS.numClan = 5; % number of Function Evaluations (FEs)
OPTIONS.numElephantInEachClan = OPTIONS.popsize/OPTIONS.numClan;
OPTIONS.MaxFEs = 0.3E4; % number of Function Evaluations (FEs)
I=imread('49.png');
I=imresize(I,0.9);
Igray=rgb2gray(I);
%Igray=I;
data=double(reshape(Igray,1,[]))/255;
%% initialization
pop_size=100; %
NumClans=5; %
keep=2;
dim = 3;
alpha = 0.5;
beta = 0.1;
Maxgen=100;
numElephantInEachClan = pop_size/NumClans;
for i=1:pop_size
population(i,:)=rand(1,dim);
fitness(i)=fitnessFunction(population(i,:),1); % intial fitness solving
using kmeans and find the segmentation
end
[~,index]=sort(fitness,'descend'); % Sort in descending order
population=population(index,:);
j = j+1;
end
%% Clan updating operator
j = 1;
popindex =1;
while popindex <= OPTIONS.popsize
for cindex = 1 : OPTIONS.numClan
ClanCenter = CaculateClanCenter(OPTIONS, Clan, cindex);
NewClan{cindex}(j).chrom = Clan{cindex}(j).chrom ...
+ alpha*(Clan{cindex}(1).chrom - Clan{cindex}(j).chrom).*
rand(1,dim);
if sum( NewClan{cindex}(j).chrom - Clan{cindex}(j).chrom) == 0
NewClan{cindex}(j).chrom = beta * ClanCenter ;
end
popindex = popindex + 1;
end % end for cindex
j = j+1;
end % end for popindex
%%
%% Separating operator
for cindex = 1 : OPTIONS.numClan
NewClan{cindex}(end).chrom = MinParValue...
+ (MaxParValue - MinParValue + 1) .* rand(1,OPTIONS.numVar);
end
%%
%% Evaluate NewClan
SavePopSize = OPTIONS.popsize;
for i=1:OPTIONS.numClan
OPTIONS.popsize = numElephantInEachClan(i);
% Make sure the population does not have duplicates.
NewClan{i} = ClearDups(NewClan{i}, MaxParValue, MinParValue);
% Make sure each individual is legal.
NewClan{i} = FeasibleFunction(OPTIONS, NewClan{i});
% Calculate cost
NewClan{i} = CostFunction(OPTIONS, NewClan{i},Histogram, q);
% the number of fitness evaluations
nEvaluations = nEvaluations + OPTIONS.popsize;
% Sort from best to worst
NewClan{i} = PopSort(NewClan{i}, range);
end % end for i
OPTIONS.popsize = SavePopSize;
%%
for evolution=1:10
disp(['->> Iteration (',num2str(evolution),'):']);
Xg=population(1,:) % global best frog
arryM=cell(1,m);
temp_pop=population;
while ~isempty(temp_pop)
for j=1:m
arryM{j}=[arryM{j};temp_pop(1,:)];
temp_pop(1,:)=[];
end
end
%% Loacal Search
for i=1:m
for j=1:iter
Xb= arryM{i}(1,:); % best frog in current arryM
Xw= arryM{i}(end,:); % worst frog in current arryM
D=rand*(Xb-Xw);
new_sol=arryM{i}(end,:)+D;
fxw=f(Xw);
if f(new_sol)>fxw
arryM{i}(end,:)=new_sol;
else
D=rand*(Xg-Xw);
new_sol=arryM{i}(end,:)+D;
if f(new_sol)>fxw
arryM{i}(end,:)=new_sol;
else
arryM{i}(end,:)=rand(1,meme);
end
end
for ii=1:size(arryM{i},1)
fitpx(ii)=f(arryM{i}(ii,:));
end
[~,idx]=sort(fitpx,'descend');
arryM{i}=arryM{i}(idx,:);
end
end
%% Shuffling
spop=[];
for i=1:m
spop=[spop;arryM{i}];
end
for i=1:pop_size
fitness(i)=fitnessFunction(spop(i,:));
end
[fit,index]=sort(fitness,'descend'); % Sort in descending order
population=spop(index,:);
cfitness=fit(1)
end
PSNR=fitnessFunction(Xg,1)
MSE= 255 * exp(-PSNR/20)
RMSE=sqrt( 255 * exp(-PSNR/20))