-
Notifications
You must be signed in to change notification settings - Fork 7
/
map.cpp
202 lines (191 loc) · 5.7 KB
/
map.cpp
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "map.h"
Map::Map(int vector_size, int init_r, int init_c) :
vectorSize(vector_size)
{
mean.reserve(vectorSize);
for(int i = 0; i < vectorSize; ++i) mean.append(0);
for(int i = 0; i < init_r; ++i)
{
QList<Neuron*> row;
for(int j = 0; j < init_c; ++j)
{
Neuron *n = new Neuron(vectorSize);
//for(int k = 0; k < vector_size; ++k) n->vector[k] = (i+j)/(init_r+init_c-2.0f);
//n->vector[0] = i / (init_r - 1.0f);
//n->vector[1] = j / (init_c - 1.0f);
row.append(n);
}
network.append(row);
}
}
Map::~Map()
{
for(int i = 0; i < network.size(); ++i)
for(int j = 0; j < network[i].size(); ++j)
delete network[i][j];
}
void Map::calcMean(const QList<Vector> &input)
{
int mean_size = mean.size();
int input_size = input.size();
for(int i = 0; i < mean_size; ++i) mean[i] = 0;
for(int i = 0; i < input_size; ++i)
for(int j = 0; j < mean_size; ++j)
mean[j] += input[i][j];
for(int i = 0; i < mean_size; ++i) mean[i] /= input_size;
mqe0 = 0;
for(int i = 0; i < input_size; ++i) mqe0 += input[i].distanceTo(mean);
mqe0 /= input_size;
}
void Map::calcQuant(const QList<Vector> &input)
{
for(int i = 0; i < network.size(); ++i)
for(int j = 0; j < network[i].size(); ++j)
network[i][j]->resetQuant();
for(int i = 0; i < input.size(); ++i)
{
Point bmu = findBestMatch(input[i]);
network[bmu.first][bmu.second]->addQuant(input[i], i);
}
mqe = 0;
for(int i = 0; i < network.size(); ++i)
{
for(int j = 0; j < network[i].size(); ++j)
{
int qs = network[i][j]->quant_list.size();
if(qs > 0)
{
network[i][j]->quant /= qs;
mqe += network[i][j]->quant;
}
}
}
mqe /= network.size() * network[0].size();
}
void Map::train(const QList<Vector> &input, int max_iterations)
{
input_size = input.size();
int sub_soms = 0;
calcMean(input);
for(;;)
{
if(iteration >= max_iterations) break;
for(int i = 0; i < lambda; ++i)
{
iteration++;
if(iteration % (lambda/2) == 0) cout << "itr: " << iteration << " [" << network.size() << ", " << network[0].size() << "] "
<< sub_soms << " => " << input_size << endl;
float my_alpha = alpha();
for(int j = 0; j < input_size; ++j) learn(input[j], my_alpha);
}
calcQuant(input);
for(int i = 0; i < network.size(); ++i)
{
for(int j = 0; j < network[i].size(); ++j)
{
Neuron *ne = network[i][j];
if(ne->has_map)
{
QList<Vector> subInput;
for(int k = 0; k < ne->quant_list.size(); ++k)
subInput.append(input[ne->quant_list[k]]);
if(subInput.size() > 0) ne->map->train(subInput, max_iterations);
}
}
}
if(mqe0 > 0 && mqe > tau_1 * mqe0)
grow();
else
{
int n = markAllHierarchyUnits();
sub_soms += n;
if(n < 1) break;
}
}
}
void Map::learn(const Vector &v, float curr_alpha)
{
Point bmu = findBestMatch(v);
for (int i = 0; i < network.size(); ++i)
{
for (int j = 0; j < network[i].size(); ++j)
{
Neuron *neuron = network[i][j];
float my_theta = theta(bmu, i, j);
for (int k = 0; k < v.size(); ++k)
{
float vk = neuron->vector[k];
neuron->vector[k] = vk + curr_alpha * my_theta * (v[k] - vk);
}
}
}
}
void Map::grow()
{
Point e = findMaxErrorUnit();
int dir = -1;
Point d = findMaxDistLink(e, dir);
QList<Neuron*> newlist;
switch(dir)
{
case 0:
newlist = addRow(d.first);
network.insert(e.first, newlist);
break;
case 2:
newlist = addRow(e.first);
network.insert(d.first, newlist);
break;
case 1:
newlist = addColumn(e.second);
for(int i = 0; i < network.size(); ++i) network[i].insert(d.second, newlist[i]);
break;
case 3:
newlist = addColumn(d.second);
for(int i = 0; i < network.size(); ++i) network[i].insert(e.second, newlist[i]);
break;
default:
cout << "dir != 0,1,2,3 !!!\n\n";
break;
}
}
QList<Neuron*> Map::addRow(int r) const
{
QList<Neuron*> result;
int size = network[r].size();
for(int i = 0; i < size; ++i)
{
Neuron* n = new Neuron(vectorSize);
for(int j = 0; j < vectorSize; ++j) n->vector[j] = (network[r][i]->vector[j] + network[r+1][i]->vector[j]) / 2.0f;
result.append(n);
}
return result;
}
QList<Neuron*> Map::addColumn(int c) const
{
QList<Neuron*> result;
for(int i = 0; i < network.size(); ++i)
{
Neuron* n = new Neuron(vectorSize);
for(int j = 0; j < vectorSize; ++j) n->vector[j] = (network[i][c]->vector[j] + network[i][c+1]->vector[j]) / 2.0f;
result.append(n);
}
return result;
}
float Map::alpha() const
{
return 0.1f;
//return qMax(0.1f, 1.0f - (iteration * 0.001f));
}
float Map::theta(Point u, int x, int y) const
{
if(u.first == x && u.second == y) return 1;
int dx = u.first - x;
int dy = u.second - y;
int dx2 = dx * dx;
int dy2 = dy * dy;
if((u.first == x && dy2 == 1)||(u.second == y && dx2 == 1)) return 0.5;
//if(dx2 == 1 && dy2 == 1) return 1.0f/1.414f;
//if(dx2<2 && dy2<2) return 1.0f / sqrt(dx2 + dy2);
return 0;
}