From 66f2448b372aba218028859be166df397ca5dbac Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sun, 11 Feb 2024 16:45:30 -0500 Subject: [PATCH] Modified DFS to perform more optimal for output analysis. --- .../java/Assignments/A1/models/BoardNode.java | 5 ++ .../A1/solving_algorithms/DFS.java | 30 ++++++----- .../A1/view/BoardViewCodeBehind.java | 4 ++ .../Assignments/A1/view/MainViewModel.java | 50 +++++++++++++++---- src/main/resources/A1/view/BoardView.fxml | 22 +------- 5 files changed, 68 insertions(+), 43 deletions(-) diff --git a/src/main/java/Assignments/A1/models/BoardNode.java b/src/main/java/Assignments/A1/models/BoardNode.java index f979453..292d35b 100644 --- a/src/main/java/Assignments/A1/models/BoardNode.java +++ b/src/main/java/Assignments/A1/models/BoardNode.java @@ -9,6 +9,7 @@ public class BoardNode implements Comparable { public int heuristic, cost, expected; public Board board; public List children; + public int depth = -1; public BoardNode(Board board, BoardNode parent) { this.board = board; @@ -19,6 +20,10 @@ public class BoardNode implements Comparable { this.children = new ArrayList<>(); } + public void setDepth(int depth) { + this.depth = depth; + } + private int getActualCost() { if (this.parent != null) { return this.parent.cost + 10; diff --git a/src/main/java/Assignments/A1/solving_algorithms/DFS.java b/src/main/java/Assignments/A1/solving_algorithms/DFS.java index 9068ae6..66bd597 100644 --- a/src/main/java/Assignments/A1/solving_algorithms/DFS.java +++ b/src/main/java/Assignments/A1/solving_algorithms/DFS.java @@ -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 tried = new ArrayList<>(); - public Board traverse(Board root, int depth) { - Stack stack = new Stack<>(); - stack.push(root); + public BoardNode traverse(Board root) { + Stack 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 moves = current.getMoves(); + List 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; diff --git a/src/main/java/Assignments/A1/view/BoardViewCodeBehind.java b/src/main/java/Assignments/A1/view/BoardViewCodeBehind.java index 02a448b..0f2f418 100644 --- a/src/main/java/Assignments/A1/view/BoardViewCodeBehind.java +++ b/src/main/java/Assignments/A1/view/BoardViewCodeBehind.java @@ -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) { diff --git a/src/main/java/Assignments/A1/view/MainViewModel.java b/src/main/java/Assignments/A1/view/MainViewModel.java index 50d173f..e17c0a3 100644 --- a/src/main/java/Assignments/A1/view/MainViewModel.java +++ b/src/main/java/Assignments/A1/view/MainViewModel.java @@ -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 rebuildTree(BoardNode root, boolean expanded) { - TreeItem treeItem = new TreeItem<>(root); - for (BoardNode child : root.children) { - TreeItem childItem = rebuildTree(child, expanded); - treeItem.getChildren().add(childItem); - } - treeItem.setExpanded(expanded); + if (this.selectedAlg.equals("DFS")) { + Stack generations = new Stack<>(); + BoardNode temp = this.current; + while (temp != null) { + generations.push(temp); + temp = temp.parent; + } + TreeItem rootItem = new TreeItem<>(root); + TreeItem currItem = rootItem; + while (!generations.isEmpty()) { + TreeItem child = new TreeItem<>(generations.pop()); + currItem.getChildren().add(child); + currItem.setExpanded(expanded); + currItem = child; + } + return rootItem; + } else { + TreeItem treeItem = new TreeItem<>(root); + for (BoardNode child : root.children) { + TreeItem 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; + } } diff --git a/src/main/resources/A1/view/BoardView.fxml b/src/main/resources/A1/view/BoardView.fxml index 848d5cc..8b9a784 100644 --- a/src/main/resources/A1/view/BoardView.fxml +++ b/src/main/resources/A1/view/BoardView.fxml @@ -2,8 +2,6 @@ - - @@ -13,25 +11,6 @@ - - - - - - - - - - - - - - - - - - -