In this assignment you will be implementing the A* search algorithm for solving 2 different search problems - EightPuzzle and LightsOut. Your single implementation of the algorithm will work for both problems, by properly instantiating an SearchState class for each of them. You will see both the power of A* as an algorithm that applies to arbitrary discrete state spaces, and the power of heuristics to speed up search.
To get started on this assignment, download the mp08.zip
from the course website. The template contains the following files and
directories:
search.py. You will edit and submit this - your
implementation of A* goes here.
state.py. You will edit and submit this - your
implementation of each problem’s State goes here.
utils.py. Some utilities we provide.
main.py. You will run this file to test your
code.
data/eight_puzzle. Text files containing example
problems for the eight puzzle task
data/lights_out/visible.txt. Text file containing
example problems for the lights out task
Please ONLY submit search.py and
state.py.
For each of the remaining parts of the assignment you will find TODOs
in search.py and state.py where you need to
write your own code. For example, for part III you will find TODOs
marked # TODO(III). We’ve provided many comments and
instructions in the code under those TODOs.
There are 2 “TODO(III)” in search.py.
Your first task is to implement a generic A* search algorithm in the
method astar_search(starting_state) in
search.py. Your implementation should:
Notice that the input to astar_search is just a starting
state. You may be wondering:
Now you should look at state.py, there are 3 “TODO(III)”
in state.py. These questions should be answered by the
comments in the code under those TODOs. You should first read through
our SearchState class, then look at an instantiation of
this class called LightsOutState and fill in the missing
functions.
LightsOutState.generate_successors(self)
generate_successors(self) function should choose different
cells to toggle based on self.cross_pattern.LightsOutState.calculate_heuristic(self) as
described in the template.LightsOutState.__lt__(self, other) as
described in the template.Once you’ve filled in the missing code, and implemented the search code, you should be able to test your algorithm. Make sure your algorithm works on all the provided tests before moving on to the next parts. If it works you will not have to touch it again!
The autograder will test your code with examples in
visible.txt, along with examples not provided in the
template.
Run A* search on all LightsOut problems:
python3 main.py --problem_type=LightsOut
If you wish to test your code without heuristics (i.e., run dijkstra) you can pass in the argument “do_not_use_heuristic”, e.g.,
python3 main.py --problem_type=LightsOut --do_not_use_heuristic
Now move on to EightPuzzleState, we’ve provided some but not all of
the code you’ll need here. There are 4 “TODO(IV)” in
state.py.
grid_distance(a,b)
EightPuzzleState.generate_successors(self)
EightPuzzleState.calculate_heuristic(self)
EightPuzzleState.__lt__(self, other) as
described in the template.
LightsOutState.__lt__(self, other)!Run A* search on EightPuzzle problems (all puzzles with puzzle_len=N can be solved in N steps):
python3 main.py --problem_type=EightPuzzle --eight_puzzle_len=5
You can choose any puzzle length among [5, 10, 27].
Submit the main part of this assignment by uploading
search.py and state.py to Gradescope.
You are expected to be familiar with the general policies on the course syllabus (e.g. academic integrity) and on the top-level MP page (e.g. code style). In particular, notice that this is an individual assignment.