-
Notifications
You must be signed in to change notification settings - Fork 2
/
SelfOrganizingMap.cs
201 lines (173 loc) · 7.14 KB
/
SelfOrganizingMap.cs
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
#region License
/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE.txt', which is part of this source code package.
*/
#endregion
using System;
using System.Collections.Generic;
using NeuralNetworks.SelfOrganizingMapHelpers;
namespace NeuralNetworks.Models
{
public class SelfOrganizingMap
{
public List<Node> _nodes;
public SelfOrganizingMap(List<Node> nodes)
{
_nodes = nodes;
}
public List<Node> StartModifyNodes(Node randomizedPoint, int neighborRadius, double shiftFactor)
{
var nearestNode = FindNearestNode(randomizedPoint);
var nearestNodeIndex = _nodes.IndexOf(nearestNode);
MoveAdjacentNodes(randomizedPoint, nearestNodeIndex, neighborRadius, shiftFactor);
return _nodes;
}
private Node FindNearestNode(Node randomizedPoint)
{
Node nearestNode = null;
double minDistance = double.MaxValue;
foreach (var node in _nodes)
{
var distance = CalculateEuclideanDistance(randomizedPoint, node);
if (distance < minDistance)
{
minDistance = distance;
nearestNode = node;
}
}
return nearestNode;
}
private double CalculateEuclideanDistance(Node firstNode, Node secondNode)
{
var powResult = Math.Pow(firstNode.X - secondNode.X, 2) + Math.Pow(firstNode.Y - secondNode.Y, 2);
return Math.Sqrt(powResult);
}
private void MoveAdjacentNodes(Node randomizedPoint, int winnerIndex, int neighborRadius, double shiftFactor)
{
MoveWinner(randomizedPoint, winnerIndex, shiftFactor);
if (neighborRadius != 0)
{
MoveLeftNeighbours(randomizedPoint, winnerIndex, neighborRadius, shiftFactor);
MoveRightNeighbours(randomizedPoint, winnerIndex, neighborRadius, shiftFactor);
}
}
private void MoveWinner(Node randomizedPoint, int winnerIndex, double shiftFactor)
{
_nodes[winnerIndex].X += (int)((randomizedPoint.X - _nodes[winnerIndex].X) * shiftFactor);
_nodes[winnerIndex].Y += (int)((randomizedPoint.Y - _nodes[winnerIndex].Y) * shiftFactor);
}
private void MoveLeftNeighbours(Node randomizedPoint, int winnerIndex, int neighborRadius, double shiftFactor)
{
for (int index = winnerIndex - 1; index >= winnerIndex - neighborRadius; index--)
{
if (index >= 0)
{
shiftFactor *= 0.95;
_nodes[index].X += (int)((randomizedPoint.X - _nodes[index].X) * shiftFactor);
_nodes[index].Y += (int)((randomizedPoint.Y - _nodes[index].Y) * shiftFactor);
}
else
{
break;
}
}
}
private void MoveRightNeighbours(Node randomizedPoint, int winnerIndex, int neighborRadius, double shiftFactor)
{
for (int index = winnerIndex + 1; index <= winnerIndex + neighborRadius; index++)
{
if (index < _nodes.Count)
{
shiftFactor *= 0.95;
_nodes[index].X += (int)((randomizedPoint.X - _nodes[index].X) * shiftFactor);
_nodes[index].Y += (int)((randomizedPoint.Y - _nodes[index].Y) * shiftFactor);
}
else
{
break;
}
}
}
#region Not done yet
private void MoveAdjacentNodesWithRectangular(Node randomizedPoint, int winnerIndex, double radius, double learningRate)
{
for (int index = winnerIndex; index < _nodes.Count; index++)
{
var distance = CalculateEuclideanDistance(randomizedPoint, _nodes[index]);
if (distance <= radius)
{
var influence = RectangularInfluence(distance, radius);
_nodes[index].X =
(int)(_nodes[index].X + learningRate * influence * (randomizedPoint.X - _nodes[index].X));
_nodes[index].Y =
(int)(_nodes[index].Y + learningRate * influence * (randomizedPoint.Y - _nodes[index].Y));
}
else
{
break;
}
}
for (int index = winnerIndex - 1; index >= 0; index--)
{
var distance = CalculateEuclideanDistance(randomizedPoint, _nodes[index]);
if (distance <= radius)
{
var influence = RectangularInfluence(distance, radius);
_nodes[index].X =
(int)(_nodes[index].X + learningRate * influence * (randomizedPoint.X - _nodes[index].X));
_nodes[index].Y =
(int)(_nodes[index].Y + learningRate * influence * (randomizedPoint.Y - _nodes[index].Y));
}
else
{
break;
}
}
}
private void MoveAdjacentNodesWithGaussian(Node randomizedPoint, int winnerIndex, double radius, double learningRate)
{
for (int index = winnerIndex; index < _nodes.Count; index++)
{
var distance = CalculateEuclideanDistance(randomizedPoint, _nodes[index]);
if (distance <= radius)
{
var influence = GaussianInfluence(distance, radius);
_nodes[index].X =
(int)(_nodes[index].X + learningRate * influence * (randomizedPoint.X - _nodes[index].X));
_nodes[index].Y =
(int)(_nodes[index].Y + learningRate * influence * (randomizedPoint.Y - _nodes[index].Y));
}
else
{
break;
}
}
for (int index = winnerIndex - 1; index >= 0; index--)
{
var distance = CalculateEuclideanDistance(randomizedPoint, _nodes[index]);
if (distance <= radius)
{
var influence = GaussianInfluence(distance, radius);
_nodes[index].X =
(int)(_nodes[index].X + learningRate * influence * (randomizedPoint.X - _nodes[index].X));
_nodes[index].Y =
(int)(_nodes[index].Y + learningRate * influence * (randomizedPoint.Y - _nodes[index].Y));
}
else
{
break;
}
}
}
private double GaussianInfluence(double distance, double radius)
{
return Math.Exp(-Math.Pow(distance, 2) / (2 * Math.Pow(radius, 2)));
}
private int RectangularInfluence(double distance, double radius)
{
return distance <= radius ? 1 : 0;
}
#endregion
}
}