-
Notifications
You must be signed in to change notification settings - Fork 7
/
GA.m
111 lines (72 loc) · 2.23 KB
/
GA.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
102
103
104
105
106
107
108
109
110
111
% Genetic Algorithm by Sadegh Salesi and Georgina Cosma %
% Programmed by Sadegh Salesi at Nottignham Trent University %
% Last revised: 2017 %
% Reference: S. Salesi and G. Cosma, A novel extended binary cuckoo search algorithm for feature selection, 2017 2nd International Conference on Knowledge Engineering and Applications (ICKEA), London, 2017, pp. 6-12.
% https://ieeexplore.ieee.org/document/8169893
% Copyright (c) 2017, Sadegh Salesi and Georgina Cosma. All rights reserved.
% -----------------------------------------------------------------
clc
clear
close all
format shortG
for nrun=1:10
%% parameters setting
X=xlsread('data_bcp');
Y=xlsread('target_bcp');
nvar=size(X,2);
fen=10000;
popsize=30;
pc=0.9;
ncross=2*round((popsize*pc)/2);
pm=0.1;
nmut=round(popsize*pm);
maxiter=floor((fen-popsize)/((2*ncross)+nmut));
%% initial population algorithm
emp.var=[];
emp.fit=[];
emp.x=[];
emp.acc=[];
emp.nfeat=[];
pop=repmat(emp,popsize,1);
for i=1:popsize
pop(i).var=round(rand(1,nvar));
if sum(pop(i).var)>0
[pop(i).fit,pop(i).acc,pop(i).nfeat]=svm(X,Y,pop(i).var);
else
pop(i).fit=Inf;
end
end
[value,index]=min([pop.fit]);
gpop=pop(index);
%% main loop algorithm
BEST=zeros(maxiter,1);
tic
for iter=1:maxiter
% crossover
crosspop=repmat(emp,ncross,1);
crosspop=crossover(crosspop,pop,nvar,X,Y,ncross);
% mutation
mutpop=repmat(emp,nmut,1);
mutpop=mutation(mutpop,pop,nvar,X,Y,nmut,popsize);
[pop]=[pop;crosspop;mutpop];
[value,index]=sort([pop.fit]);
pop=pop(index);
gpop=pop(1);
pop=pop(1:popsize);
BEST(iter)=gpop.fit;
disp([' Run = ' num2str(nrun) ' Iter = ' num2str(iter) ' BEST = ' num2str(BEST(iter)) ' Acc = ' num2str(gpop.acc) ' Nfeat = ' num2str(gpop.nfeat) ])
end
save(nrun,1)=gpop.acc;
save(nrun,2)=gpop.nfeat;
save(nrun,3)=toc;
end
%% results algorithm
%disp([ ' Best Solution = ' num2str(find(gpop.var==1))])
%disp([ ' Best Fitness = ' num2str(gpop.fit)])
%disp([ ' Time = ' num2str(toc)])
% figure(1)
% plot(BEST,'r')
% xlabel('Iteration')
% ylabel('Fitness')
% legend('BEST')
% title('GA')