Reformat code

This commit is contained in:
niklas committed 2026-11-12 13:52:04 +01:00
1 parent 51c12cc0ea
commit 60c654936a
31 files changed
+262 -466

No files matched your search

+13 -23
View File
@@ -2,14 +2,12 @@ package machine_learning;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
class VectorTest
{
class VectorTest {
@Test
void shouldInitializeZeroVector()
{
void shouldInitializeZeroVector() {
final var v = new Vector(3);
final var expected = new Vector(0d, 0d, 0d);
@@ -19,8 +17,7 @@ class VectorTest
}
@Test
void shouldReturnCorrectVectorWhenAdding()
{
void shouldReturnCorrectVectorWhenAdding() {
final var v1 = new Vector(1d, 2d);
final var v2 = new Vector(3d, 4d);
@@ -31,8 +28,7 @@ class VectorTest
}
@Test
void shouldReturnCorrectVectorWhenSubtracting()
{
void shouldReturnCorrectVectorWhenSubtracting() {
final var v1 = new Vector(1d, 2d);
final var v2 = new Vector(3d, 4d);
@@ -43,8 +39,7 @@ class VectorTest
}
@Test
void shouldReturnCorrectVectorWhenScalarMultiplying()
{
void shouldReturnCorrectVectorWhenScalarMultiplying() {
final var v1 = new Vector(1d, 2d);
final var v2 = new Vector(3d, 4d);
@@ -55,8 +50,7 @@ class VectorTest
}
@Test
void shouldReturnCorrectVectorWhenEuclid()
{
void shouldReturnCorrectVectorWhenEuclid() {
final var v1 = new Vector(1d, 2d);
final var actual = v1.euclid();
@@ -66,8 +60,7 @@ class VectorTest
}
@Test
void shouldReturnCorrectDistance()
{
void shouldReturnCorrectDistance() {
final var v1 = new Vector(1d, 2d);
final var v2 = new Vector(3d, 4d);
@@ -78,8 +71,7 @@ class VectorTest
}
@Test
void shouldReturnCorrectVectorWhenDividing()
{
void shouldReturnCorrectVectorWhenDividing() {
final var v1 = new Vector(1d, 2d);
final var div = 2d;
@@ -90,12 +82,11 @@ class VectorTest
}
@Test
void shouldDecreaseDimensionCorrect()
{
void shouldDecreaseDimensionCorrect() {
final var v = new Vector(1d, 2d, 3d, 4d);
final var decreasedDimensionVector = v.decreasedDimension();
final var actual = decreasedDimensionVector.dimension();
final var expected = 3;
@@ -103,8 +94,7 @@ class VectorTest
}
@Test
void shouldNormalizeCorrect()
{
void shouldNormalizeCorrect() {
final var v = new Vector(4d, 4d, 4d, 4d);
final var actual = v.normalized();
@@ -18,18 +18,16 @@ import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class KNearestNeighbourTest
{
class KNearestNeighbourTest {
private List<Vector> positives;
private List<Vector> negatives;
private DistanceFunction distanceFunction;
@BeforeAll
void initLearnData()
{
void initLearnData() {
this.positives = new ArrayList<>(List.of(
new Vector(8d, 4d),
new Vector(8d, 6d),
@@ -48,8 +46,7 @@ class KNearestNeighbourTest
}
@Test
public void shouldReturnCorrectClassForVectorWithKEquals3()
{
public void shouldReturnCorrectClassForVectorWithKEquals3() {
final var kNearestNeighbour = new KNearestNeighbour(this.distanceFunction, 3);
kNearestNeighbour.learn(this.positives, this.negatives);
final var vector = new Vector(8, 3.5);
@@ -61,8 +58,7 @@ class KNearestNeighbourTest
}
@Test
public void shouldReturnCorrectClassForVectorWithKEquals5()
{
public void shouldReturnCorrectClassForVectorWithKEquals5() {
final var kNearestNeighbour = new KNearestNeighbour(this.distanceFunction, 5);
kNearestNeighbour.learn(this.positives, this.negatives);
final var vector = new Vector(8, 3.5);
@@ -74,8 +70,7 @@ class KNearestNeighbourTest
}
@Test
public void shouldReturnCorrectClassesForAppendicitisData()
{
public void shouldReturnCorrectClassesForAppendicitisData() {
final var trainDataFile = "./resources/app1.data";
final var testDataFile = "./resources/app1.test";
@@ -91,20 +86,16 @@ class KNearestNeighbourTest
final var testDataVectors = readFromFile(testDataFile);
var failCount = 0;
for (final var vector : testDataVectors)
{
for (final var vector : testDataVectors) {
final var expectedClass = DataClass.valueOf(Double.valueOf(vector.get(vector.dimension() - 1)).intValue());
final var testVector = vector.decreasedDimension();
final var actualClass = kNearestNeighbour.classify(testVector.normalized());
try
{
try {
assertEquals(expectedClass, actualClass);
}
catch (final AssertionFailedError e)
{
} catch (final AssertionFailedError e) {
failCount++;
}
}
@@ -115,8 +106,7 @@ class KNearestNeighbourTest
@Disabled("Takes long")
@Test
public void shouldReturnOptimum()
{
public void shouldReturnOptimum() {
final var trainDataFile = "./resources/app1.data";
final var testDataFile = "./resources/app1.test";
@@ -130,16 +120,13 @@ class KNearestNeighbourTest
final var kNearestNeighbour = crossValidation.validate(data, data.size());
}
private List<Vector> readFromFile(final String file)
{
private List<Vector> readFromFile(final String file) {
final List<Vector> vectorList = new ArrayList<>();
try (final var reader = new BufferedReader(new FileReader(file)))
{
try (final var reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null)
{
while ((line = reader.readLine()) != null) {
vectorList.add(new Vector(
Arrays.stream(line.split(","))
.map(Double::valueOf)
@@ -147,23 +134,20 @@ class KNearestNeighbourTest
));
}
}
catch (final IOException e)
{
} catch (final IOException e) {
e.printStackTrace();
}
return vectorList;
}
private Map<DataClass, List<Vector>> splitIntoClasses(final 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)
.filter(v -> v.get(v.dimension() - 1) == 1)
.collect(Collectors.toList());
var negatives = data.stream()
.filter(v -> v.get(v.dimension()-1) == 0)
.filter(v -> v.get(v.dimension() - 1) == 0)
.collect(Collectors.toList());
positives = positives.stream()
@@ -2,21 +2,22 @@ package machine_learning.perceptron;
import machine_learning.DataClass;
import machine_learning.Vector;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import java.util.ArrayList;
import java.util.List;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class PerceptronTest
{
class PerceptronTest {
List<Vector> positives;
List<Vector> negatives;
Perceptron perceptron;
@BeforeAll
void initLearnData()
{
void initLearnData() {
final double biasUnit = 1d;
this.positives = new ArrayList<>(List.of(
new Vector(8d, 4d, biasUnit),
@@ -37,8 +38,7 @@ class PerceptronTest
}
@Test
void shouldClassifyVectorCorrectAsNegative()
{
void shouldClassifyVectorCorrectAsNegative() {
final var vector = new Vector(0d, 0d, 1d);
final var actualClass = this.perceptron.classify(vector);
@@ -48,8 +48,7 @@ class PerceptronTest
}
@Test
void shouldClassifyVectorCorrectAsPositive()
{
void shouldClassifyVectorCorrectAsPositive() {
final var vector = new Vector(9d, 3d, 1d);
final var actualClass = this.perceptron.classify(vector);
+6 -12
View File
@@ -3,11 +3,9 @@ package search;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class ChessKnightNodeTest
{
public class ChessKnightNodeTest {
@Test
public void shouldReturnTrueWhenTargetReached()
{
public void shouldReturnTrueWhenTargetReached() {
final var state = new ChessKnightNode.KnightPair('a', '1');
final var node = new ChessKnightNode(state);
@@ -17,8 +15,7 @@ public class ChessKnightNodeTest
}
@Test
public void shouldReturnFalseWhenTargetNotReached()
{
public void shouldReturnFalseWhenTargetNotReached() {
final var actualState = new ChessKnightNode.KnightPair('a', '1');
final var targetState = new ChessKnightNode.KnightPair('h', '8');
@@ -30,8 +27,7 @@ public class ChessKnightNodeTest
}
@Test
public void shouldReturnNonEmptyListOfSuccessors()
{
public void shouldReturnNonEmptyListOfSuccessors() {
final var state = new ChessKnightNode.KnightPair('a', '1');
final var node = new ChessKnightNode(state);
@@ -41,8 +37,7 @@ public class ChessKnightNodeTest
}
@Test
public void shouldReturnCorrectSuccessorsWithMaxPossibleSuccessors()
{
public void shouldReturnCorrectSuccessorsWithMaxPossibleSuccessors() {
final var state = new ChessKnightNode.KnightPair('f', '3');
final var node = new ChessKnightNode(state);
@@ -71,8 +66,7 @@ public class ChessKnightNodeTest
}
@Test
public void shouldReturnCorrectSuccessorsOnEdge()
{
public void shouldReturnCorrectSuccessorsOnEdge() {
final var state = new ChessKnightNode.KnightPair('a', '1');
final var node = new ChessKnightNode(state);
+3 -6
View File
@@ -9,11 +9,9 @@ import java.util.List;
import static search.SearchTestUtils.countNodes;
import static search.SearchTestUtils.printSolution;
public class ChessKnightTest
{
public class ChessKnightTest {
@Test
public void shouldReturnOne()
{
public void shouldReturnOne() {
final var state = new ChessKnightNode.KnightPair('a', '3');
final var root = new ChessKnightNode(state);
@@ -31,8 +29,7 @@ public class ChessKnightTest
}
@Test
public void shouldReturnThree()
{
public void shouldReturnThree() {
final var state = new ChessKnightNode.KnightPair('a', '1');
final var root = new ChessKnightNode(state);
+5 -10
View File
@@ -3,11 +3,9 @@ package search;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class EightPuzzleNodeTest
{
class EightPuzzleNodeTest {
@Test
public void shouldReturnTrueWhenTargetReached()
{
public void shouldReturnTrueWhenTargetReached() {
final int[][] state = {
{1, 2, 3},
{4, 5, 6},
@@ -21,8 +19,7 @@ class EightPuzzleNodeTest
}
@Test
public void shouldReturnFalseWhenTargetNotReached()
{
public void shouldReturnFalseWhenTargetNotReached() {
final int[][] actualState = {
{7, 1, 6},
{0, 4, 2},
@@ -42,8 +39,7 @@ class EightPuzzleNodeTest
}
@Test
public void shouldReturnNonEmptyListOfSuccessors()
{
public void shouldReturnNonEmptyListOfSuccessors() {
final int[][] state = {
{7, 1, 6},
{0, 4, 2},
@@ -57,8 +53,7 @@ class EightPuzzleNodeTest
}
@Test
public void shouldReturnCorrectSuccessors()
{
public void shouldReturnCorrectSuccessors() {
final int[][] state = {
{7, 1, 6},
{0, 4, 2},
+5 -9
View File
@@ -5,13 +5,11 @@ import search.uninformed.iterativedeepening.IterativeDeepening;
import static search.SearchTestUtils.printSolution;
public class GameHelperTest
{
public class GameHelperTest {
@Test
void shouldReturnCorrectTargetDemonologist()
{
void shouldReturnCorrectTargetDemonologist() {
final int[][] state = {
{8, 4 ,0},
{8, 4, 0},
{6, 3, 1},
{2, 5, 7}
};
@@ -30,8 +28,7 @@ public class GameHelperTest
}
@Test
void shouldReturnCorrectTargetWartales()
{
void shouldReturnCorrectTargetWartales() {
final int[][] state = {
{1, 8, 6},
{3, 2, 7},
@@ -52,8 +49,7 @@ public class GameHelperTest
}
@Test
void shouldReturnCorrectTargetLabyrinthine()
{
void shouldReturnCorrectTargetLabyrinthine() {
final boolean[][] state = {
{true, false, true},
{false, true, false},
+6 -12
View File
@@ -3,11 +3,9 @@ package search;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class LabyrinthineNodeTest
{
class LabyrinthineNodeTest {
@Test
public void shouldReturnTrueWhenTargetReached()
{
public void shouldReturnTrueWhenTargetReached() {
final boolean[][] state = {
{true, true, true},
{true, true, true},
@@ -21,8 +19,7 @@ class LabyrinthineNodeTest
}
@Test
public void shouldReturnFalseWhenTargetNotReached()
{
public void shouldReturnFalseWhenTargetNotReached() {
final boolean[][] actualState = {
{true, false, true},
{false, true, false},
@@ -42,8 +39,7 @@ class LabyrinthineNodeTest
}
@Test
public void shouldReturnNonEmptyListOfSuccessors()
{
public void shouldReturnNonEmptyListOfSuccessors() {
final boolean[][] state = {
{true, false, true},
{false, true, false},
@@ -57,8 +53,7 @@ class LabyrinthineNodeTest
}
@Test
public void shouldReturnCorrectSuccessor()
{
public void shouldReturnCorrectSuccessor() {
final boolean[][] state = {
{true, false, true},
{true, true, true},
@@ -82,8 +77,7 @@ class LabyrinthineNodeTest
}
@Test
public void shouldReturnCorrectSuccessors()
{
public void shouldReturnCorrectSuccessors() {
final boolean[][] state = {
{true, false, true},
{true, true, false},
+5 -10
View File
@@ -1,16 +1,13 @@
package search;
public class SearchTestUtils
{
public static <T> void printSolution(final Node<T> targetNode)
{
public class SearchTestUtils {
public static <T> void printSolution(final Node<T> targetNode) {
var node = targetNode;
System.out.println("Read from down to top!");
System.out.println("END");
while (node != null)
{
while (node != null) {
System.out.println(node);
node = node.getParent();
}
@@ -18,13 +15,11 @@ public class SearchTestUtils
System.out.println("START");
}
public static <T> int countNodes(final Node<T> targetNode)
{
public static <T> int countNodes(final Node<T> targetNode) {
var node = targetNode;
int nodeCount = 0;
while (node != null)
{
while (node != null) {
nodeCount++;
node = node.getParent();
}
+14 -28
View File
@@ -7,11 +7,9 @@ import search.Node;
import static search.SearchTestUtils.printSolution;
class AStarTest
{
class AStarTest {
@Test
void shouldReturnCorrectTargetCubekNodeHeuristic1()
{
void shouldReturnCorrectTargetCubekNodeHeuristic1() {
final int[][] state = {
{2, 0, 4},
{6, 7, 1},
@@ -32,8 +30,7 @@ class AStarTest
}
@Test
void shouldReturnCorrectTargetCubekNodeHeuristic2()
{
void shouldReturnCorrectTargetCubekNodeHeuristic2() {
final int[][] state = {
{2, 0, 4},
{6, 7, 1},
@@ -53,18 +50,14 @@ class AStarTest
printSolution(actual);
}
private int h1(final Node<int[][]> node, final Node<int[][]> target)
{
private int h1(final Node<int[][]> node, final Node<int[][]> target) {
final var value = node.getValue();
final var targetValue = target.getValue();
var counter = 0;
for (var row = 0; row < value.length; row++)
{
for (var col = 0; col < value[row].length; col++)
{
if (value[row][col] != 0 && value[row][col] != targetValue[row][col])
{
for (var row = 0; row < value.length; row++) {
for (var col = 0; col < value[row].length; col++) {
if (value[row][col] != 0 && value[row][col] != targetValue[row][col]) {
counter++;
}
}
@@ -73,19 +66,16 @@ class AStarTest
return counter;
}
private int h2(final Node<int[][]> node, final Node<int[][]> target)
{
private int h2(final Node<int[][]> node, final Node<int[][]> target) {
final var value = node.getValue();
final var targetValue = target.getValue();
var manhattanDistance = 0;
for (int i = 1; i <= 8; i++)
{
for (int i = 1; i <= 8; i++) {
final var targetPos = detectPositionOf(i, targetValue);
final var actualPos = detectPositionOf(i, value);
if (targetPos != null && actualPos != null)
{
if (targetPos != null && actualPos != null) {
final var xDistance = Math.abs(targetPos.x() - actualPos.x());
final var yDistance = Math.abs(targetPos.y() - actualPos.y());
@@ -96,14 +86,10 @@ class AStarTest
return manhattanDistance;
}
private IntPair detectPositionOf(final int i, final int[][] value)
{
for (var row = 0; row < value.length; row++)
{
for (var col = 0; col < value[row].length; col++)
{
if (value[row][col] == i)
{
private IntPair detectPositionOf(final int i, final int[][] value) {
for (var row = 0; row < value.length; row++) {
for (var col = 0; col < value[row].length; col++) {
if (value[row][col] == i) {
return new IntPair(col, row);
}
}
@@ -7,11 +7,9 @@ import java.util.List;
import static search.SearchTestUtils.printSolution;
class BreadthFirstSearchTest
{
class BreadthFirstSearchTest {
@Test
void shouldReturnCorrectTarget()
{
void shouldReturnCorrectTarget() {
final int[][] state = {
{5, 0, 3},
{2, 1, 6},
@@ -32,8 +30,7 @@ class BreadthFirstSearchTest
}
@Test
void shouldReturnCorrectTargetCubekNode()
{
void shouldReturnCorrectTargetCubekNode() {
final int[][] state = {
{2, 0, 4},
{6, 7, 1},
@@ -5,11 +5,9 @@ import search.EightPuzzleNode;
import static search.SearchTestUtils.printSolution;
class DepthFirstSearchTest
{
class DepthFirstSearchTest {
@Test
void shouldReturnCorrectTarget()
{
void shouldReturnCorrectTarget() {
final int[][] state = {
{1, 2, 3},
{4, 5, 6},
@@ -5,11 +5,9 @@ import search.EightPuzzleNode;
import static search.SearchTestUtils.printSolution;
class IterativeDeepeningTest
{
class IterativeDeepeningTest {
@Test
void shouldReturnCorrectTargetEightPuzzle()
{
void shouldReturnCorrectTargetEightPuzzle() {
final int[][] state = {
{5, 0, 3},
{2, 1, 6},
@@ -30,8 +28,7 @@ class IterativeDeepeningTest
}
@Test
void shouldReturnCorrectTargetCubekNodeEightPuzzle()
{
void shouldReturnCorrectTargetCubekNodeEightPuzzle() {
final int[][] state = {
{2, 0, 4},
{6, 7, 1},