Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

I WANT TO PASS A INFORMATION OF A BLOCK OF A IMAGE THROUGH binary genetic algorithm and decide it fitness function.what changes i need to make for it.

1 visualizzazione (ultimi 30 giorni)
rRGB1=RGB(:,:,1);
gRGB1=RGB(:,:,2);
bRGB1=RGB(:,:,3);
rRGB=double(rRGB1/255);
gRGB=double(gRGB1/255);
bRGB=double(bRGB1/255);
M = [0.4124564 0.3575761 0.1804375
0.2126729 0.7151522 0.0721750
0.0193339 0.1191920 0.9503041] ;
X1 = rRGB * 0.4124564 + gRGB * 0.3575761 + bRGB* 0.1804375;
Y1 = rRGB * 0.2126729 + gRGB * 0.7151522 + bRGB* 0.0721750 ;
Z1= rRGB * 0.0193339 + gRGB * 0.1191920 + bRGB* 0.9503041 ;
X = X1 / 0.950456;
Y = Y1;
Z = Z1 / 1.088754;
Cb=500 *((X/X1)- (Y/Y1));
Cr= 200 * ((Y/Y1)-(Z/Z1));
s=size(Cb);
n=4; %block size
bs=[n n];
nb=s./bs;
a_bl=mat2cell(img,repmat(bs(1),1,nb(1)),repmat(bs(2),1,nb(2)));
ff=inline('sum(a_bl,2)'); %objective function
npar=2; %number of optimization variables {set up GA}
varhi=10 ;
varlo=0; %variable limits
maxit=100; %max no of iterations
mincost=9999999; %Maximum allowable cost
%GA parameters
popsize=16; % set population size
mutrate=.15; % set mutation rate
selection=0.5; % fraction of population kept
nbits=8; % number of bits in each parameter
Nt=nbits*npar; % total no of bits in a chromosome
keep=floor(selection*popsize); %population members that survive(population)
%Create the initial population
pop=round(rand(popsize,Nt)); % random population of 1’s and 0’s (100 x 8 matrix)
par=(varhi-varlo)*rand(popsize,Nt)+varlo ; %convert binary to continious values
cost=feval(ff,par); %calculates population cost using ff
[cost,ind]=sort(cost); %min cost of element1
par=par(ind,:);pop=pop(ind,:); %sorts population with lowest cost first
minc(1)=min(cost); %min of population
meanc(1)=mean(cost); %meanc contains mean of population
probs=cost/sum(cost); % simple normalization for probabilities
%iteration
iga=0; %generation counter initilaized
while iga<maxit
iga=iga+1; %increase of generation counter
M=ceil((popsize-keep)/2); %no of matings
Nodds=length(probs);
odds=keep-probs+1;
pick1=ceil(Nodds*rand(1,M)); %mate #1
pick2=ceil(Nodds*rand(1,M)); %mate #2
%ma & pa contain the idices of the chromosome that will mate
ma=probs(pick1);
pa=probs(pick2);
%ma & pa contain the idices of the chromosome that will mate
ic=1;
while ic<=M
for id=2:keep+1;
if pick1(ic)<=odds(id) && pick1(ic)>odds(id-1)
ma(ic)=id-1;
end
if pick2(ic)<=odds(id)
pa(ic)=id-1;
else if pick2(ic)>odds(id-1)
pa(ic)=id-1;
end
end
end
ic=ic+1;
end
cross=ceil((Nt-1)*rand(M,1)); %crossover point
for ic=1:2:M
pop(ceil(M*rand),1:cross)=pop(ic,1:cross);
pop(ceil(M*rand),cross+1:Nt)=pop(ic+1,cross+1:Nt);
pop(ceil(M*rand),1:cross)=pop(ic+1,1:cross);
pop(ceil(M*rand),cross+1:Nt)=pop(ic,cross+1:Nt);
end
% Mutate the population
nmut=ceil((popsize-1)*Nt*mutrate); % total number of mutations
mrow=ceil(rand(1,nmut)*(popsize-1))+1; % row to mutate
mcol=ceil(rand(1,nmut)*Nt); % column to mutate
for ii=1:nmut
pop(mrow(ii),mcol(ii))=abs(pop(mrow(ii),mcol(ii))-1); % toggles bits
end
%The population is re-evaluated for cost
cost=feval(ff,pop); % calculates population cost using fitness function
cost(2:popsize)=feval(ff,par(2:popsize,:));
%Sort the costs and associated parameters
[cost,ind]=sort(cost); % min element in first entry
par=par(ind,:);pop=pop(ind,:);
%Do statistics for a single nonaveraging run
minc(iga+1)=min(cost);
meanc(iga+1)=mean(cost);
probs=cost/sum(cost);
%stopping criteria
if iga>maxit|| cost(1)>mincost
break
end
end
but the fitness function is not coming. what is the problem?

Risposte (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by