-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSeedGenerator.cs
More file actions
137 lines (110 loc) · 3.67 KB
/
SeedGenerator.cs
File metadata and controls
137 lines (110 loc) · 3.67 KB
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace Clusterization_algorithms
{
/// <summary>
/// Weighed random with points to k-means++ algorithm
/// Allows to calculate optimal start positions to seeds
/// </summary>
public class SeedGenerator
{
private Dictionary<Point, double> _points = new Dictionary<Point, double> { }; // <point, weight>; Weight is equal to distance
public int _seedCount = 2;
private Random _rand = new Random();
private List<Point> _seeds = new List<Point> { };
private double _sumOfWeight = 0;
public SeedGenerator()
{
}
public SeedGenerator(Dictionary<Point, int> points, int seedCount)
{
SetPoints(points);
_seedCount = seedCount;
}
public List<Point> GetSeeds()
{
int rnum = _rand.Next(0, _points.Count - 1);
Point seed1 = _points.ElementAt(rnum).Key; // get 1-st random seed from points
_seeds.Add(seed1); // add point to seed list
_points.Remove(seed1); // remove this point from points
CalculatePointsWeight(seed1);
for (int i = 1; i < _seedCount; i++)
{
FindNewSeed();
}
return _seeds;
}
private void FindNewSeed()
{
SortPoints();
double rsum = _rand.Next(0, (int)_sumOfWeight);
for (int i = 0; i < _points.Count; i++)
{
var pair = _points.ElementAt(i);
if (pair.Value >= rsum)
{
_seeds.Add(pair.Key);
_points.Remove(pair.Key);
CalculatePointsWeight(pair.Key);
break;
}
rsum -= pair.Value;
}
}
private void SortPoints()
{
Dictionary<Point, double> newPoints = new Dictionary<Point, double> { };
foreach (var pair in _points.OrderBy(ppair => ppair.Value))
{
newPoints.Add(pair.Key, pair.Value);
}
_points = newPoints;
}
private void CalculatePointsWeight(Point seed)
{
Dictionary<Point, double> newPoints = new Dictionary<Point, double> { };
_sumOfWeight = 0;
for (int i = 0; i < _points.Count; i++)
{
Point point = _points.ElementAt(i).Key;
double weigh = _points.ElementAt(i).Value;
double dist = Calculator.CalcDistance(seed, point);
weigh += dist;
_sumOfWeight += weigh;
newPoints.Add(point, weigh);
}
_points = newPoints;
}
public void SetPoints(Dictionary<Point, int> dictionary)
{
_points.Clear();
foreach (var pair in dictionary)
{
_points.Add(pair.Key, 0);
}
}
public String PrintPoints()
{
String str = "";
Console.WriteLine("Points list:");
foreach (var point in _points)
{
Console.WriteLine(point.ToString());
str += point.ToString() + "\n";
}
Console.WriteLine();
return str;
}
public void PrintSeeds()
{
Console.WriteLine("Print seeds:");
foreach (Point seed in _seeds)
{
Console.WriteLine(seed.ToString());
}
Console.WriteLine();
}
}
}