New private method for factoring out some common code.

This commit is contained in:
Gilles 2015-07-19 00:07:03 +02:00
parent a7fe613853
commit 7a8a778331
1 changed files with 22 additions and 10 deletions

View File

@ -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;
}