Compare commits
7 Commits
e0932b4721
...
136c78cd9d
Author | SHA1 | Date | |
---|---|---|---|
|
136c78cd9d | ||
|
3ac16301fb | ||
|
8f28761776 | ||
|
8d87ca5aab | ||
|
7a81bd5dc8 | ||
|
7242c5985f | ||
|
d6a0ddcd8d |
@ -1,10 +1,11 @@
|
|||||||
<component name="ProjectRunConfigurationManager">
|
<component name="ProjectRunConfigurationManager">
|
||||||
<configuration default="false" name="CS3642-Artificial_Intelligence [javafx:run]" type="MavenRunConfiguration" factoryName="Maven" nameIsGenerated="true">
|
<configuration default="false" name="CS3642-Artificial_Intelligence [javafx:run]" type="MavenRunConfiguration" factoryName="Maven" activateToolWindowBeforeRun="false" nameIsGenerated="true">
|
||||||
<MavenSettings>
|
<MavenSettings>
|
||||||
<option name="myGeneralSettings" />
|
<option name="myGeneralSettings" />
|
||||||
<option name="myRunnerSettings" />
|
<option name="myRunnerSettings" />
|
||||||
<option name="myRunnerParameters">
|
<option name="myRunnerParameters">
|
||||||
<MavenRunnerParameters>
|
<MavenRunnerParameters>
|
||||||
|
<option name="cmdOptions" />
|
||||||
<option name="profiles">
|
<option name="profiles">
|
||||||
<set />
|
<set />
|
||||||
</option>
|
</option>
|
||||||
@ -17,6 +18,9 @@
|
|||||||
<option name="profilesMap">
|
<option name="profilesMap">
|
||||||
<map />
|
<map />
|
||||||
</option>
|
</option>
|
||||||
|
<option name="projectsCmdOptionValues">
|
||||||
|
<list />
|
||||||
|
</option>
|
||||||
<option name="resolveToWorkspace" value="false" />
|
<option name="resolveToWorkspace" value="false" />
|
||||||
<option name="workingDirPath" value="$PROJECT_DIR$" />
|
<option name="workingDirPath" value="$PROJECT_DIR$" />
|
||||||
</MavenRunnerParameters>
|
</MavenRunnerParameters>
|
||||||
|
126919
null/AStar.txt
Normal file
126919
null/AStar.txt
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,152 +0,0 @@
|
|||||||
package Assignments.A1;
|
|
||||||
|
|
||||||
// Potentially will be changed to an UI Implementation with JavaFX if time permits.
|
|
||||||
|
|
||||||
import Assignments.A1.models.Board;
|
|
||||||
import Assignments.A1.models.BoardGenerator;
|
|
||||||
import Assignments.A1.models.BoardNode;
|
|
||||||
import Assignments.A1.solving_algorithms.DFS;
|
|
||||||
import Assignments.A1.solving_algorithms.comparators.AStar;
|
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Board will be used to save locations in a 2D array.
|
|
||||||
* <p>
|
|
||||||
* The design of the board locations will be numbered like so:
|
|
||||||
* 0 1 2
|
|
||||||
* 3 4 5
|
|
||||||
* 6 7 8
|
|
||||||
* <p>
|
|
||||||
* 1 2 3
|
|
||||||
* 8 0 4
|
|
||||||
* 7 6 5
|
|
||||||
* <p>
|
|
||||||
* The values will be specified with an int. Must be between (inclusive) 1-8.
|
|
||||||
* The empty spot will be represented with null.
|
|
||||||
*/
|
|
||||||
public class Driver {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
ArrayList<Long> timer = new ArrayList<>();
|
|
||||||
int successes = 0;
|
|
||||||
int runs = 1;
|
|
||||||
BoardNode node = null;
|
|
||||||
for (int run = 0; run < runs; run++) {
|
|
||||||
Board board = BoardGenerator.generateBoard();
|
|
||||||
DFS solver = new DFS();
|
|
||||||
// BFS solver = new BFS();
|
|
||||||
// UCS solver = new UCS();
|
|
||||||
// AStar solver = new AStar();
|
|
||||||
Date start = new Date();
|
|
||||||
BoardNode result = solver.traverse(board);
|
|
||||||
Date end = new Date();
|
|
||||||
if (result.board != null) {
|
|
||||||
long runtime = end.getTime() - start.getTime();
|
|
||||||
|
|
||||||
timer.add(runtime);
|
|
||||||
successes++;
|
|
||||||
node = result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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 + "/" + runs);
|
|
||||||
|
|
||||||
StringBuffer result = printTotalHierarchy(node);
|
|
||||||
writeToFile("AStar", result);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// public static void main(String[] args) {
|
|
||||||
// int[] values = {1, 2, 3, 8, 0, 5, 4, 7, 6};
|
|
||||||
// BoardNode node = new BoardNode(new Board(values), null);
|
|
||||||
// System.out.println(node.heuristic);
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
private static StringBuffer printTotalHierarchy(BoardNode root, int depth, boolean hasMoreChildren) {
|
|
||||||
StringBuffer output = new StringBuffer();
|
|
||||||
for (int i = 0; i < depth - 1; i++) {
|
|
||||||
output.append("│ ");
|
|
||||||
}
|
|
||||||
if (hasMoreChildren) {
|
|
||||||
output.append("├── ").append(root.board.toString()).append(System.lineSeparator());
|
|
||||||
} else {
|
|
||||||
output.append("└── ").append(root.board.toString()).append(System.lineSeparator());
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < root.children.size(); i++) {
|
|
||||||
if (i == root.children.size() - 1) {
|
|
||||||
output.append(printTotalHierarchy(root.children.get(i), depth+1, false));
|
|
||||||
} else {
|
|
||||||
output.append(printTotalHierarchy(root.children.get(i), depth+1, true));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static StringBuffer printTotalHierarchy(BoardNode solved) {
|
|
||||||
BoardNode root = null;
|
|
||||||
while (solved.parent != null) {
|
|
||||||
solved = solved.parent;
|
|
||||||
}
|
|
||||||
root = solved;
|
|
||||||
|
|
||||||
StringBuffer output = new StringBuffer();
|
|
||||||
output.append(root.board.toString()).append(System.lineSeparator());
|
|
||||||
if (root.children.size() == 1) {
|
|
||||||
output.append(printTotalHierarchy(root.children.get(0), 1, false));
|
|
||||||
} else {
|
|
||||||
for (int i = 0; i < root.children.size(); i++) {
|
|
||||||
if (i == root.children.size() - 1) {
|
|
||||||
output.append(printTotalHierarchy(root.children.get(i), 1, false));
|
|
||||||
} else {
|
|
||||||
output.append(printTotalHierarchy(root.children.get(i), 1, true));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void writeToFile(String name, StringBuffer values) {
|
|
||||||
URL resourcePath = Driver.class.getResource("/A1/results/" + name + ".txt");
|
|
||||||
URI resourceURI = URI.create(resourcePath.toString());
|
|
||||||
File resource = new File(resourceURI.getPath());
|
|
||||||
|
|
||||||
try (FileWriter out = new FileWriter(resource)) {
|
|
||||||
out.write(values.toString());
|
|
||||||
System.out.println("Data written to file: " + name + ".txt");
|
|
||||||
} catch (IOException e) {
|
|
||||||
System.err.println("Error writing to file: " + e.getMessage());
|
|
||||||
}
|
|
||||||
openDirectoryFile(resource);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void openDirectoryFile(File open) {
|
|
||||||
if (!Desktop.isDesktopSupported() || !Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
|
|
||||||
System.out.println("Desktop is not supported");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Desktop desktop = Desktop.getDesktop();
|
|
||||||
try {
|
|
||||||
desktop.open(open);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -3,7 +3,7 @@ package Assignments.A1;
|
|||||||
public class GUILauncher {
|
public class GUILauncher {
|
||||||
|
|
||||||
public static void main(final String[] args) {
|
public static void main(final String[] args) {
|
||||||
GUIDriver.main(args);
|
MainScene.main(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import javafx.scene.Parent;
|
|||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
public class GUIDriver extends Application {
|
public class MainScene extends Application {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Stage stage) throws Exception {
|
public void start(Stage stage) throws Exception {
|
@ -3,14 +3,29 @@ package Assignments.A1.models;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to represent a board on a spanning tree.
|
||||||
|
*
|
||||||
|
* @author Jonathan Turner
|
||||||
|
* @version Spring 2024
|
||||||
|
*/
|
||||||
public class BoardNode implements Comparable<BoardNode> {
|
public class BoardNode implements Comparable<BoardNode> {
|
||||||
|
|
||||||
|
/* Fields used to hold each node's data. */
|
||||||
public BoardNode parent;
|
public BoardNode parent;
|
||||||
public int heuristic, cost, expected;
|
|
||||||
public Board board;
|
public Board board;
|
||||||
public List<BoardNode> children;
|
public List<BoardNode> children;
|
||||||
public int depth = -1;
|
public int heuristic, cost, expected, depth;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to create a board with a parent and child nodes.
|
||||||
|
*
|
||||||
|
* @precondition none
|
||||||
|
* @postcondition none
|
||||||
|
*
|
||||||
|
* @param board the board representing the node.
|
||||||
|
* @param parent the parent node, can be null.
|
||||||
|
*/
|
||||||
public BoardNode(Board board, BoardNode parent) {
|
public BoardNode(Board board, BoardNode parent) {
|
||||||
this.board = board;
|
this.board = board;
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
@ -20,10 +35,40 @@ public class BoardNode implements Comparable<BoardNode> {
|
|||||||
this.children = new ArrayList<>();
|
this.children = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDepth(int depth) {
|
/**
|
||||||
this.depth = depth;
|
* adds a child to the node.
|
||||||
|
*
|
||||||
|
* @precondition child != null
|
||||||
|
* @postcondition the child is added..
|
||||||
|
*/
|
||||||
|
public void addChild(BoardNode node) {
|
||||||
|
children.add(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Overrides */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overrides the Object compareTo method. Is a placehold for specifying priority in queues.
|
||||||
|
* Default: BFS
|
||||||
|
*
|
||||||
|
* @param o the object to be compared.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int compareTo(BoardNode o) { // BFS
|
||||||
|
return Integer.compare(this.heuristic, o.heuristic);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the board in a single string (no line breaks)
|
||||||
|
* @return the formatting string
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.board.toString().replaceAll("\n", " ");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Private Classes: End of JavaDocs */
|
||||||
|
|
||||||
private int getActualCost() {
|
private int getActualCost() {
|
||||||
if (this.parent != null) {
|
if (this.parent != null) {
|
||||||
return this.parent.cost + 10;
|
return this.parent.cost + 10;
|
||||||
@ -32,10 +77,6 @@ public class BoardNode implements Comparable<BoardNode> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addChild(BoardNode 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++) {
|
||||||
@ -83,14 +124,4 @@ public class BoardNode implements Comparable<BoardNode> {
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int compareTo(BoardNode o) { // BFS
|
|
||||||
return Integer.compare(this.heuristic, o.heuristic);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return this.board.toString().replaceAll("\n", " ");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ import Assignments.A1.models.Board;
|
|||||||
*/
|
*/
|
||||||
public class Move {
|
public class Move {
|
||||||
|
|
||||||
private Board board;
|
private final Board board;
|
||||||
private final Pair<Integer> points;
|
private final Pair<Integer> points;
|
||||||
private boolean used;
|
private boolean used;
|
||||||
|
|
||||||
|
@ -3,9 +3,22 @@ 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;
|
/**
|
||||||
|
* Interface used for implementing a solver when there are multiple different variations and classes.
|
||||||
|
*
|
||||||
|
* @author Jonathan Turner
|
||||||
|
* @version Spring 2024
|
||||||
|
*/
|
||||||
public interface Solver {
|
public interface Solver {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Solves for, finds and creates a spanning representing the solving pattern.
|
||||||
|
*
|
||||||
|
* @precondition none
|
||||||
|
* @postcondition none
|
||||||
|
*
|
||||||
|
* @param root the first node/initial start.
|
||||||
|
* @return the solved leaf node.
|
||||||
|
*/
|
||||||
BoardNode traverse(Board root);
|
BoardNode traverse(Board root);
|
||||||
}
|
}
|
||||||
|
@ -4,18 +4,34 @@ import Assignments.A1.models.*;
|
|||||||
import Assignments.A1.models.helper.Move;
|
import Assignments.A1.models.helper.Move;
|
||||||
import Assignments.A1.models.helper.Solver;
|
import Assignments.A1.models.helper.Solver;
|
||||||
import Assignments.A1.resources.Parameters;
|
import Assignments.A1.resources.Parameters;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to evaluate a generated SpanningTree of BoardNodes with respect to DFS LIFO.
|
||||||
|
*
|
||||||
|
* @author Jonathan Turner
|
||||||
|
* @version Spring 2024
|
||||||
|
*/
|
||||||
public class DFS implements Solver {
|
public class DFS implements Solver {
|
||||||
|
|
||||||
|
/* Private Fields for Searching */
|
||||||
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<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Traverses through a Board using a Stack, allowing for DFS LIFO.
|
||||||
|
*
|
||||||
|
* @precondition root != null
|
||||||
|
* @postcondition the spanning tree is created.
|
||||||
|
*
|
||||||
|
* @param root the first node/initial start.
|
||||||
|
* @return the leaf node that is solved.
|
||||||
|
*/
|
||||||
public BoardNode traverse(Board root) {
|
public BoardNode traverse(Board root) {
|
||||||
|
tried.clear(); // Resets each run.
|
||||||
Stack<BoardNode> stack = new Stack<>();
|
Stack<BoardNode> stack = new Stack<>();
|
||||||
BoardNode rootNode = new BoardNode(root, null);
|
BoardNode rootNode = new BoardNode(root, null);
|
||||||
rootNode.setDepth(0);
|
rootNode.depth = 0;
|
||||||
stack.push(rootNode);
|
stack.push(rootNode);
|
||||||
|
|
||||||
while (!stack.isEmpty()) {
|
while (!stack.isEmpty()) {
|
||||||
@ -35,7 +51,7 @@ public class DFS implements Solver {
|
|||||||
Board child = next.getBoard();
|
Board child = next.getBoard();
|
||||||
child.swap(next);
|
child.swap(next);
|
||||||
BoardNode childNode = new BoardNode(child, current);
|
BoardNode childNode = new BoardNode(child, current);
|
||||||
childNode.setDepth(current.depth+1);
|
childNode.depth = current.depth+1;
|
||||||
stack.push(childNode);
|
stack.push(childNode);
|
||||||
current.addChild(childNode);
|
current.addChild(childNode);
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ public class PriorityTraversal implements Solver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public BoardNode traverse(Board root) {
|
public BoardNode traverse(Board root) {
|
||||||
|
visited.clear();
|
||||||
PriorityQueue<BoardNode> boards = new PriorityQueue<>(comparator);
|
PriorityQueue<BoardNode> boards = new PriorityQueue<>(comparator);
|
||||||
boards.add(new BoardNode(root, null));
|
boards.add(new BoardNode(root, null));
|
||||||
BoardNode node = null;
|
BoardNode node = null;
|
||||||
|
@ -1,11 +1,26 @@
|
|||||||
package Assignments.A1.solving_algorithms.comparators;
|
package Assignments.A1.solving_algorithms.comparators;
|
||||||
|
|
||||||
import Assignments.A1.models.Board;
|
|
||||||
import Assignments.A1.models.BoardNode;
|
import Assignments.A1.models.BoardNode;
|
||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is used to compare two BoardNodes for an AStar Traversal.
|
||||||
|
*
|
||||||
|
* @author Jonathan Turner
|
||||||
|
* @version Spring 2024
|
||||||
|
*/
|
||||||
public class AStar implements Comparator<BoardNode> {
|
public class AStar implements Comparator<BoardNode> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compares two BoardNodes and sorts them in respect to both g(n) and h(n) resulting in AStar Priority.
|
||||||
|
*
|
||||||
|
* @precondition o1 != null && o2 != null
|
||||||
|
* @postcondition none
|
||||||
|
*
|
||||||
|
* @param o1 the first object to be compared.
|
||||||
|
* @param o2 the second object to be compared.
|
||||||
|
* @return -1,0,1 based on the a(n).
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int compare(BoardNode o1, BoardNode o2) {
|
public int compare(BoardNode o1, BoardNode o2) {
|
||||||
return Integer.compare(o1.expected, o2.expected);
|
return Integer.compare(o1.expected, o2.expected);
|
||||||
|
@ -1,10 +1,26 @@
|
|||||||
package Assignments.A1.solving_algorithms.comparators;
|
package Assignments.A1.solving_algorithms.comparators;
|
||||||
|
|
||||||
import Assignments.A1.models.BoardNode;
|
import Assignments.A1.models.BoardNode;
|
||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is used to compare two BoardNodes for an BFS Traversal.
|
||||||
|
*
|
||||||
|
* @author Jonathan Turner
|
||||||
|
* @version Spring 2024
|
||||||
|
*/
|
||||||
public class BFS implements Comparator<BoardNode> {
|
public class BFS implements Comparator<BoardNode> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compares two BoardNodes and sorts them in respect to h(n) resulting in BFS Priority.
|
||||||
|
*
|
||||||
|
* @precondition o1 != null && o2 != null
|
||||||
|
* @postcondition none
|
||||||
|
*
|
||||||
|
* @param o1 the first object to be compared.
|
||||||
|
* @param o2 the second object to be compared.
|
||||||
|
* @return -1,0,1 based on the h(n).
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int compare(BoardNode o1, BoardNode o2) {
|
public int compare(BoardNode o1, BoardNode o2) {
|
||||||
return Integer.compare(o1.heuristic, o2.heuristic);
|
return Integer.compare(o1.heuristic, o2.heuristic);
|
||||||
|
@ -1,10 +1,26 @@
|
|||||||
package Assignments.A1.solving_algorithms.comparators;
|
package Assignments.A1.solving_algorithms.comparators;
|
||||||
|
|
||||||
import Assignments.A1.models.BoardNode;
|
import Assignments.A1.models.BoardNode;
|
||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is used to compare two BoardNodes for an UCS Traversal.
|
||||||
|
*
|
||||||
|
* @author Jonathan Turner
|
||||||
|
* @version Spring 2024
|
||||||
|
*/
|
||||||
public class UCS implements Comparator<BoardNode> {
|
public class UCS implements Comparator<BoardNode> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compares two BoardNodes and sorts them in respect to g(n) resulting in UCS Priority.
|
||||||
|
*
|
||||||
|
* @precondition o1 != null && o2 != null
|
||||||
|
* @postcondition none
|
||||||
|
*
|
||||||
|
* @param o1 the first object to be compared.
|
||||||
|
* @param o2 the second object to be compared.
|
||||||
|
* @return -1,0,1 based on the g(n).
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int compare(BoardNode o1, BoardNode o2) {
|
public int compare(BoardNode o1, BoardNode o2) {
|
||||||
return Integer.compare(o1.cost, o2.cost);
|
return Integer.compare(o1.cost, o2.cost);
|
||||||
|
@ -29,9 +29,6 @@ public class BoardViewCodeBehind {
|
|||||||
@FXML
|
@FXML
|
||||||
private ToggleButton expanded;
|
private ToggleButton expanded;
|
||||||
|
|
||||||
@FXML
|
|
||||||
private ToggleButton showOnlySolvedPath;
|
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private MenuItem AStar;
|
private MenuItem AStar;
|
||||||
|
|
||||||
@ -44,6 +41,12 @@ public class BoardViewCodeBehind {
|
|||||||
@FXML
|
@FXML
|
||||||
private MenuItem UCS;
|
private MenuItem UCS;
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private Button openPerformance;
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private Button runPerformanceCHeck;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private Button solve_button;
|
private Button solve_button;
|
||||||
|
|
||||||
@ -53,6 +56,9 @@ public class BoardViewCodeBehind {
|
|||||||
@FXML
|
@FXML
|
||||||
private Label no_solv_alg_err;
|
private Label no_solv_alg_err;
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private Label iteration_count;
|
||||||
|
|
||||||
private MainViewModel viewModel;
|
private MainViewModel viewModel;
|
||||||
|
|
||||||
public BoardViewCodeBehind() {
|
public BoardViewCodeBehind() {
|
||||||
@ -71,10 +77,18 @@ 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.runPerformanceCHeck.visibleProperty().bindBidirectional(viewModel.runPerfProperty());
|
||||||
|
this.openPerformance.visibleProperty().bindBidirectional(viewModel.openPerfProperty());
|
||||||
|
this.iteration_count.textProperty().bindBidirectional(viewModel.iterationCounterProperty());
|
||||||
|
|
||||||
this.current_board.textProperty().setValue(new Board().toString());
|
this.current_board.textProperty().setValue(new Board().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
void runPerformanceCheck(ActionEvent event) {
|
||||||
|
this.viewModel.runPerformanceCheck();
|
||||||
|
}
|
||||||
|
|
||||||
public void onGenerateBoard(ActionEvent actionEvent) {
|
public void onGenerateBoard(ActionEvent actionEvent) {
|
||||||
this.viewModel.generateBoard();
|
this.viewModel.generateBoard();
|
||||||
}
|
}
|
||||||
@ -97,6 +111,11 @@ public class BoardViewCodeBehind {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
void openPerformance(ActionEvent event) {
|
||||||
|
this.viewModel.openPerf();
|
||||||
|
}
|
||||||
|
|
||||||
public void onDFS(ActionEvent actionEvent) {
|
public void onDFS(ActionEvent actionEvent) {
|
||||||
viewModel.setSelectedAlg("DFS");
|
viewModel.setSelectedAlg("DFS");
|
||||||
this.menu_alg.textProperty().set(this.menu_alg.getItems().get(0).textProperty().get());
|
this.menu_alg.textProperty().set(this.menu_alg.getItems().get(0).textProperty().get());
|
||||||
|
@ -10,6 +10,7 @@ import Assignments.A1.solving_algorithms.comparators.BFS;
|
|||||||
import Assignments.A1.solving_algorithms.DFS;
|
import Assignments.A1.solving_algorithms.DFS;
|
||||||
import Assignments.A1.solving_algorithms.comparators.UCS;
|
import Assignments.A1.solving_algorithms.comparators.UCS;
|
||||||
import javafx.beans.property.*;
|
import javafx.beans.property.*;
|
||||||
|
import javafx.scene.control.Alert;
|
||||||
import javafx.scene.control.TreeItem;
|
import javafx.scene.control.TreeItem;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@ -17,32 +18,91 @@ import java.util.Stack;
|
|||||||
|
|
||||||
public class MainViewModel {
|
public class MainViewModel {
|
||||||
|
|
||||||
|
/* Private Fields used to bind */
|
||||||
private StringProperty currentBoardProperty, algSpeedProperty;
|
private StringProperty currentBoardProperty, algSpeedProperty;
|
||||||
private BooleanProperty expanded, DFS, UCS, BFS, AStar, solvingAlgErr, genBoardErr, disclaimer;
|
private StringProperty iterationCounter;
|
||||||
private String selectedAlg;
|
private BooleanProperty expanded, DFS, UCS, BFS, AStar, solvingAlgErr, genBoardErr, disclaimer, openPerf, runPerf;
|
||||||
|
|
||||||
private BoardNode current = new BoardNode(new Board(), null);
|
private BoardNode current = new BoardNode(new Board(), null);
|
||||||
private TreeItem<BoardNode> solvedRootNode = null;
|
private TreeItem<BoardNode> solvedRootNode = null;
|
||||||
private BoardNode solvedRootBoardNode = null;
|
|
||||||
|
|
||||||
|
/* Private Fields used for traversing/creating nodes. */
|
||||||
|
private BoardNode solvedRootBoardNode = null;
|
||||||
|
private long[][] hasRanPerformance;
|
||||||
|
private String selectedAlg;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes all the Properties and initial values for any fields.
|
||||||
|
*
|
||||||
|
* @precondition none
|
||||||
|
* @postcondition a VM is created.
|
||||||
|
*/
|
||||||
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.disclaimer = new SimpleBooleanProperty();
|
||||||
this.expanded = new SimpleBooleanProperty();
|
this.expanded = new SimpleBooleanProperty();
|
||||||
|
this.openPerf = new SimpleBooleanProperty();
|
||||||
|
this.runPerf = new SimpleBooleanProperty();
|
||||||
this.DFS = new SimpleBooleanProperty();
|
this.DFS = new SimpleBooleanProperty();
|
||||||
this.UCS = new SimpleBooleanProperty();
|
this.UCS = new SimpleBooleanProperty();
|
||||||
this.BFS = new SimpleBooleanProperty();
|
this.BFS = new SimpleBooleanProperty();
|
||||||
this.AStar = new SimpleBooleanProperty();
|
this.AStar = new SimpleBooleanProperty();
|
||||||
|
this.iterationCounter = new SimpleStringProperty();
|
||||||
|
|
||||||
this.solvingAlgErr = new SimpleBooleanProperty();
|
this.solvingAlgErr = new SimpleBooleanProperty();
|
||||||
this.genBoardErr = new SimpleBooleanProperty();
|
this.genBoardErr = new SimpleBooleanProperty();
|
||||||
this.solvingAlgErr.set(false);
|
this.solvingAlgErr.set(false);
|
||||||
this.genBoardErr.set(false);
|
this.genBoardErr.set(false);
|
||||||
|
|
||||||
|
this.hasRanPerformance = new long[4][102];
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSelectedAlg() {
|
|
||||||
return this.selectedAlg;
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public void runPerformanceCheck() {
|
||||||
|
Solver solver = null;
|
||||||
|
int alg = -1;
|
||||||
|
if (this.selectedAlg.equals("DFS") && this.hasRanPerformance[0][0] == 0) {
|
||||||
|
solver = new DFS();
|
||||||
|
alg = 0;
|
||||||
|
} else if (this.selectedAlg.equals("UCS") && this.hasRanPerformance[1][0] == 0) {
|
||||||
|
solver = new PriorityTraversal(new UCS());
|
||||||
|
alg = 1;
|
||||||
|
} else if (this.selectedAlg.equals("BFS") && this.hasRanPerformance[2][0] == 0) {
|
||||||
|
solver = new PriorityTraversal(new BFS());
|
||||||
|
alg = 2;
|
||||||
|
} else if (this.selectedAlg.equals("AStar") && this.hasRanPerformance[3][0] == 0) {
|
||||||
|
solver = new PriorityTraversal(new AStar());
|
||||||
|
alg = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (solver != null) {
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
Board board = BoardGenerator.generateBoard();
|
||||||
|
System.out.println(board);
|
||||||
|
long start = new Date().getTime();
|
||||||
|
BoardNode node = solver.traverse(board);
|
||||||
|
long end = new Date().getTime();
|
||||||
|
System.out.println(node.board + "\n");
|
||||||
|
if (node != null) {
|
||||||
|
this.hasRanPerformance[alg][i + 2] = end - start;
|
||||||
|
this.currentBoardProperty.setValue(node.board.toString());
|
||||||
|
} else {
|
||||||
|
this.hasRanPerformance[alg][i+2] = -1;
|
||||||
|
}
|
||||||
|
iterationCounter.setValue("Performing Analysis Iteration: " + (i+1) + "/100");
|
||||||
|
}
|
||||||
|
this.hasRanPerformance[alg][0] = 1;
|
||||||
|
|
||||||
|
this.runPerf.setValue(false);
|
||||||
|
this.openPerf.setValue(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i < this.hasRanPerformance[alg].length; i++) {
|
||||||
|
System.out.println(this.hasRanPerformance[alg][i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSelectedAlg(String value) {
|
public void setSelectedAlg(String value) {
|
||||||
@ -53,6 +113,18 @@ public class MainViewModel {
|
|||||||
this.AStar.set(value.equals("AStar"));
|
this.AStar.set(value.equals("AStar"));
|
||||||
|
|
||||||
this.disclaimer.setValue(value.equals("DFS"));
|
this.disclaimer.setValue(value.equals("DFS"));
|
||||||
|
this.openPerf.setValue(this.hasRanPerformance[convertValueToInt(value)][0] == 1);
|
||||||
|
this.runPerf.setValue(this.hasRanPerformance[convertValueToInt(value)][0] == 0);
|
||||||
|
if (this.hasRanPerformance[convertValueToInt(value)][0] == 0) {
|
||||||
|
this.iterationCounter.set("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int convertValueToInt(String value) {
|
||||||
|
if (value.equals("DFS")) return 0;
|
||||||
|
if (value.equals("UCS")) return 1;
|
||||||
|
if (value.equals("BFS")) return 2;
|
||||||
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
public StringProperty getCurrentBoardProperty() {
|
public StringProperty getCurrentBoardProperty() {
|
||||||
@ -86,6 +158,7 @@ public class MainViewModel {
|
|||||||
} else if (AStar.getValue()) {
|
} else if (AStar.getValue()) {
|
||||||
solver = new PriorityTraversal(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);
|
||||||
Date end = new Date();
|
Date end = new Date();
|
||||||
@ -102,40 +175,43 @@ public class MainViewModel {
|
|||||||
this.current = solved;
|
this.current = solved;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void openPerf() {
|
||||||
|
long totalTime = 0;
|
||||||
|
int success = 0;
|
||||||
|
long shortest = 1000000;
|
||||||
|
long longest = -1;
|
||||||
|
for (int i = 2; i < this.hasRanPerformance[convertValueToInt(this.selectedAlg)].length; i++) {
|
||||||
|
long current = this.hasRanPerformance[convertValueToInt(this.selectedAlg)][i];
|
||||||
|
if (current != -1) {
|
||||||
|
success++;
|
||||||
|
totalTime += current;
|
||||||
|
if (current < shortest) {
|
||||||
|
shortest = current;
|
||||||
|
}
|
||||||
|
if (longest < current) {
|
||||||
|
longest = current;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
long average = totalTime / (long) success;
|
||||||
|
|
||||||
|
Alert perf = new Alert(Alert.AlertType.INFORMATION);
|
||||||
|
|
||||||
|
perf.setTitle(this.selectedAlg + " Performance Report");
|
||||||
|
perf.setHeaderText(success + "/" + 100 + " Succeeded.");
|
||||||
|
String results = "Shortest Run: " + shortest + "ms\n" +
|
||||||
|
"Longest Run: " + longest + "ms\n" +
|
||||||
|
"Average Time: " + average + "ms";
|
||||||
|
perf.setContentText(results);
|
||||||
|
perf.show();
|
||||||
|
}
|
||||||
|
|
||||||
public void updateDisplay() {
|
public void updateDisplay() {
|
||||||
TreeItem<BoardNode> newRoot = null;
|
TreeItem<BoardNode> newRoot = null;
|
||||||
newRoot = this.rebuildTree(this.solvedRootBoardNode, this.expanded.getValue());
|
newRoot = this.rebuildTree(this.solvedRootBoardNode, this.expanded.getValue());
|
||||||
this.solvedRootNode = newRoot;
|
this.solvedRootNode = newRoot;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TreeItem<BoardNode> getSolvedRootNode() {
|
|
||||||
return solvedRootNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BooleanProperty DFSProperty() {
|
|
||||||
return DFS;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BooleanProperty UCSProperty() {
|
|
||||||
return UCS;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BooleanProperty BFSProperty() {
|
|
||||||
return BFS;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BooleanProperty AStarProperty() {
|
|
||||||
return AStar;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BooleanProperty solvingAlgErrProperty() {
|
|
||||||
return solvingAlgErr;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BooleanProperty genBoardErrProperty() {
|
|
||||||
return genBoardErr;
|
|
||||||
}
|
|
||||||
|
|
||||||
private TreeItem<BoardNode> rebuildTree(BoardNode root, boolean expanded) {
|
private TreeItem<BoardNode> rebuildTree(BoardNode root, boolean expanded) {
|
||||||
if (this.selectedAlg != null) {
|
if (this.selectedAlg != null) {
|
||||||
if (this.selectedAlg.equals("DFS")) {
|
if (this.selectedAlg.equals("DFS")) {
|
||||||
@ -168,6 +244,36 @@ public class MainViewModel {
|
|||||||
return new TreeItem<>();
|
return new TreeItem<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Getters for bindings */
|
||||||
|
|
||||||
|
|
||||||
|
public TreeItem<BoardNode> getSolvedRootNode() {
|
||||||
|
return solvedRootNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BooleanProperty DFSProperty() {
|
||||||
|
return DFS;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BooleanProperty UCSProperty() {
|
||||||
|
return UCS;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BooleanProperty BFSProperty() {
|
||||||
|
return BFS;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BooleanProperty AStarProperty() {
|
||||||
|
return AStar;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BooleanProperty solvingAlgErrProperty() {
|
||||||
|
return solvingAlgErr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BooleanProperty genBoardErrProperty() {
|
||||||
|
return genBoardErr;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean getExpanded() {
|
public boolean getExpanded() {
|
||||||
return expanded.get();
|
return expanded.get();
|
||||||
@ -177,14 +283,14 @@ public class MainViewModel {
|
|||||||
return expanded;
|
return expanded;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAlgSpeedProperty() {
|
|
||||||
return algSpeedProperty.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
public StringProperty algSpeedProperty() {
|
public StringProperty algSpeedProperty() {
|
||||||
return algSpeedProperty;
|
return algSpeedProperty;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public StringProperty iterationCounterProperty() {
|
||||||
|
return iterationCounter;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isDisclaimer() {
|
public boolean isDisclaimer() {
|
||||||
return disclaimer.get();
|
return disclaimer.get();
|
||||||
}
|
}
|
||||||
@ -192,4 +298,12 @@ public class MainViewModel {
|
|||||||
public BooleanProperty disclaimerProperty() {
|
public BooleanProperty disclaimerProperty() {
|
||||||
return disclaimer;
|
return disclaimer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public BooleanProperty openPerfProperty() {
|
||||||
|
return openPerf;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BooleanProperty runPerfProperty() {
|
||||||
|
return runPerf;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,6 @@
|
|||||||
<?import javafx.scene.text.Font?>
|
<?import javafx.scene.text.Font?>
|
||||||
|
|
||||||
<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>
|
|
||||||
<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" />
|
||||||
@ -41,5 +40,7 @@
|
|||||||
</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" />
|
<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>
|
<Button fx:id="runPerformanceCHeck" layoutX="77.0" layoutY="471.0" mnemonicParsing="false" onAction="#runPerformanceCheck" text="Performance Analysis (100 Runs)" visible="false" />
|
||||||
|
<Button fx:id="openPerformance" layoutX="54.0" layoutY="471.0" mnemonicParsing="false" onAction="#openPerformance" text="Open Performance Analysis (Default Editor)" />
|
||||||
|
<Label fx:id="iteration_count" layoutX="123.0" layoutY="506.0" prefHeight="36.0" prefWidth="106.0" wrapText="true" />
|
||||||
</Pane>
|
</Pane>
|
||||||
|
Loading…
Reference in New Issue
Block a user