Modified DFS to perform more optimal for output analysis.
This commit is contained in:
parent
48d2c51652
commit
66f2448b37
@ -9,6 +9,7 @@ public class BoardNode implements Comparable<BoardNode> {
|
||||
public int heuristic, cost, expected;
|
||||
public Board board;
|
||||
public List<BoardNode> children;
|
||||
public int depth = -1;
|
||||
|
||||
public BoardNode(Board board, BoardNode parent) {
|
||||
this.board = board;
|
||||
@ -19,6 +20,10 @@ public class BoardNode implements Comparable<BoardNode> {
|
||||
this.children = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void setDepth(int depth) {
|
||||
this.depth = depth;
|
||||
}
|
||||
|
||||
private int getActualCost() {
|
||||
if (this.parent != null) {
|
||||
return this.parent.cost + 10;
|
||||
|
@ -1,38 +1,42 @@
|
||||
package Assignments.A1.solving_algorithms;
|
||||
|
||||
import Assignments.A1.models.Board;
|
||||
import Assignments.A1.models.BoardGenerator;
|
||||
import Assignments.A1.models.Move;
|
||||
import Assignments.A1.models.Pair;
|
||||
import Assignments.A1.models.*;
|
||||
import Assignments.A1.resources.Parameters;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class DFS {
|
||||
public class DFS implements Solver {
|
||||
|
||||
private final Board solved = new Board();
|
||||
private final List<String> tried = new ArrayList<>();
|
||||
|
||||
public Board traverse(Board root, int depth) {
|
||||
Stack<Board> stack = new Stack<>();
|
||||
stack.push(root);
|
||||
public BoardNode traverse(Board root) {
|
||||
Stack<BoardNode> stack = new Stack<>();
|
||||
BoardNode rootNode = new BoardNode(root, null);
|
||||
rootNode.setDepth(0);
|
||||
stack.push(rootNode);
|
||||
|
||||
|
||||
while (!stack.isEmpty()) {
|
||||
Board current = stack.pop();
|
||||
if (current.equals(solved)) {
|
||||
BoardNode current = stack.pop();
|
||||
|
||||
if (current.board.equals(solved)) {
|
||||
return current;
|
||||
}
|
||||
|
||||
if (depth == Parameters.MAX_DEPTH || tried.contains(current.toString())) {
|
||||
if (current.depth > Parameters.MAX_DEPTH || tried.contains(current.toString())) {
|
||||
continue;
|
||||
}
|
||||
tried.add(current.toString());
|
||||
|
||||
List<Move> moves = current.getMoves();
|
||||
List<Move> moves = current.board.getMoves();
|
||||
for (Move next : moves) {
|
||||
Board child = next.getBoard();
|
||||
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;
|
||||
|
@ -19,6 +19,9 @@ public class BoardViewCodeBehind {
|
||||
@FXML
|
||||
private Button generate_board;
|
||||
|
||||
@FXML
|
||||
private Label disclaimer;
|
||||
|
||||
@FXML
|
||||
private MenuButton menu_alg;
|
||||
|
||||
@ -66,6 +69,7 @@ public class BoardViewCodeBehind {
|
||||
this.no_solv_alg_err.visibleProperty().bindBidirectional(viewModel.solvingAlgErrProperty());
|
||||
this.expanded.selectedProperty().bindBidirectional(viewModel.expandedProperty());
|
||||
this.alg_speed.textProperty().bindBidirectional(viewModel.algSpeedProperty());
|
||||
this.disclaimer.visibleProperty().bindBidirectional(viewModel.disclaimerProperty());
|
||||
}
|
||||
|
||||
public void onGenerateBoard(ActionEvent actionEvent) {
|
||||
|
@ -6,16 +6,18 @@ import Assignments.A1.models.BoardNode;
|
||||
import Assignments.A1.models.Solver;
|
||||
import Assignments.A1.solving_algorithms.AStar;
|
||||
import Assignments.A1.solving_algorithms.BFS;
|
||||
import Assignments.A1.solving_algorithms.DFS;
|
||||
import Assignments.A1.solving_algorithms.UCS;
|
||||
import javafx.beans.property.*;
|
||||
import javafx.scene.control.TreeItem;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Stack;
|
||||
|
||||
public class MainViewModel {
|
||||
|
||||
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 BoardNode current = new BoardNode(new Board(), null);
|
||||
@ -25,6 +27,7 @@ public class MainViewModel {
|
||||
public MainViewModel() {
|
||||
this.currentBoardProperty = new SimpleStringProperty();
|
||||
this.algSpeedProperty = new SimpleStringProperty();
|
||||
this.disclaimer = new SimpleBooleanProperty();
|
||||
this.expanded = new SimpleBooleanProperty();
|
||||
this.DFS = new SimpleBooleanProperty();
|
||||
this.UCS = new SimpleBooleanProperty();
|
||||
@ -47,6 +50,8 @@ public class MainViewModel {
|
||||
this.UCS.set(value.equals("UCS"));
|
||||
this.BFS.set(value.equals("BFS"));
|
||||
this.AStar.set(value.equals("AStar"));
|
||||
|
||||
this.disclaimer.setValue(value.equals("DFS"));
|
||||
}
|
||||
|
||||
public StringProperty getCurrentBoardProperty() {
|
||||
@ -72,7 +77,7 @@ public class MainViewModel {
|
||||
|
||||
Solver solver = null;
|
||||
if (DFS.getValue()) {
|
||||
// solver = new DFS();
|
||||
solver = new DFS();
|
||||
} else if (UCS.getValue()) {
|
||||
solver = new UCS();
|
||||
} else if (BFS.getValue()) {
|
||||
@ -93,6 +98,7 @@ public class MainViewModel {
|
||||
}
|
||||
this.solvedRootNode = rebuildTree(root, this.expanded.getValue());
|
||||
this.solvedRootBoardNode = root;
|
||||
this.current = solved;
|
||||
}
|
||||
|
||||
public void updateDisplay() {
|
||||
@ -130,14 +136,32 @@ public class MainViewModel {
|
||||
}
|
||||
|
||||
private TreeItem<BoardNode> rebuildTree(BoardNode root, boolean expanded) {
|
||||
TreeItem<BoardNode> treeItem = new TreeItem<>(root);
|
||||
for (BoardNode child : root.children) {
|
||||
TreeItem<BoardNode> childItem = rebuildTree(child, expanded);
|
||||
treeItem.getChildren().add(childItem);
|
||||
}
|
||||
treeItem.setExpanded(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);
|
||||
for (BoardNode child : root.children) {
|
||||
TreeItem<BoardNode> childItem = rebuildTree(child, expanded);
|
||||
treeItem.getChildren().add(childItem);
|
||||
}
|
||||
treeItem.setExpanded(expanded);
|
||||
|
||||
return treeItem;
|
||||
return treeItem;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -156,4 +180,12 @@ public class MainViewModel {
|
||||
public StringProperty algSpeedProperty() {
|
||||
return algSpeedProperty;
|
||||
}
|
||||
|
||||
public boolean isDisclaimer() {
|
||||
return disclaimer.get();
|
||||
}
|
||||
|
||||
public BooleanProperty disclaimerProperty() {
|
||||
return disclaimer;
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,6 @@
|
||||
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.control.Menu?>
|
||||
<?import javafx.scene.control.MenuBar?>
|
||||
<?import javafx.scene.control.MenuButton?>
|
||||
<?import javafx.scene.control.MenuItem?>
|
||||
<?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">
|
||||
<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" />
|
||||
<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" />
|
||||
@ -61,5 +40,6 @@
|
||||
<Font size="14.0" />
|
||||
</font>
|
||||
</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>
|
||||
</Pane>
|
||||
|
Loading…
Reference in New Issue
Block a user