Uploaded by Morgan Alphin

treasure hunt handout

advertisement
COMP 140: Computational Thinking
A* Search
Form a group of any 3 students to work on the exercise below and the Treasure Hunt:
Student Name
I.
Section Time
In-class TA Name
A* Search
The A* search algorithm is a “best first” search algorithm in which you try to explore the most promising
nodes first. The “best” node is found by combining the actual cost to arrive at a node from the start plus a
heuristic estimate of the remaining distance to the goal.
Each node, n, therefore, has three costs associated with it:
1. The actual cost to get to node n from the start node. This cost is often called g.
2. The heuristic estimate of the cost to get from node n to the goal node. This estimate must be a lower
bound on the actual cost. This cost is often called h.
3. The sum of the actual cost to get from the start node to node n and the estimate to get from node n to
the goal. This cost is g + h and is often called f .
The algorithm starts by initializing the costs of the start node. For the start node, g=0. You then compute
h and f for the start node. Further, you would initialize the parent of the start node to be null.
The algorithm then proceeds using the notion of an openset and a closedset. The openset is the set
of nodes that are currently under consideration. It serves a similar role to the queue or stack in BFS/DFS.
Nodes are removed from the openset in order of increasing f cost. This can efficiently be implemented as a
priority queue, but this is beyond the scope of this class. You just need to find the node in the openset with
the lowest f cost. The closedset is the set of nodes for which the shortest path to that node has already been
found, so they should not be searched again. The closedset is initially empty. As nodes are removed from
the openset, they should be placed into the closedset. You should convince yourself that you understand
why.
Just as in BFS or DFS, once a node has been removed from the openset, you need to explore its neighbors. Each neighbor should be considered in turn. There are three cases:
1. The neighbor is in the closedset. What should you do then?
2. The neighbor is in the openset. You should check the current g cost of the neighbor and see if you
have found a path with a lower g cost. If so, you have found a shorter path to that node and you should
update the g cost, the f cost (if you are storing it), and the parent.
3. The neighbor is in neither the closedset nor the openset. This means that it has not yet been explored.
What should you do then?
The algorithm continues until you find the shortest path to the goal node or you search the entire graph
without finding it.
1
II.
A* By Hand
Run the A* algorithm on the graph below to find the shortest path from node A to node G. Each edge is
labeled with the actual distance between the two neighboring nodes. The heuristic distance from each node
to the goal node, G, is given in the table.
As you run the algorithm, you should keep track of the openset and the closedset. For each node in
these sets, you should list the name of the node, the actual distance from the start to that node along the best
path found so far (the g cost), the heuristic distance from that node to the goal (the h cost), and the parent
node for the best path found so far to that node. For example, you might list a node as Z, 3, 4.2, X.
When you need to modify a node in one of the sets, you can just cross out the old information (do not
erase it) and write in the new next to it. There is no need to rewrite everything. Similarly, when you remove
a node from a set, just cross it out (do not erase it).
If there is a tie when choosing a node, you should always break the tie by choosing the node with
the lowest (closest to A) letter. This includes when you process a node’s neighbors — consider them in
alphabetical order.
When you are done, clearly write the path from the start to the goal that you found (e.g., Z → Q → G).
Be sure to have your work checked by a TA or instructor before you begin the Treasure Hunt!
Node
A
B
C
D
E
F
G
h-cost
2.2
2
2.2
1.4
1
1
0
2
III.
Treasure Hunt
Today you will exercise your knowledge of the A* algorithm by physically executing the algorithm as
you participate in a cross-campus treasure hunt. You will be given three pieces of information: a color
representing the route to follow, a start location, and an end destination. The route color will be given to you
before you leave for the treasure hunt; the start and end nodes will depend on which route you are assigned
and will be given in section IV. Your goal is to find the treasure by applying A* along your route to reach
your destination. (Yes, there is actually treasure waiting at your final destination!)
You will necessarily receive the true identity of the start location. However, you will not receive the true
identity of the end destination — that would be too easy! Instead, each node in the graph will be given a
codename. Recall that you will also be assigned a route color: Red, Yellow, Green, or Blue. Each route
contains a set of nodes labeled A through J, where each letter/codename is mapped to a particular physical
location. However, these mappings will vary from route to route. A single physical location may fall on
more than one route, but it may not necessarily have the same codename on each route. As an (imaginary)
example, Wiess College might be codenamed A on the Red route, F on the Yellow route, and not be on the
Green or Blue routes at all.
Whenever you reach a new node n in the graph, you will find a sign for each route that passes through
that physical location containing information about n’s neighbors. Again, make sure to use the sign corresponding to the route that you have been assigned, as this information may be different for each route. The
sign for your route will contain, for each neighbor nbr of n:
• the codename of nbr
• the true identity of nbr
• the edge cost from n to nbr
• the h cost from nbr to your destination
To demonstrate that you utilized the A* algorithm to reach your final destination, you must perform
some bookkeeping while you traverse the graph. First, as you run the algorithm, you should keep track of
which nodes are in the openset and the closedset at each step in the process, as in part II. Additionally,
on the next page, you will find the complete “treasure map” containing all of the nodes (keyed by their
codenames) and edges in the graph. (Note that while this is an undirected graph, A* can also be used on
directed graphs.) As you go, you should mark each edge you traverse with two things:
• its edge cost
• the order in which you traversed it
Furthermore, you should annotate each node you reached with two things:
• its true identity (a building / room on the Rice campus)
• its h cost to the destination
Finally, at the end of the treasure hunt you should turn in this map, annotated with the aforementioned
information, at the goal or in your starting room, regardless of whether or not you reached your final destination.
3
IV.
Treasure Map
Route Information: (Circle the route color that you get assigned.)
Route
Red
Yellow
Green
Blue
Start Node
A
A
A
A
Start Location
Mudd Lab
Mech Lab Entrance
Herman Brown Hall
Hamman Hall
End Node
E
E
E
E
4
End Location
???
???
???
???
Download