Modified DFS to perform more optimal for output analysis.

This commit is contained in:
Jonathan Turner 2024-02-11 16:45:30 -05:00
parent 48d2c51652
commit 66f2448b37
5 changed files with 68 additions and 43 deletions

View File

@ -9,6 +9,7 @@ public class BoardNode implements Comparable<BoardNode> {
public int heuristic, cost, expected; public int heuristic, cost, expected;
public Board board; public Board board;
public List<BoardNode> children; public List<BoardNode> children;
public int depth = -1;
public BoardNode(Board board, BoardNode parent) { public BoardNode(Board board, BoardNode parent) {
this.board = board; this.board = board;
@ -19,6 +20,10 @@ public class BoardNode implements Comparable<BoardNode> {
this.children = new ArrayList<>(); this.children = new ArrayList<>();
} }
public void setDepth(int depth) {
this.depth = depth;
}
private int getActualCost() { private int getActualCost() {
if (this.parent != null) { if (this.parent != null) {
return this.parent.cost + 10; return this.parent.cost + 10;

View File

@ -1,38 +1,42 @@
package Assignments.A1.solving_algorithms; package Assignments.A1.solving_algorithms;
import Assignments.A1.models.Board; import Assignments.A1.models.*;
import Assignments.A1.models.BoardGenerator;
import Assignments.A1.models.Move;
import Assignments.A1.models.Pair;
import Assignments.A1.resources.Parameters; import Assignments.A1.resources.Parameters;
import java.util.*; import java.util.*;
public class DFS { public class DFS implements Solver {
private final Board solved = new Board(); private final Board solved = new Board();
private final List<String> tried = new ArrayList<>(); private final List<String> tried = new ArrayList<>();
public Board traverse(Board root, int depth) { public BoardNode traverse(Board root) {
Stack<Board> stack = new Stack<>(); Stack<BoardNode> stack = new Stack<>();
stack.push(root); BoardNode rootNode = new BoardNode(root, null);
rootNode.setDepth(0);
stack.push(rootNode);
while (!stack.isEmpty()) { while (!stack.isEmpty()) {
Board current = stack.pop(); BoardNode current = stack.pop();
if (current.equals(solved)) {
if (current.board.equals(solved)) {
return current; return current;
} }
if (depth == Parameters.MAX_DEPTH || tried.contains(current.toString())) { if (current.depth > Parameters.MAX_DEPTH || tried.contains(current.toString())) {
continue; continue;
} }
tried.add(current.toString()); tried.add(current.toString());
List<Move> moves = current.getMoves(); List<Move> moves = current.board.getMoves();
for (Move next : moves) { for (Move next : moves) {
Board child = next.getBoard(); Board child = next.getBoard();
child.swap(next); child.swap(next);
stack.push(child); BoardNode childNode = new BoardNode(child, current);
childNode.setDepth(current.depth+1);
stack.push(childNode);
current.addChild(childNode);
} }
} }
return null; return null;

View File

@ -19,6 +19,9 @@ public class BoardViewCodeBehind {
@FXML @FXML
private Button generate_board; private Button generate_board;
@FXML
private Label disclaimer;
@FXML @FXML
private MenuButton menu_alg; private MenuButton menu_alg;
@ -66,6 +69,7 @@ public class BoardViewCodeBehind {
this.no_solv_alg_err.visibleProperty().bindBidirectional(viewModel.solvingAlgErrProperty()); this.no_solv_alg_err.visibleProperty().bindBidirectional(viewModel.solvingAlgErrProperty());
this.expanded.selectedProperty().bindBidirectional(viewModel.expandedProperty()); this.expanded.selectedProperty().bindBidirectional(viewModel.expandedProperty());
this.alg_speed.textProperty().bindBidirectional(viewModel.algSpeedProperty()); this.alg_speed.textProperty().bindBidirectional(viewModel.algSpeedProperty());
this.disclaimer.visibleProperty().bindBidirectional(viewModel.disclaimerProperty());
} }
public void onGenerateBoard(ActionEvent actionEvent) { public void onGenerateBoard(ActionEvent actionEvent) {

View File

@ -6,16 +6,18 @@ import Assignments.A1.models.BoardNode;
import Assignments.A1.models.Solver; import Assignments.A1.models.Solver;
import Assignments.A1.solving_algorithms.AStar; import Assignments.A1.solving_algorithms.AStar;
import Assignments.A1.solving_algorithms.BFS; import Assignments.A1.solving_algorithms.BFS;
import Assignments.A1.solving_algorithms.DFS;
import Assignments.A1.solving_algorithms.UCS; import Assignments.A1.solving_algorithms.UCS;
import javafx.beans.property.*; import javafx.beans.property.*;
import javafx.scene.control.TreeItem; import javafx.scene.control.TreeItem;
import java.util.Date; import java.util.Date;
import java.util.Stack;
public class MainViewModel { public class MainViewModel {
private StringProperty currentBoardProperty, algSpeedProperty; private StringProperty currentBoardProperty, algSpeedProperty;
private BooleanProperty expanded, DFS, UCS, BFS, AStar, solvingAlgErr, genBoardErr; private BooleanProperty expanded, DFS, UCS, BFS, AStar, solvingAlgErr, genBoardErr, disclaimer;
private String selectedAlg; private String selectedAlg;
private BoardNode current = new BoardNode(new Board(), null); private BoardNode current = new BoardNode(new Board(), null);
@ -25,6 +27,7 @@ public class MainViewModel {
public MainViewModel() { public MainViewModel() {
this.currentBoardProperty = new SimpleStringProperty(); this.currentBoardProperty = new SimpleStringProperty();
this.algSpeedProperty = new SimpleStringProperty(); this.algSpeedProperty = new SimpleStringProperty();
this.disclaimer = new SimpleBooleanProperty();
this.expanded = new SimpleBooleanProperty(); this.expanded = new SimpleBooleanProperty();
this.DFS = new SimpleBooleanProperty(); this.DFS = new SimpleBooleanProperty();
this.UCS = new SimpleBooleanProperty(); this.UCS = new SimpleBooleanProperty();
@ -47,6 +50,8 @@ public class MainViewModel {
this.UCS.set(value.equals("UCS")); this.UCS.set(value.equals("UCS"));
this.BFS.set(value.equals("BFS")); this.BFS.set(value.equals("BFS"));
this.AStar.set(value.equals("AStar")); this.AStar.set(value.equals("AStar"));
this.disclaimer.setValue(value.equals("DFS"));
} }
public StringProperty getCurrentBoardProperty() { public StringProperty getCurrentBoardProperty() {
@ -72,7 +77,7 @@ public class MainViewModel {
Solver solver = null; Solver solver = null;
if (DFS.getValue()) { if (DFS.getValue()) {
// solver = new DFS(); solver = new DFS();
} else if (UCS.getValue()) { } else if (UCS.getValue()) {
solver = new UCS(); solver = new UCS();
} else if (BFS.getValue()) { } else if (BFS.getValue()) {
@ -93,6 +98,7 @@ public class MainViewModel {
} }
this.solvedRootNode = rebuildTree(root, this.expanded.getValue()); this.solvedRootNode = rebuildTree(root, this.expanded.getValue());
this.solvedRootBoardNode = root; this.solvedRootBoardNode = root;
this.current = solved;
} }
public void updateDisplay() { public void updateDisplay() {
@ -130,6 +136,23 @@ public class MainViewModel {
} }
private TreeItem<BoardNode> rebuildTree(BoardNode root, boolean expanded) { private TreeItem<BoardNode> rebuildTree(BoardNode root, boolean expanded) {
if (this.selectedAlg.equals("DFS")) {
Stack<BoardNode> generations = new Stack<>();
BoardNode temp = this.current;
while (temp != null) {
generations.push(temp);
temp = temp.parent;
}
TreeItem<BoardNode> rootItem = new TreeItem<>(root);
TreeItem<BoardNode> currItem = rootItem;
while (!generations.isEmpty()) {
TreeItem<BoardNode> child = new TreeItem<>(generations.pop());
currItem.getChildren().add(child);
currItem.setExpanded(expanded);
currItem = child;
}
return rootItem;
} else {
TreeItem<BoardNode> treeItem = new TreeItem<>(root); TreeItem<BoardNode> treeItem = new TreeItem<>(root);
for (BoardNode child : root.children) { for (BoardNode child : root.children) {
TreeItem<BoardNode> childItem = rebuildTree(child, expanded); TreeItem<BoardNode> childItem = rebuildTree(child, expanded);
@ -139,6 +162,7 @@ public class MainViewModel {
return treeItem; return treeItem;
} }
}
public boolean getExpanded() { public boolean getExpanded() {
@ -156,4 +180,12 @@ public class MainViewModel {
public StringProperty algSpeedProperty() { public StringProperty algSpeedProperty() {
return algSpeedProperty; return algSpeedProperty;
} }
public boolean isDisclaimer() {
return disclaimer.get();
}
public BooleanProperty disclaimerProperty() {
return disclaimer;
}
} }

View File

@ -2,8 +2,6 @@
<?import javafx.scene.control.Button?> <?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?> <?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuButton?> <?import javafx.scene.control.MenuButton?>
<?import javafx.scene.control.MenuItem?> <?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.ToggleButton?> <?import javafx.scene.control.ToggleButton?>
@ -13,25 +11,6 @@
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="591.0" prefWidth="928.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Assignments.A1.view.BoardViewCodeBehind"> <Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="591.0" prefWidth="928.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Assignments.A1.view.BoardViewCodeBehind">
<children> <children>
<MenuBar layoutY="2.0" prefHeight="25.0" prefWidth="134.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<TreeView fx:id="spanning_tree" layoutX="334.0" layoutY="67.0" prefHeight="416.0" prefWidth="532.0" /> <TreeView fx:id="spanning_tree" layoutX="334.0" layoutY="67.0" prefHeight="416.0" prefWidth="532.0" />
<ToggleButton fx:id="expanded" layoutX="547.0" layoutY="33.0" mnemonicParsing="false" onAction="#showSolvedPath" text="Expand Directory" /> <ToggleButton fx:id="expanded" layoutX="547.0" layoutY="33.0" mnemonicParsing="false" onAction="#showSolvedPath" text="Expand Directory" />
<Button id="generate_board" fx:id="generate_board" layoutX="97.0" layoutY="367.0" mnemonicParsing="false" onAction="#onGenerateBoard" text="Generate Random Board" /> <Button id="generate_board" fx:id="generate_board" layoutX="97.0" layoutY="367.0" mnemonicParsing="false" onAction="#onGenerateBoard" text="Generate Random Board" />
@ -61,5 +40,6 @@
<Font size="14.0" /> <Font size="14.0" />
</font> </font>
</Label> </Label>
<Label fx:id="disclaimer" layoutX="176.0" layoutY="541.0" prefHeight="42.0" prefWidth="558.0" text="**Note: If traversing with DFS, it only shows the solving directory to prevent JavaFX StackOverflow.** However, the traversal algorithm and algorithm speed does explore all nodes despite directory display." textAlignment="CENTER" wrapText="true" />
</children> </children>
</Pane> </Pane>