-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgaGnuOct.m
90 lines (84 loc) · 2.76 KB
/
gaGnuOct.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function [x popx fitvals] = gaGnuOct(ffit, nbits, gaOptions, varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Description: Essence of a Genetic Algorithm for Matlab or Gnu Octave. %%
%% Author: Laboratory of Bioinformatics UFPR/SEPT - Roberto Tadeu Raittz. %%
%% Adaptation: Eduardo Lemons Francisco - Hexabio Bioinformatics. %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Function: Runs a canonic (binary) genetic algorithm.
%
% Example:
% x = gaGnuOct(@fitnessFunction, numberOfBits, gaOptions);
%
% "x" receives numberOfBits bits optimized according to the user function 'fitnessFunction';
% gaOptions is an struct compatible with that generated by Matlab "gaoptimset" or
% one struct var that contains at least the parameters:
%
% gaOptions.InitialPopulation
% gaOptions.PopulationSize
% gaOptions.Generations
% gaOptions.EliteCount
% gaOptions.MutationRate
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Initialization %%
m = nbits;
n = gaOptions.PopulationSize;
nger = gaOptions.Generations;
mutrate = gaOptions.MutationRate; %% Uses only uniform mutation rate (number 0..1)
nelite = gaOptions.EliteCount;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if isempty(gaOptions.InitialPopulation)
pop0 = logical(randi(2,n,m)-1);
else
pop0 = gaOptions.InitialPopulation;
end
if isempty(mutrate) || iscell(mutrate)
mutrate = 0.15;
end
if isempty(nelite)
nelite = 2;
end
nmut = fix(mutrate*n);
popx = pop0;
fits = zeros(n,1);
%% Generations %%
for j=1:nger
for i=1:n
fits(i) = ffit(popx(i,:));
end
[xx iord] = sort(fits);
%disp(['Generation: ' num2str(j) "\t\tElite: " num2str(xx(1:nelite)')]);
popx = popx(iord,:);
newpop = popx;
%% Crossing-over %%
i1 = randipow(n,1.3,n,1);
i2 = randipow(n,1.3,n,1);
pop1 = popx(i1,:);
pop2 = popx(i2,:);
icros = repmat(randi(m,n,1),1,m);
ids = repmat(1:m,n,1);
crt = (ids>icros);
newpop(crt) = pop1(crt);
newpop(~crt) = pop2(~crt);
%% Mutation %%
if nmut>0
popmut = newpop(1:nmut,:);
ids = find(popmut | ~popmut);
xmut = randi(length(ids),nmut,1);
popmut(xmut) = ~popmut(xmut);
end
%% Elitism %%
if nelite>0
newpop(1:nelite,:) = popx(1:nelite,:);
end
%% Create new population %%
newpop(1+nelite:nelite+nmut,:) = popmut;
popx = newpop;
end
%% Returns %%
[fitvals iord] = sort(fits);
popx = popx(iord,:);
x = popx(1,:);
%% Used for selection, see: http://www.sbgames.org/papers/sbgames08/posters/papers/p19.pdf %%
function M = randipow(xmax,xpow,n, m)
M = fix((xmax)*(rand(n,m).^xpow)) + 1;