0% found this document useful (0 votes)
104 views44 pages

HW3 Sol PDF

The document discusses various search algorithms including A* search, heuristic path algorithms, uniform-cost search, breadth-first search, and beam search. It provides examples of applying A* search to find the optimal path from Lugoj to Bucharest in Romania. It also proves properties of heuristics and search algorithms, including showing that the Manhattan distance heuristic for the 8-puzzle problem is admissible.

Uploaded by

bsudheertec
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)
104 views44 pages

HW3 Sol PDF

The document discusses various search algorithms including A* search, heuristic path algorithms, uniform-cost search, breadth-first search, and beam search. It provides examples of applying A* search to find the optimal path from Lugoj to Bucharest in Romania. It also proves properties of heuristics and search algorithms, including showing that the Manhattan distance heuristic for the 8-puzzle problem is admissible.

Uploaded by

bsudheertec
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/ 44

CompSci 171: Intro AI

Homework 3

Informed search
4.1 A* search: From Lugoj to Bucharest

100

f(n) = g(n) + h(n)


A* search is guided by evaluation function f(n)
A* search: From Lugoj to Bucharest
Lugoj
f = 111 + 329 = 440 f = 70 + 241 = 311
Timisoara
Mehadia
A* search: From Lugoj to Bucharest
Lugoj
f = 111 + 329 = 440 f = 70 + 241 = 311
Timisoara
Mehadia

f = (70 + 75) + 242 = 387


Dobreta
A* search: From Lugoj to Bucharest
Lugoj
f = 111 + 329 = 440 f = 70 + 241 = 311
Timisoara
Mehadia

f = (70 + 75) + 242 = 387


Dobreta

f = (145 + 120) + 160 = 425

Craiova
A* search: From Lugoj to Bucharest
Lugoj
f = 111 + 329 = 440 f = 70 + 241 = 311
Timisoara
Mehadia

f = (70 + 75) + 242 = 387


Dobreta

f = (145 + 120) + 160 = 425

Craiova

f = 604 f = 503
Rimnicu
Pitesti
A* search: From Lugoj to Bucharest
Lugoj
f = 111 + 329 = 440 f = 70 + 241 = 311
Timisoara
Mehadia

f = (70 + 75) + 242 = 387


Arad Dobreta

f = (111 + 118) + 366 = 595 f = (145 + 120) + 160 = 425

Craiova

f = 604 f = 503
Rimnicu
Pitesti
A* search: From Lugoj to Bucharest
Lugoj
f = 111 + 329 = 440 f = 70 + 241 = 311
Timisoara
Mehadia

f = (70 + 75) + 242 = 387


Dobreta Dobreta

f = (111 + 118) + 366 = 595 f = (145 + 120) + 160 = 425

Craiova

f = 604 f = 503
Rimnicu
Pitesti

f = 693 f = 504
Rimnicu
Bucharest

f = 70+75+120+138+101 = 504
4.2 Heuristic path algorithm
f(n) = (2 – w)g(n) + wh(n)
For what value of w is this algorithm guaranteed to be
optimal?

g(n): a path cost to n from a start state


h(n): a heuristic estimate of cost from n to a goal state
4.2 Heuristic path algorithm
If h(n) is admissible, the algorithm is guaranteed to be optimal
w
f  n =2− w[ g  n h n ]
2−w
which behaves exactly like A* search with a heuristic
w
f  n = g n  h n
2−w
w
To be optimal, we require 1
2−w

w1
4.2 Heuristic path algorithm

For w = 0: f(n) = 2g(n) -> Uniform-cost search

For w = 1: f(n) = g(n) + h(n) -> A* search

For w = 2: f(n) = 2h(n) -> Greedy best search


4.3
(a) Breadth-first search is a special case of
uniform-cost search

When all step costs are equal (and let’s assume


equal to 1), g(n) is just a multiple of depth n. Thus,
breadth-first search and uniform-cost search would
behave the same in this case

f(n) = g(n) = 1*(depth of n)


4.3
(b) BFS, DFS and uniform-cost search are special
cases of best-first search

– BFS: f(n) = depth(n)

– DFS: f(n) = -depth(n)

– UCS: f(n) = g(n)


4.3
(c) Uniform-cost search is special case of A*
search

A* search: f(n) = g(n) + h(n)


Uniform-cost search: f(n) = g(n)

Thus, for h(n) = 0, uniform cost search will produce the


same result as A* search
4. Prove that the Manhattan Distance
heuristic for 8-puzzle is admissible
Manhattan Distance for points
P1(x1,y1), P2(x2,y2) is defined by:

