diff --git a/src/main/java/org/apache/commons/math4/ml/neuralnet/sofm/KohonenUpdateAction.java b/src/main/java/org/apache/commons/math4/ml/neuralnet/sofm/KohonenUpdateAction.java index ffe694338..b9372916d 100644 --- a/src/main/java/org/apache/commons/math4/ml/neuralnet/sofm/KohonenUpdateAction.java +++ b/src/main/java/org/apache/commons/math4/ml/neuralnet/sofm/KohonenUpdateAction.java @@ -145,6 +145,26 @@ public class KohonenUpdateAction implements UpdateAction { return numberOfCalls.get(); } + /** + * Tries to update a neuron. + * + * @param n Neuron to be updated. + * @param features Training data. + * @param learningRate Learning factor. + * @return {@code true} if the update succeeded, {@code true} if a + * concurrent update has been detected. + */ + private boolean attemptNeuronUpdate(Neuron n, + double[] features, + double learningRate) { + final double[] expect = n.getFeatures(); + final double[] update = computeFeatures(expect, + features, + learningRate); + + return n.compareAndSetFeatures(expect, update); + } + /** * Atomically updates the given neuron. * @@ -156,11 +176,7 @@ public class KohonenUpdateAction implements UpdateAction { double[] features, double learningRate) { while (true) { - final double[] expect = n.getFeatures(); - final double[] update = computeFeatures(expect, - features, - learningRate); - if (n.compareAndSetFeatures(expect, update)) { + if (attemptNeuronUpdate(n, features, learningRate)) { break; } } @@ -181,11 +197,7 @@ public class KohonenUpdateAction implements UpdateAction { while (true) { final Neuron best = MapUtils.findBest(features, net, distance); - final double[] expect = best.getFeatures(); - final double[] update = computeFeatures(expect, - features, - learningRate); - if (best.compareAndSetFeatures(expect, update)) { + if (attemptNeuronUpdate(best, features, learningRate)) { return best; }