You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2022/01/08 13:57:05 UTC

[commons-math] 02/02: Add accessors.

This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git

commit e65ed4ff5b473eb25b1847c99884f924fb259a6e
Author: Gilles Sadowski <gi...@gmail.com>
AuthorDate: Sat Jan 8 14:52:55 2022 +0100

    Add accessors.
    
    Allow retrieval of the full state, e.g. for persistent storage (cf. MATH-1594).
---
 .../commons/math4/neuralnet/oned/NeuronString.java |  9 +++++++
 .../math4/neuralnet/twod/NeuronSquareMesh2D.java   | 30 ++++++++++++++++++++++
 .../math4/neuralnet/oned/NeuronStringTest.java     |  8 ++++--
 .../neuralnet/twod/NeuronSquareMesh2DTest.java     | 17 ++++++++++++
 4 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/oned/NeuronString.java b/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/oned/NeuronString.java
index dcef125..f223c91 100644
--- a/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/oned/NeuronString.java
+++ b/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/oned/NeuronString.java
@@ -144,6 +144,15 @@ public class NeuronString {
     }
 
     /**
+     * Indicates whether the line of neurons is wrapped.
+     *
+     * @return {@code true} if the last neuron is linked to the first neuron.
+     */
+    public boolean isWrapped() {
+        return wrap;
+    }
+
+    /**
      * Retrieves the features set from the neuron at location
      * {@code i} in the map.
      *
diff --git a/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/twod/NeuronSquareMesh2D.java b/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/twod/NeuronSquareMesh2D.java
index 4a6b185..92439ee 100644
--- a/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/twod/NeuronSquareMesh2D.java
+++ b/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/twod/NeuronSquareMesh2D.java
@@ -285,6 +285,36 @@ public class NeuronSquareMesh2D
     }
 
     /**
+     * Indicates whether the map is wrapped along the first dimension.
+     *
+     * @return {@code true} if the last neuron of a row is linked to
+     * the first neuron of that row.
+     */
+    public boolean isWrappedRow() {
+        return wrapRows;
+    }
+
+    /**
+     * Indicates whether the map is wrapped along the second dimension.
+     *
+     * @return {@code true} if the last neuron of a column is linked to
+     * the first neuron of that column.
+     */
+    public boolean isWrappedColumn() {
+        return wrapColumns;
+    }
+
+    /**
+     * Indicates the {@link SquareNeighbourhood type of connectivity}
+     * between neurons.
+     *
+     * @return the neighbourhood type.
+     */
+    public SquareNeighbourhood getSquareNeighbourhood() {
+        return neighbourhood;
+    }
+
+    /**
      * Retrieves the neuron at location {@code (i, j)} in the map.
      * The neuron at position {@code (0, 0)} is located at the upper-left
      * corner of the map.
diff --git a/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/oned/NeuronStringTest.java b/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/oned/NeuronStringTest.java
index e6aeec0..7069b04 100644
--- a/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/oned/NeuronStringTest.java
+++ b/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/oned/NeuronStringTest.java
@@ -47,8 +47,10 @@ public class NeuronStringTest {
     @Test
     public void testSegmentNetwork() {
         final FeatureInitializer[] initArray = {init};
-        final Network net = new NeuronString(4, false, initArray).getNetwork();
+        final NeuronString line = new NeuronString(4, false, initArray);
+        Assert.assertFalse(line.isWrapped());
 
+        final Network net = line.getNetwork();
         Collection<Neuron> neighbours;
 
         // Neuron 0.
@@ -92,8 +94,10 @@ public class NeuronStringTest {
     @Test
     public void testCircleNetwork() {
         final FeatureInitializer[] initArray = {init};
-        final Network net = new NeuronString(4, true, initArray).getNetwork();
+        final NeuronString line = new NeuronString(4, true, initArray);
+        Assert.assertTrue(line.isWrapped());
 
+        final Network net = line.getNetwork();
         Collection<Neuron> neighbours;
 
         // Neuron 0.
diff --git a/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/twod/NeuronSquareMesh2DTest.java b/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/twod/NeuronSquareMesh2DTest.java
index 78833d5..a5e721d 100644
--- a/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/twod/NeuronSquareMesh2DTest.java
+++ b/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/twod/NeuronSquareMesh2DTest.java
@@ -75,6 +75,23 @@ public class NeuronSquareMesh2DTest {
         Assert.assertEquals(3, net.getFeaturesSize());
     }
 
+    @Test
+    public void testAccessors() {
+        final FeatureInitializer[] initArray = {init};
+        NeuronSquareMesh2D map;
+
+        for (SquareNeighbourhood type : SquareNeighbourhood.values()) {
+            map = new NeuronSquareMesh2D(4, false, 2, true, type, initArray);
+            Assert.assertFalse(map.isWrappedRow());
+            Assert.assertTrue(map.isWrappedColumn());
+            Assert.assertEquals(type, map.getSquareNeighbourhood());
+
+            map = new NeuronSquareMesh2D(3, true, 4, false, type, initArray);
+            Assert.assertTrue(map.isWrappedRow());
+            Assert.assertFalse(map.isWrappedColumn());
+            Assert.assertEquals(type, map.getSquareNeighbourhood());
+        }
+    }
 
     /*
      * Test assumes that the network is