-
Notifications
You must be signed in to change notification settings - Fork 9
/
Network.m
95 lines (75 loc) · 3.63 KB
/
Network.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
classdef Network
% A shallow neural network of categories of contours
% Allows classification of a large dataset into a linguistic
% repertoire through refinement with training data
properties
weights
categories % Categories within the network
num_categories % Number of categories in the network
reclassifications % Number of reclassifications which have occurred
end
methods
function obj = Network(weights)
obj.weights = weights;
obj.num_categories = 0;
obj.categories = [];
end
function obj = update_network(obj, contour, parameters)
% Add a contour object to the network
% Iterates through the current categories and adds the
% contour to the one it is most similar to; if it matches
% none of them, create a new category for the contour
% Find category in network that contour fits into
matched_category = find_category(obj, contour, parameters);
% If there is no category the contour fits into, create a new
% one, with the contour as the reference for it.
if matched_category == -1
new_category = Category.category(1, contour);
obj = obj.add_category(new_category);
% Alter category property of contour
contour.category = new_category;
% If the contour was already in a category, remove it
if(contour.category) ~= 0
obj.categories(contour.category) = obj.categories(contour.category).remove(contour.category, contour);
end
% Increment number of reclassifications
obj.reclassifications = obj.reclassifications + 1;
elseif matched_category ~= contour.category
% Remove from old category if moved to new category
obj.categories(contour.category) = obj.categories(contour.category).remove(contour.category, contour);
% Add to new category
obj.categories(matched_category) = obj.categories(matched_category).add(contour.category, contour);
% Alter category property of contour
contour.category = matched_category;
% Increment number of reclassifications
obj.reclassifications = obj.reclassifications + 1;
end
end
function matched_category = find_category(obj, contour, parameters)
% Find the category a new contour fits into
% If there are no categories, no match found (-1 returned)
if obj.num_categories == 0
matched_category = -1;
else
% Create array for storing matches
a = zeros(1, obj.num_categories);
% Calculate matches
for i = 1:obj.num_categories
a(i) = obj.categories(i).compare(contour);
end
% Find max match percentage and see if it is above
% vigilance, return -1 if not
[max_match, matched_category] = max(a);
if max_match < parameters.vigilance
matched_category = -1;
end
end
end
function obj = add_category(obj,cat)
% Add new category to categories list in Network
obj.categories = [obj.categories, cat];
% Increase number of categories in the Network
obj.num_categories = obj.num_categories + 1;
end
end
end