Mid fix of Heuristics and UCS/BFS.

This commit is contained in:
Jonathan Turner 2024-02-10 08:36:18 -05:00
parent 261e49b329
commit 1b9af7a6ad
7 changed files with 204 additions and 67 deletions

View File

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="19" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="19" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />

View File

@ -4,8 +4,10 @@ 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.solving_algorithms.BFS; import Assignments.A1.solving_algorithms.BFS;
import Assignments.A1.solving_algorithms.DFS; import Assignments.A1.solving_algorithms.DFS;
import Assignments.A1.solving_algorithms.UCS;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
@ -19,6 +21,10 @@ import java.util.concurrent.TimeUnit;
* 3 4 5 * 3 4 5
* 6 7 8 * 6 7 8
* *
* 1 2 3
* 8 0 4
* 7 6 5
*
* The values will be specified with an int. Must be between (inclusive) 1-8. * The values will be specified with an int. Must be between (inclusive) 1-8.
* The empty spot will be represented with null. * The empty spot will be represented with null.
*/ */
@ -27,10 +33,12 @@ public class Driver {
public static void main(String[] args) { public static void main(String[] args) {
ArrayList<Long> timer = new ArrayList<>(); ArrayList<Long> timer = new ArrayList<>();
int successes = 0; int successes = 0;
for (int run = 0; run < 100; run++) { int runs = 1;
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();
Date start = new Date(); Date start = new Date();
Board result = solver.traverse(board); Board result = solver.traverse(board);
Date end = new Date(); Date end = new Date();
@ -38,6 +46,15 @@ public class Driver {
long runtime = end.getTime() - start.getTime(); long runtime = end.getTime() - start.getTime();
timer.add(runtime); timer.add(runtime);
successes++; successes++;
String test = "END";
System.out.println(solver.test.heuristic);
BoardNode curr = solver.test;
while (curr != null) {
test = curr.board.toString() + " -> " + test;
curr = curr.parent;
}
System.out.println(test);
System.out.println();
} }
} }
long total = 0; long total = 0;
@ -47,7 +64,14 @@ public class Driver {
} }
long average = total / (long) timer.size(); long average = total / (long) timer.size();
System.out.println("Average Runtime: " + average); System.out.println("Average Runtime: " + average);
System.out.println("Number of successful solves: " + successes + "/100"); System.out.println("Number of successful solves: " + successes + "/" + runs);
} }
// 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);
// }
} }

View File

@ -0,0 +1,79 @@
package Assignments.A1.models;
public class BoardNode implements Comparable<BoardNode> {
public BoardNode parent;
public int heuristic;
public int cost;
public Board board;
public List<String>
public BoardNode(Board board, BoardNode parent) {
this.board = board;
this.parent = parent;
this.heuristic = this.getHeuristic();
this.cost = this.getActualCost();
}
private int getActualCost() {
if (this.parent != null) {
return this.parent.cost + 1;
} else {
return 0;
}
}
private int getHeuristic() {
int cost = 0;
for (int i = 0; i < 9; i++) {
int currValue = board.getPiece(i);
int idealIndex = idealIndex(currValue);
int currRow = i / 3;
int currCol = i % 3;
int idealRow = idealIndex / 3;
int idealCol = idealIndex % 3;
cost += Math.abs(idealRow - currRow) + Math.abs(idealCol - currCol);
}
return cost;
}
private int idealIndex(int value) {
int result = -1;
switch (value) {
case 0:
result = 4;
break;
case 1:
result = 0;
break;
case 2:
result = 1;
break;
case 3:
result = 2;
break;
case 4:
result = 5;
break;
case 5:
result = 8;
break;
case 6:
result = 7;
break;
case 7:
result = 6;
break;
case 8:
result = 3;
break;
}
return result;
}
@Override
public int compareTo(BoardNode o) { // BFS
return Integer.compare(this.heuristic, o.heuristic);
}
}

View File

@ -1,4 +1,44 @@
package Assignments.A1.solving_algorithms; package Assignments.A1.solving_algorithms;
import Assignments.A1.models.Board;
import Assignments.A1.models.BoardNode;
import Assignments.A1.models.Move;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.PriorityQueue;
public class AStar { public class AStar {
private final Board solved = new Board();
private final HashSet<Board> visited = new HashSet<>();
public Board 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);
boards.add(new BoardNode(child, node));
}
}
return node.board;
}
}
class AStarPriority implements Comparator<BoardNode> {
@Override
public int compare(BoardNode o1, BoardNode o2) {
return Integer.compare(o2.cost, o1.cost);
}
} }

