GAMATLAB

2
Evolutionary Algorithms Problem Set - Genetic Algorithms in MATLAB 1. Below you find the sceleton of a canonical GA implementation in MATLAB: function [opt, fopt, stat] = ga(l, fitnessfct, decodefct, selectfct, stopeval) % Strategy parameters lambda = ... pc = ... pm = ... % Statistics administration evalcount = 0; stat.histf = zeros(1, stopeval); % Initialize population for i = 1:lambda % Generate random chromosome, decode to phenotype, and evaluate P(:,i) = ... % random bitstring g(:,i) = feval(decodefct, P(:,i)); f(i) = feval(fitnessfct, g(:,i)); % Statistics administration evalcount = evalcount + 1; [fopt, optindex] = max(f); aopt = P(:,optindex); stat.histf(evalcount) = fopt; end % Evolution loop while evalcount < stopeval % Generate new population (recombination, mutation) for i = 1:lambda p1 = feval(selectfct, P, f); if (rand() < pc) p2 = feval(selectfct, P, f); Pnew(:,i) = ... % crossover else Pnew(:,i) = ... % copy end Pnew(:,i) = ... % mutation end % Replace old population by new population P = Pnew; % Decode and evaluate for i = 1:lambda g(:,i) = feval(decodefct, P(:,i)); f(i) = feval(fitnessfct, g(:,i)); 1

Transcript of GAMATLAB

Page 1: GAMATLAB

Evolutionary AlgorithmsProblem Set - Genetic Algorithms in

MATLAB1. Below you find the sceleton of a canonical GA implementation in MATLAB:

function [opt, fopt, stat] = ga(l, fitnessfct, decodefct, selectfct, stopeval)

% Strategy parameterslambda = ...pc = ...pm = ...

% Statistics administrationevalcount = 0;stat.histf = zeros(1, stopeval);

% Initialize populationfor i = 1:lambda

% Generate random chromosome, decode to phenotype, and evaluateP(:,i) = ... % random bitstringg(:,i) = feval(decodefct, P(:,i));f(i) = feval(fitnessfct, g(:,i));

% Statistics administrationevalcount = evalcount + 1;[fopt, optindex] = max(f);aopt = P(:,optindex);stat.histf(evalcount) = fopt;

end

% Evolution loopwhile evalcount < stopeval

% Generate new population (recombination, mutation)for i = 1:lambdap1 = feval(selectfct, P, f);if (rand() < pc)p2 = feval(selectfct, P, f);Pnew(:,i) = ... % crossover

elsePnew(:,i) = ... % copy

endPnew(:,i) = ... % mutation

end

% Replace old population by new populationP = Pnew;

% Decode and evaluatefor i = 1:lambdag(:,i) = feval(decodefct, P(:,i));f(i) = feval(fitnessfct, g(:,i));

1

Page 2: GAMATLAB

end

% Statistics administration[fopt, optindex] = max(f);opt = P(:,optindex);for i = 1:lambdaevalcount = evalcount + 1;stat.histf(evalcount) = fopt;

end

% Plot statisticsclfsubplot(2,1,1)plot(stat.histf(1:evalcount))subplot(2,1,2)bar([1:l],opt)xlim([1 l])drawnow()

endend

(a) Complete the code.(b) Run the GA a couple of times on the ONEMAX problem (= counting ones problem)

provided below. Use bitstrings of length 100 and an evaluation budget of 100000 eval-uations. Use the phenotype decoding function (which is a dummy function in this caseas we don’t need any phenotype decoding) and selection function provided below:

function f = ONEMAX(a)f = sum(a);

end

function g = no_decoding(a)g = a;

end

function a = select_proportional(P, f)cumsum_f = cumsum(f);r = sum(f) * rand();i = 1;while (r >= cumsum_f(i))i = i + 1;

enda = P(:,i);

end

How does it perform? Compare also using different bitstring lengths.(c) In the lecture, also a ’fix’ for proportional selection was discussed in which the fitness

values are rescaled. Implement a function select one scaled proportional whichimplements this fixed proportional selection method. Does it make a difference on thecounting ones?

(d) Also construct a tournament selection method and compare it against the other twoselection methods.

2. Implement a genotype decoding function such that the GA can be used to optimize real-valued optimization problems. Test it on a 10-dimensional version of the sphere function(f(~x) =

∑Ni=1 x2

i with xi ∈ R), use the search interval xi ∈ [−10, 10].

2