You are on page 1of 6

Appendix B

A Simple Genetic Algorithm

The m-files given in the Appendices that follow can be downloaded from the
authors web site www.lar.ee.upatras.gr/reking

% Main_Program - ga.m

popsize=10; % Population Size


maxgen=50; % Number of Generations (iterations)
length=12; % Length of genotype (i.e. number of bits
in the binary array)
pcross=0.8; % Probability of Crossover
pm=0.01; % Probability of Mutation
bits=[length length];
vlb=[-1 -1];
vub=[1 1];
phen=init(vlb,vub,popsize,2); % Initialization of the
population of phenotypes
[gen, lchrom, coarse, nround] = encode(phen, vlb,
vub, bits); % Conversion of phenotypes to binary
string
[fitness, object]=score(phen,popsize); % Evaluation
of Fitness & Objective Function
x=-1:0.1:1; % Display of the contour graph of the
objective function
y=-1:0.1:1;
[x1,y1]=meshgrid(x,y);
z=x1.^2+y1.^2-0.3*cos(3*pi*x1)-0.4*cos(4*pi*y1)+0.7;
figure(1);
contour(x,y,z); 279
280 Appendix B

[best_obj(1), index] = min(object); % Store the best


candidate of the initial population
best_gen=gen(index,:);
best_phen=phen(index, :);
[worst_obj(1), index1] = max(object); % Store the
worst candidate of the initial population
worst_cur_gen=gen(index1);
worst_cur_phen=phen(index1);
avg_obj(1)=0; % Calculate the average performance of
the population
for k=1:popsize
avg_obj(1)=avg_obj(1)+object(k);
end;
avg_obj(1)=avg_obj(1)/popsize;
best_x(1)=best_phen(1);
best_y(1)=best_phen(2);
for i1=1:2
fprintf(1,'%f ',best_phen(1));
end;
fprintf('\n');
fprintf(1,'BEST : %f WORST : %f AVG : %f
\n',best_obj(1),worst_obj(1),avg_obj(1));
for i=1:maxgen % Start of the Genetic-Loop
newgen=reproduc(gen,fitness); % Reproduction
gen=mate(gen); % Mate two members of the
population
gen=xover(gen, pcross); % Crossover Operation
gen=mutate(gen, pm); % Mutation Operation
[phen, coa] = decode(gen, vlb, vub, bits); %
Decode the genotype of the new population
to phenotype
[fitness, object]=score(phen,popsize); %
Evaluation
of the Fitness & Objective Functions
[best_cur_obj, index] = min(object); % Store
the best candidate of the current population
best_cur_gen=gen(index, :);
best_cur_phen=phen(index, :);
[worst_obj(i+1), index1] = max(object); % Store
the worst candidate of the current population
worst_cur_gen=gen(index1);
worst_cur_phen=phen(index1);
avg_obj(i+1)=0; % Average performance of the
current population
for k=1:popsize
avg_obj(i+1)=avg_obj(i+1)+object(k);
Simple Genetic Algorithm 281

end;
avg_obj(i+1)=avg_obj(i+1)/popsize;

if(best_cur_obj > best_obj(i)) % Apply Elitist


Strategy
phen(index1,:) = best_phen;
gen(index1,:) = best_gen;
object(index1) = best_obj(i);
best_obj(i+1) = best_obj(i);
elseif(best_cur_obj <= best_obj(i))
best_phen = best_cur_phen;
best_gen = best_cur_gen;
best_obj(i+1) = best_cur_obj;
end;
best_x(i+1)=best_phen(1); % Display evolution
of the best solution on the contour graph
best_y(i+1)=best_phen(2);
hold;
line(best_x,best_y);
for i1=1:2
fprintf(1,'%f ',best_phen(i1));
end;
fprintf(1,'---> %f\n',best_obj(i+1));
fprintf('\n');
fprintf(1,'BEST : %f WORST : %f AVG : %f
\n',best_obj(i+1),worst_obj(i+1),avg_obj(i+1));
end
xx=1:maxgen+1; % Display evolution of objective
functions for the worst, average and best
solutions
figure(2);
plot(xx,best_obj,xx,worst_obj,xx,avg_obj);
grid;
% File init.m - This function creates a random
population
function phen=init(vlb,vub, siz, sea)
for i=1:siz
phen(i,:)=(vub-vlb).*rand(1, sea) + vlb;
end

% File score.m - This function computes the fitness


and the objective function values of a population
function [fitness, object]=score(phen, popsize)
for i=1:popsize
282 Appendix B

object(i)=phen(i,1)^2+phen(i,2)^2-
0.3*cos(3*pi*phen(i,1))-
0.4*cos(4*pi*phen(i,2))+0.7;
fitness(i)=1/(object(i)+1);
end

The following m-files called by the main


program can be downloaded directly from the
MATHWORKS web site www.mathworks.com and
bear the indication:
% Copyright (c) 1993 by the MathWorks, Inc.
% Andrew Potvin 1-10-93.

% File encode.m - This function converts a variable


from
real to binary
function [gen,lchrom,coarse,nround] =
encode(x,vlb,vub,bits)
lchrom = sum(bits);
coarse = (vub-vlb)./((2.^bits)-1);
[x_row,x_col] = size(x);
gen = [];
if ~isempty(x),
temp = (x-ones(x_row,1)*vlb)./ ...
(ones(x_row,1)*coarse);
b10 = round(temp);
nround = find(b10-temp>1e-4);
gen = b10to2(b10,bits);
end
% File reproduc.m - This function selects individuals
in accordance to their fitness
function [new_gen,selected] = repro-
duc(old_gen,fitness)
norm_fit = fitness/sum(fitness);
selected = rand(size(fitness));
sum_fit = 0;
for i=1:length(fitness),
sum_fit = sum_fit + norm_fit(i);
index = find(selected<sum_fit);
selected(index) = i*ones(size(index));
end
new_gen = old_gen(selected,:);

% File mate.m - This function mates two members of


the population
Simple Genetic Algorithm 283

function [new_gen,mating] = mate(old_gen)


[junk,mating] = sort(rand(size(old_gen,1),1));
new_gen = old_gen(mating,:);

% File xover.m - This function performs the Crossover


operation
function [new_gen,sites] = xover(old_gen,Pc)
lchrom = size(old_gen,2);
sites = ceil(rand(size(old_gen,1)/2,1)*(lchrom-1));
sites = sites.*(rand(size(sites))<Pc);
for i = 1:length(sites);
new_gen([2*i-1 2*i],:) =
old_gen([2*i-12*i],1:sites(i)) ...
old_gen([2*i 2*i-1],sites(i)+1:lchrom)];
end

% File mutate.m - This function performs the Mutation


operation
function [new_gen,mutated] = mutate(old_gen,Pm)
mutated = find(rand(size(old_gen))<Pm);
new_gen = old_gen;
new_gen(mutated) = 1-old_gen(mutated);

% File decode.m - This function coverts a variable


from binary to real
function [x,coarse] = decode(gen,vlb,vub,bits)
bit_count = 0;
two_pow = 2.^(0:max(bits))';
for i=1:length(bits),
pow_mat((1:bits(i))+bit_count,i) =
two_pow(bits(i):-1:1);
bit_count = bit_count + bits(i);
end
gen_row = size(gen,1);
coarse = (vub-vlb)./((2.^bits)-1);
inc = ones(gen_row,1)*coarse;
x = ones(gen_row,1)*vlb + (gen*pow_mat).*inc;

% File b10tob2 - This function converts a variable


from base 10 to base 2
function b2 = b10to2(b10,bits)
bit_count = 0;
b2_index = [];
284 Appendix B

bits_index = 1:length(bits);
for i=bits_index,
bit_count = bit_count + bits(i);
b2_index = [b2_index bit_count];
end
for i=1:max(bits),
r = rem(b10,2);
b2(:,b2_index) = r;
b10 = fix(b10/2);
tbe = find( all(b10==0) | (bits(bits_index)==i) );
if ~isempty(tbe),
b10(:,tbe) = [];
b2_index(tbe) = [];
bits_index(tbe) = [];
end
if isempty(bits_index),
return
end
b2_index = b2_index-1;
end

You might also like