Compare commits

..

No commits in common. "261e49b329a31999df8c3a35512447f3a447caaa" and "db5565c1ae31f16717610c8518d492da44fd9036" have entirely different histories.

4 changed files with 5 additions and 196 deletions

View File

@ -4,7 +4,6 @@ package Assignments.A1;
import Assignments.A1.models.Board; import Assignments.A1.models.Board;
import Assignments.A1.models.BoardGenerator; import Assignments.A1.models.BoardGenerator;
import Assignments.A1.solving_algorithms.BFS;
import Assignments.A1.solving_algorithms.DFS; import Assignments.A1.solving_algorithms.DFS;
import java.util.ArrayList; import java.util.ArrayList;
@ -29,12 +28,12 @@ public class Driver {
int successes = 0; int successes = 0;
for (int run = 0; run < 100; run++) { for (int run = 0; run < 100; run++) {
Board board = BoardGenerator.generateBoard(); Board board = BoardGenerator.generateBoard();
// DFS solver = new DFS(); DFS solver = new DFS();
BFS solver = new BFS();
Date start = new Date(); Date start = new Date();
Board result = solver.traverse(board); Board result = solver.dfs(board,0);
Date end = new Date(); Date end = new Date();
if (result != null) { if (result != null) {
System.out.println("solved");
long runtime = end.getTime() - start.getTime(); long runtime = end.getTime() - start.getTime();
timer.add(runtime); timer.add(runtime);
successes++; successes++;

View File

@ -1,103 +0,0 @@
Run from #e13806a1: (Time in Milliseconds)
Run 1: 279
Run 2: 280
Run 3: 159
Run 4: 178
Run 5: 165
Run 6: 249
Run 7: 229
Run 8: 138
Run 9: 147
Run 10: 135
Run 11: 151
Run 12: 130
Run 13: 140
Run 14: 132
Run 15: 142
Run 16: 131
Run 17: 146
Run 18: 139
Run 19: 145
Run 20: 143
Run 21: 150
Run 22: 154
Run 23: 146
Run 24: 148
Run 25: 144
Run 26: 142
Run 27: 148
Run 28: 152
Run 29: 128
Run 30: 140
Run 31: 130
Run 32: 160
Run 33: 131
Run 34: 143
Run 35: 134
Run 36: 147
Run 37: 195
Run 38: 204
Run 39: 142
Run 40: 151
Run 41: 149
Run 42: 133
Run 43: 143
Run 44: 148
Run 45: 146
Run 46: 143
Run 47: 129
Run 48: 143
Run 49: 151
Run 50: 141
Run 51: 144
Run 52: 133
Run 53: 145
Run 54: 140
Run 55: 140
Run 56: 147
Run 57: 131
Run 58: 145
Run 59: 139
Run 60: 146
Run 61: 147
Run 62: 126
Run 63: 151
Run 64: 131
Run 65: 141
Run 66: 146
Run 67: 133
Run 68: 143
Run 69: 130
Run 70: 149
Run 71: 145
Run 72: 140
Run 73: 145
Run 74: 130
Run 75: 140
Run 76: 159
Run 77: 148
Run 78: 150
Run 79: 127
Run 80: 146
Run 81: 144
Run 82: 140
Run 83: 140
Run 84: 129
Run 85: 145
Run 86: 137
Run 87: 145
Run 88: 144
Run 89: 127
Run 90: 140
Run 91: 131
Run 92: 153
Run 93: 137
Run 94: 133
Run 95: 144
Run 96: 128
Run 97: 149
Run 98: 150
Run 99: 145
Run 100: 144
Average Runtime: 148
Number of successful solves: 100/100

View File

@ -1,94 +1,7 @@
package Assignments.A1.solving_algorithms; package Assignments.A1.solving_algorithms;
import Assignments.A1.models.Board;
import Assignments.A1.models.Move;
import java.util.HashSet;
import java.util.List;
import java.util.PriorityQueue;
public class BFS { public class BFS {
private final Board solved = new Board();
private final HashSet<Board> visited = new HashSet<>();
public Board traverse(Board root) { /* Checking out new Branch for BFS Development to prevent harm to DFS existing algorithm. */
PriorityQueue<BoardNode> boards = new PriorityQueue<>();
boards.add(new BoardNode(root));
BoardNode node = null;
while (!boards.isEmpty()) {
node = boards.poll();
if (visited.contains(node.board)) {
continue;
}
visited.add(node.board);
List<Move> children = node.board.getMoves();
for (Move move : children) {
Board child = new Board(node.board);
child.swap(move);
boards.add(new BoardNode(child));
}
}
return node.board;
}
}
class BoardNode implements Comparable<BoardNode> {
public int heuristic;
public Board board;
public BoardNode(Board board) {
this.heuristic = this.getCost(board);
this.board = board;
}
private int getCost(Board board) {
int cost = 0;
for (int i = 0; i < 9; i++) {
int currValue = board.getPiece(i);
cost += Math.abs(i - this.idealIndex(i));
}
return cost;
}
private int idealIndex(int value) {
int result = -1;
switch(value) {
case 0:
result = 4;
break;
case 1:
result = 0;
break;
case 2:
result = 1;
break;
case 3:
result = 2;
break;
case 4:
result = 5;
break;
case 5:
result = 8;
break;
case 6:
result = 7;
break;
case 7:
result = 6;
break;
case 8:
result = 3;
break;
}
return result;
}
@Override
public int compareTo(BoardNode o) {
return Integer.compare(this.heuristic, o.heuristic);
}
} }