Finished designing and refractering code for GUI.
This commit is contained in:
parent
76888ebec7
commit
2da192b76d
@ -5,20 +5,17 @@ package Assignments.A1;
|
|||||||
import Assignments.A1.models.Board;
|
import Assignments.A1.models.Board;
|
||||||
import Assignments.A1.models.BoardGenerator;
|
import Assignments.A1.models.BoardGenerator;
|
||||||
import Assignments.A1.models.BoardNode;
|
import Assignments.A1.models.BoardNode;
|
||||||
import Assignments.A1.solving_algorithms.AStar;
|
import Assignments.A1.solving_algorithms.DFS;
|
||||||
import Assignments.A1.solving_algorithms.BFS;
|
import Assignments.A1.solving_algorithms.comparators.AStar;
|
||||||
import Assignments.A1.solving_algorithms.UCS;
|
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.PriorityQueue;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Board will be used to save locations in a 2D array.
|
* Board will be used to save locations in a 2D array.
|
||||||
@ -44,10 +41,10 @@ public class Driver {
|
|||||||
BoardNode node = null;
|
BoardNode node = null;
|
||||||
for (int run = 0; run < runs; run++) {
|
for (int run = 0; run < runs; run++) {
|
||||||
Board board = BoardGenerator.generateBoard();
|
Board board = BoardGenerator.generateBoard();
|
||||||
// DFS solver = new DFS();
|
DFS solver = new DFS();
|
||||||
// BFS solver = new BFS();
|
// BFS solver = new BFS();
|
||||||
// UCS solver = new UCS();
|
// UCS solver = new UCS();
|
||||||
AStar solver = new AStar();
|
// AStar solver = new AStar();
|
||||||
Date start = new Date();
|
Date start = new Date();
|
||||||
BoardNode result = solver.traverse(board);
|
BoardNode result = solver.traverse(board);
|
||||||
Date end = new Date();
|
Date end = new Date();
|
||||||
@ -125,7 +122,7 @@ public class Driver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void writeToFile(String name, StringBuffer values) {
|
private static void writeToFile(String name, StringBuffer values) {
|
||||||
URL resourcePath = Driver.class.getResource("/A1/results/AStar.txt");
|
URL resourcePath = Driver.class.getResource("/A1/results/" + name + ".txt");
|
||||||
URI resourceURI = URI.create(resourcePath.toString());
|
URI resourceURI = URI.create(resourcePath.toString());
|
||||||
File resource = new File(resourceURI.getPath());
|
File resource = new File(resourceURI.getPath());
|
||||||
|
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
package Assignments.A1.models;
|
package Assignments.A1.models;
|
||||||
|
|
||||||
|
import Assignments.A1.models.helper.Move;
|
||||||
|
import Assignments.A1.models.helper.Pair;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -236,17 +239,6 @@ public class Board {
|
|||||||
* Specifies a way of printing out the board with its current state.
|
* Specifies a way of printing out the board with its current state.
|
||||||
* @return the current state formatted.
|
* @return the current state formatted.
|
||||||
*/
|
*/
|
||||||
// @Override
|
|
||||||
// public String toString() {
|
|
||||||
// String result = "";
|
|
||||||
// for (int i = 0; i < 9; i++) {
|
|
||||||
// result += pieces[i] + " ";
|
|
||||||
// if ((i+1) % 3 == 0) {
|
|
||||||
// result += "\n";
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return result;
|
|
||||||
// }
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String result = pieces[0] + " " + pieces[1] + " " + pieces[2] + "\n"
|
String result = pieces[0] + " " + pieces[1] + " " + pieces[2] + "\n"
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package Assignments.A1.models;
|
package Assignments.A1.models;
|
||||||
|
|
||||||
|
import Assignments.A1.models.helper.Move;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -36,7 +36,6 @@ public class BoardNode implements Comparable<BoardNode> {
|
|||||||
children.add(node);
|
children.add(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private int getHeuristic() {
|
private int getHeuristic() {
|
||||||
int cost = 0;
|
int cost = 0;
|
||||||
for (int i = 0; i < 9; i++) {
|
for (int i = 0; i < 9; i++) {
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
package Assignments.A1.models;
|
package Assignments.A1.models.helper;
|
||||||
|
|
||||||
|
import Assignments.A1.models.Board;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to keep track of moves in either a stack or queues.
|
* Used to keep track of moves in either a stack or queues.
|
@ -1,4 +1,4 @@
|
|||||||
package Assignments.A1.models;
|
package Assignments.A1.models.helper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to pair two points for moves.
|
* Used to pair two points for moves.
|
@ -1,4 +1,6 @@
|
|||||||
package Assignments.A1.models;
|
package Assignments.A1.models.helper;
|
||||||
|
|
||||||
|
import Assignments.A1.models.Board;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class allows for abstraction and proper representation of a piece of the 8 puzzle.
|
* This class allows for abstraction and proper representation of a piece of the 8 puzzle.
|
@ -1,9 +1,11 @@
|
|||||||
package Assignments.A1.models;
|
package Assignments.A1.models.helper;
|
||||||
|
|
||||||
import Assignments.A1.models.Board;
|
import Assignments.A1.models.Board;
|
||||||
import Assignments.A1.models.BoardNode;
|
import Assignments.A1.models.BoardNode;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
public interface Solver {
|
public interface Solver {
|
||||||
|
|
||||||
BoardNode traverse(Board board);
|
BoardNode traverse(Board root);
|
||||||
}
|
}
|
@ -1,50 +0,0 @@
|
|||||||
package Assignments.A1.solving_algorithms;
|
|
||||||
|
|
||||||
import Assignments.A1.models.Board;
|
|
||||||
import Assignments.A1.models.BoardNode;
|
|
||||||
import Assignments.A1.models.Move;
|
|
||||||
import Assignments.A1.models.Solver;
|
|
||||||
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.PriorityQueue;
|
|
||||||
|
|
||||||
public class BFS implements Solver {
|
|
||||||
|
|
||||||
private final HashSet<Board> visited = new HashSet<>();
|
|
||||||
private static final Board solved = new Board();
|
|
||||||
|
|
||||||
public BoardNode traverse(Board root) {
|
|
||||||
PriorityQueue<BoardNode> boards = new PriorityQueue<>(new BFSPriority());
|
|
||||||
HashSet<Board> visited = new HashSet<>();
|
|
||||||
boards.add(new BoardNode(root, null));
|
|
||||||
BoardNode node = null;
|
|
||||||
Board current = new Board(root);
|
|
||||||
while (!current.equals(solved)) {
|
|
||||||
node = boards.poll();
|
|
||||||
current = node.board;
|
|
||||||
if (visited.contains(node.board)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
visited.add(node.board);
|
|
||||||
List<Move> children = node.board.getMoves();
|
|
||||||
for (Move move : children) {
|
|
||||||
Board child = new Board(node.board);
|
|
||||||
child.swap(move);
|
|
||||||
BoardNode childNode = new BoardNode(child, node);
|
|
||||||
boards.add(childNode);
|
|
||||||
node.addChild(childNode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class BFSPriority implements Comparator<BoardNode> {
|
|
||||||
@Override
|
|
||||||
public int compare(BoardNode o1, BoardNode o2) {
|
|
||||||
return Integer.compare(o1.heuristic, o2.heuristic);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
|||||||
package Assignments.A1.solving_algorithms;
|
package Assignments.A1.solving_algorithms;
|
||||||
|
|
||||||
import Assignments.A1.models.*;
|
import Assignments.A1.models.*;
|
||||||
|
import Assignments.A1.models.helper.Move;
|
||||||
|
import Assignments.A1.models.helper.Solver;
|
||||||
import Assignments.A1.resources.Parameters;
|
import Assignments.A1.resources.Parameters;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@ -16,7 +18,6 @@ public class DFS implements Solver {
|
|||||||
rootNode.setDepth(0);
|
rootNode.setDepth(0);
|
||||||
stack.push(rootNode);
|
stack.push(rootNode);
|
||||||
|
|
||||||
|
|
||||||
while (!stack.isEmpty()) {
|
while (!stack.isEmpty()) {
|
||||||
BoardNode current = stack.pop();
|
BoardNode current = stack.pop();
|
||||||
|
|
||||||
|
@ -2,24 +2,31 @@ package Assignments.A1.solving_algorithms;
|
|||||||
|
|
||||||
import Assignments.A1.models.Board;
|
import Assignments.A1.models.Board;
|
||||||
import Assignments.A1.models.BoardNode;
|
import Assignments.A1.models.BoardNode;
|
||||||
import Assignments.A1.models.Move;
|
import Assignments.A1.models.helper.Move;
|
||||||
import Assignments.A1.models.Solver;
|
import Assignments.A1.models.helper.Solver;
|
||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.PriorityQueue;
|
import java.util.PriorityQueue;
|
||||||
|
|
||||||
public class AStar implements Solver {
|
public class PriorityTraversal implements Solver {
|
||||||
|
|
||||||
private final Board solved = new Board();
|
private final Board solved = new Board();
|
||||||
private final HashSet<Board> visited = new HashSet<>();
|
private final HashSet<Board> visited = new HashSet<>();
|
||||||
|
|
||||||
|
private final Comparator<BoardNode> comparator;
|
||||||
|
|
||||||
|
public PriorityTraversal(Comparator<BoardNode> type) {
|
||||||
|
this.comparator = type;
|
||||||
|
}
|
||||||
|
|
||||||
public BoardNode traverse(Board root) {
|
public BoardNode traverse(Board root) {
|
||||||
PriorityQueue<BoardNode> boards = new PriorityQueue<>(new AStarPriority());
|
PriorityQueue<BoardNode> boards = new PriorityQueue<>(comparator);
|
||||||
boards.add(new BoardNode(root, null));
|
boards.add(new BoardNode(root, null));
|
||||||
BoardNode node = null;
|
BoardNode node = null;
|
||||||
Board current = new Board(root);
|
Board current = new Board(root);
|
||||||
while (!current.equals(solved)) {
|
while (!current.equals(solved) && !boards.isEmpty()) {
|
||||||
node = boards.poll();
|
node = boards.poll();
|
||||||
current = node.board;
|
current = node.board;
|
||||||
if (visited.contains(node.board)) {
|
if (visited.contains(node.board)) {
|
||||||
@ -38,10 +45,3 @@ public class AStar implements Solver {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class AStarPriority implements Comparator<BoardNode> {
|
|
||||||
@Override
|
|
||||||
public int compare(BoardNode o1, BoardNode o2) {
|
|
||||||
return Integer.compare(o1.expected, o2.expected);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,47 +0,0 @@
|
|||||||
package Assignments.A1.solving_algorithms;
|
|
||||||
|
|
||||||
import Assignments.A1.models.Board;
|
|
||||||
import Assignments.A1.models.BoardNode;
|
|
||||||
import Assignments.A1.models.Move;
|
|
||||||
import Assignments.A1.models.Solver;
|
|
||||||
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.PriorityQueue;
|
|
||||||
|
|
||||||
public class UCS implements Solver {
|
|
||||||
private final Board solved = new Board();
|
|
||||||
private final HashSet<Board> visited = new HashSet<>();
|
|
||||||
|
|
||||||
public BoardNode traverse(Board root) {
|
|
||||||
PriorityQueue<BoardNode> boards = new PriorityQueue<>(new UCSPriority());
|
|
||||||
boards.add(new BoardNode(root, null));
|
|
||||||
BoardNode node = null;
|
|
||||||
Board current = new Board(root);
|
|
||||||
while (!current.equals(solved)) {
|
|
||||||
node = boards.poll();
|
|
||||||
current = node.board;
|
|
||||||
if (visited.contains(node.board)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
visited.add(node.board);
|
|
||||||
List<Move> children = node.board.getMoves();
|
|
||||||
for (Move move : children) {
|
|
||||||
Board child = new Board(node.board);
|
|
||||||
child.swap(move);
|
|
||||||
BoardNode childNode = new BoardNode(child, node);
|
|
||||||
boards.add(childNode);
|
|
||||||
node.addChild(childNode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class UCSPriority implements Comparator<BoardNode> {
|
|
||||||
@Override
|
|
||||||
public int compare(BoardNode o1, BoardNode o2) {
|
|
||||||
return Integer.compare(o1.cost, o2.cost);
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,13 @@
|
|||||||
|
package Assignments.A1.solving_algorithms.comparators;
|
||||||
|
|
||||||
|
import Assignments.A1.models.Board;
|
||||||
|
import Assignments.A1.models.BoardNode;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
public class AStar implements Comparator<BoardNode> {
|
||||||
|
@Override
|
||||||
|
public int compare(BoardNode o1, BoardNode o2) {
|
||||||
|
return Integer.compare(o1.expected, o2.expected);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package Assignments.A1.solving_algorithms.comparators;
|
||||||
|
|
||||||
|
import Assignments.A1.models.BoardNode;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
public class BFS implements Comparator<BoardNode> {
|
||||||
|
@Override
|
||||||
|
public int compare(BoardNode o1, BoardNode o2) {
|
||||||
|
return Integer.compare(o1.heuristic, o2.heuristic);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
|||||||
|
package Assignments.A1.solving_algorithms.comparators;
|
||||||
|
|
||||||
|
import Assignments.A1.models.BoardNode;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
public class UCS implements Comparator<BoardNode> {
|
||||||
|
@Override
|
||||||
|
public int compare(BoardNode o1, BoardNode o2) {
|
||||||
|
return Integer.compare(o1.cost, o2.cost);
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package Assignments.A1.view;
|
package Assignments.A1.view;
|
||||||
|
|
||||||
|
import Assignments.A1.models.Board;
|
||||||
import Assignments.A1.models.BoardNode;
|
import Assignments.A1.models.BoardNode;
|
||||||
import javafx.event.ActionEvent;
|
import javafx.event.ActionEvent;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
@ -70,6 +71,8 @@ public class BoardViewCodeBehind {
|
|||||||
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());
|
this.disclaimer.visibleProperty().bindBidirectional(viewModel.disclaimerProperty());
|
||||||
|
|
||||||
|
this.current_board.textProperty().setValue(new Board().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onGenerateBoard(ActionEvent actionEvent) {
|
public void onGenerateBoard(ActionEvent actionEvent) {
|
||||||
@ -86,6 +89,7 @@ public class BoardViewCodeBehind {
|
|||||||
public void onSolveButton(ActionEvent actionEvent) {
|
public void onSolveButton(ActionEvent actionEvent) {
|
||||||
viewModel.solveBoard();
|
viewModel.solveBoard();
|
||||||
viewModel.updateDisplay();
|
viewModel.updateDisplay();
|
||||||
|
this.spanning_tree.setRoot(new TreeItem<>());
|
||||||
this.spanning_tree.setRoot(viewModel.getSolvedRootNode());
|
this.spanning_tree.setRoot(viewModel.getSolvedRootNode());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,11 +3,12 @@ package Assignments.A1.view;
|
|||||||
import Assignments.A1.models.Board;
|
import Assignments.A1.models.Board;
|
||||||
import Assignments.A1.models.BoardGenerator;
|
import Assignments.A1.models.BoardGenerator;
|
||||||
import Assignments.A1.models.BoardNode;
|
import Assignments.A1.models.BoardNode;
|
||||||
import Assignments.A1.models.Solver;
|
import Assignments.A1.models.helper.Solver;
|
||||||
import Assignments.A1.solving_algorithms.AStar;
|
import Assignments.A1.solving_algorithms.PriorityTraversal;
|
||||||
import Assignments.A1.solving_algorithms.BFS;
|
import Assignments.A1.solving_algorithms.comparators.AStar;
|
||||||
|
import Assignments.A1.solving_algorithms.comparators.BFS;
|
||||||
import Assignments.A1.solving_algorithms.DFS;
|
import Assignments.A1.solving_algorithms.DFS;
|
||||||
import Assignments.A1.solving_algorithms.UCS;
|
import Assignments.A1.solving_algorithms.comparators.UCS;
|
||||||
import javafx.beans.property.*;
|
import javafx.beans.property.*;
|
||||||
import javafx.scene.control.TreeItem;
|
import javafx.scene.control.TreeItem;
|
||||||
|
|
||||||
@ -79,11 +80,11 @@ public class MainViewModel {
|
|||||||
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 PriorityTraversal(new UCS());
|
||||||
} else if (BFS.getValue()) {
|
} else if (BFS.getValue()) {
|
||||||
solver = new BFS();
|
solver = new PriorityTraversal(new BFS());
|
||||||
} else if (AStar.getValue()) {
|
} else if (AStar.getValue()) {
|
||||||
solver = new AStar();
|
solver = new PriorityTraversal(new AStar());
|
||||||
}
|
}
|
||||||
Date start = new Date();
|
Date start = new Date();
|
||||||
BoardNode solved = solver.traverse(this.current.board);
|
BoardNode solved = solver.traverse(this.current.board);
|
||||||
@ -136,6 +137,7 @@ public class MainViewModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private TreeItem<BoardNode> rebuildTree(BoardNode root, boolean expanded) {
|
private TreeItem<BoardNode> rebuildTree(BoardNode root, boolean expanded) {
|
||||||
|
if (this.selectedAlg != null) {
|
||||||
if (this.selectedAlg.equals("DFS")) {
|
if (this.selectedAlg.equals("DFS")) {
|
||||||
Stack<BoardNode> generations = new Stack<>();
|
Stack<BoardNode> generations = new Stack<>();
|
||||||
BoardNode temp = this.current;
|
BoardNode temp = this.current;
|
||||||
@ -163,6 +165,8 @@ public class MainViewModel {
|
|||||||
return treeItem;
|
return treeItem;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return new TreeItem<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean getExpanded() {
|
public boolean getExpanded() {
|
||||||
|
Loading…
Reference in New Issue
Block a user