-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedit_rmhc.m
60 lines (53 loc) · 1.6 KB
/
edit_rmhc.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
% Random Mutation Hill Climbing (Skalak94, winner of Garcia12)
% ------------------------------------------------------------------------
% Input:
% b -- the data set;
% l -- the labels (vector with integers; labels must be: 1,2,...,c
% PARAM.NumberOfPrototypes
%
% Output
% v: the set prototypes;
% v_lab: prototype class labels
% ------------------------------------------------------------------------
% Author: Lucy Kuncheva 07/09/2019
function [v,v_lab] = edit_rmhc(b,l,PARAM)
m = PARAM.NumberOfPrototypes;
n = size(b,1);
all = 1:n; % all available prototypes
K = 40; % default number of runs
if isfield(PARAM,'PopulationSize')
if ~isempty(PARAM.PopulationSize)
K = PARAM.PopulationSize; % number of runs
end
end
T = 300; % default number of mutations
if isfield(PARAM,'NumberOfGenerations')
if ~isempty(PARAM.NumberOfGenerations)
T = PARAM.NumberOfGenerations;
end
end
B = zeros(1,K);
for i = 1:K
S = randperm(n,m);
knn = fitcknn(b(S,:),l(S));
best_ac = mean(predict(knn,b) == l);
for j = 1:T
Snew = S;
to_mutate = randi(m);
to_choose_from = setxor(all,S); % n-m remaining prototypes
replacement_index = randi(n-m); % index into to_choose_from
Snew(to_mutate) = to_choose_from(replacement_index);
% Check fitness
knn = fitcknn(b(Snew,:),l(S));
ac = mean(predict(knn,b) == l);
if ac >= best_ac
S = Snew;
best_ac = ac;
end
end
B(i) = best_ac;
BS(i,:) = S;
end
[~,index_best] = max(B);
v = b(BS(index_best,:),:);
v_lab = l(BS(index_best,:));