Skip to content

Commit

Permalink
A* Search (8-puzzle Problem)
Browse files Browse the repository at this point in the history
  • Loading branch information
adnanhakim committed Oct 6, 2020
1 parent b92dd94 commit e412e8f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ A fun side project to perform AI algorithms using plain java code.
1. Simple Hill Climbing (Search Tree)
1. Steepest Hill Climbing (Search Tree)
1. Steepest Hill Climbing (N Queens)
1. A* Search (8-puzzle Problem)

## Developer

Expand Down
77 changes: 77 additions & 0 deletions src/AStarSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

public class AStarSearch {
private static boolean found = false;

private static void aStar(int[][] current, int[][] goal, int gScore) {
if (found) return;

for (int[] row : current)
System.out.println(Arrays.toString(row));
System.out.println();

if (calculateHeuristic(current, goal) == 0) {
System.out.println("G Score: " + gScore);
System.out.println("Reached goal state");
found = true;
return;
}

PriorityQueue<int[][]> priorityQueue = new PriorityQueue<>(Comparator.comparingInt(a -> calculateHeuristic(a, goal)));
int[] index = indexOfSpace(current);
int row = index[0], col = index[1];

// Move space above
if (row > 0)
priorityQueue.offer(copy(current, row, col,row - 1, col));

// Move space below
if (row < current.length - 1)
priorityQueue.offer(copy(current, row, col, row + 1, col));

// Move space to the left
if (col > 0)
priorityQueue.offer(copy(current, row, col, row, col - 1));

// Move space to the right
if (col < current[0].length - 1)
priorityQueue.offer(copy(current, row, col, row, col + 1));

while (!priorityQueue.isEmpty())
aStar(priorityQueue.poll(), goal, gScore + 1);
}

private static int calculateHeuristic(int[][] current, int[][] goal) {
int heuristic = 0;
for (int i = 0; i < current.length; i++)
for (int j = 0; j < current[0].length; j++)
if (current[i][j] != goal[i][j])
heuristic++;
return heuristic;
}

private static int[] indexOfSpace(int[][] current) {
for (int i = 0; i < current.length; i++)
for (int j = 0; j < current[0].length; j++)
if (current[i][j] == 0)
return new int[]{i, j};
return new int[]{0, 0};
}

private static int[][] copy(int[][] current, int currentRow, int currentCol, int swapRow, int swapCol) {
int[][] copy = new int[current.length][current[0].length];
for (int i = 0; i < current.length; i++)
System.arraycopy(current[i], 0, copy[i], 0, current[0].length);
copy[currentRow][currentCol] = current[swapRow][swapCol];
copy[swapRow][swapCol] = current[currentRow][currentCol];
return copy;
}

public static void main(String[] args) {
int[][] start = {{1, 2, 0}, {4, 5, 3}, {7, 8, 6}};
int[][] goal = {{1, 2, 3}, {4, 5, 6}, {7, 8, 0}};
aStar(start, goal, 0);
}
}

0 comments on commit e412e8f

Please sign in to comment.