@@ -53,19 +53,22 @@ public static void main(String[]args) throws IOException
53
53
54
54
ArrayList <double []> cluster = new ArrayList <double []>();
55
55
ArrayList <ArrayList <double []>> masterclusters = new ArrayList <ArrayList <double []>>();
56
- ArrayList <ArrayList <double []>> oldcluster = new ArrayList <ArrayList <double []>>();
56
+ //Old ArrayList<ArrayList<double[]>> oldcluster = new ArrayList<ArrayList<double[]>>();
57
57
58
58
for (int a =0 ; a <NumOfClstr ; a ++) {
59
59
60
60
cluster = new ArrayList <double []>(cluster );
61
61
masterclusters .add (cluster );
62
-
62
+
63
+ /**** Do not use unless you want to use the old check equality method ****
63
64
cluster= new ArrayList<double[]>(cluster);
64
65
oldcluster.add(cluster);
65
-
66
+ *************************************************************************/
66
67
}
67
68
68
69
ArrayList <double []> centroid =new ArrayList <double []>();
70
+ ArrayList <double []> oldcentroids = new ArrayList <double []>();
71
+
69
72
System .out .println ("\n Please pick a number between 1 and " +dataset .length +"\n \n " );
70
73
71
74
for (int b =1 ; b <NumOfClstr +1 ; b ++) {
@@ -76,7 +79,8 @@ public static void main(String[]args) throws IOException
76
79
int input = sc .nextInt ();
77
80
78
81
centroid .add (dataset [input -1 ]);
79
-
82
+ oldcentroids .add (dataset [input - 1 ]);
83
+
80
84
if (b ==NumOfClstr ) {
81
85
82
86
sc .close ();
@@ -118,10 +122,13 @@ public static void main(String[]args) throws IOException
118
122
119
123
}
120
124
121
- oldcluster =resetcluster (oldcluster ,masterclusters );
125
+ // Old oldcluster=resetcluster(oldcluster,masterclusters);
126
+ oldcentroids = resetcluster (oldcentroids , centroid );
127
+
122
128
int iterations =0 ;
123
129
long startTime = System .nanoTime ();
124
-
130
+ int rand = 0 ;
131
+
125
132
while (true ) {
126
133
127
134
for (int g =0 ; g <masterclusters .size ();g ++) {
@@ -161,12 +168,29 @@ public static void main(String[]args) throws IOException
161
168
162
169
print2D (ConvToArr (centroid ));
163
170
iterations ++;
164
-
171
+ /* Old
165
172
if(iterations >= MaxIt || checkEquality(oldcluster, masterclusters))
166
173
break;
167
174
else
168
175
oldcluster=resetcluster(oldcluster, masterclusters);
169
-
176
+ */
177
+ if (iterations >= MaxIt )
178
+ break ;
179
+
180
+ else if (checkEquality (centroid , oldcentroids )) {
181
+
182
+ rand ++;
183
+ if (rand == 10 )
184
+ break ;
185
+
186
+ }
187
+ else {
188
+ rand =0 ;
189
+ }
190
+
191
+ oldcentroids = resetcluster (oldcentroids , centroid );
192
+
193
+
170
194
}
171
195
172
196
long endTime = System .nanoTime ();
@@ -267,40 +291,63 @@ public static double[][] ConvToArr(ArrayList<double[]> input)
267
291
}
268
292
269
293
//Checks if old clusters are equal to new clusters
270
- static boolean checkEquality (ArrayList <ArrayList <double []>> oldClusters , ArrayList <ArrayList <double []>> newClusters )
294
+
295
+ /*****Use this only when you want to compare each member of the cluster to the old cluster. [Comparatively slower Process]*****
296
+
297
+ static boolean checkEqual(ArrayList<ArrayList<double[]>> oldClusters, ArrayList<ArrayList<double[]>> newClusters)
271
298
{
272
- for (int i = 0 ; i < newClusters .size (); i ++) {
299
+ for (int i = 0; i < newClusters.size(); i++) {
273
300
274
- if ( oldClusters . size () != newClusters .size ())
301
+ for (int j = 0; j < newClusters.get(i). size(); j++) {
275
302
276
- return false ;
303
+ if (oldClusters.get(i).size() != newClusters.get(i).size()) {
304
+ return false;
277
305
278
- for (int j =0 ; j <newClusters .get (i ).size (); j ++) {
306
+ }
307
+ out: while (true) {
308
+ for (double[] check1 : oldClusters.get(i)) {
279
309
280
- if ( oldClusters . get ( i ). size () != newClusters .get (i ).size ()) {
310
+ System.out.println(check1 + " " + newClusters.get(i).get(j));
281
311
282
- return false ;
312
+ if (check1 == newClusters.get(i).get(j)) {
313
+
314
+ break out;
315
+
316
+ }
283
317
318
+ }
319
+ return false;
284
320
}
285
- for (int v =0 ;v <newClusters .get (i ).get (j ).length ;v ++ ) {
321
+ }
322
+
323
+ }
286
324
287
- if ( oldClusters . get ( i ). get ( j )[ v ] != newClusters . get ( i ). get ( j )[ v ]) {
325
+ return true;
288
326
289
- return false ;
327
+ }
328
+ ********************************************************************************************************/
290
329
291
- }
330
+ //checks if old centroid is equal to new centroid
331
+ static boolean checkEquality (ArrayList <double []> oldCentroids , ArrayList <double []> newCentroids ) {
332
+ for (int i = 0 ; i < newCentroids .size (); i ++) {
333
+
334
+ for (int j = 0 ; j < newCentroids .get (i ).length ; j ++) {
335
+
336
+ if (Math .abs (oldCentroids .get (i )[j ] - newCentroids .get (i )[j ]) > 0.000001 ) {
337
+
338
+ return false ;
292
339
293
340
}
294
341
295
342
}
296
-
297
343
}
298
344
299
345
return true ;
300
346
301
347
}
302
348
303
349
//sets old clusters equal to new clusters
350
+ /*********Do not use unless you want to use old check equality method****************
304
351
static ArrayList<ArrayList<double[]>> resetcluster(ArrayList<ArrayList<double[]>> oldClusters, ArrayList<ArrayList<double[]>> newClusters)
305
352
{
306
353
for(int i=0; i<oldClusters.size(); i++) {
@@ -324,7 +371,18 @@ static ArrayList<ArrayList<double[]>> resetcluster(ArrayList<ArrayList<double[]>
324
371
return oldClusters;
325
372
326
373
}
374
+ ********************************************************************************************************/
375
+ // updated reset cluster for comparing centroids
376
+
377
+ static ArrayList <double []> resetcluster (ArrayList <double []> oldCentroids , ArrayList <double []> newCentroids ) {
378
+ for (int i = 0 ; i < oldCentroids .size (); i ++) {
327
379
380
+ oldCentroids .set (i , newCentroids .get (i ));
381
+
382
+ }
383
+ return oldCentroids ;
384
+ }
385
+
328
386
public static void print2D (double mat [][])
329
387
{
330
388
// Loop through all rows
0 commit comments