For this assignment, you will implement a solver for the Hua Rong Dao sliding puzzle. Hua Rong Dao is a sliding puzzle that is popular in China. Check out the following page for some background story on the puzzle and an English description of the rules.
http://chinesepuzzles.org/huarong-pass-sliding-block-puzzle/
The puzzle board is four spaces wide and five spaces tall. We will consider the variants of this puzzle with ten pieces. There are four kinds of pieces:
Once we place the ten pieces on the board, two empty spaces should remain.
Look at the most classic initial configuration of this puzzle below. (Don't worry about the Chinese characters. They are not crucial for understanding the puzzle.) In this configuration, one 1x2 piece is horizontal, and the other four 1x2 pieces are vertical.
The goal is to move the pieces until the 2x2 piece is above the bottom opening (i.e. helping Cao Cao escape through the Hua Rong Dao/Pass). You may move each piece horizontally or vertically only into an available space. You are not allowed to rotate any piece or move it diagonally.
You can find many other initial configurations for this puzzle on this webpage. Scroll down to see 32 configurations. The first link below each configuration opens another page where you can play the puzzle and see the solution.
Counting Moves: We will count moves differently from how the website does it. On the website, consecutive moves of a single piece count as one move. However, we will count the consecutive moves separately. Suppose that I move a single piece by two spaces to the right. We will count it as two moves, whereas the website counts it as one move. For the most classic initial configuration, the optimal solution takes 81 moves on the website, whereas it takes 116 moves based on our counting rule.
You will submit a file named hrd.py, which contains your implementation of a program that uses Depth-First Search and A* Search to solve a Huarongdao puzzle.
Your program must use python3 and run on the teach.cs server (where we run our auto-testing script).
We will test your program using several initial configurations of various difficulties. For each initial configuration, we will run the following two commands:
python3 hrd.py --algo astar --inputfile <input file> --outputfile <output file> python3 hrd.py --algo dfs --inputfile <input file> --outputfile <output file>
Each command specifies the search algorithm, one plain-text input file, and one plain-text output file containing the solution found by the search algorithm.
For example, if we run the following commands for an input file hrd5.txt:
python3 hrd.py --algo astar --inputfile hrd5.txt --outputfile hrd5sol_astar.txt
python3 hrd.py --algo dfs --inputfile hrd5.txt --outputfile hrd5sol_dfs.txt
The DFS solution will be found in hrd5sol_dfs.txt, and the A* solution will be found in hrd5sol_astar.txt.
Our tests will verify that
In the input and output files, we will represent each state in the following format.
Here is an example of a state.
^^^^
vvvv
22..
11<>
1122
Each input file contains one state, representing a puzzle's initial state.
Each output file contains a sequence of states. The first state in the sequence is the initial configuration of the puzzle. The last state is a goal state. There is one empty line between any two consecutive states.
Here are examples of an input file and an output file. The output file is an optimal solution for the puzzle found by A* search.
A* search requires an admissible heuristic function to find an optimal solution. You will implement a basic heuristic function (the Manhattan distance heuristic) as part of your A* search implementation. In addition, you will propose an advanced heuristic function that is better than the Manhattan distance heuristic.
(1) The Manhattan distance heuristic function
The simplest admissible heuristic function for this problem is the Manhattan distance heuristic function. Suppose that we relax this problem such that the pieces can overlap. Given this relaxation, we can solve the puzzle by moving the 2x2 piece over the other pieces until it is at the goal. The cost of this solution would be the Manhattan distance between the 2x2 piece's current location and the bottom opening. For example, for the most classic initial configuration, the heuristic value is 3.
Manhattan Distance: The name "Manhattan distance" comes from how people navigate Manhattan in New York City. As you may know, Manhattan is a grid. To get from one place to another in Manhattan, you need to travel either horizontally or vertically through the grid. The Manhattan distance between two locations is the sum of the horizontal and vertical distances between the two locations.
(2) Your advanced heuristic function
Your next task is to propose an advanced heuristic function better than the Manhattan distance heuristic function. Your advanced heuristic function should satisfy the two requirements below.
Submit a one-page file named advanced.pdf. (We will only grade the first page if your file is longer than one page.) In this file, answer the three questions below.
You should submit two files on MarkUs.
The mark breakdown is as follows:
We will test your program on 15 puzzles. There are 5 easy puzzles, 5 medium puzzles, and 5 hard puzzles. For each puzzle and search algorithm combination, your program must terminate in under 2 minutes 5 minutes to earn marks. This time limit is quite generous. For your information, our A* search implementation has the following runtimes:
Optimize your program and test your program on the teach.cs server to ensure that it terminates under the time limit.
We will provide 3 of the 15 puzzles to you on MarkUs. We highly recommend that you test the correctness and efficiency of your program using these 3 puzzles. In addition, we recommend you create other test cases using the other puzzle configurations on the Wikipedia page.
We have provided some starter code on Markus in a file named hrd_starter.py. This file is provided as is, and we do NOT guarantee that the code is correct or bug-free. You can feel free to copy or modify the starter code in any way you like.
The file has the following components:
This assignment requires you to write a substantial amount of code. Also, your code needs to be reasonably efficient to terminate within the time limits. Therefore, we strongly suggest you write the program incrementally --- start by writing helper functions and testing them individually.
Here are some suggested steps to tackle this assignment. Good luck!
Write a few classes to represent the elements of the puzzle.
Write some useful helper functions before writing the main search functions.
Write the main search functions.