Make variables final where possible
This commit is contained in:
1 parent
e1c93881af
commit
51c12cc0ea
15 files changed
+148
-146
No files matched your search
@@ -5,7 +5,7 @@ public enum DataClass
|
||||
NEGATIVE,
|
||||
POSITIVE;
|
||||
|
||||
public static DataClass valueOf(int i)
|
||||
public static DataClass valueOf(final int i)
|
||||
{
|
||||
return i == 0 ? NEGATIVE : POSITIVE;
|
||||
}
|
||||
|
||||
@@ -8,21 +8,21 @@ public class Vector
|
||||
{
|
||||
private final List<Double> values;
|
||||
|
||||
public Vector(int dim)
|
||||
public Vector(final int dim)
|
||||
{
|
||||
this(IntStream.range(0, dim)
|
||||
.mapToDouble(i -> 0d)
|
||||
.toArray());
|
||||
}
|
||||
|
||||
public Vector(double... value)
|
||||
public Vector(final double... value)
|
||||
{
|
||||
this(Arrays.stream(value)
|
||||
.boxed()
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
public Vector(List<Double> values)
|
||||
public Vector(final List<Double> values)
|
||||
{
|
||||
this.values = values;
|
||||
}
|
||||
@@ -32,7 +32,7 @@ public class Vector
|
||||
return this.values.size();
|
||||
}
|
||||
|
||||
public Vector add(Vector b)
|
||||
public Vector add(final Vector b)
|
||||
{
|
||||
checkEqualDimensions(b);
|
||||
return new Vector(IntStream.range(0,
|
||||
@@ -43,7 +43,7 @@ public class Vector
|
||||
|
||||
}
|
||||
|
||||
public Vector subtract(Vector b)
|
||||
public Vector subtract(final Vector b)
|
||||
{
|
||||
checkEqualDimensions(b);
|
||||
return new Vector(IntStream.range(0,
|
||||
@@ -53,7 +53,7 @@ public class Vector
|
||||
);
|
||||
}
|
||||
|
||||
public double scalar(Vector b)
|
||||
public double scalar(final Vector b)
|
||||
{
|
||||
checkEqualDimensions(b);
|
||||
return IntStream.range(0,
|
||||
@@ -70,7 +70,7 @@ public class Vector
|
||||
.sum());
|
||||
}
|
||||
|
||||
public double distance(Vector b)
|
||||
public double distance(final Vector b)
|
||||
{
|
||||
return Math.sqrt(IntStream.range(0,
|
||||
this.dimension())
|
||||
@@ -78,14 +78,14 @@ public class Vector
|
||||
.sum());
|
||||
}
|
||||
|
||||
public Vector divide(double div)
|
||||
public Vector divide(final double div)
|
||||
{
|
||||
return new Vector(IntStream.range(0, this.dimension())
|
||||
.mapToObj(i -> this.values.get(i) / div)
|
||||
.collect(Collectors.toCollection(ArrayList::new)));
|
||||
}
|
||||
|
||||
public double get(int index)
|
||||
public double get(final int index)
|
||||
{
|
||||
return this.values.get(index);
|
||||
}
|
||||
@@ -101,7 +101,7 @@ public class Vector
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o)
|
||||
public boolean equals(final Object o)
|
||||
{
|
||||
if (this == o)
|
||||
{
|
||||
@@ -112,7 +112,7 @@ public class Vector
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector vector = (Vector) o;
|
||||
final Vector vector = (Vector) o;
|
||||
|
||||
return Objects.equals(values, vector.values);
|
||||
}
|
||||
@@ -129,7 +129,7 @@ public class Vector
|
||||
return this.values.toString();
|
||||
}
|
||||
|
||||
private void checkEqualDimensions(Vector b)
|
||||
private void checkEqualDimensions(final Vector b)
|
||||
{
|
||||
if (this.dimension() != b.dimension())
|
||||
throw new IllegalArgumentException("Dimensions must be equal");
|
||||
|
||||
@@ -14,46 +14,46 @@ public class CrossValidation
|
||||
private final int paramMin;
|
||||
private final int paramMax;
|
||||
|
||||
public CrossValidation(int paramMin, int paramMax)
|
||||
public CrossValidation(final int paramMin, final int paramMax)
|
||||
{
|
||||
this.paramMin = paramMin;
|
||||
this.paramMax = paramMax;
|
||||
}
|
||||
|
||||
public KNearestNeighbour validate(List<Vector> data, int chunkSize)
|
||||
public KNearestNeighbour validate(final List<Vector> data, final int chunkSize)
|
||||
{
|
||||
Collections.shuffle(data);
|
||||
var counter = new AtomicInteger(0);
|
||||
var chunks = data.stream()
|
||||
.collect(Collectors.groupingBy(v -> counter.getAndIncrement() / (data.size() / chunkSize)))
|
||||
final var counter = new AtomicInteger(0);
|
||||
final var chunks = data.stream()
|
||||
.collect(Collectors.groupingBy(_ -> counter.getAndIncrement() / (data.size() / chunkSize)))
|
||||
.values();
|
||||
|
||||
var averageFailRates = new HashMap<Double, Integer>();
|
||||
final var averageFailRates = new HashMap<Double, Integer>();
|
||||
|
||||
IntStream.range(paramMin, paramMax).forEach(i -> {
|
||||
|
||||
var failRate = new AtomicReference<>(0d);
|
||||
final var failRate = new AtomicReference<>(0d);
|
||||
|
||||
chunks.forEach(chunk -> {
|
||||
var dataWithoutChunk = data.parallelStream()
|
||||
final var dataWithoutChunk = data.parallelStream()
|
||||
.filter(v -> !chunk.contains(v))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
var mapOfClasses = splitIntoClasses(dataWithoutChunk);
|
||||
var negatives = mapOfClasses.get(DataClass.NEGATIVE);
|
||||
var positives = mapOfClasses.get(DataClass.POSITIVE);
|
||||
final var mapOfClasses = splitIntoClasses(dataWithoutChunk);
|
||||
final var negatives = mapOfClasses.get(DataClass.NEGATIVE);
|
||||
final var positives = mapOfClasses.get(DataClass.POSITIVE);
|
||||
|
||||
var kNearestNeighbour = new KNearestNeighbour(Vector::distance, i);
|
||||
final var kNearestNeighbour = new KNearestNeighbour(Vector::distance, i);
|
||||
kNearestNeighbour.learn(positives, negatives);
|
||||
|
||||
var failCount = 0;
|
||||
|
||||
for (var vector : chunk)
|
||||
for (final var vector : chunk)
|
||||
{
|
||||
var expectedClass = DataClass.valueOf(Double.valueOf(vector.get(vector.dimension() - 1)).intValue());
|
||||
final var expectedClass = DataClass.valueOf(Double.valueOf(vector.get(vector.dimension() - 1)).intValue());
|
||||
|
||||
var testVector = vector.decreasedDimension().normalized();
|
||||
var actualClass = kNearestNeighbour.classify(testVector);
|
||||
final var testVector = vector.decreasedDimension().normalized();
|
||||
final var actualClass = kNearestNeighbour.classify(testVector);
|
||||
|
||||
if (expectedClass != actualClass)
|
||||
{
|
||||
@@ -67,20 +67,20 @@ public class CrossValidation
|
||||
averageFailRates.put(failRate.get() / chunkSize, i);
|
||||
});
|
||||
|
||||
var optimalParam = averageFailRates.get(averageFailRates.keySet().stream().min(Double::compareTo).get());
|
||||
var finalKNearestNeighbour = new KNearestNeighbour(Vector::distance, optimalParam);
|
||||
final var optimalParam = averageFailRates.get(averageFailRates.keySet().stream().min(Double::compareTo).get());
|
||||
final var finalKNearestNeighbour = new KNearestNeighbour(Vector::distance, optimalParam);
|
||||
|
||||
System.out.println("Optimaler Parameter k = " + optimalParam + " mit Fehlerrate " + averageFailRates.keySet().stream().min(Double::compareTo).get()*100 + " %");
|
||||
System.out.printf("Optimaler Parameter k = %d mit Fehlerrate %s %%%n", optimalParam, averageFailRates.keySet().stream().min(Double::compareTo).get() * 100);
|
||||
|
||||
var classes = splitIntoClasses(data);
|
||||
var negatives = classes.get(DataClass.NEGATIVE);
|
||||
var positives = classes.get(DataClass.POSITIVE);
|
||||
final var classes = splitIntoClasses(data);
|
||||
final var negatives = classes.get(DataClass.NEGATIVE);
|
||||
final var positives = classes.get(DataClass.POSITIVE);
|
||||
finalKNearestNeighbour.learn(positives, negatives);
|
||||
|
||||
return finalKNearestNeighbour;
|
||||
}
|
||||
|
||||
private Map<DataClass, List<Vector>> splitIntoClasses(List<Vector> data)
|
||||
private Map<DataClass, List<Vector>> splitIntoClasses(final List<Vector> data)
|
||||
{
|
||||
var positives = data.parallelStream()
|
||||
.filter(v -> v.get(v.dimension()-1) == 1)
|
||||
@@ -93,12 +93,12 @@ public class CrossValidation
|
||||
positives = positives.parallelStream()
|
||||
.map(Vector::decreasedDimension)
|
||||
.map(Vector::normalized)
|
||||
.collect(Collectors.toList());
|
||||
.toList();
|
||||
|
||||
negatives = negatives.parallelStream()
|
||||
.map(Vector::decreasedDimension)
|
||||
.map(Vector::normalized)
|
||||
.collect(Collectors.toList());
|
||||
.toList();
|
||||
|
||||
return Map.ofEntries(Map.entry(DataClass.NEGATIVE, negatives), Map.entry(DataClass.POSITIVE, positives));
|
||||
}
|
||||
|
||||
@@ -19,36 +19,38 @@ public class KNearestNeighbour implements MachineLearning
|
||||
|
||||
private final int k;
|
||||
|
||||
public KNearestNeighbour(DistanceFunction distanceFunction)
|
||||
public KNearestNeighbour(final DistanceFunction distanceFunction)
|
||||
{
|
||||
this(distanceFunction, 1);
|
||||
}
|
||||
|
||||
public KNearestNeighbour(DistanceFunction distanceFunction, int k)
|
||||
public KNearestNeighbour(final DistanceFunction distanceFunction, final int k)
|
||||
{
|
||||
this.distanceFunction = distanceFunction;
|
||||
this.k = k;
|
||||
}
|
||||
|
||||
public void learn(List<Vector> positives, List<Vector> negatives)
|
||||
@Override
|
||||
public void learn(final List<Vector> positives, final List<Vector> negatives)
|
||||
{
|
||||
this.positives = positives;
|
||||
this.negatives = negatives;
|
||||
}
|
||||
|
||||
public DataClass classify(Vector toClassify)
|
||||
@Override
|
||||
public DataClass classify(final Vector toClassify)
|
||||
{
|
||||
var nearestNeighbours = this.nearestNeighbours(
|
||||
final var nearestNeighbours = this.nearestNeighbours(
|
||||
Stream.concat(this.positives.stream(), this.negatives.stream())
|
||||
.collect(Collectors.toList()),
|
||||
toClassify
|
||||
);
|
||||
|
||||
var positivesWithNearestNeighboursAmount = nearestNeighbours.stream()
|
||||
final var positivesWithNearestNeighboursAmount = nearestNeighbours.stream()
|
||||
.filter(this.positives::contains)
|
||||
.count();
|
||||
|
||||
var negativesWithNearestNeighboursAmount = nearestNeighbours.stream()
|
||||
final var negativesWithNearestNeighboursAmount = nearestNeighbours.stream()
|
||||
.filter(this.negatives::contains)
|
||||
.count();
|
||||
|
||||
@@ -64,7 +66,7 @@ public class KNearestNeighbour implements MachineLearning
|
||||
return new Random().nextBoolean() ? DataClass.POSITIVE : DataClass.NEGATIVE;
|
||||
}
|
||||
|
||||
private List<Vector> nearestNeighbours(List<Vector> vectors, Vector vector)
|
||||
private List<Vector> nearestNeighbours(final List<Vector> vectors, final Vector vector)
|
||||
{
|
||||
return vectors.parallelStream()
|
||||
.map(v -> Map.entry(this.distanceFunction.distance(v, vector), v))
|
||||
|
||||
@@ -10,14 +10,15 @@ public class Perceptron implements MachineLearning
|
||||
{
|
||||
private Vector weight;
|
||||
|
||||
public void learn(List<Vector> positives, List<Vector> negatives)
|
||||
@Override
|
||||
public void learn(final List<Vector> positives, final List<Vector> negatives)
|
||||
{
|
||||
var iterationCounter = 0;
|
||||
this.weight = this.getInitializationVector(positives, negatives);
|
||||
|
||||
do
|
||||
{
|
||||
for (var x : positives)
|
||||
for (final var x : positives)
|
||||
{
|
||||
if (weight.scalar(x) <= 0)
|
||||
{
|
||||
@@ -26,7 +27,7 @@ public class Perceptron implements MachineLearning
|
||||
}
|
||||
}
|
||||
|
||||
for (var x : negatives)
|
||||
for (final var x : negatives)
|
||||
{
|
||||
if (weight.scalar(x) > 0)
|
||||
{
|
||||
@@ -44,21 +45,22 @@ public class Perceptron implements MachineLearning
|
||||
System.out.println("-----------------------------------------------------------------");
|
||||
}
|
||||
|
||||
public DataClass classify(Vector vector)
|
||||
@Override
|
||||
public DataClass classify(final Vector vector)
|
||||
{
|
||||
return this.weight.scalar(vector) > 0 ? DataClass.POSITIVE : DataClass.NEGATIVE;
|
||||
}
|
||||
|
||||
private Vector getInitializationVector(List<Vector> positives, List<Vector> negatives)
|
||||
private Vector getInitializationVector(final List<Vector> positives, final List<Vector> negatives)
|
||||
{
|
||||
var a = new Vector(positives.get(0).dimension());
|
||||
for (var x : positives)
|
||||
var a = new Vector(positives.getFirst().dimension());
|
||||
for (final var x : positives)
|
||||
{
|
||||
a = a.add(x);
|
||||
}
|
||||
|
||||
var b = new Vector(positives.get(0).dimension());
|
||||
for (var x : negatives)
|
||||
var b = new Vector(positives.getFirst().dimension());
|
||||
for (final var x : negatives)
|
||||
{
|
||||
b = b.add(x);
|
||||
}
|
||||
@@ -66,14 +68,14 @@ public class Perceptron implements MachineLearning
|
||||
return a.subtract(b);
|
||||
}
|
||||
|
||||
private boolean elementsAreCorrectClassified(List<Vector> positives, List<Vector> negatives, Vector weight)
|
||||
private boolean elementsAreCorrectClassified(final List<Vector> positives, final List<Vector> negatives, final Vector weight)
|
||||
{
|
||||
for (var x : positives)
|
||||
for (final var x : positives)
|
||||
{
|
||||
if (weight.scalar(x) <= 0) return false;
|
||||
}
|
||||
|
||||
for (var x : negatives)
|
||||
for (final var x : negatives)
|
||||
{
|
||||
if (weight.scalar(x) > 0) return false;
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ public class ChessKnightNode extends Node<ChessKnightNode.KnightPair> {
|
||||
return "%c%c".formatted(super.value.col, super.value.row);
|
||||
}
|
||||
|
||||
protected record KnightPair(char col, char row) {
|
||||
public record KnightPair(char col, char row) {
|
||||
public KnightPair {
|
||||
if (col < 'a' || col > 'h') throw new ChessKnightException("Row out of bounds at index %c".formatted(col));
|
||||
if (row < '1' || row > '8') throw new ChessKnightException("Column out of bounds at index %c".formatted(row));
|
||||
|
||||
@@ -111,7 +111,7 @@ public class LabyrinthineNode extends Node<boolean[][]>
|
||||
return copy;
|
||||
}
|
||||
|
||||
private void toggleStateAtPos(boolean[][] newState, IntPair pos)
|
||||
private void toggleStateAtPos(final boolean[][] newState, final IntPair pos)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
@@ -15,14 +15,14 @@ public class DepthFirstSearch
|
||||
|
||||
while (!newNodes.isEmpty())
|
||||
{
|
||||
final var resultNode = depthFirstSearch(newNodes.get(0), target);
|
||||
final var resultNode = depthFirstSearch(newNodes.getFirst(), target);
|
||||
|
||||
if (resultNode != null)
|
||||
{
|
||||
return resultNode;
|
||||
}
|
||||
|
||||
newNodes.remove(0);
|
||||
newNodes.removeFirst();
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@@ -30,14 +30,14 @@ public class IterativeDeepening
|
||||
|
||||
while (!newNodes.isEmpty() && depth < barrier)
|
||||
{
|
||||
final var resultNode = depthFirstSearch(newNodes.get(0), target, ++depth, barrier);
|
||||
final var resultNode = depthFirstSearch(newNodes.getFirst(), target, ++depth, barrier);
|
||||
|
||||
if (resultNode != null)
|
||||
{
|
||||
return resultNode;
|
||||
}
|
||||
|
||||
newNodes.remove(0);
|
||||
newNodes.removeFirst();
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@@ -10,9 +10,9 @@ class VectorTest
|
||||
@Test
|
||||
void shouldInitializeZeroVector()
|
||||
{
|
||||
var v = new Vector(3);
|
||||
final var v = new Vector(3);
|
||||
|
||||
var expected = new Vector(0d, 0d, 0d);
|
||||
final var expected = new Vector(0d, 0d, 0d);
|
||||
|
||||
assertEquals(3, v.dimension());
|
||||
assertEquals(expected, v);
|
||||
@@ -21,11 +21,11 @@ class VectorTest
|
||||
@Test
|
||||
void shouldReturnCorrectVectorWhenAdding()
|
||||
{
|
||||
var v1 = new Vector(1d, 2d);
|
||||
var v2 = new Vector(3d, 4d);
|
||||
final var v1 = new Vector(1d, 2d);
|
||||
final var v2 = new Vector(3d, 4d);
|
||||
|
||||
var actual = v1.add(v2);
|
||||
var expected = new Vector(4d, 6d);
|
||||
final var actual = v1.add(v2);
|
||||
final var expected = new Vector(4d, 6d);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
@@ -33,11 +33,11 @@ class VectorTest
|
||||
@Test
|
||||
void shouldReturnCorrectVectorWhenSubtracting()
|
||||
{
|
||||
var v1 = new Vector(1d, 2d);
|
||||
var v2 = new Vector(3d, 4d);
|
||||
final var v1 = new Vector(1d, 2d);
|
||||
final var v2 = new Vector(3d, 4d);
|
||||
|
||||
var actual = v1.subtract(v2);
|
||||
var expected = new Vector(-2d, -2d);
|
||||
final var actual = v1.subtract(v2);
|
||||
final var expected = new Vector(-2d, -2d);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
@@ -45,11 +45,11 @@ class VectorTest
|
||||
@Test
|
||||
void shouldReturnCorrectVectorWhenScalarMultiplying()
|
||||
{
|
||||
var v1 = new Vector(1d, 2d);
|
||||
var v2 = new Vector(3d, 4d);
|
||||
final var v1 = new Vector(1d, 2d);
|
||||
final var v2 = new Vector(3d, 4d);
|
||||
|
||||
var actual = v1.scalar(v2);
|
||||
var expected = 11d;
|
||||
final var actual = v1.scalar(v2);
|
||||
final var expected = 11d;
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
@@ -57,10 +57,10 @@ class VectorTest
|
||||
@Test
|
||||
void shouldReturnCorrectVectorWhenEuclid()
|
||||
{
|
||||
var v1 = new Vector(1d, 2d);
|
||||
final var v1 = new Vector(1d, 2d);
|
||||
|
||||
var actual = v1.euclid();
|
||||
var expected = Math.sqrt(5);
|
||||
final var actual = v1.euclid();
|
||||
final var expected = Math.sqrt(5);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
@@ -68,11 +68,11 @@ class VectorTest
|
||||
@Test
|
||||
void shouldReturnCorrectDistance()
|
||||
{
|
||||
var v1 = new Vector(1d, 2d);
|
||||
var v2 = new Vector(3d, 4d);
|
||||
final var v1 = new Vector(1d, 2d);
|
||||
final var v2 = new Vector(3d, 4d);
|
||||
|
||||
var actual = v1.distance(v2);
|
||||
var expected = Math.sqrt(8);
|
||||
final var actual = v1.distance(v2);
|
||||
final var expected = Math.sqrt(8);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
@@ -80,11 +80,11 @@ class VectorTest
|
||||
@Test
|
||||
void shouldReturnCorrectVectorWhenDividing()
|
||||
{
|
||||
var v1 = new Vector(1d, 2d);
|
||||
var div = 2d;
|
||||
final var v1 = new Vector(1d, 2d);
|
||||
final var div = 2d;
|
||||
|
||||
var actual = v1.divide(div);
|
||||
var expected = new Vector(0.5d, 1d);
|
||||
final var actual = v1.divide(div);
|
||||
final var expected = new Vector(0.5d, 1d);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
@@ -92,12 +92,12 @@ class VectorTest
|
||||
@Test
|
||||
void shouldDecreaseDimensionCorrect()
|
||||
{
|
||||
var v = new Vector(1d, 2d, 3d, 4d);
|
||||
final var v = new Vector(1d, 2d, 3d, 4d);
|
||||
|
||||
var decreasedDimensionVector = v.decreasedDimension();
|
||||
final var decreasedDimensionVector = v.decreasedDimension();
|
||||
|
||||
var actual = decreasedDimensionVector.dimension();
|
||||
var expected = 3;
|
||||
final var actual = decreasedDimensionVector.dimension();
|
||||
final var expected = 3;
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
@@ -105,11 +105,11 @@ class VectorTest
|
||||
@Test
|
||||
void shouldNormalizeCorrect()
|
||||
{
|
||||
var v = new Vector(4d, 4d, 4d, 4d);
|
||||
final var v = new Vector(4d, 4d, 4d, 4d);
|
||||
|
||||
var actual = v.normalized();
|
||||
final var actual = v.normalized();
|
||||
|
||||
var expected = new Vector(0.5d, 0.5d, 0.5d, 0.5d);
|
||||
final var expected = new Vector(0.5d, 0.5d, 0.5d, 0.5d);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@@ -50,12 +50,12 @@ class KNearestNeighbourTest
|
||||
@Test
|
||||
public void shouldReturnCorrectClassForVectorWithKEquals3()
|
||||
{
|
||||
var kNearestNeighbour = new KNearestNeighbour(this.distanceFunction, 3);
|
||||
final var kNearestNeighbour = new KNearestNeighbour(this.distanceFunction, 3);
|
||||
kNearestNeighbour.learn(this.positives, this.negatives);
|
||||
var vector = new Vector(8, 3.5);
|
||||
final var vector = new Vector(8, 3.5);
|
||||
|
||||
var actualClass = kNearestNeighbour.classify(vector);
|
||||
var expectedClass = DataClass.NEGATIVE;
|
||||
final var actualClass = kNearestNeighbour.classify(vector);
|
||||
final var expectedClass = DataClass.NEGATIVE;
|
||||
|
||||
assertEquals(expectedClass, actualClass);
|
||||
}
|
||||
@@ -63,12 +63,12 @@ class KNearestNeighbourTest
|
||||
@Test
|
||||
public void shouldReturnCorrectClassForVectorWithKEquals5()
|
||||
{
|
||||
var kNearestNeighbour = new KNearestNeighbour(this.distanceFunction, 5);
|
||||
final var kNearestNeighbour = new KNearestNeighbour(this.distanceFunction, 5);
|
||||
kNearestNeighbour.learn(this.positives, this.negatives);
|
||||
var vector = new Vector(8, 3.5);
|
||||
final var vector = new Vector(8, 3.5);
|
||||
|
||||
var actualClass = kNearestNeighbour.classify(vector);
|
||||
var expectedClass = DataClass.POSITIVE;
|
||||
final var actualClass = kNearestNeighbour.classify(vector);
|
||||
final var expectedClass = DataClass.POSITIVE;
|
||||
|
||||
assertEquals(expectedClass, actualClass);
|
||||
}
|
||||
@@ -76,34 +76,34 @@ class KNearestNeighbourTest
|
||||
@Test
|
||||
public void shouldReturnCorrectClassesForAppendicitisData()
|
||||
{
|
||||
var trainDataFile = "./resources/app1.data";
|
||||
var testDataFile = "./resources/app1.test";
|
||||
final var trainDataFile = "./resources/app1.data";
|
||||
final var testDataFile = "./resources/app1.test";
|
||||
|
||||
var trainDataVectors = readFromFile(trainDataFile);
|
||||
final var trainDataVectors = readFromFile(trainDataFile);
|
||||
|
||||
var dataClasses = splitIntoClasses(trainDataVectors);
|
||||
var negatives = dataClasses.get(DataClass.NEGATIVE);
|
||||
var positives = dataClasses.get(DataClass.POSITIVE);
|
||||
final var dataClasses = splitIntoClasses(trainDataVectors);
|
||||
final var negatives = dataClasses.get(DataClass.NEGATIVE);
|
||||
final var positives = dataClasses.get(DataClass.POSITIVE);
|
||||
|
||||
var kNearestNeighbour = new KNearestNeighbour(Vector::distance);
|
||||
final var kNearestNeighbour = new KNearestNeighbour(Vector::distance);
|
||||
kNearestNeighbour.learn(positives, negatives);
|
||||
|
||||
var testDataVectors = readFromFile(testDataFile);
|
||||
final var testDataVectors = readFromFile(testDataFile);
|
||||
var failCount = 0;
|
||||
|
||||
for (var vector : testDataVectors)
|
||||
for (final var vector : testDataVectors)
|
||||
{
|
||||
var expectedClass = DataClass.valueOf(Double.valueOf(vector.get(vector.dimension() - 1)).intValue());
|
||||
final var expectedClass = DataClass.valueOf(Double.valueOf(vector.get(vector.dimension() - 1)).intValue());
|
||||
|
||||
var testVector = vector.decreasedDimension();
|
||||
final var testVector = vector.decreasedDimension();
|
||||
|
||||
var actualClass = kNearestNeighbour.classify(testVector.normalized());
|
||||
final var actualClass = kNearestNeighbour.classify(testVector.normalized());
|
||||
|
||||
try
|
||||
{
|
||||
assertEquals(expectedClass, actualClass);
|
||||
}
|
||||
catch (AssertionFailedError e)
|
||||
catch (final AssertionFailedError e)
|
||||
{
|
||||
failCount++;
|
||||
}
|
||||
@@ -111,31 +111,30 @@ class KNearestNeighbourTest
|
||||
|
||||
System.out.println(failCount + " of " + testDataVectors.size() + " are not correct classified.");
|
||||
System.out.println("Fail rate of " + Math.round(100d * failCount / testDataVectors.size()) + " %");
|
||||
|
||||
}
|
||||
|
||||
@Disabled("Takes long")
|
||||
@Test
|
||||
public void shouldReturnOptimum()
|
||||
{
|
||||
var trainDataFile = "./resources/app1.data";
|
||||
var testDataFile = "./resources/app1.test";
|
||||
final var trainDataFile = "./resources/app1.data";
|
||||
final var testDataFile = "./resources/app1.test";
|
||||
|
||||
var trainDataVectors = readFromFile(trainDataFile);
|
||||
var testDataVectors = readFromFile(testDataFile);
|
||||
var data = Stream.concat(trainDataVectors.stream(), testDataVectors.stream())
|
||||
final var trainDataVectors = readFromFile(trainDataFile);
|
||||
final var testDataVectors = readFromFile(testDataFile);
|
||||
final var data = Stream.concat(trainDataVectors.stream(), testDataVectors.stream())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
var crossValidation = new CrossValidation(1, 100);
|
||||
final var crossValidation = new CrossValidation(1, 100);
|
||||
|
||||
var kNearestNeighbour = crossValidation.validate(data, data.size());
|
||||
final var kNearestNeighbour = crossValidation.validate(data, data.size());
|
||||
}
|
||||
|
||||
private List<Vector> readFromFile(String file)
|
||||
private List<Vector> readFromFile(final String file)
|
||||
{
|
||||
List<Vector> vectorList = new ArrayList<>();
|
||||
final List<Vector> vectorList = new ArrayList<>();
|
||||
|
||||
try (var reader = new BufferedReader(new FileReader(file)))
|
||||
try (final var reader = new BufferedReader(new FileReader(file)))
|
||||
{
|
||||
String line;
|
||||
|
||||
@@ -149,7 +148,7 @@ class KNearestNeighbourTest
|
||||
}
|
||||
|
||||
}
|
||||
catch (IOException e)
|
||||
catch (final IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -157,7 +156,7 @@ class KNearestNeighbourTest
|
||||
return vectorList;
|
||||
}
|
||||
|
||||
private Map<DataClass, List<Vector>> splitIntoClasses(List<Vector> data)
|
||||
private Map<DataClass, List<Vector>> splitIntoClasses(final List<Vector> data)
|
||||
{
|
||||
var positives = data.stream()
|
||||
.filter(v -> v.get(v.dimension()-1) == 1)
|
||||
@@ -170,12 +169,12 @@ class KNearestNeighbourTest
|
||||
positives = positives.stream()
|
||||
.map(Vector::decreasedDimension)
|
||||
.map(Vector::normalized)
|
||||
.collect(Collectors.toList());
|
||||
.toList();
|
||||
|
||||
negatives = negatives.stream()
|
||||
.map(Vector::decreasedDimension)
|
||||
.map(Vector::normalized)
|
||||
.collect(Collectors.toList());
|
||||
.toList();
|
||||
|
||||
return Map.ofEntries(Map.entry(DataClass.NEGATIVE, negatives), Map.entry(DataClass.POSITIVE, positives));
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ class PerceptronTest
|
||||
@BeforeAll
|
||||
void initLearnData()
|
||||
{
|
||||
double biasUnit = 1d;
|
||||
final double biasUnit = 1d;
|
||||
this.positives = new ArrayList<>(List.of(
|
||||
new Vector(8d, 4d, biasUnit),
|
||||
new Vector(8d, 6d, biasUnit),
|
||||
@@ -39,10 +39,10 @@ class PerceptronTest
|
||||
@Test
|
||||
void shouldClassifyVectorCorrectAsNegative()
|
||||
{
|
||||
var vector = new Vector(0d, 0d, 1d);
|
||||
final var vector = new Vector(0d, 0d, 1d);
|
||||
|
||||
var actualClass = this.perceptron.classify(vector);
|
||||
var expectedClass = DataClass.NEGATIVE;
|
||||
final var actualClass = this.perceptron.classify(vector);
|
||||
final var expectedClass = DataClass.NEGATIVE;
|
||||
|
||||
Assertions.assertEquals(expectedClass, actualClass);
|
||||
}
|
||||
@@ -50,10 +50,10 @@ class PerceptronTest
|
||||
@Test
|
||||
void shouldClassifyVectorCorrectAsPositive()
|
||||
{
|
||||
var vector = new Vector(9d, 3d, 1d);
|
||||
final var vector = new Vector(9d, 3d, 1d);
|
||||
|
||||
var actualClass = this.perceptron.classify(vector);
|
||||
var expectedClass = DataClass.POSITIVE;
|
||||
final var actualClass = this.perceptron.classify(vector);
|
||||
final var expectedClass = DataClass.POSITIVE;
|
||||
|
||||
Assertions.assertEquals(expectedClass, actualClass);
|
||||
}
|
||||
|
||||
@@ -24,8 +24,8 @@ public class ChessKnightTest
|
||||
final var actual = new BreadthFirstSearch().breadthFirstSearch(List.of(root), expected);
|
||||
|
||||
printSolution(actual);
|
||||
var nodeCount = countNodes(actual);
|
||||
var edgeCount = nodeCount - 1;
|
||||
final var nodeCount = countNodes(actual);
|
||||
final var edgeCount = nodeCount - 1;
|
||||
|
||||
Assertions.assertEquals(1, edgeCount);
|
||||
}
|
||||
@@ -43,8 +43,8 @@ public class ChessKnightTest
|
||||
final var actual = new BreadthFirstSearch().breadthFirstSearch(List.of(root), expected);
|
||||
|
||||
printSolution(actual);
|
||||
var nodeCount = countNodes(actual);
|
||||
var edgeCount = nodeCount - 1;
|
||||
final var nodeCount = countNodes(actual);
|
||||
final var edgeCount = nodeCount - 1;
|
||||
|
||||
Assertions.assertEquals(3, edgeCount);
|
||||
}
|
||||
|
||||
@@ -76,9 +76,9 @@ class LabyrinthineNodeTest
|
||||
{true, true, true},
|
||||
};
|
||||
|
||||
System.out.println(successors.get(0));
|
||||
System.out.println(successors.getFirst());
|
||||
|
||||
Assertions.assertArrayEquals(targetSuccessor, successors.get(0).getValue());
|
||||
Assertions.assertArrayEquals(targetSuccessor, successors.getFirst().getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -2,7 +2,6 @@ package search.uninformed.iterativedeepening;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import search.EightPuzzleNode;
|
||||
import search.LabyrinthineNode;
|
||||
|
||||
import static search.SearchTestUtils.printSolution;
|
||||
|
||||
|
||||
Reference in new issue
Block a user