Skip to content

Commit c6edd90

Browse files
authored
Add files via upload
1 parent 95c122f commit c6edd90

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+6142
-0
lines changed

A_Main.m

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
%% Wrapper Feature Selection Demostration
2+
3+
% Source code of the FS methods are written based on pseudocode
4+
% There are more than 40 wrapper FS methods are offered
5+
% You may open < List_Method.m file > to check all available methods
6+
7+
%---Usage-------------------------------------------------------------
8+
% If you wish to use 'PSO' (see example 1) then you write
9+
% FS = jfs('pso',feat,label,opts);
10+
11+
% If you want to use 'SMA' (see example 2) then you write
12+
% FS = jfs('sma',feat,label,opts);
13+
14+
% * All methods have different name/abbrevation (refer jfs.m file)
15+
16+
17+
%---Input-------------------------------------------------------------
18+
% feat : Feature vector matrix (Instances x Features)
19+
% label : Label matrix (Instances x 1)
20+
% opts : Parameter settings
21+
% opts.N : Number of solutions / population size (* for all methods)
22+
% opts.T : Maximum number of iterations (* for all methods)
23+
% opts.k : Number of k in k-nearest neighbor
24+
25+
% Some methods have their specific parameters (example: PSO, GA, DE)
26+
% if you do not set them then they will define as default settings
27+
% * you may open the < m.file > to view or change the parameters
28+
% * you may use 'opts' to set the parameters of method (see example 1)
29+
% * you may also change the < jFitnessFunction.m file >
30+
31+
32+
%---Output------------------------------------------------------------
33+
% FS : Feature selection model (It contains several results)
34+
% FS.sf : Index of selected features
35+
% FS.ff : Selected features
36+
% FS.nf : Number of selected features
37+
% FS.c : Convergence curve
38+
% Acc : Accuracy of validation model
39+
40+
41+
%% Example 1: Particle Swarm Optimization (PSO)
42+
clear, clc, close;
43+
% Number of k in K-nearest neighbor
44+
opts.k = 5;
45+
% Ratio of validation data
46+
ho = 0.2;
47+
% Common parameter settings
48+
opts.N = 10; % number of solutions
49+
opts.T = 100; % maximum number of iterations
50+
% Parameters of PSO
51+
opts.c1 = 2;
52+
opts.c2 = 2;
53+
opts.w = 0.9;
54+
% Load dataset
55+
load ionosphere.mat;
56+
% Divide data into training and validation sets
57+
HO = cvpartition(label,'HoldOut',ho);
58+
opts.Model = HO;
59+
% Perform feature selection
60+
FS = jfs('pso',feat,label,opts);
61+
% Define index of selected features
62+
sf_idx = FS.sf;
63+
% Accuracy
64+
Acc = jknn(feat(:,sf_idx),label,opts);
65+
% Plot convergence
66+
plot(FS.c); grid on;
67+
xlabel('Number of Iterations');
68+
ylabel('Fitness Value');
69+
title('PSO');
70+
71+
72+
%% Example 2: Slime Mould Algorithm (SMA)
73+
clear, clc, close;
74+
% Number of k in K-nearest neighbor
75+
opts.k = 5;
76+
% Ratio of validation data
77+
ho = 0.2;
78+
% Common parameter settings
79+
opts.N = 10; % number of solutions
80+
opts.T = 100; % maximum number of iterations
81+
% Load dataset
82+
load ionosphere.mat;
83+
% Divide data into training and validation sets
84+
HO = cvpartition(label,'HoldOut',ho);
85+
opts.Model = HO;
86+
% Perform feature selection
87+
FS = jfs('sma',feat,label,opts);
88+
% Define index of selected features
89+
sf_idx = FS.sf;
90+
% Accuracy
91+
Acc = jknn(feat(:,sf_idx),label,opts);
92+
% Plot convergence
93+
plot(FS.c); grid on;
94+
xlabel('Number of Iterations');
95+
ylabel('Fitness Value');
96+
title('SMA');
97+
98+
99+
%% Example 3: Whale Optimization Algorithm (WOA)
100+
clear, clc, close;
101+
% Number of k in K-nearest neighbor
102+
opts.k = 5;
103+
% Ratio of validation data
104+
ho = 0.2;
105+
% Common parameter settings
106+
opts.N = 10; % number of solutions
107+
opts.T = 100; % maximum number of iterations
108+
% Parameter of WOA
109+
opts.b = 1;
110+
% Load dataset
111+
load ionosphere.mat;
112+
% Divide data into training and validation sets
113+
HO = cvpartition(label,'HoldOut',ho);
114+
opts.Model = HO;
115+
% Perform feature selection
116+
FS = jfs('woa',feat,label,opts);
117+
% Define index of selected features
118+
sf_idx = FS.sf;
119+
% Accuracy
120+
Acc = jknn(feat(:,sf_idx),label,opts);
121+
% Plot convergence
122+
plot(FS.c); grid on;
123+
xlabel('Number of Iterations');
124+
ylabel('Fitness Value');
125+
title('WOA');
126+
127+
128+
129+

