This commit is contained in:
Gilles Sadowski 2021-09-01 13:36:40 +02:00
parent b89f677c67
commit 470bdbb5f3
18 changed files with 74 additions and 64 deletions

View File

@ -131,7 +131,7 @@ class ChineseRings {
/** Data. */
private final Vector3D[] points = getPoints();
/** Number of samples. */
private int n = 0;
private int n;
/** {@inheritDoc} */
@Override

View File

@ -124,23 +124,23 @@ class ChineseRingsClassifier {
* z coordinates of the features array of the neurons.
*/
private FeatureInitializer[] makeInitializers() {
final SummaryStatistics[] centre = new SummaryStatistics[] {
final SummaryStatistics[] centre = {
new SummaryStatistics(),
new SummaryStatistics(),
new SummaryStatistics()
};
for (Vector3D p : rings.getPoints()) {
for (final Vector3D p : rings.getPoints()) {
centre[0].addValue(p.getX());
centre[1].addValue(p.getY());
centre[2].addValue(p.getZ());
}
final double[] mean = new double[] {
final double[] mean = {
centre[0].getMean(),
centre[1].getMean(),
centre[2].getMean()
};
final double[] dev = new double[] {
final double[] dev = {
0.1 * centre[0].getStandardDeviation(),
0.1 * centre[1].getStandardDeviation(),
0.1 * centre[2].getStandardDeviation()
@ -168,7 +168,7 @@ class ChineseRingsClassifier {
/** RNG. */
private final UniformRandomProvider rng = RandomSource.create(RandomSource.KISS);
/** Number of samples. */
private long n = 0;
private long n;
/** {@inheritDoc} */
@Override

View File

@ -87,7 +87,7 @@ public class City {
Set<City> cities) {
City closest = null;
double min = Double.POSITIVE_INFINITY;
for (City c : cities) {
for (final City c : cities) {
final double d = c.distance(x, y);
if (d < min) {
min = d;
@ -108,7 +108,7 @@ public class City {
double yB = 0;
int count = 0;
for (City c : cities) {
for (final City c : cities) {
final double[] coord = c.getCoordinates();
xB += coord[0];
yB += coord[1];
@ -132,7 +132,7 @@ public class City {
double y,
Set<City> cities) {
double maxDist = 0;
for (City c : cities) {
for (final City c : cities) {
final double dist = c.distance(x, y);
if (dist > maxDist) {
maxDist = dist;

View File

@ -55,7 +55,7 @@ public final class StandAlone implements Callable<Void> {
/** The output file. */
@Option(names = { "-o" }, paramLabel = "outputFile", required = true,
description = "Output file name.")
private String outputFile = null;
private String outputFile;
/**
* Program entry point.
@ -69,7 +69,7 @@ public final class StandAlone implements Callable<Void> {
@Override
public Void call() throws FileNotFoundException, UnsupportedEncodingException {
// Cities (in optimal travel order).
final City[] cities = new City[] {
final City[] cities = {
new City("o0", 0, 0),
new City("o1", 1, 0),
new City("o2", 2, 0),
@ -158,8 +158,7 @@ public final class StandAlone implements Callable<Void> {
out.println("# Travel distance: " + computeDistance(travel));
out.println("# Optimal travel distance: " + optimalDistance);
for (int i = 0; i < travel.length; i++) {
final City c = travel[i];
for (final City c : travel) {
final double[] coord = c.getCoordinates();
out.println(coord[0] + " " + coord[1] + " # " + c.getName());
}

View File

@ -115,12 +115,12 @@ public final class TravellingSalesmanSolver {
numUpdates / numTasks);
final List<Future<?>> execOutput = new ArrayList<>();
// Run tasks.
for (Runnable r : tasks) {
for (final Runnable r : tasks) {
execOutput.add(service.submit(r));
}
// Wait for completion (ignoring return value).
try {
for (Future<?> f : execOutput) {
for (final Future<?> f : execOutput) {
f.get();
}
} catch (InterruptedException | ExecutionException e) {
@ -189,7 +189,7 @@ public final class TravellingSalesmanSolver {
return new Iterator<double[]>() {
/** Number of samples. */
private long n = 0;
private long n;
/** {@inheritDoc} */
@Override
public boolean hasNext() {
@ -245,7 +245,7 @@ public final class TravellingSalesmanSolver {
// Sequence of coordinates.
final List<double[]> coordinatesList = new ArrayList<>();
for (Neuron n : getNeuronList()) {
for (final Neuron n : getNeuronList()) {
coordinatesList.add(n.getFeatures());
}
@ -262,7 +262,8 @@ public final class TravellingSalesmanSolver {
final List<double[]> coord = getCoordinatesList();
final List<City> cityList = new ArrayList<>();
City previous = null;
for (int i = 0, max = coord.size(); i < max; i++) {
final int max = coord.size();
for (int i = 0; i < max; i++) {
final double[] c = coord.get(i);
final City next = City.closest(c[0], c[1], cities);
if (!next.equals(previous)) {

View File

@ -46,7 +46,7 @@ public class MapRanking {
DistanceMeasure distance) {
this.distance = distance;
for (Neuron n : neurons) {
for (final Neuron n : neurons) {
map.add(n); // No defensive copy.
}
}
@ -109,7 +109,7 @@ public class MapRanking {
}
final List<Neuron> result = new ArrayList<>(m);
for (PairNeuronDouble p : list) {
for (final PairNeuronDouble p : list) {
result.add(p.getNeuron());
}

View File

@ -50,7 +50,7 @@ public final class MapUtils {
double d = 0;
int count = 0;
for (double[] f : data) {
for (final double[] f : data) {
++count;
d += distance.applyAsDouble(f, rank.rank(f, 1).get(0).getFeatures());
}
@ -80,7 +80,7 @@ public final class MapUtils {
int notAdjacentCount = 0;
int count = 0;
for (double[] f : data) {
for (final double[] f : data) {
++count;
final List<Neuron> p = rank.rank(f, 2);
if (!net.getNeighbours(p.get(0)).contains(p.get(1))) {

View File

@ -114,7 +114,7 @@ public class Network
for (int i = 0; i < numNeurons; i++) {
final long aId = neuronList[i].getIdentifier();
final Set<Long> aLinks = linkMap.get(aId);
for (Long bId : neighbourIdList[i]) {
for (final Long bId : neighbourIdList[i]) {
if (neuronMap.get(bId) == null) {
throw new IllegalStateException();
}
@ -150,11 +150,11 @@ public class Network
featureSize);
for (Map.Entry<Long, Neuron> e : neuronMap.entrySet()) {
for (final Map.Entry<Long, Neuron> e : neuronMap.entrySet()) {
copy.neuronMap.put(e.getKey(), e.getValue().copy());
}
for (Map.Entry<Long, Set<Long>> e : linkMap.entrySet()) {
for (final Map.Entry<Long, Set<Long>> e : linkMap.entrySet()) {
copy.linkMap.put(e.getKey(), new HashSet<>(e.getValue()));
}
@ -245,13 +245,12 @@ public class Network
*/
public void addLink(Neuron a,
Neuron b) {
final long aId = a.getIdentifier();
final long bId = b.getIdentifier();
// Check that the neurons belong to this network.
final long aId = a.getIdentifier();
if (a != getNeuron(aId)) {
throw new NoSuchElementException(Long.toString(aId));
}
final long bId = b.getIdentifier();
if (b != getNeuron(bId)) {
throw new NoSuchElementException(Long.toString(bId));
}
@ -283,13 +282,12 @@ public class Network
*/
public void deleteLink(Neuron a,
Neuron b) {
final long aId = a.getIdentifier();
final long bId = b.getIdentifier();
// Check that the neurons belong to this network.
final long aId = a.getIdentifier();
if (a != getNeuron(aId)) {
throw new NoSuchElementException(Long.toString(aId));
}
final long bId = b.getIdentifier();
if (b != getNeuron(bId)) {
throw new NoSuchElementException(Long.toString(bId));
}
@ -385,13 +383,13 @@ public class Network
Iterable<Neuron> exclude) {
final Set<Long> idList = linkMap.get(neuron.getIdentifier());
if (exclude != null) {
for (Neuron n : exclude) {
for (final Neuron n : exclude) {
idList.remove(n.getIdentifier());
}
}
final List<Neuron> neuronList = new ArrayList<>();
for (Long id : idList) {
for (final Long id : idList) {
neuronList.add(getNeuron(id));
}
@ -429,7 +427,7 @@ public class Network
final Collection<Neuron> neighbours = getNeighbours(neuronList[i]);
final long[] neighboursId = new long[neighbours.size()];
int count = 0;
for (Neuron n : neighbours) {
for (final Neuron n : neighbours) {
neighboursId[count] = n.getIdentifier();
++count;
}

View File

@ -33,6 +33,8 @@ import org.apache.commons.math4.neuralnet.Network;
public class NeuronString implements Serializable {
/** Serial version ID. */
private static final long serialVersionUID = 1L;
/** Minimal number of neurons. */
private static final int MIN_NEURONS = 2;
/** Underlying network. */
private final Network network;
/** Number of neurons. */
@ -59,8 +61,8 @@ public class NeuronString implements Serializable {
double[][] featuresList) {
size = featuresList.length;
if (size < 2) {
throw new NeuralNetException(NeuralNetException.TOO_SMALL, size, 2);
if (size < MIN_NEURONS) {
throw new NeuralNetException(NeuralNetException.TOO_SMALL, size, MIN_NEURONS);
}
this.wrap = wrap;
@ -100,8 +102,8 @@ public class NeuronString implements Serializable {
public NeuronString(int num,
boolean wrap,
FeatureInitializer[] featureInit) {
if (num < 2) {
throw new NeuralNetException(NeuralNetException.TOO_SMALL, num, 2);
if (num < MIN_NEURONS) {
throw new NeuralNetException(NeuralNetException.TOO_SMALL, num, MIN_NEURONS);
}
size = num;

View File

@ -120,7 +120,7 @@ public class KohonenUpdateAction implements UpdateAction {
neighbours = net.getNeighbours(neighbours, exclude);
// Update all the neighbours.
for (Neuron n : neighbours) {
for (final Neuron n : neighbours) {
updateNeighbouringNeuron(n, features, neighbourhoodDecay.applyAsDouble(radius));
}

View File

@ -50,6 +50,8 @@ public class NeuronSquareMesh2D
Serializable {
/** Serial version ID. */
private static final long serialVersionUID = 1L;
/** Minimal number of rows or columns. */
private static final int MIN_ROWS = 2;
/** Underlying network. */
private final Network network;
/** Number of rows. */
@ -114,11 +116,11 @@ public class NeuronSquareMesh2D
numberOfRows = featuresList.length;
numberOfColumns = featuresList[0].length;
if (numberOfRows < 2) {
throw new NeuralNetException(NeuralNetException.TOO_SMALL, numberOfRows, 2);
if (numberOfRows < MIN_ROWS) {
throw new NeuralNetException(NeuralNetException.TOO_SMALL, numberOfRows, MIN_ROWS);
}
if (numberOfColumns < 2) {
throw new NeuralNetException(NeuralNetException.TOO_SMALL, numberOfColumns, 2);
if (numberOfColumns < MIN_ROWS) {
throw new NeuralNetException(NeuralNetException.TOO_SMALL, numberOfColumns, MIN_ROWS);
}
wrapRows = wrapRowDim;
@ -171,11 +173,11 @@ public class NeuronSquareMesh2D
boolean wrapColDim,
SquareNeighbourhood neighbourhoodType,
FeatureInitializer[] featureInit) {
if (numRows < 2) {
throw new NeuralNetException(NeuralNetException.TOO_SMALL, numRows, 2);
if (numRows < MIN_ROWS) {
throw new NeuralNetException(NeuralNetException.TOO_SMALL, numRows, MIN_ROWS);
}
if (numCols < 2) {
throw new NeuralNetException(NeuralNetException.TOO_SMALL, numCols, 2);
if (numCols < MIN_ROWS) {
throw new NeuralNetException(NeuralNetException.TOO_SMALL, numCols, MIN_ROWS);
}
numberOfRows = numRows;
@ -559,7 +561,7 @@ public class NeuronSquareMesh2D
}
final Neuron aNeuron = network.getNeuron(identifiers[i][j]);
for (long b : linkEnd) {
for (final long b : linkEnd) {
final Neuron bNeuron = network.getNeuron(b);
// Link to all neighbours.
// The reverse links will be added as the loop proceeds.
@ -715,7 +717,7 @@ public class NeuronSquareMesh2D
final double[][] uMatrix = new double[nR][nC];
int numSamples = 0;
for (double[] sample : data) {
for (final double[] sample : data) {
++numSamples;
final List<Neuron> winners = rank.rank(sample, 2);
@ -746,7 +748,7 @@ public class NeuronSquareMesh2D
final double[] features = neuron.getFeatures();
double uDistance = 0;
int neighbourCount = 0;
for (Neuron n : neighbours) {
for (final Neuron n : neighbours) {
++neighbourCount;
uDistance += DISTANCE.applyAsDouble(features, n.getFeatures());
}

View File

@ -18,7 +18,7 @@
package org.apache.commons.math4.neuralnet.twod.util;
import java.util.Map;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.math4.neuralnet.Neuron;
import org.apache.commons.math4.neuralnet.twod.NeuronSquareMesh2D;
@ -28,7 +28,7 @@ import org.apache.commons.math4.neuralnet.twod.NeuronSquareMesh2D;
*/
public class LocationFinder {
/** Identifier to location mapping. */
private final Map<Long, Location> locations = new HashMap<>();
private final Map<Long, Location> locations = new ConcurrentHashMap<>();
/**
* Container holding a (row, column) pair.

View File

@ -84,7 +84,7 @@ public class SmoothedDataHistogram implements MapDataVisualization {
// Histogram bins.
final double[][] histo = new double[nR][nC];
for (double[] sample : data) {
for (final double[] sample : data) {
final List<Neuron> sorted = rank.rank(sample);
for (int i = 0; i < smoothingBins; i++) {
final LocationFinder.Location loc = finder.getLocation(sorted.get(i));

View File

@ -113,13 +113,14 @@ public class FastCosineTransform implements RealTransform {
* not a power of two plus one.
*/
private double[] fct(double[] f) {
final double[] transformed = new double[f.length];
final int n = f.length - 1;
if (!ArithmeticUtils.isPowerOfTwo(n)) {
throw new TransformException(TransformException.NOT_POWER_OF_TWO_PLUS_ONE,
Integer.valueOf(f.length));
}
final double[] transformed = new double[f.length];
if (n == 1) { // trivial case
transformed[0] = 0.5 * (f[0] + f[1]);
transformed[1] = 0.5 * (f[0] - f[1]);

View File

@ -40,6 +40,8 @@ import org.apache.commons.numbers.complex.Complex;
* Computation, 32 (1978), 175 - 199.
*/
public class FastFourierTransform implements ComplexTransform {
/** Number of array slots: 1 for "real" parts 1 for "imaginary" parts. */
private static final int NUM_PARTS = 2;
/**
* {@code W_SUB_N_R[i]} is the real part of
* {@code exp(- 2 * i * pi / n)}:
@ -125,9 +127,9 @@ public class FastFourierTransform implements ComplexTransform {
* or the array is not rectangular.
*/
public void transformInPlace(final double[][] dataRI) {
if (dataRI.length != 2) {
if (dataRI.length != NUM_PARTS) {
throw new TransformException(TransformException.SIZE_MISMATCH,
dataRI.length, 2);
dataRI.length, NUM_PARTS);
}
final double[] dataR = dataRI[0];
final double[] dataI = dataRI[1];
@ -228,7 +230,7 @@ public class FastFourierTransform implements ComplexTransform {
while (lastN0 < n) {
final int n0 = lastN0 << 1;
final int logN0 = lastLogN0 + 1;
double wSubN0R = W_SUB_N_R[logN0];
final double wSubN0R = W_SUB_N_R[logN0];
double wSubN0I = W_SUB_N_I[logN0];
if (inverse) {
wSubN0I = -wSubN0I;
@ -281,7 +283,7 @@ public class FastFourierTransform implements ComplexTransform {
*/
@Override
public Complex[] apply(final double[] f) {
final double[][] dataRI = new double[][] {
final double[][] dataRI = {
Arrays.copyOf(f, f.length),
new double[f.length]
};

View File

@ -216,13 +216,14 @@ public class FastHadamardTransform implements RealTransform {
*/
private double[] fht(double[] x) {
final int n = x.length;
final int halfN = n / 2;
if (!ArithmeticUtils.isPowerOfTwo(n)) {
throw new TransformException(TransformException.NOT_POWER_OF_TWO,
n);
}
final int halfN = n / 2;
// Instead of creating a matrix with p+1 columns and n rows, we use two
// one dimension arrays which we are used in an alternating way.
double[] yPrevious = new double[n];
@ -263,13 +264,13 @@ public class FastHadamardTransform implements RealTransform {
*/
private int[] fht(int[] x) {
final int n = x.length;
final int halfN = n / 2;
if (!ArithmeticUtils.isPowerOfTwo(n)) {
throw new TransformException(TransformException.NOT_POWER_OF_TWO,
n);
}
final int halfN = n / 2;
// Instead of creating a matrix with p+1 columns and n rows, we use two
// one dimension arrays which we are used in an alternating way.

View File

@ -30,6 +30,7 @@ public interface RealTransform extends UnaryOperator<double[]> {
* @return the transformed array (spectrum).
* @throws IllegalArgumentException if the transform cannot be performed.
*/
@Override
double[] apply(double[] f);
/**

View File

@ -25,6 +25,9 @@ import org.apache.commons.numbers.complex.Complex;
* Class is package-private (for internal use only).
*/
final class TransformUtils {
/** Number of array slots: 1 for "real" parts 1 for "imaginary" parts. */
private static final int NUM_PARTS = 2;
/** Utility class. */
private TransformUtils() {}
@ -99,9 +102,9 @@ final class TransformUtils {
* array is not two, or the array is not rectangular.
*/
static Complex[] createComplex(final double[][] dataRI) {
if (dataRI.length != 2) {
if (dataRI.length != NUM_PARTS) {
throw new TransformException(TransformException.SIZE_MISMATCH,
dataRI.length, 2);
dataRI.length, NUM_PARTS);
}
final double[] dataR = dataRI[0];
final double[] dataI = dataRI[1];