0% found this document useful (0 votes)
10 views

Code

The document describes an optimization algorithm called Elephant Herding Optimization that is used for K-means image segmentation. It takes parameters like population size, number of generations, image to read, and initializes the population randomly. It then performs optimization over multiple generations by dividing the population into clans, updating positions based on clan centers, and combining the populations. The best solutions are tracked over generations and the algorithm stops when the standard deviation of best solutions over last 30 generations is less than 0.01.

Uploaded by

Abhay Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Code

The document describes an optimization algorithm called Elephant Herding Optimization that is used for K-means image segmentation. It takes parameters like population size, number of generations, image to read, and initializes the population randomly. It then performs optimization over multiple generations by dividing the population into clans, updating positions based on clan centers, and combining the populations. The best solutions are tracked over generations and the algorithm stops when the standard deviation of best solutions over last 30 generations is less than 0.01.

Uploaded by

Abhay Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

%--------------------------------------------------------------

% The Kmeans segmentation optimization using Elephant Herding Optimization


Algorithm
% Input parameters Taken:
% Static parameters:
% The image to be segmented: im;
% Number of generations: iteration;
% Dynamic parameters:
% Population size = 100;
% Number of clans = 5;
% Keep = 2;
% Alpha = 0.5;
% Beta = 0.1;
% Upper Bound for image thresholding (UB) = 1;
% Lower Bound for image thresholding (LB) = 0.
% as the image is scaled to 0-1 by dividing it by 255

%% optimized k-menas by frog leaping algorithm for image segmentation


clear
clc
close all
global I data
parametros.pop_size = 200;
parametros.numClan = 5;
parametros.LB = 2;
parametros.UB = 253;
%quantos elefantes deixar para a próxima geração
parametros.Keep = 2;
%fator de influencia da matriarca no resto do clã

% 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,:);

% Begin the optimization loop


IndexG = 1;
while IndexG < OPTIONS.Maxgen

%% Save the best elephants in a temporary array.


for j = 1 : keep
chromKeep(j,:) = population(j).chrom;
costKeep(j) = population(j).cost;
end
%%

%% Divide the whole elephant population into some clans


j =1;
popindex = 1;
while popindex <= OPTIONS.popsize
for cindex = 1 : OPTIONS.numClan
Clan{cindex}(j) = Population(popindex);
popindex = popindex + 1;
end

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;
%%

%% Combine Population1 with Population2 to generate a new Population


Population = CombineClan(OPTIONS, NewClan);
% Sort from best to worst
Population = PopSort(Population, range);
%%

%% Replace the worst with the previous generation's elites.


n = length(Population);
for k3 = 1 : Keep
Population(n-k3+1).chrom = chromKeep(k3,:);
Population(n-k3+1).cost = costKeep(k3);
end % end for k3
%%

% Sort from best to worst


Population = PopSort(Population, range);
% Compute the average cost
[AverageCost, nLegal] = ComputeAveCost(Population);
% Display info to screen
MinCost = [MinCost Population(1).cost];
AvgCost = [AvgCost AverageCost];
if DisplayFlag
disp(['The best and mean of Generation # ', num2str(IndexG), ' are
',...
num2str(MinCost(end)), ' and ', num2str(AvgCost(end))]);
end
% % % % % % % % % % % End of Precess and output the results
%%%%%%%%%% %% %
%%
melhores_avaliacoes(IndexG, 1) = Population(1).cost;
if IndexG > 30 && std(melhores_avaliacoes(IndexG-30:IndexG)) < 0.01
numGeracoes = IndexG;
break;
end
%% Update generation number
IndexG = IndexG+1;

end % end for nEvaluations

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))

You might also like