using Clustering Core Common using System using System Collections Gen

 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
using Clustering.Core.Common;
using System;
using System.Collections.Generic;
namespace Clustering.Core
{
public class ForelAlgorithm
{
public static List<Cluster> DoClustering(PointCollection points, double radius, int dimension)
{
var clusters = new List<Cluster>();
var getDistance = new Func<Point, Point, double>((pt1, pt2) => Point.FindDistance(pt1, pt2, dimension));
var getRandomPoint = new Func<Point>(() =>
{
var limit = points.Count;
var randomIndex = new Random().Next(limit);
return points[randomIndex];
});
var getNeighboringPoints = new Func<Point, PointCollection>((randomPoint) =>
{
var neighboringPoints = new PointCollection(dimension);
foreach (var point in points)
{
if (getDistance(randomPoint, point) <= radius)
{
neighboringPoints.Add(point);
}
}
return neighboringPoints;
});
var deletePoints = new Action<Point, PointCollection>((centerPoint, neighboringPoints) =>
{
var cluster = new Cluster()
{
CenterPoint = centerPoint,
Points = neighboringPoints
};
clusters.Add(cluster);
foreach (var neighboringPoint in neighboringPoints)
{
points.Remove(neighboringPoint);
}
});
while (points.Count > 0)
{
Point currentPoint = getRandomPoint();
PointCollection neighboringPoints = getNeighboringPoints(currentPoint);
Point centerPoint = neighboringPoints.GetCenterPoint();
while (!centerPoint.Equals(currentPoint))
{
currentPoint = centerPoint;
neighboringPoints = getNeighboringPoints(currentPoint);
centerPoint = neighboringPoints.GetCenterPoint();
}
deletePoints(currentPoint, neighboringPoints);
}
return clusters;
}
}
}