Professional Documents
Culture Documents
5 6
5 2 5 2 5 2
1 7 1 7
4 5
7 8
L is 2 in this example
Intuition: Expand node at the deepest level Intuition: Expand node at the deepest level,
(breaking ties left to right) but limit depth to L
• Complete? No (Yes on finite trees, with no loops). • Complete? Yes if there is a goal state at a depth less than L
• Optimal? No • Optimal? No
Picking the right value for L is
• Time Complexity: O(bm), where m is the maximum depth. • Time Complexity: O(bL), where L is the cutoff. a difficult, Suppose we chose
• Space Complexity: O(bm), where m is the maximum depth. • Space Complexity: O(bL), where L is the cutoff. 7 for FWGC, we will fail to
find a solution...
9 10
1+10+100+1000+10,000+100,000 = 111,111
1
Intuition: Combine the Optimality and
1+10
completeness of Breadth first search, with the
1+10+100
• Complete? Yes low space complexity of Depth first search
1+10+100+1000
• Optimal? Yes 1+10+100+1000+10,000
• Time Complexity: O(bd), where d is the depth of the solution. 1+10+100+1000+10,000+100,000
• Space Complexity: O(bd), where d is the depth of the solution. = 123,456
11 12
Example: Farmer, Wolf, Goat, and
Example - Problem Design Cabbage Problem
State-space search problem A farmer with his wolf, goat, and cabbage come to the
• World can be in one of several states edge of a river they wish to cross.
• Moves can change the state of the world There is a boat at the river's edge, but, of course, only the
farmer can row.
Design/create data structures to represent The boat can carry a maximum of the farmer and one
the “world” other animal/vegetable.
If the wolf is ever left alone with the goat, the wolf will
Design/create functions representing eat the goat.
“moves” in the world Similarly, if the goat is left alone with the cabbage, the
goat will eat the cabbage.
Devise a sequence of crossings of the river so that all
four of them arrive safely on the other side of the river.
Artificial Intelligence
13 14
15 16
17 18
F WG C 19 F WG C 20
WG C G C W C WG WG C G C W C WG
F F W F G F C F F W F G F C
F W C F WG C
G
Search Tree for “Farmer, Wolf, Goat, Cabbage” Illegal State Search Tree for “Farmer, Wolf, Goat, Cabbage” Illegal State Repeated State
F WG C 21 22
F WG C F WG C
Initial State
WG C G C W C WG
F F W F G F C
W C
F G F
W
G
C Farmer takes goat to left bank
F W C F WG C
G
F W C
G
F W
G
C Farmer returns alone
W C C W
F G F WG F G C C
F WG F WG
C Farmer takes wolf to left bank
F C F W C F G C F W F WG F W C
F G C
WG G W GC C G
W
F
W
G C
Farmer returns with goat
G C C G WG G W G G
F W F WG F W C F C F W C F GC F W C F W C
Farmer takes cabbage to left
bank
F G F G
F G F WG F G C
W C C W
W C W C
Farmer returns alone
23 24
25
27
29 30
31
38
39 40
Start
Goal
46
A* search example
47 48
Example: 8-puzzle
51 52
; A better heuristic for the n-puzzle problem than # of (defun heuristic-eval-2 (state goal)
tiles out of place. "Sum of distances plus 2 times the number of tile reversals"
(+
(defun heuristic-eval-1 (state goal) (heuristic-eval-1 state goal)
"Sum the distance for each tile from its goal position." (* 2 (tile-reversals state goal)) ))
(do ; loop adding distance
((sum 0) (tile 1 (+ tile 1)));for tiles 1 to (n^2 - 1) ; Call the desired heuristic here from heuristic-eval
((equal tile (* *n* *n*)) (defun heuristic-eval (state goal)
sum) ; return sum when done (heuristic-eval-2 state goal))
(setq sum (+ sum (distance tile state goal)))) )
; TILE-REVERSALS - Return the number of tiles directly reversed between state and goal. 53 ; TILE-REVERSE - Return 1 if the tiles are reversed in 54
(defun tile-reversals (state goal)
the two states, else ; return 0. If one of the tiles is
"Calculate the number of tile reversals between two states"
(+
the blank, it doesn't count (return 0).
(do (defun tile-reverse (pos1 pos2 state1 state2)
((sum 0) (i 0 (1+ i))) ; loop checking row adjacencies
((equal i (- *n* 1)) sum) ; until i = n-2, return sum "Return 1 if the tiles in the two positions are
(do reversed, else 0"
((j 0 (1+ j)))
((equal j *n*) ) ; until j = n-1, add sum to return
(cond
(setq sum
((or
(+ sum
(tile-reverse (+ (* i *n*) j) (+ (* i *n*) j 1) (equal (nth pos1 state1) *blank*); blanks don't count
state goal)))))
(do
(equal (nth pos2 state1) *blank*))
((sum 0) (i 0 (1+ i))) ; loop checking column adjacencies
0) ; return 0
((equal i *n* ) sum) ; until i = n-1, return sum
(do ((and ; the tiles are reversed
((j 0 (1+ j)))
((equal j (- *n* 1)) ) ; until j = n-2, add sum to return
(equal (nth pos1 state1) (nth pos2 state2))
(setq sum
(equal (nth pos2 state1) (nth pos1 state2)))
(+ sum
(tile-reverse (+ (* i *n*) j) (+ (* i *n*) j 1) 1) ; return 1
state goal)))) )))
(t 0)) ) ; else return 0
Artificial Intelligence
55 56
h1(S) = ? 8
h2(S) = ? 3+1+2+2+2+3+3+2 = 18
Summary
Problem formulation usually requires abstracting
away real-world details to define a state space that
can feasibly be explored
Informed Searches
• Best-first search
• Greedy best-first search
• A* search
• Heuristics
Local search algorithms
• Hill-climbing search
• Simulated annealing search
• Local beam search
Genetic algorithms
Chapter 4 79