_List_Methods.m

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
%---List of available wrapper FS methods------------------------------
2+
3+
% 2020
4+
'mpa' : Marine Predators Algorithm;
5+
'gndo' : Generalized Normal Distribution Optimization;
6+
'sma' : Slime Mould Algorithm;
7+
'eo' : Equilibrium Optimizer;
8+
'mrfo' : Manta Ray Foraging Optimization;
9+
% 2019
10+
'aso' : Atom Search Optimization;
11+
'hho' : Harris Hawks Optimization;
12+
'hgso' : Henry Gas Solubility Optimization;
13+
'pfa' : Path Finder Algorithm;
14+
'pro' : Poor And Rich Optimization;
15+
% 2018
16+
'boa' : Butterfly Optimization Algorithm;
17+
'epo' : Emperor Penguin Optimizer;
18+
'tga' : Tree Growth Algorithm;
19+
% 2017
20+
'abo' : Artificial Butterfly Optimization;
21+
'ssa' : Salp Swarm Algorithm;
22+
'sbo' : Satin Bower Bird Optimization;
23+
'wsa' : Weighted Superposition Attraction;
24+
% 2016
25+
'ja' : Jaya Algorithm;
26+
'csa' : Crow Search Algorithm;
27+
'sca' : Sine Cosine Algorithm;
28+
'woa' : Whale Optimization Algorithm;
29+
% 2015
30+
'alo' : Ant Lion Optimizer;
31+
'hlo' : Human Learning Optimization;
32+
'mbo' : Monarch Butterfly Optimization;
33+
'mfo' : Moth Flame Optimization;
34+
'mvo' : Multi Verse Optimizer;
35+
'tsa' : Tree Seed Algorithm;
36+
% 2014
37+
'gwo' : Grey Wolf Optimizer;
38+
'sos' : Symbiotic Organisms Search;
39+
% 2012
40+
'fpa' : Flower Pollination Algorithm;
41+
'foa' : Fruit Fly Optimization Algorithm;
42+
% 2009 - 2010
43+
'ba' : Bat Algorithm;
44+
'fa' : Firefly Algorithm;
45+
'cs' : Cuckoo Search Algorithm;
46+
'gsa' : Gravitational Search Algorithm;
47+
% Traditional
48+
'abc' : Artificial Bee Colony;
49+
'hs' : Harmony Search;
50+
'de' : Differential Evolution;
51+
'aco' : Ant Colony Optimization;
52+
'acs' : Ant Colony System;
53+
'pso' : Particle Swarm Optimization;
54+
'gat' : Genetic Algorithm (Tournament);
55+
'ga' : Genetic Algorithm (Roulette Wheel);
56+
'sa' : Simulated Annealing;
57+
58+

ionosphere.mat

56.9 KB
Binary file not shown.

