-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
312 lines (250 loc) · 7.62 KB
/
main.cpp
File metadata and controls
312 lines (250 loc) · 7.62 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <phpcpp.h>
#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <climits>
using namespace std;
class Point{
private:
int pointId, clusterId;
int dimensions;
vector<double> values;
public:
Point(int id, double x, double y){
dimensions = 2;
pointId = id;
values.push_back(x);
values.push_back(y);
clusterId = 0; //Initially not assigned to any cluster
}
int getDimensions(){
return dimensions;
}
int getCluster(){
return clusterId;
}
int getID(){
return pointId;
}
void setCluster(int val){
clusterId = val;
}
double getVal(int pos){
return values[pos];
}
};
class Cluster{
private:
int clusterId;
vector<double> centroid;
vector<Point> points;
public:
Cluster(int clusterId, Point centroid){
this->clusterId = clusterId;
for(int i=0; i<centroid.getDimensions(); i++){
this->centroid.push_back(centroid.getVal(i));
}
this->addPoint(centroid);
}
void addPoint(Point p){
p.setCluster(this->clusterId);
points.push_back(p);
}
bool removePoint(int pointId){
int size = points.size();
for(int i = 0; i < size; i++)
{
if(points[i].getID() == pointId)
{
points.erase(points.begin() + i);
return true;
}
}
return false;
}
int getId(){
return clusterId;
}
Point getPoint(int pos){
return points[pos];
}
int getSize(){
return points.size();
}
double getCentroidByPos(int pos) {
return centroid[pos];
}
void setCentroidByPos(int pos, double val){
this->centroid[pos] = val;
}
};
class KMeans{
private:
int K, iters, dimensions, total_points;
vector<Cluster> clusters;
int getNearestClusterId(Point point){
double sum = 0.0, min_dist;
int NearestClusterId;
for(int i = 0; i < dimensions; i++)
{
sum += pow(clusters[0].getCentroidByPos(i) - point.getVal(i), 2.0);
}
min_dist = sqrt(sum);
NearestClusterId = clusters[0].getId();
for(int i = 1; i < K; i++)
{
double dist;
sum = 0.0;
for(int j = 0; j < dimensions; j++)
{
sum += pow(clusters[i].getCentroidByPos(j) - point.getVal(j), 2.0);
}
dist = sqrt(sum);
if(dist < min_dist)
{
min_dist = dist;
NearestClusterId = clusters[i].getId();
}
}
return NearestClusterId;
}
public:
KMeans(int K, int iterations){
this->K = K;
this->iters = iterations;
}
vector<Cluster> run(vector<Point>& all_points){
total_points = all_points.size();
dimensions = all_points[0].getDimensions();
//Initializing Clusters
vector<int> used_pointIds;
for(int i=1; i<=K; i++)
{
while(true)
{
int index = rand() % total_points;
if(find(used_pointIds.begin(), used_pointIds.end(), index) == used_pointIds.end())
{
used_pointIds.push_back(index);
all_points[index].setCluster(i);
Cluster cluster(i, all_points[index]);
clusters.push_back(cluster);
break;
}
}
}
//cout<<"Clusters initialized = "<<clusters.size()<<endl<<endl;
//cout<<"Running K-Means Clustering.."<<endl;
int iter = 1;
while(true)
{
//cout<<"Iter - "<<iter<<"/"<<iters<<endl;
bool done = true;
// Add all points to their nearest cluster
for(int i = 0; i < total_points; i++)
{
int currentClusterId = all_points[i].getCluster();
int nearestClusterId = getNearestClusterId(all_points[i]);
if(currentClusterId != nearestClusterId)
{
if(currentClusterId != 0){
for(int j=0; j<K; j++){
if(clusters[j].getId() == currentClusterId){
clusters[j].removePoint(all_points[i].getID());
}
}
}
for(int j=0; j<K; j++){
if(clusters[j].getId() == nearestClusterId){
clusters[j].addPoint(all_points[i]);
}
}
all_points[i].setCluster(nearestClusterId);
done = false;
}
}
// Recalculating the center of each cluster
for(int i = 0; i < K; i++)
{
int ClusterSize = clusters[i].getSize();
for(int j = 0; j < dimensions; j++)
{
double sum = 0.0;
if(ClusterSize > 0)
{
for(int p = 0; p < ClusterSize; p++)
sum += clusters[i].getPoint(p).getVal(j);
clusters[i].setCentroidByPos(j, sum / ClusterSize);
}
}
}
if(done || iter >= iters)
{
//cout << "Clustering completed in iteration : " <<iter<<endl<<endl;
break;
}
iter++;
}
/*
//Print pointIds in each cluster
for(int i=0; i<K; i++){
cout<<"Points in cluster "<<clusters[i].getId()<<" : ";
for(int j=0; j<clusters[i].getSize(); j++){
cout<<clusters[i].getPoint(j).getID()<<" ";
}
cout<<endl<<endl;
}
cout<<"========================"<<endl<<endl;
*/
return clusters;
}
};
Php::Value cpp_kmeans(Php::Parameters ¶ms)
{
int K = params[0];
Php::Array results = params[1];
vector<Point> all_points;
for (auto &res : results){
Point point(res.first, res.second[0], res.second[1]);
all_points.push_back(point);
}
int iters = 100;
KMeans kmeans(K, iters);
vector<Cluster> clusters = kmeans.run(all_points);
Php::Array php_clusters;
for(int i=0; i<K; i++){
Php::Array this_cluster;
for(int j=0; j<clusters[i].getSize(); j++){
Php::Array the_point({clusters[i].getPoint(j).getVal(0), clusters[i].getPoint(j).getVal(1)});
this_cluster[clusters[i].getPoint(j).getID()] = the_point;
}
php_clusters[i] = this_cluster;
}
return php_clusters;
}
/**
* tell the compiler that the get_module is a pure C function
*/
extern "C" {
/**
* Function that is called by PHP right after the PHP process
* has started, and that returns an address of an internal PHP
* strucure with all the details and features of your extension
*
* @return void* a pointer to an address that is understood by PHP
*/
PHPCPP_EXPORT void *get_module()
{
// static(!) Php::Extension object that should stay in memory
// for the entire duration of the process (that's why it's static)
static Php::Extension extension("kmeans", "1.0");
extension.add<cpp_kmeans>("cpp_kmeans", {
Php::ByVal("k", Php::Type::Numeric),
Php::ByVal("results", Php::Type::Array)
});
return extension;
}
}