Modified project files and structure to fit with Maven for JavaFX support.

This commit is contained in:
Jonathan Turner 2024-02-11 00:37:10 -05:00
parent 75c215e4ec
commit 5c2fde6c69
21 changed files with 247 additions and 129 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,4 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<output url="file://$PROJECT_DIR$/out" />
</component>

View File

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

17
pom.xml Normal file
View File

@ -0,0 +1,17 @@
<?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>
</project>

View File

@ -1,115 +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.BFS;
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();
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);
String result = printTotalHierarchy(node);
System.out.println(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 String printTotalHierarchy(BoardNode root, int depth, boolean hasMoreChildren) {
String result = "";
for (int i = 0; i < depth - 1; i++) {
result += "";
}
if (hasMoreChildren) {
result += "├── " + root.board.toString() + System.lineSeparator();
} else {
result += "└── " + root.board.toString() + System.lineSeparator();
}
for (int i = 0; i < root.children.size(); i++) {
if (i == root.children.size() - 1) {
result += printTotalHierarchy(root.children.get(i), depth+1, false);
} else {
result += printTotalHierarchy(root.children.get(i), depth+1, true);
}
}
return result;
}
public static String printTotalHierarchy(BoardNode solved) {
BoardNode root = null;
while (solved.parent != null) {
solved = solved.parent;
}
root = solved;
String result = root.board.toString() + System.lineSeparator();
if (root.children.size() == 1) {
result += printTotalHierarchy(root.children.get(0), 1, false);
} else {
for (int i = 0; i < root.children.size(); i++) {
if (i == root.children.size() - 1) {
result += printTotalHierarchy(root.children.get(i), 1, false);
} else {
result += printTotalHierarchy(root.children.get(i), 1, true);
}
}
}
return result;
}
}

View File

@ -0,0 +1,167 @@
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.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) {
File directory = new File("src/Assignments/A1/results");
if (!directory.exists()) {
directory.mkdirs();
System.out.println("Creating Directory: results");
}
File output = new File(directory, name + ".txt");
try {
if (!output.createNewFile()) {
System.out.println("File Already Exists: " + name + ".txt");
} else {
System.out.println("File Created: " + name + ".txt");
}
} catch (IOException e) {
System.err.println("Error creating file: " + e.getMessage());
}
try (FileWriter out = new FileWriter(output)) {
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(output);
}
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

@ -6,8 +6,7 @@ import java.util.List;
public class BoardNode implements Comparable<BoardNode> {
public BoardNode parent;
public int heuristic;
public int cost;
public int heuristic, cost, expected;
public Board board;
public List<BoardNode> children;
@ -16,12 +15,13 @@ public class BoardNode implements Comparable<BoardNode> {
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 + 1;
return this.parent.cost + 10;
} else {
return 0;
}

View File

@ -14,7 +14,7 @@ public class AStar {
private final HashSet<Board> visited = new HashSet<>();
public BoardNode traverse(Board root) {
PriorityQueue<BoardNode> boards = new PriorityQueue<>(new UCSPriority());
PriorityQueue<BoardNode> boards = new PriorityQueue<>(new AStarPriority());
boards.add(new BoardNode(root, null));
BoardNode node = null;
Board current = new Board(root);
@ -29,7 +29,9 @@ public class AStar {
for (Move move : children) {
Board child = new Board(node.board);
child.swap(move);
boards.add(new BoardNode(child, node));
BoardNode childNode = new BoardNode(child, node);
boards.add(childNode);
node.addChild(childNode);
}
}
return node;
@ -39,6 +41,6 @@ public class AStar {
class AStarPriority implements Comparator<BoardNode> {
@Override
public int compare(BoardNode o1, BoardNode o2) {
return Integer.compare(o2.cost, o1.cost);
return Integer.compare(o1.expected, o2.expected);
}
}

View File

@ -5,7 +5,6 @@ import Assignments.A1.models.BoardGenerator;
import Assignments.A1.models.Move;
import Assignments.A1.models.Pair;
import Assignments.A1.resources.Parameters;
import org.junit.experimental.theories.internal.ParameterizedAssertionError;
import java.util.*;

View File

@ -29,7 +29,9 @@ public class UCS {
for (Move move : children) {
Board child = new Board(node.board);
child.swap(move);
boards.add(new BoardNode(child, node));
BoardNode childNode = new BoardNode(child, node);
boards.add(childNode);
node.addChild(childNode);
}
}
return node;