I’ve been meaning to post a sample solution to Assignment 01. A student reminded me yesterday during office hours, so I packaged up the solution I wrote earlier this semester and you can now download it here: assignment01-sample-solution.tar.gz.
Some notes, in no particular order:
- There is code for both A* and IDDFS. I wrote the latter first, then added A* support. The Solver class included uses the A* implementation, but it’s a one-line change.
- The heuristic that A* uses is inadmissible. That’s fine, as the assignment wanted a solution, not an optimal solution.
- A* explores the frontier of the search tree, and keeps a map of the
knownCost
s to reach nodes it finds. It uses this map for two things: to determine if a node has never been seen before, or to determine if the new path to the node is cheaper than any previously seen path. If either of those conditions holds, it updates its knowledge of the path to the node. - Paths are stored in a map called
predecessor
; it tracks how to get to a given board by storing its predecessor, which is a pair of the prior board, and the move that transforms that board to the successor. When A* finds the solution, the path to it is recovered by walking back through the relevant entries in this map, adding the moves along the path to a list (then reversing the list, since they’re in reverse order). - The code is set up using Maven, which is the de facto standard Java build and packaging tool. Eclipse has plugins to support it, and IntelliJ supports it out of the box. If you load it up in your IDE, things will go more smoothly if you make sure your IDE recognizes it as a Maven project.
- The A* solver depends upon a third-party JAR, available at http://www.javatuples.org/ (or that Maven will download for you).
- From the root directory of the project, you can use
mvn package
to build theassignment01-1.0.jar
file, then usejava -cp assignment01-1.0.jar Validator ...
to run the validator, orjava -cp assignment01-1.0.jar:/path/to/javatuples-1.2.jar Solver ...
to run the solver. You can also compile the.java
files separately usingjavac
, but you’ll need the javatuples jar on your classpath when compiling the Solver. - The project uses JUnit4 to drive test cases, which you can see in
src/main/test
. - Looking back it at now, there are some obviously kludgy things I did in this project (e.g., should have used an
ArrayList
andtoArray()
when reading in the board; not sure why I created anEnum
, etc.). Live and learn.