Added a function to print out traversal nodes. Modified Algorithms to return boardnode instead board.

This commit is contained in:
Jonathan Turner 2024-02-10 23:13:48 -05:00
parent cbd6f48667
commit 75c215e4ec
3 changed files with 62 additions and 37 deletions

View File

@ -6,26 +6,23 @@ import Assignments.A1.models.Board;
import Assignments.A1.models.BoardGenerator; import Assignments.A1.models.BoardGenerator;
import Assignments.A1.models.BoardNode; import Assignments.A1.models.BoardNode;
import Assignments.A1.solving_algorithms.BFS; import Assignments.A1.solving_algorithms.BFS;
import Assignments.A1.solving_algorithms.DFS;
import Assignments.A1.solving_algorithms.UCS;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.Iterator; import java.util.PriorityQueue;
import java.util.concurrent.TimeUnit;
/** /**
* Board will be used to save locations in a 2D array. * Board will be used to save locations in a 2D array.
* * <p>
* The design of the board locations will be numbered like so: * The design of the board locations will be numbered like so:
* 0 1 2 * 0 1 2
* 3 4 5 * 3 4 5
* 6 7 8 * 6 7 8
* * <p>
* 1 2 3 * 1 2 3
* 8 0 4 * 8 0 4
* 7 6 5 * 7 6 5
* * <p>
* The values will be specified with an int. Must be between (inclusive) 1-8. * The values will be specified with an int. Must be between (inclusive) 1-8.
* The empty spot will be represented with null. * The empty spot will be represented with null.
*/ */
@ -35,6 +32,7 @@ public class Driver {
ArrayList<Long> timer = new ArrayList<>(); ArrayList<Long> timer = new ArrayList<>();
int successes = 0; int successes = 0;
int runs = 1; int runs = 1;
BoardNode node = null;
for (int run = 0; run < runs; run++) { for (int run = 0; run < runs; run++) {
Board board = BoardGenerator.generateBoard(); Board board = BoardGenerator.generateBoard();
// DFS solver = new DFS(); // DFS solver = new DFS();
@ -47,6 +45,7 @@ public class Driver {
long runtime = end.getTime() - start.getTime(); long runtime = end.getTime() - start.getTime();
timer.add(runtime); timer.add(runtime);
successes++; successes++;
node = result;
} }
} }
long total = 0; long total = 0;
@ -58,30 +57,59 @@ public class Driver {
System.out.println("Average Runtime: " + average); System.out.println("Average Runtime: " + average);
System.out.println("Number of successful solves: " + successes + "/" + runs); System.out.println("Number of successful solves: " + successes + "/" + runs);
StringBuffer buffer = new StringBuffer(); String result = printTotalHierarchy(node);
System.out.println(result);
} }
private void print(StringBuilder buffer, BoardNode root) {
buffer.append(root.board.toString());
buffer.append("BFS");
buffer.append('\n');
for (Iterator<BoardNode> it = root.children.iterator(); it.hasNext();) {
BoardNode next = it.next();
if (it.hasNext()) {
buffer.append("├──").append(next.board.toString());
} else {
buffer.append(next.board.toString() + "\n└── ").append(next.board.toString() + " ");
}
}
}
// public static void main(String[] args) { // public static void main(String[] args) {
// int[] values = {1, 2, 3, 8, 0, 5, 4, 7, 6}; // int[] values = {1, 2, 3, 8, 0, 5, 4, 7, 6};
// BoardNode node = new BoardNode(new Board(values), null); // BoardNode node = new BoardNode(new Board(values), null);
// System.out.println(node.heuristic); // System.out.println(node.heuristic);
// } // }
private static String printTotalHierarchy(BoardNode root, int depth, boolean hasMoreChildren) {
String result = "";
for (int i = 0; i < depth - 1; i++) {
result += "";
}
if (hasMoreChildren) {
result += "├── " + root.board.toString() + System.lineSeparator();
} else {
result += "└── " + root.board.toString() + System.lineSeparator();
}
for (int i = 0; i < root.children.size(); i++) {
if (i == root.children.size() - 1) {
result += printTotalHierarchy(root.children.get(i), depth+1, false);
} else {
result += printTotalHierarchy(root.children.get(i), depth+1, true);
}
}
return result;
}
public static String printTotalHierarchy(BoardNode solved) {
BoardNode root = null;
while (solved.parent != null) {
solved = solved.parent;
}
root = solved;
String result = root.board.toString() + System.lineSeparator();
if (root.children.size() == 1) {
result += printTotalHierarchy(root.children.get(0), 1, false);
} else {
for (int i = 0; i < root.children.size(); i++) {
if (i == root.children.size() - 1) {
result += printTotalHierarchy(root.children.get(i), 1, false);
} else {
result += printTotalHierarchy(root.children.get(i), 1, true);
}
}
}
return result;
}
} }

View File

@ -13,7 +13,7 @@ public class AStar {
private final Board solved = new Board(); private final Board solved = new Board();
private final HashSet<Board> visited = new HashSet<>(); private final HashSet<Board> visited = new HashSet<>();
public Board traverse(Board root) { public BoardNode traverse(Board root) {
PriorityQueue<BoardNode> boards = new PriorityQueue<>(new UCSPriority()); PriorityQueue<BoardNode> boards = new PriorityQueue<>(new UCSPriority());
boards.add(new BoardNode(root, null)); boards.add(new BoardNode(root, null));
BoardNode node = null; BoardNode node = null;
@ -32,7 +32,7 @@ public class AStar {
boards.add(new BoardNode(child, node)); boards.add(new BoardNode(child, node));
} }
} }
return node.board; return node;
} }
} }

View File

@ -13,9 +13,7 @@ public class UCS {
private final Board solved = new Board(); private final Board solved = new Board();
private final HashSet<Board> visited = new HashSet<>(); private final HashSet<Board> visited = new HashSet<>();
public BoardNode test = null; public BoardNode traverse(Board root) {
public Board traverse(Board root) {
PriorityQueue<BoardNode> boards = new PriorityQueue<>(new UCSPriority()); PriorityQueue<BoardNode> boards = new PriorityQueue<>(new UCSPriority());
boards.add(new BoardNode(root, null)); boards.add(new BoardNode(root, null));
BoardNode node = null; BoardNode node = null;
@ -34,8 +32,7 @@ public class UCS {
boards.add(new BoardNode(child, node)); boards.add(new BoardNode(child, node));
} }
} }
test = node; return node;
return node.board;
} }
} }