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

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

Introduction

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

Prototype

public KMeansPlusPlusClusterer(final Random random) 

Source Link

Document

Build a clusterer.

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)));
    }/*  www  .  j  a v a2 s .  co m*/

    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.  j a  v a  2s. c  o  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));
    }
}