View Javadoc
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    *      https://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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.clustering;
24  
25  
26  import java.util.Arrays;
27  import java.util.List;
28  
29  import org.junit.Assert;
30  import org.junit.Test;
31  
32  public class MultiKMeansPlusPlusClustererTest {
33  
34      @Test
35      public void dimension2() {
36          MultiKMeansPlusPlusClusterer<DoublePoint> transformer =
37              new MultiKMeansPlusPlusClusterer<DoublePoint>(
38                      new KMeansPlusPlusClusterer<DoublePoint>(3, 10), 5);
39  
40          DoublePoint[] points = new DoublePoint[] {
41  
42                  // first expected cluster
43                  new DoublePoint(new int[] { -15,  3 }),
44                  new DoublePoint(new int[] { -15,  4 }),
45                  new DoublePoint(new int[] { -15,  5 }),
46                  new DoublePoint(new int[] { -14,  3 }),
47                  new DoublePoint(new int[] { -14,  5 }),
48                  new DoublePoint(new int[] { -13,  3 }),
49                  new DoublePoint(new int[] { -13,  4 }),
50                  new DoublePoint(new int[] { -13,  5 }),
51  
52                  // second expected cluster
53                  new DoublePoint(new int[] { -1,  0 }),
54                  new DoublePoint(new int[] { -1, -1 }),
55                  new DoublePoint(new int[] {  0, -1 }),
56                  new DoublePoint(new int[] {  1, -1 }),
57                  new DoublePoint(new int[] {  1, -2 }),
58  
59                  // third expected cluster
60                  new DoublePoint(new int[] { 13,  3 }),
61                  new DoublePoint(new int[] { 13,  4 }),
62                  new DoublePoint(new int[] { 14,  4 }),
63                  new DoublePoint(new int[] { 14,  7 }),
64                  new DoublePoint(new int[] { 16,  5 }),
65                  new DoublePoint(new int[] { 16,  6 }),
66                  new DoublePoint(new int[] { 17,  4 }),
67                  new DoublePoint(new int[] { 17,  7 })
68  
69          };
70          List<CentroidCluster<DoublePoint>> clusters = transformer.cluster(Arrays.asList(points));
71  
72          Assert.assertEquals(3, clusters.size());
73          boolean cluster1Found = false;
74          boolean cluster2Found = false;
75          boolean cluster3Found = false;
76          double epsilon = 1e-6;
77          for (CentroidCluster<DoublePoint> cluster : clusters) {
78              Clusterable center = cluster.getCenter();
79              double[] point = center.getPoint();
80              if (point[0] < 0) {
81                  cluster1Found = true;
82                  Assert.assertEquals(8, cluster.getPoints().size());
83                  Assert.assertEquals(-14, point[0], epsilon);
84                  Assert.assertEquals( 4, point[1], epsilon);
85              } else if (point[1] < 0) {
86                  cluster2Found = true;
87                  Assert.assertEquals(5, cluster.getPoints().size());
88                  Assert.assertEquals( 0, point[0], epsilon);
89                  Assert.assertEquals(-1, point[1], epsilon);
90              } else {
91                  cluster3Found = true;
92                  Assert.assertEquals(8, cluster.getPoints().size());
93                  Assert.assertEquals(15, point[0], epsilon);
94                  Assert.assertEquals(5, point[1], epsilon);
95              }
96          }
97          Assert.assertTrue(cluster1Found);
98          Assert.assertTrue(cluster2Found);
99          Assert.assertTrue(cluster3Found);
100 
101     }
102 
103 }