This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git
The following commit(s) were added to refs/heads/master by this push:
new 54345ef8f Fix Network.getNeighbours deleting links to excluded neurons
(#345)
54345ef8f is described below
commit 54345ef8fcb755bb4bd467253d3ba36485ac0f58
Author: insaf021 <[email protected]>
AuthorDate: Tue Nov 3 16:29:47 2026 +0530
Fix Network.getNeighbours deleting links to excluded neurons (#345)
getNeighbours(Neuron, Iterable) removed the excluded identifiers from the
network's own link set rather than from a copy, so the query deleted the
links from the neuron to every excluded neuron.
---
.../apache/commons/math4/neuralnet/Network.java | 2 +-
.../commons/math4/neuralnet/NetworkTest.java | 29 ++++++++++++++++++++++
2 files changed, 30 insertions(+), 1 deletion(-)
diff --git
a/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/Network.java
b/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/Network.java
index 36d12f30e..957359beb 100644
---
a/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/Network.java
+++
b/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/Network.java
@@ -367,7 +367,7 @@ public class Network
*/
public Collection<Neuron> getNeighbours(Neuron neuron,
Iterable<Neuron> exclude) {
- final Set<Long> idList = linkMap.get(neuron.getIdentifier());
+ final Set<Long> idList = new
HashSet<>(linkMap.get(neuron.getIdentifier()));
if (exclude != null) {
for (final Neuron n : exclude) {
idList.remove(n.getIdentifier());
diff --git
a/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/NetworkTest.java
b/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/NetworkTest.java
index f959c2a52..a0f861d9c 100644
---
a/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/NetworkTest.java
+++
b/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/NetworkTest.java
@@ -108,6 +108,35 @@ public class NetworkTest {
Assert.assertEquals(1, net.getNeighbours(net.getNeuron(3)).size());
}
+ /*
+ * Test assumes that the network is
+ *
+ * 0-----1
+ * | |
+ * | |
+ * 2-----3
+ */
+ @Test
+ public void testGetNeighboursWithExcludeDoesNotModifyNetwork() {
+ final FeatureInitializer[] initArray = {init};
+ final Network net = new NeuronSquareMesh2D(2, false,
+ 2, false,
+
SquareNeighbourhood.VON_NEUMANN,
+ initArray).getNetwork();
+ final Neuron n0 = net.getNeuron(0);
+ final Neuron n1 = net.getNeuron(1);
+ final Neuron n2 = net.getNeuron(2);
+
+ // Excluded neuron is not returned.
+ final Collection<Neuron> neighbours = net.getNeighbours(n0,
Arrays.asList(n1));
+ Assert.assertEquals(1, neighbours.size());
+ Assert.assertTrue(neighbours.contains(n2));
+
+ // Link from 0 to 1 still exists.
+ Assert.assertEquals(2, net.getNeighbours(n0).size());
+ Assert.assertTrue(net.getNeighbours(n0).contains(n1));
+ }
+
@Test
public void testIdentifierAssignment() {
final FeatureInitializer[] initArray = {init};