Example usage for org.apache.commons.math3.stat.clustering KMeansPlusPlusClusterer cluster

List of usage examples for org.apache.commons.math3.stat.clustering KMeansPlusPlusClusterer cluster

Introduction

In this page you can find the example usage for org.apache.commons.math3.stat.clustering KMeansPlusPlusClusterer cluster.

Prototype

public List<Cluster<T>> cluster(final Collection<T> points, final int k, final int maxIterations)
        throws MathIllegalArgumentException, ConvergenceException 

Source Link

Document

Runs the K-means++ clustering algorithm.

Usage

From source file:org.moeaframework.core.operator.ParentCentricVariationTest.java

@Override
protected void check(Solution[] parents, Solution[] offspring) {
    List<ClusterablePoint> points = new ArrayList<ClusterablePoint>();

    for (Solution solution : offspring) {
        points.add(new ClusterablePoint(EncodingUtils.getReal(solution)));
    }//from  w w  w. j a  v a 2 s.c  om

    KMeansPlusPlusClusterer<ClusterablePoint> clusterer = new KMeansPlusPlusClusterer<ClusterablePoint>(
            new Random());

    List<Cluster<ClusterablePoint>> clusters = clusterer.cluster(points, parents.length, 100);

    for (Solution solution : parents) {
        boolean match = false;

        for (int i = 0; i < clusters.size(); i++) {
            boolean allEqual = true;

            double[] centroid = clusters.get(i).getCenter().getPoint();
            double[] parent = EncodingUtils.getReal(solution);

            for (int j = 0; j < parent.length; j++) {
                if (Math.abs(parent[j] - centroid[j]) > TestThresholds.VARIATION_EPS) {
                    allEqual = false;
                }
            }

            if (allEqual) {
                match = true;
            }
        }

        Assert.assertTrue(match);
    }
}

From source file:org.moeaframework.core.operator.real.AdaptiveMetropolisTest.java

/**
 * Tests if each cluster formed around the parents exhibits the same
 * covariance as the parent solutions./*from www.  jav a2  s . co m*/
 */
@Test
public void testCovariance() {
    //the smaller jump rate is used to ensure separation between clusters
    double jumpRateCoefficient = 0.5;

    AdaptiveMetropolis am = new AdaptiveMetropolis(3, TestThresholds.SAMPLES, jumpRateCoefficient);

    Solution[] parents = new Solution[] { newSolution(0.0, 0.0), newSolution(0.0, 5.0), newSolution(2.0, 0.0) };

    Solution[] offspring = am.evolve(parents);

    List<ClusterablePoint> points = new ArrayList<ClusterablePoint>();

    for (Solution solution : offspring) {
        points.add(new ClusterablePoint(EncodingUtils.getReal(solution)));
    }

    KMeansPlusPlusClusterer<ClusterablePoint> clusterer = new KMeansPlusPlusClusterer<ClusterablePoint>(
            new Random());

    List<Cluster<ClusterablePoint>> clusters = clusterer.cluster(points, parents.length, 100);

    for (Cluster<ClusterablePoint> cluster : clusters) {
        TestUtils.assertEquals(getCovariance(cluster), getCovariance(parents, jumpRateCoefficient),
                new RelativeError(0.1));
    }
}