jAntColonyOptimization.m

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
%[2009]-"Text feature selection using ant colony optimization"
2+
3+
% (9/12/2020)
4+
5+
function ACO = jAntColonyOptimization(feat,label,opts)
6+
% Parameters
7+
tau = 1; % pheromone value
8+
eta = 1; % heuristic desirability
9+
alpha = 1; % control pheromone
10+
beta = 0.1; % control heuristic
11+
rho = 0.2; % pheromone trail decay coefficient
12+
13+
if isfield(opts,'N'), N = opts.N; end
14+
if isfield(opts,'T'), max_Iter = opts.T; end
15+
if isfield(opts,'tau'), tau = opts.tau; end
16+
if isfield(opts,'alpha'), alpha = opts.alpha; end
17+
if isfield(opts,'beta'), beta = opts.beta; end
18+
if isfield(opts,'rho'), rho = opts.rho; end
19+
if isfield(opts,'eta'), eta = opts.eta; end
20+
21+
% Objective function
22+
fun = @jFitnessFunction;
23+
% Number of dimensions
24+
dim = size(feat,2);
25+
% Initial Tau & Eta
26+
tau = tau * ones(dim,dim);
27+
eta = eta * ones(dim,dim);
28+
% Pre
29+
fitG = inf;
30+
fit = zeros(1,N);
31+
32+
curve = inf;
33+
t = 1;
34+
% Iterations
35+
while t <= max_Iter
36+
% Reset ant
37+
X = zeros(N,dim);
38+
for i = 1:N
39+
% Random number of features
40+
num_feat = randi([1,dim]);
41+
% Ant start with random position
42+
X(i,1) = randi([1,dim]);
43+
k = [];
44+
if num_feat > 1
45+
for d = 2:num_feat
46+
% Start with previous tour
47+
k = [k(1:end), X(i, d-1)];
48+
% Edge/Probability Selection (2)
49+
P = (tau(k(end),:) .^ alpha) .* (eta(k(end),:) .^ beta);
50+
% Set selected position = 0 probability (2)
51+
P(k) = 0;
52+
% Convert probability (2)
53+
prob = P ./ sum(P(:));
54+
% Roulette Wheel selection
55+
route = jRouletteWheelSelection(prob);
56+
% Store selected position to be next tour
57+
X(i,d) = route;
58+
end
59+
end
60+
end
61+
% Binary
62+
X_bin = zeros(N,dim);
63+
for i = 1:N
64+
% Binary form
65+
ind = X(i,:);
66+
ind(ind == 0) = [];
67+
X_bin(i,ind) = 1;
68+
end
69+
% Fitness
70+
for i = 1:N
71+
% Fitness
72+
fit(i) = fun(feat,label,X_bin(i,:),opts);
73+
% Global update
74+
if fit(i) < fitG
75+
Xgb = X(i,:);
76+
fitG = fit(i);
77+
end
78+
end
79+
%---// [Pheromone update rule on tauK] //
80+
tauK = zeros(dim,dim);
81+
for i = 1:N
82+
% Update Phromones
83+
tour = X(i,:);
84+
tour(tour == 0) = [];
85+
% Number of features
86+
len_x = length(tour);
87+
tour = [tour(1:end), tour(1)];
88+
for d = 1:len_x
89+
% Feature selected on graph
90+
x = tour(d);
91+
y = tour(d + 1);
92+
% Update delta tau k on graph (3)
93+
tauK(x,y) = tauK(x,y) + (1 / (1 + fit(i)));
94+
end
95+
end
96+
%---// [Pheromone update rule on tauG] //
97+
tauG = zeros(dim,dim);
98+
tour = Xgb;
99+
tour(tour == 0) = [];
100+
% Number of features
101+
len_g = length(tour);
102+
tour = [tour(1:end), tour(1)];
103+
for d = 1:len_g
104+
% Feature selected on graph
105+
x = tour(d);
106+
y = tour(d + 1);
107+
% Update delta tau G on graph
108+
tauG(x,y) = 1 / (1 + fitG);
109+
end
110+
%---// Evaporate pheromone // (4)
111+
tau = (1 - rho) * tau + tauK + tauG;
112+
% Save
113+
curve(t) = fitG;
114+
fprintf('\nIteration %d Best (ACO)= %f',t,curve(t))
115+
t = t + 1;
116+
end
117+
% Select features based on selected index
118+
Sf = Xgb;
119+
Sf(Sf == 0) = [];
120+
sFeat = feat(:,Sf);
121+
% Store results
122+
ACO.sf = Sf;
123+
ACO.ff = sFeat;
124+
ACO.nf = length(Sf);
125+
ACO.c = curve;
126+
ACO.f = feat;
127+
ACO.l = label;
128+
end
129+
130+
131+
%// Roulette Wheel Selection //
132+
function Index = jRouletteWheelSelection(prob)
133+
% Cummulative summation
134+
C = cumsum(prob);
135+
% Random one value, most probability value [0~1]
136+
P = rand();
137+
% Roulette wheel
138+
for i = 1:length(C)
139+
if C(i) > P
140+
Index = i;
141+
break;
142+
end
143+
end
144+
end
145+

0 commit comments

Comments
 (0)