View File

@ -1,8 +1,10 @@
package Assignments.A1.solving_algorithms; package Assignments.A1.solving_algorithms;
import Assignments.A1.models.Board; import Assignments.A1.models.Board;
import Assignments.A1.models.BoardNode;
import Assignments.A1.models.Move; import Assignments.A1.models.Move;
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;
@ -11,13 +13,16 @@ public class BFS {
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<>();
public BoardNode test;
public Board traverse(Board root) { public Board traverse(Board root) {
PriorityQueue<BoardNode> boards = new PriorityQueue<>(); PriorityQueue<BoardNode> boards = new PriorityQueue<>(new BFSPriority());
boards.add(new BoardNode(root)); boards.add(new BoardNode(root, null));
BoardNode node = null; BoardNode node = null;
while (!boards.isEmpty()) { Board current = new Board(root);
while (!current.equals(solved)) {
node = boards.poll(); node = boards.poll();
current = node.board;
if (visited.contains(node.board)) { if (visited.contains(node.board)) {
continue; continue;
} }
@ -26,69 +31,18 @@ public class BFS {
for (Move move : children) { for (Move move : children) {
Board child = new Board(node.board); Board child = new Board(node.board);
child.swap(move); child.swap(move);
boards.add(new BoardNode(child)); boards.add(new BoardNode(child, node));
} }
} }
test = node;
return node.board; return node.board;
} }
} }
class BoardNode implements Comparable<BoardNode> { class BFSPriority implements Comparator<BoardNode> {
public int heuristic;
public Board board;
public BoardNode(Board board) {
this.heuristic = this.getCost(board);
this.board = board;
}
private int getCost(Board board) {
int cost = 0;
for (int i = 0; i < 9; i++) {
int currValue = board.getPiece(i);
cost += Math.abs(i - this.idealIndex(i));
}
return cost;
}
private int idealIndex(int value) {
int result = -1;
switch(value) {
case 0:
result = 4;
break;
case 1:
result = 0;
break;
case 2:
result = 1;
break;
case 3:
result = 2;
break;
case 4:
result = 5;
break;
case 5:
result = 8;
break;
case 6:
result = 7;
break;
case 7:
result = 6;
break;
case 8:
result = 3;
break;
}
return result;
}
@Override @Override
public int compareTo(BoardNode o) { public int compare(BoardNode o1, BoardNode o2) {
return Integer.compare(this.heuristic, o.heuristic); return Integer.compare(o1.heuristic, o2.heuristic);
} }
} }

View File

@ -11,9 +11,8 @@ import java.util.*;
public class DFS { public class DFS {
private int counter = 0;
private final Board solved = new Board(); private final Board solved = new Board();
private List<String> tried = new ArrayList<>(); private final List<String> tried = new ArrayList<>();
/* Commented out for future reference. */ /* Commented out for future reference. */
// public Board dfs(Board root, int depth, ArrayList<String> visited) { // public Board dfs(Board root, int depth, ArrayList<String> visited) {
@ -44,8 +43,7 @@ public class DFS {
// return null; // return null;
// } // }
public Board dfs(Board root, int depth) { public Board traverse(Board root, int depth) {
counter++;
Stack<Board> stack = new Stack<>(); Stack<Board> stack = new Stack<>();
stack.push(root); stack.push(root);

View File

@ -1,4 +1,47 @@
package Assignments.A1.solving_algorithms; package Assignments.A1.solving_algorithms;
import Assignments.A1.models.Board;
import Assignments.A1.models.BoardNode;
import Assignments.A1.models.Move;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.PriorityQueue;
public class UCS { public class UCS {
private final Board solved = new Board();
private final HashSet<Board> visited = new HashSet<>();
public BoardNode test = null;
public Board 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);
boards.add(new BoardNode(child, node));
}
}
test = node;
return node.board;
}
}
class UCSPriority implements Comparator<BoardNode> {
@Override
public int compare(BoardNode o1, BoardNode o2) {
return Integer.compare(o1.cost, o2.cost);
}
} }