1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.math.stat.clustering;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.util.Arrays;
24  import java.util.List;
25  import java.util.Random;
26  
27  import org.junit.Test;
28  
29  public class KMeansPlusPlusClustererTest {
30  
31      @Test
32      public void dimension2() {
33          KMeansPlusPlusClusterer<EuclideanIntegerPoint> transformer =
34              new KMeansPlusPlusClusterer<EuclideanIntegerPoint>(new Random(1746432956321l));
35          EuclideanIntegerPoint[] points = new EuclideanIntegerPoint[] {
36  
37                  // first expected cluster
38                  new EuclideanIntegerPoint(new int[] { -15,  3 }),
39                  new EuclideanIntegerPoint(new int[] { -15,  4 }),
40                  new EuclideanIntegerPoint(new int[] { -15,  5 }),
41                  new EuclideanIntegerPoint(new int[] { -14,  3 }),
42                  new EuclideanIntegerPoint(new int[] { -14,  5 }),
43                  new EuclideanIntegerPoint(new int[] { -13,  3 }),
44                  new EuclideanIntegerPoint(new int[] { -13,  4 }),
45                  new EuclideanIntegerPoint(new int[] { -13,  5 }),
46  
47                  // second expected cluster
48                  new EuclideanIntegerPoint(new int[] { -1,  0 }),
49                  new EuclideanIntegerPoint(new int[] { -1, -1 }),
50                  new EuclideanIntegerPoint(new int[] {  0, -1 }),
51                  new EuclideanIntegerPoint(new int[] {  1, -1 }),
52                  new EuclideanIntegerPoint(new int[] {  1, -2 }),
53  
54                  // third expected cluster
55                  new EuclideanIntegerPoint(new int[] { 13,  3 }),
56                  new EuclideanIntegerPoint(new int[] { 13,  4 }),
57                  new EuclideanIntegerPoint(new int[] { 14,  4 }),
58                  new EuclideanIntegerPoint(new int[] { 14,  7 }),
59                  new EuclideanIntegerPoint(new int[] { 16,  5 }),
60                  new EuclideanIntegerPoint(new int[] { 16,  6 }),
61                  new EuclideanIntegerPoint(new int[] { 17,  4 }),
62                  new EuclideanIntegerPoint(new int[] { 17,  7 })
63  
64          };
65          List<Cluster<EuclideanIntegerPoint>> clusters =
66              transformer.cluster(Arrays.asList(points), 3, 10);
67  
68          assertEquals(3, clusters.size());
69          boolean cluster1Found = false;
70          boolean cluster2Found = false;
71          boolean cluster3Found = false;
72          for (Cluster<EuclideanIntegerPoint> cluster : clusters) {
73              int[] center = cluster.getCenter().getPoint();
74              if (center[0] < 0) {
75                  cluster1Found = true;
76                  assertEquals(8, cluster.getPoints().size());
77                  assertEquals(-14, center[0]);
78                  assertEquals( 4, center[1]);
79              } else if (center[1] < 0) {
80                  cluster2Found = true;
81                  assertEquals(5, cluster.getPoints().size());
82                  assertEquals( 0, center[0]);
83                  assertEquals(-1, center[1]);
84              } else {
85                  cluster3Found = true;
86                  assertEquals(8, cluster.getPoints().size());
87                  assertEquals(15, center[0]);
88                  assertEquals(5, center[1]);
89              }
90          }
91          assertTrue(cluster1Found);
92          assertTrue(cluster2Found);
93          assertTrue(cluster3Found);
94  
95      }
96  
97  }