Compare commits
5 Commits
6b88415ac2
...
2da192b76d
Author | SHA1 | Date | |
---|---|---|---|
|
2da192b76d | ||
76888ebec7 | |||
66f2448b37 | |||
48d2c51652 | |||
|
f096276422 |
@ -0,0 +1,27 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="CS3642-Artificial_Intelligence [javafx:run]" type="MavenRunConfiguration" factoryName="Maven" nameIsGenerated="true">
|
||||||
|
<MavenSettings>
|
||||||
|
<option name="myGeneralSettings" />
|
||||||
|
<option name="myRunnerSettings" />
|
||||||
|
<option name="myRunnerParameters">
|
||||||
|
<MavenRunnerParameters>
|
||||||
|
<option name="profiles">
|
||||||
|
<set />
|
||||||
|
</option>
|
||||||
|
<option name="goals">
|
||||||
|
<list>
|
||||||
|
<option value="javafx:run" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
<option name="pomFileName" />
|
||||||
|
<option name="profilesMap">
|
||||||
|
<map />
|
||||||
|
</option>
|
||||||
|
<option name="resolveToWorkspace" value="false" />
|
||||||
|
<option name="workingDirPath" value="$PROJECT_DIR$" />
|
||||||
|
</MavenRunnerParameters>
|
||||||
|
</option>
|
||||||
|
</MavenSettings>
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
11
pom.xml
11
pom.xml
@ -14,20 +14,15 @@
|
|||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
|
||||||
<groupId>org.fxmisc.richtext</groupId>
|
|
||||||
<artifactId>richtextfx</artifactId>
|
|
||||||
<version>0.11.0</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.openjfx</groupId>
|
<groupId>org.openjfx</groupId>
|
||||||
<artifactId>javafx-controls</artifactId>
|
<artifactId>javafx-controls</artifactId>
|
||||||
<version>12.0.2</version>
|
<version>18.0.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.openjfx</groupId>
|
<groupId>org.openjfx</groupId>
|
||||||
<artifactId>javafx-fxml</artifactId>
|
<artifactId>javafx-fxml</artifactId>
|
||||||
<version>20.0.1</version>
|
<version>18.0.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
@ -42,7 +37,7 @@
|
|||||||
<artifactId>javafx-maven-plugin</artifactId>
|
<artifactId>javafx-maven-plugin</artifactId>
|
||||||
<version>0.0.8</version>
|
<version>0.0.8</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<mainClass>edu.jturn.cs3642.CS3642-Artificial_Intelligence/Assignments.A1.GUIDriver</mainClass>
|
<mainClass>Assignments/A1/GUILauncher</mainClass>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
|
@ -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,15 +41,16 @@ 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();
|
||||||
if (result.board != null) {
|
if (result.board != null) {
|
||||||
long runtime = end.getTime() - start.getTime();
|
long runtime = end.getTime() - start.getTime();
|
||||||
|
|
||||||
timer.add(runtime);
|
timer.add(runtime);
|
||||||
successes++;
|
successes++;
|
||||||
node = result;
|
node = result;
|
||||||
@ -124,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.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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;
|
||||||
@ -31,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,67 +1,43 @@
|
|||||||
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.helper.Move;
|
||||||
import Assignments.A1.models.Move;
|
import Assignments.A1.models.helper.Solver;
|
||||||
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<>();
|
||||||
|
|
||||||
/* Commented out for future reference. */
|
public BoardNode traverse(Board root) {
|
||||||
// public Board dfs(Board root, int depth, ArrayList<String> visited) {
|
Stack<BoardNode> stack = new Stack<>();
|
||||||
// counter++;
|
BoardNode rootNode = new BoardNode(root, null);
|
||||||
// if (root.equals(solved)) {
|
rootNode.setDepth(0);
|
||||||
// return root;
|
stack.push(rootNode);
|
||||||
// }
|
|
||||||
//
|
|
||||||
// ArrayList<String> directParents = new ArrayList<>(visited);
|
|
||||||
// if (depth == Parameters.MAX_DEPTH || visited.contains(root.toString()) || tried.contains(root.toString())) {
|
|
||||||
// return null;
|
|
||||||
// }
|
|
||||||
// directParents.add(root.toString());
|
|
||||||
// tried.add(root.toString());
|
|
||||||
//
|
|
||||||
// List<Move> moves = root.getMoves();
|
|
||||||
// int moveNum = 1;
|
|
||||||
// for (Move next : moves) {
|
|
||||||
//
|
|
||||||
// Board child = next.getBoard();
|
|
||||||
// child.swap(next);
|
|
||||||
// moveNum++;
|
|
||||||
// Board board = dfs(child, depth+1, directParents);
|
|
||||||
// if (board != null) {
|
|
||||||
// return board;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return null;
|
|
||||||
// }
|
|
||||||
|
|
||||||
public Board traverse(Board root, int depth) {
|
|
||||||
Stack<Board> stack = new Stack<>();
|
|
||||||
stack.push(root);
|
|
||||||
|
|
||||||
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;
|
||||||
|
@ -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,12 +1,11 @@
|
|||||||
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;
|
||||||
import javafx.scene.control.*;
|
import javafx.scene.control.*;
|
||||||
|
|
||||||
import javax.swing.tree.TreeNode;
|
|
||||||
|
|
||||||
public class BoardViewCodeBehind {
|
public class BoardViewCodeBehind {
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
@ -15,14 +14,23 @@ public class BoardViewCodeBehind {
|
|||||||
@FXML
|
@FXML
|
||||||
private Label current_board;
|
private Label current_board;
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private Label alg_speed;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private Button generate_board;
|
private Button generate_board;
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private Label disclaimer;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private MenuButton menu_alg;
|
private MenuButton menu_alg;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private ToggleButton showSolvedPath;
|
private ToggleButton expanded;
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private ToggleButton showOnlySolvedPath;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private MenuItem AStar;
|
private MenuItem AStar;
|
||||||
@ -39,9 +47,6 @@ public class BoardViewCodeBehind {
|
|||||||
@FXML
|
@FXML
|
||||||
private Button solve_button;
|
private Button solve_button;
|
||||||
|
|
||||||
@FXML
|
|
||||||
private ProgressBar solving_prog;
|
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private Label gen_board_err;
|
private Label gen_board_err;
|
||||||
|
|
||||||
@ -63,8 +68,11 @@ public class BoardViewCodeBehind {
|
|||||||
this.AStar.disableProperty().bindBidirectional(viewModel.AStarProperty());
|
this.AStar.disableProperty().bindBidirectional(viewModel.AStarProperty());
|
||||||
this.gen_board_err.visibleProperty().bindBidirectional(viewModel.genBoardErrProperty());
|
this.gen_board_err.visibleProperty().bindBidirectional(viewModel.genBoardErrProperty());
|
||||||
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.alg_speed.textProperty().bindBidirectional(viewModel.algSpeedProperty());
|
||||||
|
this.disclaimer.visibleProperty().bindBidirectional(viewModel.disclaimerProperty());
|
||||||
|
|
||||||
|
this.current_board.textProperty().setValue(new Board().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onGenerateBoard(ActionEvent actionEvent) {
|
public void onGenerateBoard(ActionEvent actionEvent) {
|
||||||
@ -72,11 +80,16 @@ public class BoardViewCodeBehind {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void showSolvedPath(ActionEvent actionEvent) {
|
public void showSolvedPath(ActionEvent actionEvent) {
|
||||||
|
if (this.viewModel.getSolvedRootNode() != null) {
|
||||||
|
this.viewModel.updateDisplay();
|
||||||
|
}
|
||||||
|
this.spanning_tree.setRoot(viewModel.getSolvedRootNode());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onSolveButton(ActionEvent actionEvent) {
|
public void onSolveButton(ActionEvent actionEvent) {
|
||||||
viewModel.solveBoard();
|
viewModel.solveBoard();
|
||||||
|
viewModel.updateDisplay();
|
||||||
|
this.spanning_tree.setRoot(new TreeItem<>());
|
||||||
this.spanning_tree.setRoot(viewModel.getSolvedRootNode());
|
this.spanning_tree.setRoot(viewModel.getSolvedRootNode());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,32 +3,33 @@ 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.UCS;
|
import Assignments.A1.solving_algorithms.comparators.BFS;
|
||||||
|
import Assignments.A1.solving_algorithms.DFS;
|
||||||
|
import Assignments.A1.solving_algorithms.comparators.UCS;
|
||||||
import javafx.beans.property.*;
|
import javafx.beans.property.*;
|
||||||
import javafx.collections.FXCollections;
|
|
||||||
import javafx.collections.ObservableList;
|
|
||||||
import javafx.scene.control.TreeItem;
|
import javafx.scene.control.TreeItem;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
public class MainViewModel {
|
public class MainViewModel {
|
||||||
|
|
||||||
private StringProperty currentBoardProperty, algSpeedProperty;
|
private StringProperty currentBoardProperty, algSpeedProperty;
|
||||||
private BooleanProperty showSolvedPath, DFS, UCS, BFS, AStar, solvingAlgErr, genBoardErr;
|
private BooleanProperty expanded, DFS, UCS, BFS, AStar, solvingAlgErr, genBoardErr, disclaimer;
|
||||||
private DoubleProperty solvingProgressProperty;
|
|
||||||
private String selectedAlg;
|
private String selectedAlg;
|
||||||
private final ObservableList<BoardNode> spanningTree = FXCollections.observableArrayList();
|
|
||||||
private final ObjectProperty<ObservableList<BoardNode>> spanningTreeProperty = new SimpleObjectProperty<>(spanningTree);
|
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
public MainViewModel() {
|
public MainViewModel() {
|
||||||
this.currentBoardProperty = new SimpleStringProperty();
|
this.currentBoardProperty = new SimpleStringProperty();
|
||||||
this.algSpeedProperty = new SimpleStringProperty();
|
this.algSpeedProperty = new SimpleStringProperty();
|
||||||
this.showSolvedPath = new SimpleBooleanProperty();
|
this.disclaimer = new SimpleBooleanProperty();
|
||||||
this.solvingProgressProperty = new SimpleDoubleProperty();
|
this.expanded = 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();
|
||||||
@ -50,6 +51,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() {
|
||||||
@ -75,35 +78,40 @@ 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 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();
|
||||||
BoardNode solved = solver.traverse(this.current.board);
|
BoardNode solved = solver.traverse(this.current.board);
|
||||||
|
Date end = new Date();
|
||||||
|
long runtime = end.getTime() - start.getTime();
|
||||||
|
this.algSpeedProperty.setValue(String.valueOf(runtime));
|
||||||
|
|
||||||
currentBoardProperty.setValue(solved.board.toString());
|
currentBoardProperty.setValue(solved.board.toString());
|
||||||
BoardNode root = solved;
|
BoardNode root = solved;
|
||||||
while (root.parent != null) {
|
while (root.parent != null) {
|
||||||
root = root.parent;
|
root = root.parent;
|
||||||
}
|
}
|
||||||
this.solvedRootNode = rebuildTree(root);
|
this.solvedRootNode = rebuildTree(root, this.expanded.getValue());
|
||||||
|
this.solvedRootBoardNode = root;
|
||||||
|
this.current = solved;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateDisplay() {
|
||||||
|
TreeItem<BoardNode> newRoot = null;
|
||||||
|
newRoot = this.rebuildTree(this.solvedRootBoardNode, this.expanded.getValue());
|
||||||
|
this.solvedRootNode = newRoot;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TreeItem<BoardNode> getSolvedRootNode() {
|
public TreeItem<BoardNode> getSolvedRootNode() {
|
||||||
return solvedRootNode;
|
return solvedRootNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ObservableList<BoardNode> getSpanningTree() {
|
|
||||||
return spanningTreeProperty.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
public ObjectProperty<ObservableList<BoardNode>> spanningTreeProperty() {
|
|
||||||
return spanningTreeProperty;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BooleanProperty DFSProperty() {
|
public BooleanProperty DFSProperty() {
|
||||||
return DFS;
|
return DFS;
|
||||||
}
|
}
|
||||||
@ -128,17 +136,60 @@ public class MainViewModel {
|
|||||||
return genBoardErr;
|
return genBoardErr;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TreeItem<BoardNode> rebuildTree(BoardNode root) {
|
private TreeItem<BoardNode> rebuildTree(BoardNode root, boolean expanded) {
|
||||||
|
if (this.selectedAlg != null) {
|
||||||
|
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);
|
||||||
|
|
||||||
// Recursively create TreeItems for child nodes
|
|
||||||
for (BoardNode child : root.children) {
|
for (BoardNode child : root.children) {
|
||||||
TreeItem<BoardNode> childItem = rebuildTree(child);
|
TreeItem<BoardNode> childItem = rebuildTree(child, expanded);
|
||||||
treeItem.getChildren().add(childItem);
|
treeItem.getChildren().add(childItem);
|
||||||
}
|
}
|
||||||
|
treeItem.setExpanded(expanded);
|
||||||
|
|
||||||
return treeItem;
|
return treeItem;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return new TreeItem<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean getExpanded() {
|
||||||
|
return expanded.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public BooleanProperty expandedProperty() {
|
||||||
|
return expanded;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAlgSpeedProperty() {
|
||||||
|
return algSpeedProperty.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public StringProperty algSpeedProperty() {
|
||||||
|
return algSpeedProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDisclaimer() {
|
||||||
|
return disclaimer.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public BooleanProperty disclaimerProperty() {
|
||||||
|
return disclaimer;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,11 +2,8 @@
|
|||||||
|
|
||||||
<?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.ProgressBar?>
|
|
||||||
<?import javafx.scene.control.ToggleButton?>
|
<?import javafx.scene.control.ToggleButton?>
|
||||||
<?import javafx.scene.control.TreeView?>
|
<?import javafx.scene.control.TreeView?>
|
||||||
<?import javafx.scene.layout.Pane?>
|
<?import javafx.scene.layout.Pane?>
|
||||||
@ -14,30 +11,10 @@
|
|||||||
|
|
||||||
<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="showSolvedPath" layoutX="545.0" layoutY="34.0" mnemonicParsing="false" onAction="#showSolvedPath" text="Show Solved Path" />
|
<ToggleButton fx:id="expanded" layoutX="547.0" layoutY="33.0" mnemonicParsing="false" onAction="#showSolvedPath" text="Expand Directory" />
|
||||||
<ProgressBar id="solve_board_progress" fx:id="solving_prog" layoutX="500.0" layoutY="538.0" mouseTransparent="true" prefWidth="200.0" progress="0.0" />
|
|
||||||
<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" />
|
||||||
<Label fx:id="current_board" alignment="CENTER" layoutX="104.0" layoutY="129.0" prefHeight="184.0" prefWidth="134.0" text="0 1 2 8 0 4 7 6 5" textAlignment="JUSTIFY" wrapText="true">
|
<Label fx:id="current_board" alignment="CENTER" layoutX="104.0" layoutY="129.0" prefHeight="184.0" prefWidth="134.0" textAlignment="JUSTIFY" wrapText="true">
|
||||||
<font>
|
<font>
|
||||||
<Font size="41.0" />
|
<Font size="41.0" />
|
||||||
</font>
|
</font>
|
||||||
@ -52,7 +29,7 @@
|
|||||||
</items>
|
</items>
|
||||||
</MenuButton>
|
</MenuButton>
|
||||||
<Label layoutX="728.0" layoutY="507.0" text="Algorithm Speed (ms): " />
|
<Label layoutX="728.0" layoutY="507.0" text="Algorithm Speed (ms): " />
|
||||||
<Label layoutX="849.0" layoutY="507.0" text="0" />
|
<Label fx:id="alg_speed" layoutX="849.0" layoutY="507.0" text="0" />
|
||||||
<Label fx:id="gen_board_err" layoutX="37.0" layoutY="525.0" prefHeight="26.0" prefWidth="325.0" text="You must generate a board before solving" textFill="RED" visible="false">
|
<Label fx:id="gen_board_err" layoutX="37.0" layoutY="525.0" prefHeight="26.0" prefWidth="325.0" text="You must generate a board before solving" textFill="RED" visible="false">
|
||||||
<font>
|
<font>
|
||||||
<Font size="17.0" />
|
<Font size="17.0" />
|
||||||
@ -63,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>
|
||||||
|
Loading…
Reference in New Issue
Block a user