clc;clear;
%rand('state',0); %reset the random var
%--------------------------------------------------------------------
%----------- Enter the induction generator parameters ---------------
%--------------------------------------------------------------------
%Rs=0.0982;
%Rr=0.0621;
%zlo=1.505;
%pf=1;
%Xs=0.112;
%Xr=0.0952;
%u=1.0;
%RL=zlo*pf;
%XL=sqrt(zlo^2-RL^2);
%Xm(1)=2.58; %(the initial value of Xm)
%P=[Rs Rr RL XL Xs Xr u Xm(1)];
popo_size=20; % numbre of individual
real_var=2; % numbre of variables
upper=[5 10]; % upper limits of variables
lower=[0 0]; % lower limits of variabes
varlength=[6 6]; % number of digits by variable
decimal=[2 2]; % number of digits for each variable in the chromosome
Elitisme=1; % on or off of Elitism, (0 is off and 1 is on)
cross_prob=0.8; % Probabilty of crossover
mutat_prob=0.05; % probability of mutation
MAX_GENE=1000; %maximum number of generation
% is not stoped by another
acc_gene=0.01; % if the obtained solution
% by this accuracy, the pro
del_gene=100; % if the fitness changes is less
% than acc_gene within del_gene the program is terminated
n=9; % numbre of function summets; this increases fifficlty degree
generation=1;g=1;
for i=1:popo_size
for j=1:real_var
population(j,i,generation)=...
(rand-(1/2))*(upper(j)-lower(j))+...
(1/2)*(upper(j)+lower(j));
%upper(j)-rand*(upper(j)-lower(j));
end;
end;
ppp=population(:,:,generation);
fitness_bar=CostFunction(ppp);
clear ppp;
chromo_length=sum(varlength)+real_var;
signdigits(1)=1;%
for i=1:real_var
signdigits(i+1)=signdigits(i)+varlength(i)+1;
end;
% fitness_chrom=nsummet(population(:,:,generation));% calculate the intial fitness
[bestfit(g),rangeb]=max(fitness_bar);% save the best fitness
% and the rank of the individual
% and the rank of the individual
bestind(:,g)=population(:,rangeb,generation);%save the best individual
while generation <=MAX_GENE
g=g+1;
for i=1:popo_size
for j=1:real_var
% make sure that population is within its limits
if population(j,i,generation)>upper(j)
population(j,i,generation)=upper(j);
elseif population(j,i,generation)<lower(j)
population(j,i,generation)=lower(j);
end % end of if
%sign encoding
%if population(j,i,generation)<0, then signdigit(j,i)=0 else
%signdigit(j,i)=9
if population(j,i,generation)<0
coded_popo(signdigits(j),i)=0;
else
coded_popo(signdigits(j),i)=9;
end % end of if
% elimination of sign from the original population
popo_1(j,i,generation)= abs(population(j,i,generation));
% here starts the 10 base coding
% the numbre of decimal degits is 2, then we the devide the number by 10
% then by iterative way, we the save integer gigit until the last one
% which depends on the preset variable size
popo_1(j,i,generation)=popo_1(j,i,generation)/(10^(decimal(j)-1));
for k=signdigits(j)+1:signdigits(j+1)-1
coded_popo(k,i)=popo_1(j,i,generation)-...
rem(popo_1(j,i,generation),1);
popo_1(j,i,generation)=(popo_1(j,i,generation)-...
coded_popo(k,i))*10;
end % enf of for k
end; %enf for j
end; % end for i
% fitness calculation, each individual will give some fitness that will
% be used to choose the parent pool
summefitness=0;
ppp=population(:,:,generation);
fitness_bar=CostFunction(ppp);
clear ppp;
% fitness_chrom=nsummet(population(:,:,generation));
min=0;%-4;
for i=1:popo_size
% fitness(i)=fitness_chrom(i)+min;
fitness(i)=fitness_bar(i)+min;
summefitness=summefitness+fitness(i);
end
[bestfitness(generation),rangeb]=max(fitness);% save the best fitness
% and the rank of the individual
%[worstfitness(generation),rangew]=min(fitness);% save also the worst fitness
% and the rank of the individual
bestindividual(:,generation)=population(:,rangeb,generation);%save the best individual
%worstindividual(:,generation)=population(:,rangew,generation);%save the worst individual
fitness_average(generation)=summefitness/popo_size;% calculate the average of the fitness
if bestfit(g-1)<bestfitness(generation)
bestfit(g)=bestfitness(generation);
bestind(:,g)=bestindividual(:,generation);
else
bestfit(g)=bestfit(g-1);
bestind(:,g)=bestind(:,g-1);
end,
% before we procced to breeding offsprings, the mating pool must be
% determined, this depends on the method used to select parents for
% reproduction. if the elitisme is selected the best individual will
% forwardly go over the next generation. in this program the roulette
% method has been chosen to select parent. A random fitness is generated
% and the whell is spin untill the individual having fitness greater than
% this randam value is reached.
for i=1:popo_size
if Elitisme==1&i==rangeb
pop_parent(:,i)=coded_popo(:,rangeb);
else
pointer=rand*summefitness;% roulette method
member=1;
tot=fitness(1);
while tot<pointer %tot<pointer
member=member+1;
tot=tot+fitness(member);
end %end of while
pop_parent(:,i)=coded_popo(:,member);
end;%end of if
end
% to breed children by crossover, tow parents must be chosen randomly from
% the mating pool, once done, if the crossover probabilty is greater than
% a genarted random number [0,1], the crossover occurs. if the elitisme is
% on the elit individual goes over the incomming children.
for parent1=1:popo_size
if Elitisme==1&parent1==rangeb
childc(:,parent1)=pop_parent(:,rangeb);
else
parent2=parent1;
while parent2==parent1
parent2=rand*popo_size;
parent2=parent2-rem(parent2,1)+1;
end % end of while
if cross_prob>rand % if true the
site=rand*chromo_length;
site=site-rem(site,1)+1; % the crossover site
childc(1:site,parent1)=pop_parent(1:site,parent1);
childc(site+1:chromo_length,parent1)=...
pop_parent(site+1:chromo_length,parent2);
else
childc(:,parent1)=pop_parent(:,parent1);
end; % end of if cross
end %end of if elitism
end %end of for parent1
% the next operation is the mutation, breeding offsprings by mutation
% occurs if the mutation probabilty is greater than a genrated random
% number. If it's so, a mutation site will be dtermined and a new digit
% is generated between [0 9] to replace the mutation site digit.
for i=1:popo_size
if Elitisme==1&i==rangeb
childc(:,i)=childc(:,rangeb);
else
for site=1:chromo_length
if mutat_prob>rand
digit_site=rand*10;
while childc(site,i)==digit_site-rem(digit_site,1)
digit_site=rand*10;
end;
childc(site,i)=digi
评论0