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