If we use the same algorithm as used in the last project for depth
first search and breadth first search but use a priority queue as the
storage structure, we get a new kind of search which can take into
account the promise of unsearched nodes for reaching a solution. This
new algorithm is called "Best First Search" (which is not generally
abbreviated for obvious reasons). Best First Search requires a
The algorithm for Best First Search is: insert the entrance location into the priority queue. Then, until the priority queue is empty or the exit is found, delete the minimum location from the priority queue, find its children, and insert each child location onto the priority queue as long as it has not already been visited. That should sound very familiar.
Your heap implementation should also automatically resize its array if more items are insterted than can fit into the array. Because of this, the 20x20 limit on maze size from the previous assignment is removed!
Note that on this assignment it may be necessary for your MazeRunner to refer to SquareMazeNode and SquareMaze explicitly in order to calculate values for the heuristic. Try to restrict this to as small a portion of your code as possible (in particular, your compator class). Elsewhere, refer to MazeNode and Maze.
Here is the heuristic function for rating the value of a SquareMazeNode which you should implement and use as the innards of your comparator class:
value(node) = (|nodex - exitx| +
|nodey - exity|) * maze_height * maze_width +
nodex * maze_height + nodey
Note that this is Manhattan distance plus some extra stuff. The extra
stuff ensures that every priority value is unique.
Teams
You may work on a team of at most two on this assignment. You may choose how to divide the work under three conditions: first, document each team member's effort in the README file; second, work together and both understand your answers to the questions below; and third, understand at least at a high level how your team member's code is structured and how it works. Remember to test your team's code as a whole to make sure that your portions work properly! Also, be aware that except in extreme cases when you notify us in advance of the deadline, all team members will receive the same grade for the project.
We strongly encourage working in teams of two!. However, if you end up working alone, you do not have to implement dynamic resizing of the heap.
What we've provided
As a starting point we've provided a few files:
class SquareMazeNodeCompare { bool operator() (const SquareMazeNode * larg, const SquareMazeNode * rarg); };
Note that the word template does not appear here; SquareMazeNodeCompare does not need to be (and probably shouldn't be) a templated type.
make
will use this to build your program and the
sort program. You will need to modify the makefile to add any new
files you create./cse/courses/cse326/00wi/project2
Input files
SquareMaze reads a maze file from the standard input. The file format
is a picture of the maze. There is also some header information at the
front that gives the dimensions of the maze. In the maze, '-
' and '|
'
are used for walls, '
' for rooms and unblocked passages, '+
' for the
corners of the rooms, '*
' for the entrance, and 'X
' for the exit. For
example:
7 5 +-+-+-+-+-+-+-+ |*| | | | | + +-+ + + +-+ + | | | | + + + +-+ +-+ + | | | | | + +-+-+ +-+-+ + | | | +-+ + +-+-+ +-+ | | X| +-+-+-+-+-+-+-+
There are some sample input files in the directory project2/inputs that you can use to test your program.
Outputting a solution
The program should spit out one solution path calculated by Best First
Search. The path should be given on a single line and
should consist of the printed locations (using the builtin print
method) separated by spaces.
The path should have a line before it saying "Best First".
For example, the path output for the maze
above might be:
Best First (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (3,2) (4,2) (4,1) (5,1) (6,1) (6,2) (6,3) (5,3) (5,4) (6,4)The solution path looks like:
+-+-+-+-+-+-+-+ |*| | | | | +.+-+ + + +-+ + |. | |.....| +.+ + +-+.+-+.+ |.| |... |.| +.+-+-+.+-+-+.+ |.......| ...| +-+ + +-+-+.+-+ | | ..X| +-+-+-+-+-+-+-+
Handing in the code
You are required to hand in your project2 directory with your implementation of the MazeRunner. Included in this directory should be at least the files containing your MazeRunner, your heap implementation, your solution to the sort question below, and your comparator class, along with your README and any additional files you used or changed.
Note: of the files we provide to you, you should need to change only Makefile and runmaze.cpp (of course, you may create whatever files you wish). If you want to change any other files in the project, contact one of us first!
Turn your files in electronically using turnin
:
% cd ~/326stuff/myproj2dir % make clean % cd ../ % turnin -c cse326=AA -p p2 myproj2dir
The README
Your README file should document your submission (though it does not by any means replace comments in the code!). It should contain the following:
Maze generator
The shell script mazegen generates random mazes to standard output. For example, invoking:
% ./mazegen -d20x12 > myMaze.txtwill put a rectangular maze with 20 columns and 12 rows into myMaze.txt. The -m option asks for a maze with multiple paths from start to finish, rather than a maze with a single solution. Type mazegen -h to see all its options.
Sample maze solver
The executable bestfirstsolve is our sample solution for this assignment. Use it to see what we expect from you.
% ./bin/bestfirstsolve < mazefile > mazesolnwill solve mazefile and put the paths into the file mazesoln
Viewing solutions
The sample code supplied for your programs has some extra flags that will display a solution. It will go into "Show Solution" mode if you pass it the flags -v -s, like this:
% cat mazefile mazesoln | runmaze -v -sThis will display the maze and solution paths from above in a window.
The cat mazefile mazesoln part is important - the show solution mode expects to see a maze, followed by one or more solutions.
Displaying progress towards a solution
You can run the 'runmaze' program with the supplied code with the -v flag to visualize its progress. Try this with the random solver so you can see how it works (you have to call setVisitationState in MazeNode to make the colors change).
Utility scripts
We have also provided two scripts, called maze.sh and soln.sh. We do not support these, but they may make it easier for you to run your program and compare it with our sample solution. Run maze.sh using the same parameters as for mazegen. This will generate a maze and run your program on it, showing results using pathshow. Then run soln.sh to run our bestfirstsolve on the maze, displaying the results using pathshow.
Extras (bonus)