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.
import Assignments.A1.models.BoardGenerator;
import Assignments.A1.models.Board;
import Assignments.A1.models.Piece;
import Assignments.A1.models.BoardGenerator;
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,16 +24,29 @@ import java.util.ArrayList;
public class Driver {
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();
boolean result = solver.traverse(board);
int count = 0;
while (count != 100 || !result) {
result = solver.traverse(board);
System.out.println(count);
count++;
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++;
}
}
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() {
Board ideal = new Board();
Random random = new Random();
int numOfMoves = random.nextInt(30,50);
do {
int numOfMoves = random.nextInt(100, 150);
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;
}
}

View File

@ -43,68 +43,29 @@ public class DFS {
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;
public Board dfs(Board root, int depth) {
counter++;
Stack<Board> stack = new Stack<>();
stack.push(root);
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());
}
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);
}
}
List<Move> moves = current.getMoves();
for (Move next : moves) {
Board child = next.getBoard();
child.swap(next);
stack.push(child);
}
}
return null;
}
}