Compare commits

...

8 Commits

28 changed files with 584 additions and 76 deletions

13
.idea/compiler.xml Normal file
View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="CS3642-Artificial_Intelligence" />
</profile>
</annotationProcessing>
</component>
</project>

7
.idea/encodings.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

20
.idea/jarRepositories.xml Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>

View File

@ -1,5 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<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" />
</component> </component>

View File

@ -1,12 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4"> <module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true"> <component name="AdditionalModuleElements">
<exclude-output /> <content url="file://$MODULE_DIR$" dumb="true">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/src/main/java/Assignments/A1/results" />
</content> </content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="junit" level="project" />
</component> </component>
</module> </module>

45
pom.xml Normal file
View File

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.jturn.cs3642</groupId>
<artifactId>CS3642-Artificial_Intelligence</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>12.0.2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>20.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
<mainClass>edu.jturn.cs3642.CS3642-Artificial_Intelligence/Assignments.A1.GUIDriver</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,52 +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.solving_algorithms.DFS;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/**
* Board will be used to save locations in a 2D array.
*
* The design of the board locations will be numbered like so:
* 0 1 2
* 3 4 5
* 6 7 8
*
* 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;
for (int run = 0; run < 100; run++) {
Board board = BoardGenerator.generateBoard();
DFS solver = new DFS();
Date start = new Date();
Board result = solver.dfs(board,0);
Date end = new Date();
if (result != null) {
System.out.println("solved");
long runtime = end.getTime() - start.getTime();
timer.add(runtime);
successes++;
}
}
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 + "/100");
}
}

View File

@ -1,4 +0,0 @@
package Assignments.A1.solving_algorithms;
public class AStar {
}

View File

@ -1,4 +0,0 @@
package Assignments.A1.solving_algorithms;
public class BFS {
}

View File

@ -1,4 +0,0 @@
package Assignments.A1.solving_algorithms;
public class UCS {
}

View File

@ -0,0 +1,154 @@
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.AStar;
import Assignments.A1.solving_algorithms.BFS;
import Assignments.A1.solving_algorithms.UCS;
import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.PriorityQueue;
/**
* 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/AStar.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();
}
}
}

View File

@ -0,0 +1,87 @@
package Assignments.A1.models;
import java.util.ArrayList;
import java.util.List;
public class BoardNode implements Comparable<BoardNode> {
public BoardNode parent;
public int heuristic, cost, expected;
public Board board;
public List<BoardNode> children;
public BoardNode(Board board, BoardNode parent) {
this.board = board;
this.parent = parent;
this.heuristic = this.getHeuristic();
this.cost = this.getActualCost();
this.expected = this.cost + this.expected;
this.children = new ArrayList<>();
}
private int getActualCost() {
if (this.parent != null) {
return this.parent.cost + 10;
} else {
return 0;
}
}
public void addChild(BoardNode node) {
children.add(node);
}
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

@ -0,0 +1,103 @@
Run from #e13806a1: (Time in Milliseconds)
Run 1: 279
Run 2: 280
Run 3: 159
Run 4: 178
Run 5: 165
Run 6: 249
Run 7: 229
Run 8: 138
Run 9: 147
Run 10: 135
Run 11: 151
Run 12: 130
Run 13: 140
Run 14: 132
Run 15: 142
Run 16: 131
Run 17: 146
Run 18: 139
Run 19: 145
Run 20: 143
Run 21: 150
Run 22: 154
Run 23: 146
Run 24: 148
Run 25: 144
Run 26: 142
Run 27: 148
Run 28: 152
Run 29: 128
Run 30: 140
Run 31: 130
Run 32: 160
Run 33: 131
Run 34: 143
Run 35: 134
Run 36: 147
Run 37: 195
Run 38: 204
Run 39: 142
Run 40: 151
Run 41: 149
Run 42: 133
Run 43: 143
Run 44: 148
Run 45: 146
Run 46: 143
Run 47: 129
Run 48: 143
Run 49: 151
Run 50: 141
Run 51: 144
Run 52: 133
Run 53: 145
Run 54: 140
Run 55: 140
Run 56: 147
Run 57: 131
Run 58: 145
Run 59: 139
Run 60: 146
Run 61: 147
Run 62: 126
Run 63: 151
Run 64: 131
Run 65: 141
Run 66: 146
Run 67: 133
Run 68: 143
Run 69: 130
Run 70: 149
Run 71: 145
Run 72: 140
Run 73: 145
Run 74: 130
Run 75: 140
Run 76: 159
Run 77: 148
Run 78: 150
Run 79: 127
Run 80: 146
Run 81: 144
Run 82: 140
Run 83: 140
Run 84: 129
Run 85: 145
Run 86: 137
Run 87: 145
Run 88: 144
Run 89: 127
Run 90: 140
Run 91: 131
Run 92: 153
Run 93: 137
Run 94: 133
Run 95: 144
Run 96: 128
Run 97: 149
Run 98: 150
Run 99: 145
Run 100: 144
Average Runtime: 148
Number of successful solves: 100/100

View File

@ -0,0 +1,46 @@
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 {
private final Board solved = new Board();
private final HashSet<Board> visited = new HashSet<>();
public BoardNode traverse(Board root) {
PriorityQueue<BoardNode> boards = new PriorityQueue<>(new AStarPriority());
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 AStarPriority implements Comparator<BoardNode> {
@Override
public int compare(BoardNode o1, BoardNode o2) {
return Integer.compare(o1.expected, o2.expected);
}
}

View File

@ -0,0 +1,49 @@
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 BFS {
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);
}
}

View File

@ -5,15 +5,13 @@ import Assignments.A1.models.BoardGenerator;
import Assignments.A1.models.Move; import Assignments.A1.models.Move;
import Assignments.A1.models.Pair; import Assignments.A1.models.Pair;
import Assignments.A1.resources.Parameters; import Assignments.A1.resources.Parameters;
import org.junit.experimental.theories.internal.ParameterizedAssertionError;
import java.util.*; 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 +42,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

@ -0,0 +1,46 @@
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 {
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);
}
}

View File

View File

View File

View File