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.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,41 +45,71 @@ 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;
|
||||||
for (int i = 0; i < timer.size(); i++) {
|
for (int i = 0; i < timer.size(); i++) {
|
||||||
total += timer.get(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();
|
long average = total / (long) timer.size();
|
||||||
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user