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(); 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. * Atomically updates the given neuron.
* *
@ -156,11 +176,7 @@ public class KohonenUpdateAction implements UpdateAction {
double[] features, double[] features,
double learningRate) { double learningRate) {
while (true) { while (true) {
final double[] expect = n.getFeatures(); if (attemptNeuronUpdate(n, features, learningRate)) {
final double[] update = computeFeatures(expect,
features,
learningRate);
if (n.compareAndSetFeatures(expect, update)) {
break; break;
} }
} }
@ -181,11 +197,7 @@ public class KohonenUpdateAction implements UpdateAction {
while (true) { while (true) {
final Neuron best = MapUtils.findBest(features, net, distance); final Neuron best = MapUtils.findBest(features, net, distance);
final double[] expect = best.getFeatures(); if (attemptNeuronUpdate(best, features, learningRate)) {
final double[] update = computeFeatures(expect,
features,
learningRate);
if (best.compareAndSetFeatures(expect, update)) {
return best; return best;
} }