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