Removed unused imports and provided JavaDocs.
This commit is contained in:
parent
7242c5985f
commit
7a81bd5dc8
@ -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);
|
||||||
|
Loading…
Reference in New Issue
Block a user