0% found this document useful (0 votes)
337 views12 pages

Tutorial2 Final Final Answersv15

Here is the trace of A* search applied to getting from Lugoj to Bucharest using the straight-line distance heuristic: Initial State: Lugoj g = 0, h = 70, f = 70 Expand Lugoj: Timisoara g = 70, h = 111, f = 181 Arad g = 70, h = 140, f = 210 Expand Timisoara: Oradea g = 141, h = 71, f = 212 Expand Arad: Sibiu g = 140, h = 80, f = 220 Expand Oradea: Zerind g = 212, h = 75, f = 287

Uploaded by

seadon fcrit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
337 views12 pages

Tutorial2 Final Final Answersv15

Here is the trace of A* search applied to getting from Lugoj to Bucharest using the straight-line distance heuristic: Initial State: Lugoj g = 0, h = 70, f = 70 Expand Lugoj: Timisoara g = 70, h = 111, f = 181 Arad g = 70, h = 140, f = 210 Expand Timisoara: Oradea g = 141, h = 71, f = 212 Expand Arad: Sibiu g = 140, h = 80, f = 220 Expand Oradea: Zerind g = 212, h = 75, f = 287

Uploaded by

seadon fcrit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

CS 367: Tutorial Week 2

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

(g) Given the table above is h1 admissible?

no

(h) Given the table above is h1 consistent?

no

(i) Given the table above is h2 admissible?

yes

(j) Given the table above is h2 consistent?


(k)
no
(l) Given the table above is h3 admissible?

yes

(m) Given the table above is h3 consistent?

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:

The agent is directional and at all times faces some direction d Î


( N , S , E , W ) . W i t h a s i n g l e a c t i o n t h e agent can either move forward at an
adjustable velocity v or turn. The turning actions are left and right, which change the
agent’s direction by 90 degrees. Turning is only permitted when the velocity is zero
(and leaves it at zero). The moving actions are fast and slow. Fast increments the
velocity by 1 and slow decrements the velocity by 1; in both cases the agent then
moves a number of squares equal to its NEW adjusted velocity (see example
below). A consequence of this formulation is that it is impossible for the agent to
move in the same nonzero velocity for two consecutive timesteps. Any action that
would result in a collision with a wall crashes the agent and is illegal. Any action that
would reduce v below 0 or above a maximum speed Vmax is also illegal. The agent’s
goal is to find a plan which parks it (stationary) on the exit square using as few
actions (time steps) as possible.

As an example: if at timestep t the agent’s current velocity is 2, by taking the fast


action, the agent first increases the velocity to 3 and move 3 squares forward, such
that at timestep t + 1 the agent’s current velocity will be 3 and will be 3 squares away
from where it was at timestep t. If instead the agent takes the slow action, it first
decreases its velocity to 1 and then moves 1 square forward, such that at timestep
t + 1 the agent’s current velocity will be 1 and will be 1 squares away from where it
was at timestep t. If, with an instantaneous velocity of 1 at timestep t + 1, it takes the
slow action again, the agent’s current velocity will become 0, and it will not move at
timestep t + 1. Then at timestep t + 2, it will be free to turn if it wishes. Note that the
agent could not have turned at timestep t + 1 when it had a current velocity of 1,
because it has to be stationary to turn.

(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.

(d) If we used an inadmissible heuristic in A* graph search, would the search be


complete? Would it be optimal?
If the heuristic function is bounded, then A* graph search would visit all the nodes eventually, and would
find a path to the goal state if there exists one. An inadmissible heuristic does not guarantee optimality
as it can make the good optimal goal look as though it is very far off, and take you to a suboptimal
goal.
(e) If we used an admissible heuristic in A* graph search, is it guaranteed to
return an optimal solution? What if the heuristic was consistent? What if we
were using A* tree search instead of A* graph search?
Although admissible heuristics guarantee optimality for A* tree search, they do not necessarily
guarantee optimality for A* graph search; they are only guaranteed to return an optimal solution if they
are consistent as well or it is a version of A* that correctly handles re-openings.

(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.

Q9: (extra for experts)


n vehicles occupy squares (1, 1) through (n, 1) (i.e., the bottom row) of an n x n grid.
The vehicles must be moved to the top row but in reverse order; so the vehicle i that starts
in (i, 1) must end up in (n−i+1, n). On each time step, every one of the n vehicles can move
one square up, down, left, or right, or stay put; but if a vehicle stays put, one other adjacent
vehicle (but not more than one) can hop over it. Two vehicles cannot occupy the same
square.
(a) Calculate the size of the state space as a function of n.
(b) Calculate the branching factor as a function of n.
(c) Suppose that vehicle i is at (xi, yi); write a nontrivial admissible heuristic hi for the
number of moves it will require to get to its goal location (n − i + 1, n), assuming no
other vehicles are on the grid.
(d) Which of the following heuristics are admissible for the problem of moving all n
vehicles to their destinations? Explain.
(i) ∑"!#$ ℎ!
(ii) max{h1, . . . , hn}.
(iii) min{h1, . . . , hn}.
Answer

You might also like