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<>();
Board board = BoardGenerator.generateBoard(); int successes = 0;
boolean result = solver.traverse(board); for (int run = 0; run < 100; run++) {
int count = 0; Board board = BoardGenerator.generateBoard();
while (count != 100 || !result) { DFS solver = new DFS();
result = solver.traverse(board); Date start = new Date();
System.out.println(count); Board result = solver.dfs(board,0);
count++; Date end = new Date();
board = BoardGenerator.generateBoard(); 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() { 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 {
for (int i = 0; i < numOfMoves; i++) { int numOfMoves = random.nextInt(100, 150);
List<Move> moves = ideal.getMoves(); for (int i = 0; i < numOfMoves; i++) {
Move randomMove = moves.get(random.nextInt(moves.size())); List<Move> moves = ideal.getMoves();
ideal.swap(randomMove); Move randomMove = moves.get(random.nextInt(moves.size()));
} 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(); while (!stack.isEmpty()) {
if (curr.board.equals(solved)) { Board current = stack.pop();
return true; if (current.equals(solved)) {
return current;
} }
List<Move> moves = curr.board.getMoves();
for (Move move : moves) { if (depth == Parameters.MAX_DEPTH || tried.contains(current.toString())) {
Board child = new Board(curr.board); continue;
child.swap(move); }
if (!curr.hasAncestor(child)) { tried.add(current.toString());
BoardNode childNode = new BoardNode(child, curr);
curr.addChild(childNode); List<Move> moves = current.getMoves();
lvr.add(childNode); for (Move next : moves) {
} Board child = next.getBoard();
child.swap(next);
stack.push(child);
} }
} }
return false; return null;
} }
}
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);
}
}
} }