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  package org.hipparchus.clustering;
23  
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import org.hamcrest.CoreMatchers;
29  import org.hamcrest.MatcherAssert;
30  import org.hipparchus.clustering.distance.CanberraDistance;
31  import org.hipparchus.clustering.distance.DistanceMeasure;
32  import org.hipparchus.exception.MathIllegalArgumentException;
33  import org.hipparchus.exception.NullArgumentException;
34  import org.hipparchus.random.JDKRandomGenerator;
35  import org.hipparchus.random.RandomGenerator;
36  import org.junit.Assert;
37  import org.junit.Test;
38  
39  /**
40   * Test cases for FuzzyKMeansClusterer.
41   *
42   */
43  public class FuzzyKMeansClustererTest {
44  
45      @Test
46      public void testCluster() {
47          final List<DoublePoint> points = new ArrayList<DoublePoint>();
48  
49          // create 10 data points: [1], ... [10]
50          for (int i = 1; i <= 10; i++) {
51              final DoublePoint p = new DoublePoint(new double[] { i } );
52              points.add(p);
53          }
54  
55          final FuzzyKMeansClusterer<DoublePoint> transformer =
56                  new FuzzyKMeansClusterer<DoublePoint>(3, 2.0);
57          final List<CentroidCluster<DoublePoint>> clusters = transformer.cluster(points);
58  
59          // we expect 3 clusters:
60          //   [1], [2], [3]
61          //   [4], [5], [6], [7]
62          //   [8], [9], [10]
63          final List<DoublePoint> clusterOne = Arrays.asList(points.get(0), points.get(1), points.get(2));
64          final List<DoublePoint> clusterTwo = Arrays.asList(points.get(3), points.get(4), points.get(5), points.get(6));
65          final List<DoublePoint> clusterThree = Arrays.asList(points.get(7), points.get(8), points.get(9));
66  
67          boolean cluster1Found = false;
68          boolean cluster2Found = false;
69          boolean cluster3Found = false;
70          Assert.assertEquals(3, clusters.size());
71          for (final Cluster<DoublePoint> cluster : clusters) {
72              if (cluster.getPoints().containsAll(clusterOne)) {
73                  cluster1Found = true;
74              }
75              if (cluster.getPoints().containsAll(clusterTwo)) {
76                  cluster2Found = true;
77              }
78              if (cluster.getPoints().containsAll(clusterThree)) {
79                  cluster3Found = true;
80              }
81          }
82          Assert.assertTrue(cluster1Found);
83          Assert.assertTrue(cluster2Found);
84          Assert.assertTrue(cluster3Found);
85      }
86  
87      @Test(expected = MathIllegalArgumentException.class)
88      public void testTooSmallFuzzynessFactor() {
89          new FuzzyKMeansClusterer<DoublePoint>(3, 1.0);
90      }
91  
92      @Test(expected = NullArgumentException.class)
93      public void testNullDataset() {
94          final FuzzyKMeansClusterer<DoublePoint> clusterer = new FuzzyKMeansClusterer<DoublePoint>(3, 2.0);
95          clusterer.cluster(null);
96      }
97  
98      @Test
99      public void testGetters() {
100         final DistanceMeasure measure = new CanberraDistance();
101         final RandomGenerator random = new JDKRandomGenerator();
102         final FuzzyKMeansClusterer<DoublePoint> clusterer =
103                 new FuzzyKMeansClusterer<DoublePoint>(3, 2.0, 100, measure, 1e-6, random);
104 
105         Assert.assertEquals(3, clusterer.getK());
106         Assert.assertEquals(2.0, clusterer.getFuzziness(), 1e-6);
107         Assert.assertEquals(100, clusterer.getMaxIterations());
108         Assert.assertEquals(1e-6, clusterer.getEpsilon(), 1e-12);
109         MatcherAssert.assertThat(clusterer.getDistanceMeasure(), CoreMatchers.is(measure));
110         MatcherAssert.assertThat(clusterer.getRandomGenerator(), CoreMatchers.is(random));
111     }
112 
113     @Test
114     public void testSingleCluster() {
115         final List<DoublePoint> points = new ArrayList<DoublePoint>();
116         points.add(new DoublePoint(new double[] { 1, 1 }));
117 
118         final FuzzyKMeansClusterer<DoublePoint> transformer =
119                 new FuzzyKMeansClusterer<DoublePoint>(1, 2.0);
120         final List<CentroidCluster<DoublePoint>> clusters = transformer.cluster(points);
121 
122         Assert.assertEquals(1, clusters.size());
123     }
124 
125     @Test
126     public void testClusterCenterEqualsPoints() {
127         final List<DoublePoint> points = new ArrayList<DoublePoint>();
128         points.add(new DoublePoint(new double[] { 1, 1 }));
129         points.add(new DoublePoint(new double[] { 1.00001, 1.00001 }));
130         points.add(new DoublePoint(new double[] { 2, 2 }));
131         points.add(new DoublePoint(new double[] { 3, 3 }));
132 
133         final FuzzyKMeansClusterer<DoublePoint> transformer =
134                 new FuzzyKMeansClusterer<DoublePoint>(3, 2.0);
135         final List<CentroidCluster<DoublePoint>> clusters = transformer.cluster(points);
136 
137         Assert.assertEquals(3, clusters.size());
138     }
139 
140 }