-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cluster.cpp
129 lines (101 loc) · 3.31 KB
/
Cluster.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
//
// Created by gevagorou on 19/04/16.
//
#include "Cluster.h"
using std::cout;
using std::endl;
using std::vector;
using std::cout;
long Cluster::id_generator = 0;
void Cluster::insertPoint(Point p) {
cluster_points.push_back(p);
num_of_points++;
//adjust stats
for (unsigned long i = 0; i < p.coordinates.size(); ++i) {
//called from the constructor
if (i >= sum_vector.size()) {
//if nothing exists at that index
sum_vector.push_back(p.coordinates.at(i));
centroid.push_back(sum_vector.at(i) / num_of_points);
}
else {
sum_vector.at(i) += p.coordinates.at(i);
centroid.at(i) = (sum_vector.at(i) / num_of_points);
}
}
}
Cluster::Cluster(Point p) : num_of_points(0), cluster_id(id_generator) {
/*tell the container how many elements it
should be prepared to hold.*/
sum_vector.reserve(p.coordinates.size());
centroid.reserve(p.coordinates.size());
//insert point to this cluster
insertPoint(p);
id_generator++;
}
long Cluster::getCluster_id() const {
return cluster_id;
}
Cluster &Cluster::operator=(const Cluster &right) {
num_of_points = right.num_of_points;
cluster_id = right.cluster_id;
sum_vector = right.sum_vector;
cluster_points = right.cluster_points;
centroid = right.centroid;
return *this;
}
Cluster::Cluster(const Cluster &cl) :
num_of_points(cl.num_of_points), cluster_id(cl.cluster_id), sum_vector(cl.sum_vector),
cluster_points(cl.cluster_points), centroid(cl.centroid) {
}
/**
* Estimates euclidean distance between two clusters
*/
double Cluster::euclidean_distance(const Cluster &a, const Cluster &b) {
double sum = 0;
for (unsigned long i = 0; i < b.centroid.size(); ++i) {
sum += std::pow((a.centroid.at(i) - b.centroid.at(i)), 2);
}
return std::sqrt(sum);
}
/**
* Merges two clusters
*/
Cluster &Cluster::merge(const Cluster &right) {
vector<Point>::const_iterator points_begin = right.cluster_points.cbegin();
vector<Point>::const_iterator points_end = right.cluster_points.cend();
//iterate over all points in the right cluster
while (points_begin != points_end) {
//insert all points from right cluster to left cluster
insertPoint(*points_begin);
++points_begin;
}
//the left cluster is essentially a new cluster - so change its cluster id
this->cluster_id = id_generator;
++id_generator;
return *this;
}
/**
* Friend method that prints the Cluster object
*/
std::ostream &operator<<(std::ostream &out_stream, const Cluster &cl) {
out_stream.precision(17);
out_stream << "--------------------Cluster" << " " << cl.cluster_id << "--------------------" << std::endl;
out_stream << "Number of points:" << cl.cluster_points.size() << std::endl;
out_stream << "Points:" << std::endl;
for (Point p:cl.cluster_points) {
std::cout << p;
}
std::cout << "sum vector:";
for (double sum:cl.sum_vector) {
out_stream << sum;
out_stream << " ";
}
out_stream << std::endl << "Centroid:";
for (double d:cl.centroid) {
out_stream << d << ",";
}
out_stream << std::endl;
out_stream << "------------------------------------------------" << std::endl;
return out_stream;
}