-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSeedGenerator.cs
148 lines (117 loc) · 4.07 KB
/
SeedGenerator.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace Clusterization_algorithms
{
/// <summary>
/// Weighed random with points to k-means++ algorithm
/// Need to set start position to seeds autimatically and optimaly
/// </summary>
class SeedGenerator
{
private Dictionary<Point, double> points = new Dictionary<Point, double> { }; // <point, weight>; Weight = Distance
public int seedCount = 2;
private Random rand = new Random();
private List<Point> seeds = new List<Point> { };
private double sumOfWeigh = 0;
public SeedGenerator()
{
}
public SeedGenerator(Dictionary<Point, int> points, int seedCount)
{
SetPoints(points);
this.seedCount = seedCount;
}
public List<Point> GetSeeds()
{ // CALL THIS METHOD TO START!!!
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
//Console.WriteLine("1-st seed: " + seed1.ToString() + '\n');
calculatePointsWeight(seed1);
for (int i = 1; i < seedCount; i++)
findNewSeed();
return seeds;
}
private void findNewSeed()
{ // !!!
SortPoints();
//Console.WriteLine("Sum of weigth: " + sumOfWeigh);
double rsum = rand.Next(0, (int)sumOfWeigh);
//Console.WriteLine("Random sum: " + rsum + '\n');
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);
//Console.WriteLine("new seed: " + pair.Key);
break;
}
rsum -= pair.Value;
//Console.WriteLine(rsum);
}
//Console.WriteLine();
//printSeeds();
}
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;
//printPoints();
}
private void calculatePointsWeight(Point seed)
{
Dictionary<Point, double> newPoints = new Dictionary<Point, double> { };
sumOfWeigh = 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;
sumOfWeigh += weigh;
newPoints.Add(point, weigh);
}
points = newPoints;
//printPoints();
}
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();
}
}
}