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.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -40,21 +41,12 @@ public class Driver {
|
|||||||
BFS solver = new BFS();
|
BFS solver = new BFS();
|
||||||
// UCS solver = new UCS();
|
// UCS solver = new UCS();
|
||||||
Date start = new Date();
|
Date start = new Date();
|
||||||
Board result = solver.traverse(board);
|
BoardNode result = solver.traverse(board);
|
||||||
Date end = new Date();
|
Date end = new Date();
|
||||||
if (result != null) {
|
if (result.board != null) {
|
||||||
long runtime = end.getTime() - start.getTime();
|
long runtime = end.getTime() - start.getTime();
|
||||||
timer.add(runtime);
|
timer.add(runtime);
|
||||||
successes++;
|
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;
|
long total = 0;
|
||||||
@ -65,6 +57,24 @@ public class Driver {
|
|||||||
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();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
package Assignments.A1.models;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class BoardNode implements Comparable<BoardNode> {
|
public class BoardNode implements Comparable<BoardNode> {
|
||||||
|
|
||||||
public BoardNode parent;
|
public BoardNode parent;
|
||||||
public int heuristic;
|
public int heuristic;
|
||||||
public int cost;
|
public int cost;
|
||||||
public Board board;
|
public Board board;
|
||||||
public List<String>
|
public List<BoardNode> children;
|
||||||
|
|
||||||
public BoardNode(Board board, BoardNode parent) {
|
public BoardNode(Board board, BoardNode parent) {
|
||||||
this.board = board;
|
this.board = board;
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
this.heuristic = this.getHeuristic();
|
this.heuristic = this.getHeuristic();
|
||||||
this.cost = this.getActualCost();
|
this.cost = this.getActualCost();
|
||||||
|
this.children = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getActualCost() {
|
private int getActualCost() {
|
||||||
@ -23,6 +27,10 @@ public class BoardNode implements Comparable<BoardNode> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addChild(BoardNode node) {
|
||||||
|
children.add(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private int getHeuristic() {
|
private int getHeuristic() {
|
||||||
int cost = 0;
|
int cost = 0;
|
||||||
|
@ -11,12 +11,12 @@ import java.util.PriorityQueue;
|
|||||||
|
|
||||||
public class BFS {
|
public class BFS {
|
||||||
|
|
||||||
private final Board solved = new Board();
|
|
||||||
private final HashSet<Board> visited = new HashSet<>();
|
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());
|
PriorityQueue<BoardNode> boards = new PriorityQueue<>(new BFSPriority());
|
||||||
|
HashSet<Board> visited = new HashSet<>();
|
||||||
boards.add(new BoardNode(root, null));
|
boards.add(new BoardNode(root, null));
|
||||||
BoardNode node = null;
|
BoardNode node = null;
|
||||||
Board current = new Board(root);
|
Board current = new Board(root);
|
||||||
@ -31,11 +31,12 @@ public class BFS {
|
|||||||
for (Move move : children) {
|
for (Move move : children) {
|
||||||
Board child = new Board(node.board);
|
Board child = new Board(node.board);
|
||||||
child.swap(move);
|
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;
|
||||||
return node.board;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user