Compare commits
No commits in common. "dbfe4c6fe8b25a73d5f4cf170bce5e5dc86bb2c9" and "41c04cacecce38cf1b350b66ccfc34150a52b4f8" have entirely different histories.
dbfe4c6fe8
...
41c04cacec
@ -2,13 +2,13 @@ package Assignments.A1;
|
||||
|
||||
// Potentially will be changed to an UI Implementation with JavaFX if time permits.
|
||||
|
||||
import Assignments.A1.models.Board;
|
||||
|
||||
import Assignments.A1.models.BoardGenerator;
|
||||
import Assignments.A1.models.Board;
|
||||
import Assignments.A1.models.Piece;
|
||||
import Assignments.A1.solving_algorithms.DFS;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Board will be used to save locations in a 2D array.
|
||||
@ -24,29 +24,16 @@ import java.util.concurrent.TimeUnit;
|
||||
public class Driver {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ArrayList<Long> timer = new ArrayList<>();
|
||||
int successes = 0;
|
||||
for (int run = 0; run < 100; run++) {
|
||||
Board board = BoardGenerator.generateBoard();
|
||||
DFS solver = new DFS();
|
||||
Date start = new Date();
|
||||
Board result = solver.dfs(board,0);
|
||||
Date end = new Date();
|
||||
if (result != null) {
|
||||
System.out.println("solved");
|
||||
long runtime = end.getTime() - start.getTime();
|
||||
timer.add(runtime);
|
||||
successes++;
|
||||
Board board = BoardGenerator.generateBoard();
|
||||
boolean result = solver.traverse(board);
|
||||
int count = 0;
|
||||
while (count != 100 || !result) {
|
||||
result = solver.traverse(board);
|
||||
System.out.println(count);
|
||||
count++;
|
||||
board = BoardGenerator.generateBoard();
|
||||
}
|
||||
}
|
||||
long total = 0;
|
||||
for (int i = 0; i < timer.size(); i++) {
|
||||
total += 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 + "/100");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -21,14 +21,12 @@ public class BoardGenerator {
|
||||
public static Board generateBoard() {
|
||||
Board ideal = new Board();
|
||||
Random random = new Random();
|
||||
do {
|
||||
int numOfMoves = random.nextInt(100, 150);
|
||||
int numOfMoves = random.nextInt(30,50);
|
||||
for (int i = 0; i < numOfMoves; i++) {
|
||||
List<Move> moves = ideal.getMoves();
|
||||
Move randomMove = moves.get(random.nextInt(moves.size()));
|
||||
ideal.swap(randomMove);
|
||||
}
|
||||
} while (ideal.equals(new Board()));
|
||||
return ideal;
|
||||
}
|
||||
}
|
||||
|
@ -1,103 +0,0 @@
|
||||
Run from #d8bea791: (Time in Milliseconds)
|
||||
Run 1: 5677
|
||||
Run 2: 1012
|
||||
Run 3: 17058
|
||||
Run 4: 11860
|
||||
Run 5: 45404
|
||||
Run 6: 10
|
||||
Run 7: 10250
|
||||
Run 8: 29170
|
||||
Run 9: 26660
|
||||
Run 10: 22485
|
||||
Run 11: 5372
|
||||
Run 12: 28510
|
||||
Run 13: 1269
|
||||
Run 14: 37239
|
||||
Run 15: 3
|
||||
Run 16: 36978
|
||||
Run 17: 38774
|
||||
Run 18: 42041
|
||||
Run 19: 662
|
||||
Run 20: 2951
|
||||
Run 21: 11334
|
||||
Run 22: 1581
|
||||
Run 23: 1738
|
||||
Run 24: 2604
|
||||
Run 25: 17161
|
||||
Run 26: 5572
|
||||
Run 27: 14
|
||||
Run 28: 8316
|
||||
Run 29: 25494
|
||||
Run 30: 7669
|
||||
Run 31: 43046
|
||||
Run 32: 20427
|
||||
Run 33: 16146
|
||||
Run 34: 30095
|
||||
Run 35: 11381
|
||||
Run 36: 0
|
||||
Run 37: 1366
|
||||
Run 38: 10928
|
||||
Run 39: 529
|
||||
Run 40: 10643
|
||||
Run 41: 19734
|
||||
Run 42: 3631
|
||||
Run 43: 17513
|
||||
Run 44: 3892
|
||||
Run 45: 21224
|
||||
Run 46: 105
|
||||
Run 47: 30568
|
||||
Run 48: 5313
|
||||
Run 49: 6464
|
||||
Run 50: 21124
|
||||
Run 51: 31758
|
||||
Run 52: 1619
|
||||
Run 53: 35134
|
||||
Run 54: 20679
|
||||
Run 55: 98
|
||||
Run 56: 2760
|
||||
Run 57: 13905
|
||||
Run 58: 15965
|
||||
Run 59: 39522
|
||||
Run 60: 2
|
||||
Run 61: 2293
|
||||
Run 62: 41910
|
||||
Run 63: 17853
|
||||
Run 64: 2950
|
||||
Run 65: 56304
|
||||
Run 66: 45523
|
||||
Run 67: 17346
|
||||
Run 68: 10577
|
||||
Run 69: 703
|
||||
Run 70: 324
|
||||
Run 71: 1081
|
||||
Run 72: 3846
|
||||
Run 73: 13315
|
||||
Run 74: 28206
|
||||
Run 75: 47831
|
||||
Run 76: 7488
|
||||
Run 77: 12499
|
||||
Run 78: 17101
|
||||
Run 79: 7968
|
||||
Run 80: 8936
|
||||
Run 81: 1494
|
||||
Run 82: 0
|
||||
Run 83: 19235
|
||||
Run 84: 15755
|
||||
Run 85: 4979
|
||||
Run 86: 22015
|
||||
Run 87: 11589
|
||||
Run 88: 27377
|
||||
Run 89: 16111
|
||||
Run 90: 25775
|
||||
Run 91: 7022
|
||||
Run 92: 27261
|
||||
Run 93: 6351
|
||||
Run 94: 26618
|
||||
Run 95: 4594
|
||||
Run 96: 13039
|
||||
Run 97: 23729
|
||||
Run 98: 16315
|
||||
Run 99: 35539
|
||||
Run 100: 2126
|
||||
Average Runtime: 15314
|
||||
Number of successful solves: 100/100
|
@ -15,58 +15,96 @@ public class DFS {
|
||||
private final Board solved = new Board();
|
||||
private List<String> tried = new ArrayList<>();
|
||||
|
||||
/* Commented out for future reference. */
|
||||
// public Board dfs(Board root, int depth, ArrayList<String> visited) {
|
||||
// counter++;
|
||||
// if (root.equals(solved)) {
|
||||
// return root;
|
||||
// }
|
||||
//
|
||||
// ArrayList<String> directParents = new ArrayList<>(visited);
|
||||
// if (depth == Parameters.MAX_DEPTH || visited.contains(root.toString()) || tried.contains(root.toString())) {
|
||||
// return null;
|
||||
// }
|
||||
// directParents.add(root.toString());
|
||||
// tried.add(root.toString());
|
||||
//
|
||||
// List<Move> moves = root.getMoves();
|
||||
// int moveNum = 1;
|
||||
// for (Move next : moves) {
|
||||
//
|
||||
// Board child = next.getBoard();
|
||||
// child.swap(next);
|
||||
// moveNum++;
|
||||
// Board board = dfs(child, depth+1, directParents);
|
||||
// if (board != null) {
|
||||
// return board;
|
||||
// }
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
|
||||
public Board dfs(Board root, int depth) {
|
||||
public Board dfs(Board root, int depth, ArrayList<String> visited) {
|
||||
counter++;
|
||||
Stack<Board> stack = new Stack<>();
|
||||
stack.push(root);
|
||||
|
||||
while (!stack.isEmpty()) {
|
||||
Board current = stack.pop();
|
||||
if (current.equals(solved)) {
|
||||
return current;
|
||||
if (root.equals(solved)) {
|
||||
return root;
|
||||
}
|
||||
|
||||
if (depth == Parameters.MAX_DEPTH || tried.contains(current.toString())) {
|
||||
continue;
|
||||
ArrayList<String> directParents = new ArrayList<>(visited);
|
||||
if (depth == Parameters.MAX_DEPTH || visited.contains(root.toString()) || tried.contains(root.toString())) {
|
||||
return null;
|
||||
}
|
||||
tried.add(current.toString());
|
||||
directParents.add(root.toString());
|
||||
tried.add(root.toString());
|
||||
|
||||
List<Move> moves = current.getMoves();
|
||||
List<Move> moves = root.getMoves();
|
||||
int moveNum = 1;
|
||||
for (Move next : moves) {
|
||||
|
||||
Board child = next.getBoard();
|
||||
child.swap(next);
|
||||
stack.push(child);
|
||||
moveNum++;
|
||||
Board board = dfs(child, depth+1, directParents);
|
||||
if (board != null) {
|
||||
return board;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean traverse(Board root) {
|
||||
BoardNode current = new BoardNode(root);
|
||||
Stack<BoardNode> lvr = new Stack<>();
|
||||
lvr.push(current);
|
||||
while (!lvr.isEmpty()) {
|
||||
BoardNode curr = lvr.pop();
|
||||
if (curr.board.equals(solved)) {
|
||||
return true;
|
||||
}
|
||||
List<Move> moves = curr.board.getMoves();
|
||||
for (Move move : moves) {
|
||||
Board child = new Board(curr.board);
|
||||
child.swap(move);
|
||||
if (!curr.hasAncestor(child)) {
|
||||
BoardNode childNode = new BoardNode(child, curr);
|
||||
curr.addChild(childNode);
|
||||
lvr.add(childNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
class BoardNode {
|
||||
|
||||
public Board board;
|
||||
public BoardNode parent;
|
||||
public List<BoardNode> children;
|
||||
|
||||
public BoardNode(Board child, BoardNode parent) {
|
||||
this.board = child;
|
||||
this.parent = parent;
|
||||
this.children = new ArrayList<>();
|
||||
}
|
||||
|
||||
public BoardNode (Board child) {
|
||||
this.board = child;
|
||||
this.parent = null;
|
||||
this.children = new ArrayList<>();
|
||||
}
|
||||
|
||||
public boolean hasAncestor(Board board) {
|
||||
BoardNode current = this;
|
||||
while (current.parent != null) {
|
||||
current = current.parent;
|
||||
if (current.board.equals(board)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void addChild(BoardNode child) {
|
||||
if (child != null) {
|
||||
children.add(child);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user