Repository: commons-math
Updated Branches:
  refs/heads/master 138f84bfa -> 67a7fdc19


MATH-1264

Sort units according to distance from a given vector.


Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/6c21bdb4
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/6c21bdb4
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/6c21bdb4

Branch: refs/heads/master
Commit: 6c21bdb4a107b49e57c9e5d6598d13ce6f85fc5d
Parents: 138f84b
Author: Gilles <er...@apache.org>
Authored: Tue Sep 1 14:16:36 2015 +0200
Committer: Gilles <er...@apache.org>
Committed: Tue Sep 1 14:16:36 2015 +0200

----------------------------------------------------------------------
 .../commons/math4/ml/neuralnet/MapUtils.java    | 76 ++++++++++++++++++++
 .../math4/ml/neuralnet/MapUtilsTest.java        | 18 +++++
 2 files changed, 94 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/6c21bdb4/src/main/java/org/apache/commons/math4/ml/neuralnet/MapUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/ml/neuralnet/MapUtils.java 
b/src/main/java/org/apache/commons/math4/ml/neuralnet/MapUtils.java
index a80b0a3..6683f25 100644
--- a/src/main/java/org/apache/commons/math4/ml/neuralnet/MapUtils.java
+++ b/src/main/java/org/apache/commons/math4/ml/neuralnet/MapUtils.java
@@ -19,6 +19,9 @@ package org.apache.commons.math4.ml.neuralnet;
 
 import java.util.HashMap;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.ArrayList;
 
 import org.apache.commons.math4.exception.NoDataException;
 import org.apache.commons.math4.ml.distance.DistanceMeasure;
@@ -105,6 +108,44 @@ public class MapUtils {
     }
 
     /**
+     * Creates a list of neurons sorted in increased order of the distance
+     * to the given {@code features}.
+     *
+     * @param features Data.
+     * @param neurons List of neurons to scan. If it is empty, an empty array
+     * will be returned.
+     * @param distance Distance function.
+     * @return the neurons, sorted in increasing order of distance in data
+     * space.
+     * @throws org.apache.commons.math4.exception.DimensionMismatchException
+     * if the size of the input is not compatible with the neurons features
+     * size.
+     *
+     * @see #findBest(double[],Iterable,DistanceMeasure)
+     * @see #findBestAndSecondBest(double[],Iterable,DistanceMeasure)
+     */
+    public static Neuron[] sort(double[] features,
+                                Iterable<Neuron> neurons,
+                                DistanceMeasure distance) {
+        final List<PairNeuronDouble> list = new ArrayList<PairNeuronDouble>();
+
+        for (final Neuron n : neurons) {
+            final double d = distance.compute(n.getFeatures(), features);
+            list.add(new PairNeuronDouble(n, d));
+        }
+
+        Collections.sort(list);
+
+        final int len = list.size();
+        final Neuron[] sorted = new Neuron[len];
+
+        for (int i = 0; i < len; i++) {
+            sorted[i] = list.get(i).getNeuron();
+        }
+        return sorted;
+    }
+
+    /**
      * Computes the <a href="http://en.wikipedia.org/wiki/U-Matrix";>
      *  U-matrix</a> of a two-dimensional map.
      *
@@ -245,4 +286,39 @@ public class MapUtils {
 
         return ((double) notAdjacentCount) / count;
     }
+
+    /**
+     * Helper data structure holding a (Neuron, double) pair.
+     */
+    private static class PairNeuronDouble implements 
Comparable<PairNeuronDouble> {
+        /** Key */
+        private final Neuron neuron;
+        /** Value */
+        private final double value;
+
+        /**
+         * @param neuron Neuron.
+         * @param value Value.
+         */
+        public PairNeuronDouble(Neuron neuron,
+                                double value) {
+            this.neuron = neuron;
+            this.value = value;
+        }
+
+        /** @return the neuron. */
+        public Neuron getNeuron() {
+            return neuron;
+        }
+
+        /** @return the value. */
+        public double getValue() {
+            return value;
+        }
+
+        /** {@inheritDoc} */
+        public int compareTo(PairNeuronDouble other) {
+            return Double.compare(this.value, other.value);
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/commons-math/blob/6c21bdb4/src/test/java/org/apache/commons/math4/ml/neuralnet/MapUtilsTest.java
----------------------------------------------------------------------
diff --git 
a/src/test/java/org/apache/commons/math4/ml/neuralnet/MapUtilsTest.java 
b/src/test/java/org/apache/commons/math4/ml/neuralnet/MapUtilsTest.java
index d395ba7..f716fc8 100644
--- a/src/test/java/org/apache/commons/math4/ml/neuralnet/MapUtilsTest.java
+++ b/src/test/java/org/apache/commons/math4/ml/neuralnet/MapUtilsTest.java
@@ -94,4 +94,22 @@ public class MapUtilsTest {
 
         Assert.assertEquals(3, allBest.size());
     }
+
+    @Test
+    public void testSort() {
+        final Set<Neuron> list = new HashSet<Neuron>();
+
+        for (int i = 0; i < 4; i++) {
+            list.add(new Neuron(i, new double[] { i - 0.5 }));
+        }
+
+        final Neuron[] sorted = MapUtils.sort(new double[] { 3.4 },
+                                              list,
+                                              new EuclideanDistance());
+
+        final long[] expected = new long[] { 3, 2, 1, 0 };
+        for (int i = 0; i < list.size(); i++) {
+            Assert.assertEquals(expected[i], sorted[i].getIdentifier());
+        }
+    }
 }

Reply via email to