Still development of DFS, nonfunctional. Pushing to access on other device.

This commit is contained in:
Jonathan Turner 2024-02-07 01:01:13 -05:00
parent 41c04cacec
commit d8bea791fb
3 changed files with 54 additions and 78 deletions

View File

@ -2,13 +2,13 @@ package Assignments.A1;
// Potentially will be changed to an UI Implementation with JavaFX if time permits. // Potentially will be changed to an UI Implementation with JavaFX if time permits.
import Assignments.A1.models.BoardGenerator;
import Assignments.A1.models.Board; import Assignments.A1.models.Board;
import Assignments.A1.models.Piece; import Assignments.A1.models.BoardGenerator;
import Assignments.A1.solving_algorithms.DFS; import Assignments.A1.solving_algorithms.DFS;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
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.
@ -24,16 +24,29 @@ import java.util.ArrayList;
public class Driver { public class Driver {
public static void main(String[] args) { public static void main(String[] args) {
DFS solver = new DFS(); ArrayList<Long> timer = new ArrayList<>();
int successes = 0;
for (int run = 0; run < 100; run++) {
Board board = BoardGenerator.generateBoard(); Board board = BoardGenerator.generateBoard();
boolean result = solver.traverse(board); DFS solver = new DFS();
int count = 0; Date start = new Date();
while (count != 100 || !result) { Board result = solver.dfs(board,0);
result = solver.traverse(board); Date end = new Date();
System.out.println(count); if (result != null) {
count++; System.out.println("solved");
board = BoardGenerator.generateBoard(); long runtime = end.getTime() - start.getTime();
timer.add(runtime);
successes++;
} }
} }
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");
}
} }

View File

@ -21,12 +21,14 @@ public class BoardGenerator {
public static Board generateBoard() { public static Board generateBoard() {
Board ideal = new Board(); Board ideal = new Board();
Random random = new Random(); Random random = new Random();
int numOfMoves = random.nextInt(30,50); do {
int numOfMoves = random.nextInt(100, 150);
for (int i = 0; i < numOfMoves; i++) { for (int i = 0; i < numOfMoves; i++) {
List<Move> moves = ideal.getMoves(); List<Move> moves = ideal.getMoves();
Move randomMove = moves.get(random.nextInt(moves.size())); Move randomMove = moves.get(random.nextInt(moves.size()));
ideal.swap(randomMove); ideal.swap(randomMove);
} }
} while (ideal.equals(new Board()));
return ideal; return ideal;
} }
} }

View File

@ -43,68 +43,29 @@ public class DFS {
return null; return null;
} }
public boolean traverse(Board root) { public Board dfs(Board root, int depth) {
BoardNode current = new BoardNode(root); counter++;
Stack<BoardNode> lvr = new Stack<>(); Stack<Board> stack = new Stack<>();
lvr.push(current); stack.push(root);
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;
while (!stack.isEmpty()) {
Board current = stack.pop();
if (current.equals(solved)) {
return current;
} }
if (depth == Parameters.MAX_DEPTH || tried.contains(current.toString())) {
continue;
}
tried.add(current.toString());
List<Move> moves = current.getMoves();
for (Move next : moves) {
} Board child = next.getBoard();
child.swap(next);
class BoardNode { stack.push(child);
}
public Board board; }
public BoardNode parent; return null;
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);
}
}
} }