Added ability to add children and track children nodes for printout.
This commit is contained in:
parent
1b9af7a6ad
commit
cbd6f48667
@ -11,6 +11,7 @@ import Assignments.A1.solving_algorithms.UCS;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
@ -40,21 +41,12 @@ public class Driver {
|
||||
BFS solver = new BFS();
|
||||
// UCS solver = new UCS();
|
||||
Date start = new Date();
|
||||
Board result = solver.traverse(board);
|
||||
BoardNode result = solver.traverse(board);
|
||||
Date end = new Date();
|
||||
if (result != null) {
|
||||
if (result.board != null) {
|
||||
long runtime = end.getTime() - start.getTime();
|
||||
timer.add(runtime);
|
||||
successes++;
|
||||
String test = "END";
|
||||
System.out.println(solver.test.heuristic);
|
||||
BoardNode curr = solver.test;
|
||||
while (curr != null) {
|
||||
test = curr.board.toString() + " -> " + test;
|
||||
curr = curr.parent;
|
||||
}
|
||||
System.out.println(test);
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
long total = 0;
|
||||
@ -65,6 +57,24 @@ public class Driver {
|
||||
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();
|
||||
|
||||
}
|
||||
|
||||
|
||||
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() + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,18 +1,22 @@
|
||||
package Assignments.A1.models;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BoardNode implements Comparable<BoardNode> {
|
||||
|
||||
public BoardNode parent;
|
||||
public int heuristic;
|
||||
public int cost;
|
||||
public Board board;
|
||||
public List<String>
|
||||
public List<BoardNode> children;
|
||||
|
||||
public BoardNode(Board board, BoardNode parent) {
|
||||
this.board = board;
|
||||
this.parent = parent;
|
||||
this.heuristic = this.getHeuristic();
|
||||
this.cost = this.getActualCost();
|
||||
this.children = new ArrayList<>();
|
||||
}
|
||||
|
||||
private int getActualCost() {
|
||||
@ -23,6 +27,10 @@ public class BoardNode implements Comparable<BoardNode> {
|
||||
}
|
||||
}
|
||||
|
||||
public void addChild(BoardNode node) {
|
||||
children.add(node);
|
||||
}
|
||||
|
||||
|
||||
private int getHeuristic() {
|
||||
int cost = 0;
|
||||
|
@ -11,12 +11,12 @@ import java.util.PriorityQueue;
|
||||
|
||||
public class BFS {
|
||||
|
||||
private final Board solved = new Board();
|
||||
private final HashSet<Board> visited = new HashSet<>();
|
||||
public BoardNode test;
|
||||
private static final Board solved = new Board();
|
||||
|
||||
public Board traverse(Board root) {
|
||||
public BoardNode traverse(Board root) {
|
||||
PriorityQueue<BoardNode> boards = new PriorityQueue<>(new BFSPriority());
|
||||
HashSet<Board> visited = new HashSet<>();
|
||||
boards.add(new BoardNode(root, null));
|
||||
BoardNode node = null;
|
||||
Board current = new Board(root);
|
||||
@ -31,11 +31,12 @@ public class BFS {
|
||||
for (Move move : children) {
|
||||
Board child = new Board(node.board);
|
||||
child.swap(move);
|
||||
boards.add(new BoardNode(child, node));
|
||||
BoardNode childNode = new BoardNode(child, node);
|
||||
boards.add(childNode);
|
||||
node.addChild(childNode);
|
||||
}
|
||||
}
|
||||
test = node;
|
||||
return node.board;
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user