-
Notifications
You must be signed in to change notification settings - Fork 19
/
instructions_search.html
250 lines (147 loc) · 19.5 KB
/
instructions_search.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Project 1: Search in Pacman</title>
<link href="projects.css" rel="stylesheet" type="text/css">
</head>
<body>
<h2>Project 1: Search in Pacman</h2>
<!--announcements-->
<blockquote>
<center>
<img src="maze.png" width="400px" />
</center>
<p><cite><center>All those colored walls,<br>
Mazes give Pacman the blues,<br>
So teach him to search.</center></cite></p>
</blockquote>
<h3>Introduction</h3>
<p>In this project, your Pacman agent will find paths through his maze world, both to reach a particular location and to collect food efficiently. You will build general search algorithms and apply them to Pacman scenarios. </p>
<p>The code for this project consists of several Python files, some of which you will need to read and understand in order to complete the assignment, and some of which you can ignore. You can download all the code and supporting files (including this description) as a <a href="search.zip">zip archive</a>.</p>
<p>The assignment files are not compatible with Python 3. Please use Python 2 to finish the assignment. Version 2.7.2 or higher is recommended</p>
<h4>????</h4>
<p>??????,????????Search???,???Pacman???(agent)?????????????,?????????:??????,??????????????????</p>
<p>??<a href="search.zip">search.zip</a>????????????????,?????????Python??,???????????(???)???????</p>
<p>???????????Python 3???Python 2??????????Python 2.7.2???????</p>
<table border="0" cellpadding="10">
<tr><td colspan="2"><b>Files you'll edit:</b></td></tr>
<tr><td><code><a href="docs/search.html">search.py</a></code></td>
<td>Where all of your search algorithms will reside.</td></tr>
<tr><td><code><a href="docs/searchAgents.html">searchAgents.py</a></code></td>
<td>Where all of your search-based agents will reside.</td></tr>
<tr><td colspan="2"><b>Files you might want to look at:</b></td></tr>
<tr><td><code><a href="docs/pacman.html">pacman.py</a></code></td>
<td>The main file that runs Pacman games. This file describes a Pacman GameState type, which you use in this project.</td></tr>
<tr><td><code><a href="docs/game.html">game.py</a></code></td>
<td>The logic behind how the Pacman world works. This file describes several supporting types like AgentState, Agent, Direction, and Grid.</td></tr>
<tr><td><code><a href="docs/util.html">util.py</a></code></td>
<td>Useful data structures for implementing search algorithms.</td></tr>
<tr><td colspan="2"><b>Supporting files you can ignore:</b></td></tr>
<tr><td><code><a href="docs/graphicsDisplay.html">graphicsDisplay.py</a></code></td>
<td>Graphics for Pacman</td></tr>
<tr><td><code><a href="docs/graphicsUtils.html">graphicsUtils.py</a></code></td>
<td>Support for Pacman graphics</td></tr>
<tr><td><code><a href="docs/textDisplay.html">textDisplay.py</a></code></td>
<td>ASCII graphics for Pacman</td></tr>
<tr><td><code><a href="docs/ghostAgents.html">ghostAgents.py</a></code></td>
<td>Agents to control ghosts</td></tr>
<tr><td><code><a href="docs/keyboardAgents.html">keyboardAgents.py</a></code></td>
<td>Keyboard interfaces to control Pacman</td></tr>
<tr><td><code><a href="docs/layout.html">layout.py</a></code></td>
<td>Code for reading layout files and storing their contents</td></tr>
</table>
<p>
<p><strong>Deadline:</strong> 2014 / 12 / 2, 11:59 pm </p>
<p><strong>What to submit:</strong> You need implement 6 questions by filling in portions of <code><a href="docs/search.html">search.py</a></code> during the assignment. You should submit <em>this file only</em>. Submit <code><a href="docs/search.html">search.py</a></code> as <em>Output Submission</em>, <code><a href="docs/searchAgents.html">search.py</a></code> as <em>Additional Submission</em> to the <a href="https://class.coursera.org/ai-001/assignment">Programming Assignments</a> Section .</p>
<p><strong>Evaluation:</strong> Your code will be graded for technical
correctness. Please <em>do not</em> change the names of any provided functions or classes within the code, or you will break our tester. However, the correctness of your implementation -- not the tester's output -- will be the final judge of your score.
<p><strong>Getting Help:</strong> You are not alone! If you find yourself stuck on something, feel free to address it in the <a href="https://class.coursera.org/ai-001/forum/list?forum_id=10009">forum</a> for help! We want these projects to be rewarding and instructional, not frustrating and demoralizing. But, we don't know when or how to help unless you ask. One more piece of advice: if you don't know what a variable does or what kind of values it takes, print it out.
<h4>????</h4>
<p><strong>????:</strong> 2014 / 12 / 2, 11:59 pm </p>
<p><strong>????:</strong> ??????????,???<code><a href="docs/search.html">search.py</a></code>?<code><a href="docs/searchAgents.html">searchAgents.py</a></code>?????,?<code><a href="docs/search.html">search.py</a></code>?<em>Output Submission</em>????<code><a href="docs/searchAgents.html">searchAgents.py</a></code>?<em>Additional Submission</em>???,??<a href="https://class.coursera.org/ai-001/assignment">?????</a>? ??:?????????<code><a href="docs/search.html">search.py</a></code>?<code><a href="docs/searchAgents.html">search.py</a></code>,????????????!</p>
<p><strong>????:</strong> ???????????????????????? <em>??</em>?????????????????,?????????????????
<p><strong>?????:</strong> ??????????,???<a href="https://class.coursera.org/ai-001/forum/list?forum_id=10009">?????</a>??!????????????????????,???????????????????????,?????????????????! ??:?Python?????,???????????(?<code>var1</code>)?????,?????(<code>print var1</code>)??!
<h3> Welcome to Pacman </h3>
After downloading the code (<a href="search.zip">search.zip</a>), unzipping it and changing to the <em>search</em> directory, you should be able to play a game of Pacman by typing the following at the command line:
<pre>python pacman.py</pre>
Note: if you get error messages regarding python-tk, use your package manager to install <i>python-tk</i>, or see <a href=http://tkinter.unpythonic.net/wiki/How_to_install_Tkinter>this page</a> for more detailed instructions.
Pacman lives in a shiny blue world of twisting corridors and tasty round treats. Navigating this world efficiently will be Pacman's first step in mastering his domain.
<p> The simplest agent in <a href="docs/searchAgents.html">searchAgents.py</a> is called the <code>GoWestAgent</code>, which always goes West (a trivial reflex agent). This agent can occasionally win:
<pre>python pacman.py --layout testMaze --pacman GoWestAgent</pre>
But, things get ugly for this agent when turning is required:
<pre>python pacman.py --layout tinyMaze --pacman GoWestAgent</pre>
If pacman gets stuck, you can exit the game by typing CTRL-c into your terminal.
Soon, your agent will solve not only <code>tinyMaze</code>, but any maze you want.
Note that <code><a href="docs/pacman.html">pacman.py</a></code> supports a number of options that can each be expressed in a long way (e.g., <code>--layout</code>) or a short way (e.g., <code>-l</code>). You can see the list of all options and their default values via:
<pre>python pacman.py -h</pre>
Also, all of the commands that appear in this project also appear in <a href="commands.txt">commands.txt</a>, for easy copying and pasting. In UNIX/Mac OS X, you can even run all these commands in order with <code>bash commands.txt</code>.
<h3> Finding a Fixed Food Dot using Search Algorithms </h3>
In <code><a href="docs/searchAgents.html">searchAgents.py</a></code>, you'll find a fully implemented <code>SearchAgent</code>, which plans out a path through Pacman's world and then executes that path step-by-step. The search algorithms for formulating a plan are not implemented -- that's your job. As you work through the following questions, you might need to refer to this <a href="#Glossary">glossary of objects in the code</a>.
First, test that the <code>SearchAgent</code> is working correctly by running:
<pre>python pacman.py -l tinyMaze -p SearchAgent -a fn=tinyMazeSearch</pre>
The command above tells the <code>SearchAgent</code> to use <code>tinyMazeSearch</code> as its search algorithm, which is implemented in <code><a href="docs/search.html">search.py</a></code>. Pacman should navigate the maze successfully.
<p>Now it's time to write full-fledged generic search functions to help Pacman plan routes! Pseudocode for the search algorithms you'll write can be found in the lecture slides and textbook. Remember that a search node must contain not only a state but also the information necessary to reconstruct the path (plan) which gets to that state.
<p><em>Important note:</em> All of your search functions need to return a list of <em>actions</em> that will lead the agent from the start to the goal. These actions all have to be legal moves (valid directions, no moving through walls).
<p><em>Hint:</em> Each algorithm is very similar. Algorithms for DFS, BFS, UCS, and A* differ only in the details of how the fringe is managed. So, concentrate on getting DFS right and the rest should be relatively straightforward. Indeed, one possible implementation requires only a single generic search method which is configured with an algorithm-specific queuing strategy. (Your implementation need <em>not</em> be of this form to receive full credit).
<p><em>Hint:</em> Make sure to check out the <code>Stack, Queue</code> and <code>PriorityQueue</code> types provided to you in <code><a href="docs/util.html">util.py</a></code>! </p>
<p><em><strong>Question 1 (1 points) </strong></em> Implement the depth-first search (DFS) algorithm in the
<code>depthFirstSearch</code> function in <code><a href="docs/search.html">search.py</a></code>. To make your algorithm <em>complete</em>, write the graph search version of DFS, which avoids expanding any already visited states (textbook section 3.5).
<p>Your code should quickly find a solution for:
<pre>python pacman.py -l tinyMaze -p SearchAgent</pre>
<pre>python pacman.py -l mediumMaze -p SearchAgent</pre>
<pre>python pacman.py -l bigMaze -z .5 -p SearchAgent</pre>
The Pacman board will show an overlay of the states explored, and the order in which they were explored (brighter red means earlier exploration). Is the exploration order what you would have expected? Does Pacman actually go to all the explored squares on his way to the goal?
<p> <em>Hint:</em> If you use a <code>Stack</code> as your data structure, the solution found by your DFS algorithm for <code>mediumMaze</code> should have a length of 130 (provided you push successors onto the fringe in the order provided by getSuccessors; you might get 244 if you push them in the reverse order). Is this a least cost solution? If not, think about what depth-first search is doing wrong.
<p><em><strong>Question 2 (1 point) </strong></em> Implement the breadth-first search (BFS) algorithm in the
<code>breadthFirstSearch</code> function in <code><a href="docs/search.html">search.py</a></code>. Again, write a graph search algorithm that avoids expanding any already visited states. Test your code the same way you did for depth-first search.
<pre>python pacman.py -l mediumMaze -p SearchAgent -a fn=bfs</pre>
<pre>python pacman.py -l bigMaze -p SearchAgent -a fn=bfs -z .5</pre>
Does BFS find a least cost solution? If not, check your implementation.
<p> <em>Hint:</em> If Pacman moves too slowly for you, try the option <code>--frameTime 0</code>.
<p> <em>Note:</em> If you've written your search code generically, your code should work equally well for the eight-puzzle search problem (textbook section 3.2) without any changes.
<pre>python eightpuzzle.py</pre>
<h3> Varying the Cost Function </h3>
While BFS will find a fewest-actions path to the goal, we might want to find paths that are "best" in other senses. Consider <code><a href="layouts/mediumDottedMaze.lay">mediumDottedMaze</a></code> and <code><a href="layouts/mediumScaryMaze.lay">mediumScaryMaze</a></code>. By changing the cost function, we can encourage Pacman to find different paths. For example, we can charge more for dangerous steps in ghost-ridden areas or less for steps in food-rich areas, and a rational Pacman agent should adjust its behavior in response.
<p><em><strong>Question 3 (1 points) </strong></em> Implement the uniform-cost graph search algorithm in
the <code>uniformCostSearch</code> function in <code><a href="docs/search.html">search.py</a></code>. We encourage you to look through <code><a href="docs/util.html">util.py</a></code> for some data structures that may be useful in your implementation. You should now observe successful behavior in all three of the following layouts, where the agents below are all UCS agents that differ only in the cost function they use (the agents and cost functions are written for you):
<pre>python pacman.py -l mediumMaze -p SearchAgent -a fn=ucs</pre>
<pre>python pacman.py -l mediumDottedMaze -p StayEastSearchAgent</pre>
<pre>python pacman.py -l mediumScaryMaze -p StayWestSearchAgent</pre>
<p> <em>Note:</em> You should get very low and very high path costs for the <code>StayEastSearchAgent</code> and <code>StayWestSearchAgent</code> respectively, due to their exponential cost functions (see <code><a href="docs/searchAgents.html">searchAgents.py</a></code> for details).
<h3> A* search </h3>
<p><em><strong> Question 4 (1 points) </strong></em> Implement A* graph search in the empty function <code>aStarSearch</code> in <code><a href="docs/search.html">search.py</a></code>. A* takes a heuristic function as an argument. Heuristics take two arguments: a state in the search problem (the main argument), and the problem itself (for reference information). The <code>nullHeuristic</code> heuristic function in <code><a href="docs/search.html">search.py</a></code> is a trivial example.
<p>You can test your A* implementation on the original problem of finding a path through a maze to a fixed position using the Manhattan distance heuristic (implemented already as <code>manhattanHeuristic</code> in <code><a href="docs/searchAgents.html">searchAgents.py</a></code>).
<pre>python pacman.py -l bigMaze -z .5 -p SearchAgent -a fn=astar,heuristic=manhattanHeuristic </pre>
You should see that A* finds the optimal solution slightly faster than uniform cost search (about 549 vs. 620 search nodes expanded in our implementation, but ties in priority may make your numbers differ slightly). What happens on <code>openMaze</code> for the various search strategies?
<h3> Finding All the Corners </h3>
<p> The real power of A* will only be apparent with a more challenging search problem. Now, it's time to formulate a new problem and design a heuristic for it.
<p>In <em>corner mazes</em>, there are four dots, one in each corner. Our new search problem is to find the shortest path through the maze that touches all four corners (whether the maze actually has food there or not). Note that for some mazes like <a href="layouts/tinyCorners.lay">tinyCorners</a>, the shortest path does not always go to the closest food first! <em>Hint</em>: the shortest path through <code>tinyCorners</code> takes 28 steps.
<p><em><strong> Question 5 (2 points) </strong></em> Implement the <code>CornersProblem</code> search problem in <code><a href="docs/searchAgents.html">searchAgents.py</a></code>. You will need to choose a state representation that encodes all the information necessary to detect whether all four corners have been reached. Now, your search agent should solve:
<pre>python pacman.py -l tinyCorners -p SearchAgent -a fn=bfs,prob=CornersProblem</pre>
<pre>python pacman.py -l mediumCorners -p SearchAgent -a fn=bfs,prob=CornersProblem</pre>
To receive full credit, you need to define an abstract state representation that <em>does not</em> encode irrelevant information (like the position of ghosts, where extra food is, etc.). In particular, do not use a Pacman <code>GameState</code> as a search state. Your code will be very, very slow if you do (and also wrong).
<p> <em>Hint:</em> The only parts of the game state you need to reference in your implementation are the starting Pacman position and the location of the four corners.
<p>Our implementation of <code>breadthFirstSearch</code> expands just under 2000 search nodes on <a href="layouts/mediumCorners.lay">mediumCorners</a>. However, heuristics (used with A* search) can reduce the amount of searching required.
<p><em><strong> Question 6 (2 points) </strong></em> Implement an admissible heuristic for the <code>CornersProblem</code> in <code>cornersHeuristic</code>. Grading: inadmissible heuristics will get <em>no</em> credit. You get 0.5 point for any non-trivial admissible heuristic, another 0.5 point for expanding fewer than 1600 nodes, another 0.5 point for expanding fewer than 1200 nodes, and yet another 0.5 point for expanding fewer than 1000 nodes. If you have some extra time, try to expand fewer than 800!
<pre>python pacman.py -l mediumCorners -p AStarCornersAgent -z 0.5</pre>
<p> <em>Hint:</em> Remember, heuristic functions just return numbers, which, to be admissible, must be lower bounds on the actual shortest path cost to the nearest goal.
<p> <em>Note:</em> <code>AStarCornersAgent</code> is a shortcut for <code>-p SearchAgent -a fn=aStarSearch,prob=CornersProblem,heuristic=cornersHeuristic</code>.
<h3> <a name="Glossary">Object Glossary</a> </h3>
<p> Here's a glossary of the key objects in the code base related to search problems, for your reference:
<dl>
<dt><code>SearchProblem (search.py)</code></dt>
<dd>A SearchProblem is an abstract object that represents the state space, successor function, costs, and goal state of a problem. You will interact with any SearchProblem only through the methods defined at the top of <code><a href="docs/search.html">search.py</a></code></dd>
<dt><code>PositionSearchProblem (searchAgents.py)</code></dt>
<dd>A specific type of SearchProblem that you will be working with --- it corresponds to searching for a single pellet in a maze. </dd>
<dt><code>CornersProblem (searchAgents.py)</code></dt>
<dd>A specific type of SearchProblem that you will define --- it corresponds to searching for a path through all four corners of a maze. </dd>
<dt><code>FoodSearchProblem (searchAgents.py)</code></dt>
<dd>A specific type of SearchProblem that you will be working with --- it corresponds to searching for a way to eat all the pellets in a maze. </dd>
<dt>Search Function</dt>
<dd>A search function is a function which takes an instance of SearchProblem as a parameter, runs some algorithm, and returns a sequence of actions that lead to a goal. Example of search functions are <code>depthFirstSearch</code> and <code>breadthFirstSearch</code>, which you have to write. You are provided <code>tinyMazeSearch</code> which is a very bad search function that only works correctly on <code>tinyMaze</code>
<dt><code>SearchAgent</code></dt>
<dd><code>SearchAgent</code> is is a class which implements an Agent (an object that interacts with the world) and does its planning through a search function. The <code>SearchAgent</code> first uses the search function provided to make a plan of actions to take to reach the goal state, and then executes the actions one at a time.</dd>
</dl>
</body>
</html>