In this assignment you will be implementing the A* search algorithm for solving 2 different search problems - EightPuzzle and WordLadder. Your single implementation of the algorithm will work for both problems, by properly instantiating an AbstractState 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 template code. 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/word_ladder
. Text file containing example problems for the word ladder task, along with a dictionary of english words
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 best first search algorithm in the method best_first_search(starting_state)
in search.py
. Your implementation should:
Notice that the input to best_first_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 AbstractState
class, then look at an instantiation of this class called WordLadderState
.
In order to test your search code we’ve provided almost all of WordLadderState
. 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!
Run best_first_search on all WordLadder problems:
python3 main.py --problem_type=WordLadder
You can also run the word ladder problems where the cost of changing a vowel is larger (10) than the cost of changing a consonant (1).
python3 main.py --problem_type=WordLadder --add_cost_to_vowels
This gives some different results, in particular take a look at the task of going from “small” to “thing”.
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=WordLadder --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
.
manhattan(a,b)
EightPuzzleState.get_neighbors(self)
EightPuzzleState.compute_heuristic(self)
EightPuzzleState.__lt__(self, other)
Run best_first_search on EightPuzzle problems (all puzzles with puzzle_len=N can be solved in N steps):
python3 main.py --problem_type=EightPuzzle --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.