forked from ivogeorg/ucd-csci2312-pa3
/
KMeans.cpp
159 lines (121 loc) · 4.19 KB
/
KMeans.cpp
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
//
// Created by Jose on 3/28/2016.
//
#include <iostream>
#include <fstream>
#include <limits>
#include "KMeans.h"
#include "Exceptions.h"
#include "Cluster.h"
namespace Clustering
{
KMeans::KMeans(unsigned int dim, unsigned int k, std::string filename, unsigned int maxIter) :
__dimensionality(dim), __k(k), __iFileName(filename), __maxIter(maxIter) {
if (k == 0)
throw ZeroClustersEx();
std::ifstream iFile(filename.c_str());
if (iFile.fail())
throw DataFileOpenEx(filename);
iFile.close();
}
KMeans::~KMeans() {
for (int i = 0; i < __k; ++i) {
delete __initCentroids[i];
}
delete [] __initCentroids;
for (int i = 0; i < __k; ++i) {
delete __clusters[i];
}
delete [] __clusters;
}
//**********************************************************************************************
unsigned int KMeans::getMaxIter() {
return __maxIter;
}
unsigned int KMeans::getNumIters() {
return __numIter;
}
unsigned int KMeans::getNumNonemptyClusters() {
return __numNonempty;
}
unsigned int KMeans::getNumMovesLastIter() {
return __numMovesLastIter;
}
//**********************************************************************************************
Cluster &KMeans::operator[](unsigned int u) {
if (__k == 0) {
throw ZeroClustersEx();
}
if (u >= __k) {
throw DimensionalityMismatchEx(__k, u);
}
return *__clusters[u];
}
const Cluster &KMeans::operator[](unsigned int u) const {
if (__k == 0) {
throw ZeroClustersEx();
}
if (u >= __k) {
throw DimensionalityMismatchEx(__k, u);
}
return *__clusters[u];
}
//**********************************************************************************************
std::ostream &operator<<(std::ostream &os, const KMeans &kmeans) {
Point hold(kmeans.__dimensionality);
for (int i = 0; i < kmeans.__dimensionality; ++i) {
hold[i] = std::numeric_limits<double>::max();
}
for (int i = 0; i < kmeans.__k; ++i) {
if (!kmeans.__clusters[i]->centroid.equal(hold)) {
os << *kmeans.__clusters[i] << std::endl;
}
}
return os;
}
void KMeans::run() {
int moves = 100;
int iterator = 0;
int nonempty = 0;
while (moves > 0 && iterator < __maxIter) {
moves = 0;
for (int i = 0; i < __k; ++i) {
for (int j = 0; j < __clusters[i]->getSize(); ++j)
{
Cluster &c = *(__clusters[i]);
Point current_point(__dimensionality);
current_point = c[j];
int smallest_dist_index = 0;
double smallest_dist = current_point.distanceTo(*__initCentroids[0]);
for (int e = 0; e < __k; e++) {
if (current_point.distanceTo(*__initCentroids[e]) < smallest_dist) {
smallest_dist = current_point.distanceTo(*__initCentroids[e]);
smallest_dist_index = e;
}
}
Cluster::Move change_clusters(current_point, *__clusters[i], *__clusters[smallest_dist_index]);
change_clusters.perform();
for (int c = 0; c < __k; ++c) {
__clusters[c]->centroid.compute();
}
if (*__clusters[i] != *__clusters[smallest_dist_index]) {
moves++;
}
}
}
iterator++;
}
Point inf(__dimensionality);
for (int i = 0; i < __dimensionality; ++i) {
inf[i] = std::numeric_limits<double>::max();
}
for (int i = 0; i < __k; ++i) {
if (__clusters[i]->centroid.get() != inf) {
++nonempty;
}
}
__numIter = iterator;
__numMovesLastIter = moves;
__numNonempty = nonempty;
}
}