d  p1 , p 2 =∣x 1 − x 2∣∣ y 1 − y 2∣

8
Heuristic: h=∑ d  n
n= 1
•Tiles cannot move along diagonals, so each tile has to move at least d(n)
steps to its goal

•Any move can only move one tile at a time


5. Eight Queens problem

h – number of pairs of queens that are attacking each other


5. Eight Queens problem
h=2
However, number of conflicts for Queen at “b3” are:

1
2
2
3
0
2
2
2
5. Eight Queens problem

Therefore, the true cost to reach the goal state h* is 1


Thus, h>h* heuristic is not admissible
5. Eight Queens problem

We can propose another heuristic.


For example, we can propose heuristic derived form a relaxed (and trivial)
version of 8-Queens problem, that the eight queens must be placed in the
board so that no two queens are on the same row.

Thus, h = Σ( # queens that are on the same row – 1) for all conflicting rows
4.11
(a) Local beam search with k=1

– We would randomly generate 1 start state


– At each step we would generate all the
successors, and retain the 1 best state

– Equivalent to HILL-CLIMBING
4.11
(b) Local beam search with k=∞

– 1 initial state and no limit of the number of states


retained
– We start at initial state and generate all successor
states (no limit how many)
– If one of those is a goal, we stop
– Otherwise, we generate all successors of those states
(2 steps from the initial state), and continue

– Equivalent to BREADTH-FIRST SEARCH


4.11
(c) Simulated annealing with T = 0 at all times

– If T is very small, the probability of accepting an


arbitrary neighbor with lower value is
approximately 0
– This means that we choose a successor state
randomly and move to that state if it is better
than the current state

– Equivalent to FIRST-CHOICE HILL CLIMBING


4.11
(d) Genetic algorithm with population size N = 1

– If selection step necessarily chooses the single


population member twice, so the crossover steo
does nothing.
– Moreover, if we think of the mutation step as
selecting a successor at random, there is no
guarantee that the successor is an improvement over
the parent

– Equivalent to RANDOM WALK


4-Queens problem
Min-conflict algorithm:
1. Randomly choose a variable from set of
problematic variables
2. Reassign its value to the one that results in the
fewest conflicts overall
3. Continue until there are no conflicts
Q1
Q2
Q3
Q4
4-Queens problem
A B C D
3 1 2 1 All queens are
4 Q1 attacked.

Pick Q2 randomly
1 3 1 2
3 Q2 We can move Q2 to
B2 or B4
2 1 3 1
Randomly, move
2 Q3 Q2 to B4

1 2 1 3
1 Q4

Number of conflicts
4-Queens problem
A B C D
3 1 2 2 All queens are
4 Q1 Q2 attacked.

Pick Q1 randomly
1 3 1 1
3 We can move Q1 to
A1 ~ A3
1 1 2 2
Randomly, move
2 Q3 Q1 to A2

1 2 1 2
1 Q4
4-Queens problem
A B C D
3 0 2 1 Q1, Q3 and Q4 are
4 Q2 attacked.

Pick Q3 randomly
1 3 1 1
3 We can move Q3 to
C1 or C3
1 2 2 3
Randomly, we
2 Q1 Q3 select C1

1 3 1 1
1 Q4
4-Queens problem
A B C D
2 0 2 1 Q3 and Q4 are
4 Q2 attacked.

Pick Q4 randomly
2 2 1 0
3 We can move Q4
D3
0 2 2 3
2 Q1

2 3 1 1
1 Q3 Q4
4-Queens problem
A B C D
2 0 2 1 No conflicts!
4 Q2

2 2 1 0
3 Q4

0 2 2 3
2 Q1

2 3 1 1
1 Q3
8. Compute the following gradients
f ( x, y, z , t )  ( x  1)(2  y ) z  (t 3  1) xyz
1
g ( x, y ) 
1  exp( (ax  by  c))
h( x, y, z )  ( x  1) 2 exp( x)  ( y  2) 3 z 3
c ( x, y , z )  ( x  z  2 y  2 ) b
g ( x, y )  2( x  1) 2  2( y  2) 2  2( x  1)( y  2)
a, b, c are some arbitrary constants
8. Compute the following gradients
f ( x, y, z , t )  ( x  1)(2  y ) z  (t 3  1) xyz
1
g ( x, y ) 
1  exp( (ax  by  c))
h( x, y, z )  ( x  1) 2 exp( x)  ( y  2) 3 z 3
c ( x, y , z )  ( x  z  2 y  2 ) b
g ( x, y )  2( x  1) 2  2( y  2) 2  2( x  1)( y  2)
8. Compute the following gradients
f  x , y , z , t = x −1 2− y  zt 3−1 xyz
3 3 3 2
∇ f = 2− y z t −1 yz ,− x−1 zt −1 xz ,  x −1 2− y  t −1 xy , 3 t xyz 

1
g  x , y =
1exp − axby c 
a exp − ax by c  b exp − ax by c
∇ g= 2
, 2

 1exp − ax byc  1exp − axby c 

h  x , y , z = x −1 2 exp  x  y −23 z 3


∇ h= x 2 −1 exp  x  , 3  y −22 z 3, 3 y−2 3 z 2 
−2 b
c  x , y , z = x − z− 2y 
∇ c= b x− z −2y −2  b−1 , 4b  x − z −2y −2 b−1 y −3 , − b x− z −2y −2  b−1 

2 2
g  x , y = 2 x−1 2  y −2 − 2 x−1 y−2
∇ g= 4x−2y , −2x4y −6
Pseudo code for gradient descent
algorithm that minimize g(x, y)
Uniform Cost Search
B
G 3 5
1

A
I
4
2
C 1 F
4 3
H D
2

Goal: path AI

Queue: A (root)
Uniform Cost Search
B
G 3 5
1

A
I
4
2
C 1 F
4 3
H D
2

Step 1: E
At node A:
Queue: D=2, G=3, C=4, B=5
Note: nodes in the queue are sorted by distance from the root
Uniform Cost Search
B
G 3 5
1

A
I
4
2
C 1 F
4 3
H D
2

Step 2: E
At node D:
Queue: G=3, C=4, E=4, B=5, H=5
Note: nodes in the queue are sorted by distance from the root
Uniform Cost Search
B
G 3 5
1

A
I
4
2
C 1 F
4 3
H D
2

E
Step 3:
At node G:
Queue: I=4, C=4, E=4, B=5, H=5
Note: nodes in the queue are sorted by distance from the root
Uniform Cost Search
B
G 3 5
1

A
I
4
2
C 1 F
4 3
H D
2

E
Step 4:
At node I:
GOAL NODE FOUND!!!!!
A* Search
f(n) = g(n) + h(n)

Where:
f(n) – estimated total cost of path
through n to goal
R
9 g(n) – cost so far to reach n
1
h(n) – estimated cost from n to goal
A B

1 2 Heuristic Estimates:
C
D
h(B -> G2) = 9
h(D -> G2) = 10
1 h(A -> G1) = 2
10

G1 G2
h(C -> G1) = 1
A* Search f(n) = g(n) + h(n)

h(B -> G2) = 9


h(D -> G2) = 10
h(A -> G1) = 2
h(C -> G1) = 1
f(A) = g(A) + h(A->G1) R
f = 9 + 2 = 11 f = 1 + 9 = 10 f (A) = 11 > f(B) = 10
A B

C
D

G1 G2
A* Search f(n) = g(n) + h(n)

h(B -> G2) = 9


h(D -> G2) = 10
h(A -> G1) = 2
h(C -> G1) = 1
f(A) = g(A) + h(A->G1) R
f = 9 + 2 = 11 f = 1 + 9 = 10

A B

f = (1 + 2) + 10 = 13 f (A) = 11 < f(D) = 13


C
D

G1 G2
A* Search f(n) = g(n) + h(n)

h(B -> G2) = 9


h(D -> G2) = 10
h(A -> G1) = 2
h(C -> G1) = 1
f(A) = g(A) + h(A->G1) R
f = 9 + 2 = 11 f = 1 + 9 = 10

A B

f = (1 + 2) + 10 = 13

f = (9 + 1) + 1 = 11 C f (C) = 11 < f(D) = 13


D

G1 G2
A* Search f(n) = g(n) + h(n)

h(B -> G2) = 9


h(D -> G2) = 10
h(A -> G1) = 2
h(C -> G1) = 1
f(A) = g(A) + h(A->G1) R
f = 9 + 2 = 11 f = 1 + 9 = 10

A B

f = (9 + 1) + 1 = 11 f = (1 + 2) + 10 = 13
C
D f (G1) = 11 < f(D) = 13

f = 11

G1 G2
A* Search Order:
R – B – A – C – G1

There is no need to check the unfinished


path (cost of G2), because it already costs
more than the current path does.
f(A) = g(A) + h(A->G1) R
f = 9 + 2 = 11 f = 1 + 9 = 10

A B

f = (9 + 1) + 1 = 11 f = (1 + 2) + 10 = 13
C
D f (G1) = 11 < f(D) = 13

f = 11

G1 G2

You might also like