-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredictKMeans.cpp
More file actions
178 lines (160 loc) · 3.09 KB
/
PredictKMeans.cpp
File metadata and controls
178 lines (160 loc) · 3.09 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
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
#include "PredictKMeans.h"
KMeansData::KMeansData() : mIndex(-1), mQuntity(0)
{
}
KMeansData::KMeansData(int index, float quntity) : mIndex(index), mQuntity(quntity)
{
}
KMeansData& KMeansData::operator= (const KMeansData& data)
{
mQuntity = data.mQuntity;
mIndex = data.mIndex;
return *this;
}
KMeansData::~KMeansData()
{
}
float KMeansData::DistanceTo(const KMeansGroup& other)
{
float d = this->mQuntity - other.mQuntity;
return d < 0 ? -d : d;
}
KMeansGroup& KMeansGroup::operator= (const KMeansGroup& data)
{
if (this != &data)
{
mIndexGroup.Clear();
mQuntity = data.mQuntity;
mIndexGroup = data.mIndexGroup;
}
return *this;
}
KMeansGroup::KMeansGroup()
{
}
KMeansGroup::~KMeansGroup()
{
Clear();
}
void KMeansGroup::Clear()
{
mIndexGroup.Clear();
}
bool KMeansGroup::Calculate()
{
int size = mIndexGroup.Size();
float temp = 0;
for (int i = 0; i < size; ++i)
{
temp += mIndexGroup[i].mQuntity;
}
if (size > 0)
{
temp = temp * 1.0f / size;
}
if (temp - mQuntity < 1e-5f && temp - mQuntity > -1e-5f)
{
return true;
}
mQuntity = temp;
return false;
}
int KMeansDataCompare(const float& quntity, const KMeansData& data)
{
if (quntity < data.mQuntity)
return -1;
if (quntity == data.mQuntity)
return 0;
return 1;
}
int KMeansGroupCompare(const float& quntity, const KMeansGroup& data)
{
if (quntity < data.mQuntity)
return -1;
if (quntity == data.mQuntity)
return 0;
return 1;
}
void KMeansGroup::KMeans(KMEANS_SOURCE* sources, KMEANS_GROUP* initialGroups)
{
int size1 = sources->Size();
KMeansGroup* group = 0;
for (int i1 = 0; i1 < size1; ++i1)
{
KMeansData* source = &(*sources)[i1];
int index;
int minD = 1e9;
int size2 = initialGroups->Size();
for (int i2 = 0; i2 < size2; ++i2)
{
group = &(*initialGroups)[i2];
int d = source->DistanceTo(*group);
if (minD > d)
{
minD = d;
index = i2;
}
}
group = &(*initialGroups)[index];
group->mIndexGroup.Insert(source->mQuntity, *source, true);
}
int size2 = initialGroups->Size();
bool needAgain = false;
for (int i2 = 0; i2 < size2; ++i2)
{
group = &(*initialGroups)[i2];
needAgain = !group->Calculate();
}
if (needAgain)
{
for (int i2 = 0; i2 < size2; ++i2)
{
group = &(*initialGroups)[i2];
group->Clear();
}
KMeans(sources, initialGroups);
}
}
void KMeansGroup::KMeans(KMEANS_SOURCE* sources, int groups, KMEANS_GROUP* result)
{
int size = sources->Size();
KMEANS_SOURCE tempList;
for (int i = 0; i < size; ++i)
{
KMeansData* data = &(*sources)[i];
tempList.Insert(data->mQuntity, *data, false);
}
int unique = tempList.Size();
if (unique < groups)
{
return;
}
result->Clear();
int step = 0;
int temp = 0;
if (unique > groups * 2)
{
step = unique / (groups * 2);
temp = step;
step *= 2;
}
else
{
step = 1;
}
for (int i = 0; i < groups; ++i)
{
KMeansGroup group_;
group_.mQuntity = tempList[temp].mQuntity;
result->Insert(group_.mQuntity, group_, true);
temp += step;
}
tempList.Clear();
KMeans(sources, result);
int size2 = result->Size();
KMeansGroup* group;
for (int i2 = 0; i2 < size2; ++i2)
{
group = &(*result)[i2];
}
}