From e65ed4ff5b473eb25b1847c99884f924fb259a6e Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Sat, 8 Jan 2022 14:52:55 +0100 Subject: [PATCH] Add accessors. Allow retrieval of the full state, e.g. for persistent storage (cf. MATH-1594). --- .../math4/neuralnet/oned/NeuronString.java | 9 ++++++ .../neuralnet/twod/NeuronSquareMesh2D.java | 30 +++++++++++++++++++ .../neuralnet/oned/NeuronStringTest.java | 8 +++-- .../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 dcef12543..f223c91d9 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 @@ -143,6 +143,15 @@ public class NeuronString { return size; } + /** + * 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 4a6b1857d..92439ee48 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 @@ -284,6 +284,36 @@ public class NeuronSquareMesh2D return numberOfColumns; } + /** + * 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 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 e6aeec0e0..7069b04d0 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 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 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 78833d5fa..a5e721d1c 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