Added a function to print out traversal nodes. Modified Algorithms to return boardnode instead board.
This commit is contained in:
parent
cbd6f48667
commit
75c215e4ec
@ -6,26 +6,23 @@ import Assignments.A1.models.Board;
|
||||
import Assignments.A1.models.BoardGenerator;
|
||||
import Assignments.A1.models.BoardNode;
|
||||
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.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.PriorityQueue;
|
||||
|
||||
/**
|
||||
* Board will be used to save locations in a 2D array.
|
||||
*
|
||||
* <p>
|
||||
* The design of the board locations will be numbered like so:
|
||||
* 0 1 2
|
||||
* 3 4 5
|
||||
* 6 7 8
|
||||
*
|
||||
* <p>
|
||||
* 1 2 3
|
||||
* 8 0 4
|
||||
* 7 6 5
|
||||
*
|
||||
* <p>
|
||||
* The values will be specified with an int. Must be between (inclusive) 1-8.
|
||||
* The empty spot will be represented with null.
|
||||
*/
|
||||
@ -35,6 +32,7 @@ public class Driver {
|
||||
ArrayList<Long> timer = new ArrayList<>();
|
||||
int successes = 0;
|
||||
int runs = 1;
|
||||
BoardNode node = null;
|
||||
for (int run = 0; run < runs; run++) {
|
||||
Board board = BoardGenerator.generateBoard();
|
||||
// DFS solver = new DFS();
|
||||
@ -47,41 +45,71 @@ public class Driver {
|
||||
long runtime = end.getTime() - start.getTime();
|
||||
timer.add(runtime);
|
||||
successes++;
|
||||
node = result;
|
||||
}
|
||||
}
|
||||
long total = 0;
|
||||
for (int i = 0; i < timer.size(); i++) {
|
||||
total += timer.get(i);
|
||||
System.out.println("Run " + (i+1) + ": " + timer.get(i));
|
||||
System.out.println("Run " + (i + 1) + ": " + timer.get(i));
|
||||
}
|
||||
long average = total / (long) timer.size();
|
||||
System.out.println("Average Runtime: " + average);
|
||||
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) {
|
||||
// int[] values = {1, 2, 3, 8, 0, 5, 4, 7, 6};
|
||||
// BoardNode node = new BoardNode(new Board(values), null);
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ public class AStar {
|
||||
private final Board solved = new Board();
|
||||
private final HashSet<Board> visited = new HashSet<>();
|
||||
|
||||
public Board traverse(Board root) {
|
||||
public BoardNode traverse(Board root) {
|
||||
PriorityQueue<BoardNode> boards = new PriorityQueue<>(new UCSPriority());
|
||||
boards.add(new BoardNode(root, null));
|
||||
BoardNode node = null;
|
||||
@ -32,7 +32,7 @@ public class AStar {
|
||||
boards.add(new BoardNode(child, node));
|
||||
}
|
||||
}
|
||||
return node.board;
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -13,9 +13,7 @@ public class UCS {
|
||||
private final Board solved = new Board();
|
||||
private final HashSet<Board> visited = new HashSet<>();
|
||||
|
||||
public BoardNode test = null;
|
||||
|
||||
public Board traverse(Board root) {
|
||||
public BoardNode traverse(Board root) {
|
||||
PriorityQueue<BoardNode> boards = new PriorityQueue<>(new UCSPriority());
|
||||
boards.add(new BoardNode(root, null));
|
||||
BoardNode node = null;
|
||||
@ -34,8 +32,7 @@ public class UCS {
|
||||
boards.add(new BoardNode(child, node));
|
||||
}
|
||||
}
|
||||
test = node;
|
||||
return node.board;
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user