Implemented A* at full state
This commit is contained in:
@ -2,20 +2,20 @@ package search.heuristic;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import search.EightPuzzleNode;
|
||||
import search.IntPair;
|
||||
import search.Node;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static search.SearchTestUtils.printSolution;
|
||||
|
||||
class AStarTest
|
||||
{
|
||||
@Test
|
||||
void shouldReturnCorrectTargetCubekNodeHeuristik1()
|
||||
void shouldReturnCorrectTargetCubekNodeHeuristic1()
|
||||
{
|
||||
final int[][] state = {
|
||||
{3, 5, 0},
|
||||
{1, 2, 6},
|
||||
{4, 7, 8}
|
||||
{2, 0, 4},
|
||||
{6, 7, 1},
|
||||
{8, 5, 3}
|
||||
};
|
||||
final var root = new EightPuzzleNode(state);
|
||||
|
||||
@ -63,7 +63,7 @@ class AStarTest
|
||||
{
|
||||
for (var col = 0; col < value[row].length; col++)
|
||||
{
|
||||
if (value[row][col] != targetValue[row][col])
|
||||
if (value[row][col] != 0 && value[row][col] != targetValue[row][col])
|
||||
{
|
||||
counter++;
|
||||
}
|
||||
@ -75,6 +75,40 @@ class AStarTest
|
||||
|
||||
private int h2(final Node<int[][]> node, final Node<int[][]> target)
|
||||
{
|
||||
return 0;
|
||||
final var value = node.getValue();
|
||||
final var targetValue = target.getValue();
|
||||
var manhattanDistance = 0;
|
||||
|
||||
for (int i = 1; i <= 8; i++)
|
||||
{
|
||||
final var targetPos = detectPositionOf(i, targetValue);
|
||||
final var actualPos = detectPositionOf(i, value);
|
||||
|
||||
if (targetPos != null && actualPos != null)
|
||||
{
|
||||
final var xDistance = Math.abs(targetPos.getX() - actualPos.getX());
|
||||
final var yDistance = Math.abs(targetPos.getY() - actualPos.getY());
|
||||
|
||||
manhattanDistance += xDistance + yDistance;
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
return new IntPair(col, row);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user