Tutorial2 Final Final Answersv15
Tutorial2 Final Final Answersv15
Please bring worked solutions to tutorial to go over with the tutors in tutorial.
You will learn much more if you try them on your own first.
Q1:
Solve the crypt-arithmetic problem:
SEND + MORE = MONEY
Phrase this question as a search problem. In a crypt-arithmetic problem, each letter
represents a different digit, the leftmost digit cannot be zero, and the sum must be correct
considering each sequence of letters as a base ten numeral.
The state: an array one element for each distinct letter S,E,N,D,M,O,R,Y.
The initial state: has each element set to NIL.
The Goal test: all elements have been set to a distinct integer. The S element can be any
integer between 1 and nine inclusively. The other elements can be any integer between 0
and 9 inclusively. When the elements are summed as shown in the problem, they do add up
correctly.
Actions: There is only one action, assign(val, var), where the precondition is that var, a state
element, must have a current value of NIL and the effect is that it has the new value of val.
Q2:
When applying informed search strategies, a crucial task is to select a good heuristic h. In
particular, we need the heuristic to be admissible.
Suppose you want to solve the 8-puzzle problem using the A∗ algorithm. Propose two
admissible heuristics that you think may help the algorithm to quickly find a solution.
Discuss which one is better in terms of the efficiency of the resulting search.
We can use the two heuristics misplaced tiles, h1, and Total Manhattan Distance, h2. h2
dominates h1. h1(s) = number of tiles in s that are not in their goal location, which is the
same as summing whether the tiles are in their goal locations, where the value for a tile is a
1 if the tile is out of place and a 0 if it is in place. h2(s) is the sum of the distances the tiles
are currently from their goal location using the tile Manhattan Distance measure. Since
when h1(s) = 0, h2(s) must also be zero, both h1 and h2 only return non-negative integer
values, and if for tile t, its tile h1 value = 1, i.e., it is out of place, then its tile h2 value >= 1,
then for all s h1(s) =< h2(s), i.e., h2 dominates h1.
Because h2 dominates h1, any node, n, expanded when using h2 must also be expanded
when using h1 because g(n) regardless of which heuristic is used and h1(n)=< h2(n), f(n)
using h1 will be less than or equal to f(n) when using h2.
Q3:
On page 105, in the book, we defined the relaxation of the 8-puzzle in which a tile can move
from square A to square B if B is blank. The exact solution of this problem defines Gaschnig’s
heuristic (Gaschnig, 1979). Explain why Gaschnig’s heuristic is at least as accurate as h1
(misplaced tiles), and show cases where it is more accurate than both h1 and h2 (Manhattan
distance). Explain how to calculate Gaschnig’s heuristic efficiently.
The misplaced-tiles heuristic is exact for the problem where a tile can move from
square A to square B. As this is a relaxation of the condition that a tile can move from
square A to square B if B is blank, Gaschnig’s heuristic cannot be less than the misplaced tiles
heuristic. As it is also admissible (being exact for a relaxation of the original problem),
Gaschnig’s heuristic is therefore more accurate.
If we permute two adjacent tiles in the goal state, we have a state where misplaced tiles
and Manhattan both return 2, but Gaschnig’s heuristic returns 3.
To compute Gaschnig’s heuristic, repeat the following until the goal state is reached:
let B be the current location of the blank; if B is occupied by tile X (not the blank) in the
goal state, move X to B; otherwise, move any misplaced tile to B. Students could be asked to
prove that this is the optimal solution to the relaxed problem.
Q4:
Answer the following questions about the search problem shown below. Break any ties
alphabetically. For the questions that ask for a solution path, please give your answers in the
form `S - A - D - G.'
(a) What order would the nodes be expanded in breadth-first graph search for this search
problem?
S,A,G
(b) What solution path would breadth-first graph search return for this search problem?
S-G
(c) What order would the nodes be expanded in depth-first graph search for this search
problem?
S,A,B,D,G
(d) What solution path would depth-first graph search return for this search problem?
S-A-B-D-G
(e) What order would the nodes be expanded in uniform cost graph search for this search
problem?
S,A,C,D,B,G
(f) What solution path would uniform cost graph search return for this search problem?
S-A-C-G
State h1 h2 h3
S 5 4 3
A 3 2 2
B 6 6 5
C 2 1 1
D 3 3 2
G 0 0 0
no
no
yes
yes
yes
(n) What order would the nodes be expanded in A* graph search for this search problem
using the heuristic above which is admissible and consistent?
S ,A, C, G
(o) What solution path would A* graph search, using a consistent heuristic, return for this
search problem?
S-A-C-G
Q5:
Consider the grid problem as above. The agent is at grid location s and wants to go to grid
location g. We will use a greedy best-first search to guide the search.
Apply this method to the problem assuming h(x) of a given cell in the grid equals to the
Manhattan distance from x to g.
Assume this is the graph version of this search algorithm and that duplicate state
elimination.
You can “name” states by giving the ordered pair (row, column) location of the agent,
starting in the upper right hand corner with the agent at (0,0). The state after a right move
from that is the state (0,1) and a down move from that state would be (1,0). So the start
state is (5,3) and the goal state is (2,2).
Tie breaks are handled as follows: if two nodes have the same f-value then the node where
the agent’s row value is less is preferred to ones with higher row values, and if there nodes
with both the same f-value and row value then pick the one with the lower column value.
Please give both the order of states expanded and the final solution path found.
heuristics
2 1 0
3 2 a a a a a 6
4 a 2 3 4 5 6 7
5 4 a 4 5 6 a
5 4 5 6 7
6 5 6
(5,3) 4
(4,3) 3
(4,2) 2
(4,4) 4
(4,5) 5
(5,4) 5
(6, 3) 5
(6,2) 4
(6,1) 5
(5,1) 4
(5,0) 5
(4,0) 4
(3,0) 3
(2,0) 2
(2,1) 1
(2, 2) 0
(5,3)-(6-3)-(6,2)-(6,1)-(5,1)-(5,0)-(4,0)-(3,0)-(2,0)-(2,1)-(2,2)
10 steps
16 expanded nodes
Q6:
Consider the grid problem as above. Apply A∗ algorithm to this problem.
Apply this method to the problem assuming h(x) of a given cell in the grid equals to the
Manhattan distance from x to g.
Assume we use the graph version of the algorithm. Tie breaks are handled as in Q5 above.
Please give both the order of states expanded and the final solution path found. “Name”
states using the same method as Q5.
f-values
10 10 10
10 10 a a a a a
10 a 4 4 6 8 10 12
10 8 a 4 6 8 a
10 8 6 6 8 10 12
12 10 8 8 10 12
(5,3) 4
(4,3) 4
(4,2) 4
(4,4) 6
(5,4) 6
(6,3) 6
(6,2) 6
(4,5) 8
(5,5) 8
(6,1) 8
(5,1) 8
(6,4) 8
(7,2 ) 8
(7,3) 8
(4,6) 10
(5,0) 10
(4,0) 10
(3,0) 10
(2,0) 10
(2,1) 10
(2,2) 10
Solution
(5,3)-(6,3)-(6,2)-(6-1)-(5-1)-(5-0)-(4-0)-(3-0)-(2-0)-(2-1)-(2-2)
10 steps
21 nodes expanded
Q7:
Trace the operation of A∗ search applied to the problem of getting to Bucharest from
Lugoj using the straight-line distance heuristic. That is, show the sequence of nodes that the
algorithm will consider and the f, g, and h score for each node.
h(n) = straight-line
Oradea
distance to Bucharest
71
Neamt
Zerind 87
75 151
Iasi
Arad
140
92
Sibiu Fagaras
99
118
Vaslui
80
Rimnicu Vilcea
Timisoara
142
111 Pitesti 211
Lugoj 97
70 98
85 Hirsova
Mehadia 146 101 Urziceni
75 138 86
Bucharest
Drobeta 120
90
Craiova Eforie
Giurgiu
City G H F
Lugoj 0 244 244
Mehadia 70 241 311
Drobeta 145 242 387
Craiova 265 160 425
Timisoara 111 329 440
Petesti 403 100 503
Bucharest 504 0 504
Q8:
Imagine a car-like agent wishes to exit a maze like the one shown below:
(a) If the grid is M by N , what is the size of the state space? Justify your answer.
You should assume that all configurations are reachable from the start state.
The size of the state space is 4MN (Vmax + 1). The state representation is (direction facing, x, y,
speed). Note that the speed can take any value in {0, ..., Vmax}.
(b) Is the Manhattan distance from the agent’s location to the exit’s location
admissible? Why or why not?
No, Manhattan distance is not an admissible heuristic. The agent can move at an average speed of
greater than 1 (by first speeding up to Vmax and then slowing down to 0 as it reaches the goal), and so
can reach the goal in less time steps than there are squares between it and the goal. A specific example:
A timestep 0, the agent’s starts stationary at square 0 and the target is 9 squares away at square 9. At
timestep 0, the agent takes the fast action and ends up at square 1 with a velocity of 1. At timestep
1, the agent takes the fast action and ends up at square 3 with a velocity of 2. At timestep 2, the agent
takes the fast action and ends up at square 6 with a velocity of 3. At timestep 3, the agent takes the
slow action and ends up at square 8 with a velocity of 2. At timestep 4, the agent takes the slow action
and ends up at square 9 with a velocity of 1. At timestep 5, the agent takes the slow action and stays
at square 9 with a velocity of 0. Therefore, the agent can move 9 squares by taking 6 actions.
(c) State and justify a non-trivial admissible heuristic for this problem which is
not the Manhattan distance to the exit.
(f) Give a general advantage that an inadmissible heuristic might have over an
admissible one.
The time to solve an A* search problem is a function of two factors: the number of nodes expanded,
and the time spent per node. An inadmissible heuristic may be faster to compute, leading to a solution
that is obtained faster due to less time spent per node. It can also be a closer estimate to the actual
cost function (even though at times it will overestimate!), thus expanding less nodes. We lose the
guarantee of optimality by using an inadmissible heuristic. But sometimes we may be okay with finding
a suboptimal solution to a search problem.