List of usage examples for org.apache.commons.math3.ml.distance DistanceMeasure compute
double compute(double[] a, double[] b);
From source file:KMeansRecommender.MyKMeansPlusPlusClusterer.java
/** * Computes the centroid for a set of points. * * @param points the set of points//from w ww .j av a 2s . co m * @param dimension the point dimension * @return the computed centroid for the set of points */ private Clusterable centroidOf(final Collection<T> points, final int dimension) { final double[] centroid = new double[dimension]; DistanceMeasure measure = getDistanceMeasure(); double minSumDistance = -1; T minPoint = null; for (final T p : points) { final double[] tempCenterPoint = p.getPoint(); double tempSumDistance = 0; for (final T q : points) { tempSumDistance += measure.compute(tempCenterPoint, q.getPoint()); } if (tempSumDistance < minSumDistance || minSumDistance == -1) { minSumDistance = tempSumDistance; minPoint = p; } } final double[] centerPoint = minPoint.getPoint(); for (int i = 0; i < centroid.length; i++) { centroid[i] = centerPoint[i]; } return new DoublePoint(centroid); }
From source file:ec.coevolve.MultiPopCoevolutionaryEvaluatorExtra.java
protected Individual[] behaviourElite(EvolutionState state, int subpop) { // Generate the dataset ArrayList<IndividualClusterable> points = new ArrayList<IndividualClusterable>(); if (novelChampionsOrigin == NovelChampionsOrigin.halloffame) { for (int i = 0; i < hallOfFame[subpop].size(); i++) { points.add(new IndividualClusterable(hallOfFame[subpop].get(i), i)); }/*www . j a v a 2 s.c o m*/ } else if (novelChampionsOrigin == NovelChampionsOrigin.archive) { for (ArchiveEntry ae : archives[subpop]) { points.add(new IndividualClusterable(ae.getIndividual(), ae.getGeneration())); } } // Cap -- only use the individuals with the highest fitness scores if (novelChampionsCap > 0) { // calculate the percentile DescriptiveStatistics ds = new DescriptiveStatistics(); for (IndividualClusterable ic : points) { ds.addValue(ic.getFitness()); } double percentile = ds.getPercentile(novelChampionsCap); // remove those below the percentile Iterator<IndividualClusterable> iter = points.iterator(); while (iter.hasNext()) { IndividualClusterable next = iter.next(); if (next.getFitness() < percentile) { iter.remove(); } } } // Check if there are enough points for clustering if (points.size() <= novelChampions) { Individual[] elite = new Individual[points.size()]; for (int i = 0; i < elite.length; i++) { elite[i] = points.get(i).getIndividual(); } return elite; } // Do the k-means clustering KMeansPlusPlusClusterer<IndividualClusterable> clusterer = new KMeansPlusPlusClusterer<IndividualClusterable>( novelChampions, 100); List<CentroidCluster<IndividualClusterable>> clusters = clusterer.cluster(points); // Return one from each cluster Individual[] elite = new Individual[novelChampions]; for (int i = 0; i < clusters.size(); i++) { CentroidCluster<IndividualClusterable> cluster = clusters.get(i); List<IndividualClusterable> clusterPoints = cluster.getPoints(); if (novelChampionsMode == NovelChampionsMode.random) { int randIndex = state.random[0].nextInt(clusterPoints.size()); elite[i] = clusterPoints.get(randIndex).getIndividual(); } else if (novelChampionsMode == NovelChampionsMode.last) { IndividualClusterable oldest = null; for (IndividualClusterable ic : clusterPoints) { if (oldest == null || ic.age > oldest.age) { oldest = ic; } } elite[i] = oldest.getIndividual(); } else if (novelChampionsMode == NovelChampionsMode.centroid) { DistanceMeasure dm = clusterer.getDistanceMeasure(); double[] centroid = cluster.getCenter().getPoint(); IndividualClusterable closest = null; double closestDist = Double.MAX_VALUE; for (IndividualClusterable ic : clusterPoints) { double dist = dm.compute(centroid, ic.getPoint()); if (dist < closestDist) { closestDist = dist; closest = ic; } } elite[i] = closest.getIndividual(); } else if (novelChampionsMode == NovelChampionsMode.best) { IndividualClusterable best = null; float highestFit = Float.NEGATIVE_INFINITY; for (IndividualClusterable ic : clusterPoints) { if (ic.getFitness() > highestFit) { best = ic; highestFit = ic.getFitness(); } } elite[i] = best.getIndividual(); } } return elite; }
From source file:org.apache.solr.client.solrj.io.eval.DistanceEvaluator.java
private Matrix distance(DistanceMeasure distanceMeasure, Matrix matrix) { double[][] data = matrix.getData(); RealMatrix realMatrix = new Array2DRowRealMatrix(data); realMatrix = realMatrix.transpose(); data = realMatrix.getData();/* w w w . j av a2 s.c o m*/ double[][] distanceMatrix = new double[data.length][data.length]; for (int i = 0; i < data.length; i++) { double[] row = data[i]; for (int j = 0; j < data.length; j++) { double[] row2 = data[j]; double dist = distanceMeasure.compute(row, row2); distanceMatrix[i][j] = dist; } } return new Matrix(distanceMatrix); }
From source file:ro.hasna.ts.math.ml.distance.util.DistanceTester.java
public DistanceTester withDistanceMeasure(final DistanceMeasure distance) { this.distance = new GenericDistanceMeasure<double[]>() { private static final long serialVersionUID = -7065467026544814688L; @Override/*from www. j a v a 2s . c o m*/ public double compute(double[] a, double[] b) { return distance.compute(a, b); } @Override public double compute(double[] a, double[] b, double cutOffValue) { return distance.compute(a, b); } }; return this; }