-
Notifications
You must be signed in to change notification settings - Fork 0
/
jGA2.m
101 lines (101 loc) · 3.18 KB
/
jGA2.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
91
92
93
94
95
96
97
98
99
100
101
%-------------------------------------------------------------------------%
% Genetic Algorithm (GA) source codes demo version %
% %
% Programmer: Jingwei Too %
% %
% E-Mail: [email protected] %
%-------------------------------------------------------------------------%
function [sFeat,Sf,Nf,curve]=jGA2(feat,label,N,T,CR,MR)
%---Inputs-----------------------------------------------------------------
% feat: features
% label: labelling
% N: Number of chromosomes
% T: Maximum number of generations
% CR: Crossover rate
% MR: Mutation rate
%---Outputs----------------------------------------------------------------
% sFeat: Selected features
% Sf: Selected feature index
% Nf: Number of selected features
% curve: Convergence curve
%--------------------------------------------------------------------------
% Objective function
fun=@jFitnessFunction;
% Number of dimensions
D=size(feat,2);
% Initial population
X=zeros(N,D); fit=zeros(1,N);
for i=1:N
for d=1:D
if rand() > 0.5
X(i,d)=1;
end
end
end
% Number of crossover
CR=round(CR*N);
% Fitness
for i=1:N
fit(i)=fun(feat,label,X(i,:));
end
% Pre
X1=zeros(CR,D); X2=zeros(CR,D); Fnew=zeros(1,2*CR); curve=inf; t=1;
figure(1); clf; axis([1 100 0 0.5]); xlabel('Number of Iterations');
ylabel('Fitness Value'); title('Convergence Curve'); grid on;
%---Generations start------------------------------------------------------
while t <= T
% Convert error to accuracy (inverse of fitness)
Ifit=1-fit;
% Get probability
Prob=Ifit/sum(Ifit);
% {1} Crossover
for i=1:CR
% Select two parents
k1=jRouletteWheelSelection(Prob); k2=jRouletteWheelSelection(Prob);
% Store parents
P1=X(k1,:); P2=X(k2,:);
% Random select one crossover point
ind=randi([1,D]);
% Single point crossover between 2 parents
X1(i,:)=[P1(1:ind),P2(ind+1:D)]; X2(i,:)=[P2(1:ind),P1(ind+1:D)];
end
% Union
Xnew=[X1;X2];
% {2} Mutation
for i=1:2*CR
for d=1:D
if rand() <= MR
% Mutate from '0' to '1' or '1' to '0'
Xnew(i,d)=1-Xnew(i,d);
end
end
% Fitness
Fnew(i)=fun(feat,label,Xnew(i,:));
end
% Merge population
XX=[X;Xnew]; FF=[fit,Fnew];
% Select N best solution
[FF,idx]=sort(FF); X=XX(idx(1:N),:); fit=FF(1:N);
% Best chromosome
Xgb=X(1,:); fitG=fit(1); curve(t)=fitG;
% Plot convergence curve
pause(0.000000001); hold on;
CG=plot(t,fitG,'Color','r','Marker','.'); set(CG,'MarkerSize',5);
t=t+1;
end
% Select features based on selected index
Pos=1:D; Sf=Pos(Xgb==1); Nf=length(Sf); sFeat=feat(:,Sf);
end
%---Call Function----------------------------------------------------------
function Route=jRouletteWheelSelection(Prob)
% Cummulative summation
C=cumsum(Prob);
% Random one value, most probability value [0~1]
P=rand();
% Route wheel
for i=1:length(C)
if C(i) > P
Route=i; break;
end
end
end