Title: Search
1Search
2Problem State Space
- What is Problem State Space?
- What is valid move within the space?
- Rule of the Game
- Characteristic of the State Space
- How to find the Solution Path in the State Space?
- Search Methods
3What is Problem State Space?
1
2
3
3
2
1
8
4
4
8
5
6
5
6
7
7
3
2
3
8
2
2
3
8
1
1
4
4
4
1
8
6
7
7
5
6
7
5
5
6
a
c
b
4State Space of 8-Puzzle(1)
- List Representation
- (2 0 3 1 8 4 7 6 5) Initial State
- (1 2 3 8 0 4 7 6 5) Goal State
- (0 2 3 1 8 4 7 6 5) Intermediate States
- (2 8 3 1 0 4 7 6 5)
- ..
5State Space of 8-Puzzle(2)
- Position Function
- Pos(1, 1) 2
- Pos(1, 2) 8
- .
- Or Predicate?
- Pos(1, 1, 2) True
- Empty(3, 2) True
6Valid Moves of 8-Puzzle
- List
- (2 0 3 1 8 4 7 6 5) ? (0 2 3 1 8 4 7 6 5)
- (2 0 3 1 8 4 7 6 5) ? (2 8 3 1 0 4 7 6 5)
- (2 0 3 1 8 4 7 6 5) ? (2 3 0 1 8 4 7 6 5)
- Predicate
- (empty 1 2) ? (pos 1 1 x) ?
- (empty 1 1) ? (pos 1 2 x)
7Water Jug Problem
- 2 water jugs 4 liter, 3 liter
- Need to get exactly 2 liters of water
- State space?
- (0, 0) initial (2, 0) Goal
- Valid move?
- (4,0) ?(1, 3) ? (0, 1)?(4, 1)? (2,3)? (2,0 )
- (0,3) ? (3, 0) ? (3, 3) ? (4, 2) ? (0, 2)
8Examples
- Missionaries Cannibals Problem
- Blocks world
- Tower of Hamoi
9Missionaries Cannibals Problem
- 3?? ???? 3?? ????(cannibal)? ? ? ??? ???.
- ??? ??? ?? ??? ???, ??? 1??? 1? ??? ? ?? 2?? ???
??? - ???? 1?? ?? ?????......
- ?? ???? ?? ????? ???(??? ????) ???? ???? ?? ????.
???? ??? ??? 6? ?? ?? ??? ??.
10 The Missionaries and Cannibals
Domain (defun goal-test (state) "The goal is to
have no missionaries or cannibals left on the
first side." ( 0 (m1 state) (c1
state))) (defun successors (state) "Return a
list of (action . state) pairs. An action is a
triple of the form (delta-m delta-c delta-b),
where a positive delta means to move from side
1 to side 2 negative is the opposite. For
example, the action (1 0 1) means move one
missionary and 1 boat from side 1 to side 2."
(let ((pairs nil)) (loop for action in '((1
0 1) (0 1 1) (2 0 1) (0 2 1) (1 1 1)
(-1 0 -1) (0 -1 -1) (-2 0 -1) (0 -2 -1) (-1 -1
-1)) do (let ((new-state (take-the-boat state
action))) (when (and new-state (not
(cannibals-can-eat? new-state))) (push (cons
action new-state) pairs)))) pairs))
- (defun main ()
- (print-actions (solve))
- 'done)
- (defun solve ()
- (labels ((iter (state path)
- (if (goal-test state)
- (reverse path)
- (let ((ss (remove-cyclic (successors state)
path))) - (and ss (some '(lambda (s)
- (iter (cdr s) (cons s path)))
- (suffle ss)))))))
- (iter (make-cannibal-state) (list (cons
'Begin (make-cannibal-state)))))) - (defun remove-cyclic (succs path)
- (remove-if '(lambda (s) (member s path test
'(lambda (a b) - (equal (cdr a) (cdr b))))) succs))
- (defun print-actions (path)
11(defstruct (cannibal-state (conc-name nil)
(type list)) "The state says how many
missionaries, cannibals, and boats on each
side. The components m1,c1,b1 stand for the
number of missionaries, cannibals and boats,
respectively, on the first side of the river.
The components m2,c2,b2 are for the other side of
the river." We need to represent both sides
(rather than just one as on p 68) because
we have generalized from 33 people to MC.
Incidently, we also generalized from 1 boat
to B boats. (m1 3) (c1 3) (b1 1) (m2 0) (c2 0)
(b2 0)) (defun take-the-boat (state action)
"Move a certain number of missionaries,
cannibals, and boats (if possible)."
(destructuring-bind (delta-m delta-c delta-b)
action (if (or (and ( delta-b 1) (gt (b1
state) 0)) (and ( delta-b -1) (gt (b2 state)
0))) (let ((new (copy-cannibal-state state)))
(decf (m1 new) delta-m) (incf (m2 new) delta-m)
(decf (c1 new) delta-c) (incf (c2 new) delta-c)
(decf (b1 new) delta-b) (incf (b2 new)
delta-b) (if (and (gt (m1 new) 0) (gt (m2 new)
0) (gt (c1 new) 0) (gt (c2 new) 0))
new nil)) nil))) (defun
cannibals-can-eat? (state) "The cannibals feast
if they outnumber the missionaries on either
side." (or (gt (c1 state) (m1 state) 0) (gt
(c2 state) (m2 state) 0)))
CL-USER 1 gt (main) Action M1 C1 B1 M2 C2
B2 BEGIN 3 3 1 0 0 0 (0 2 1)
3 1 0 0 2 1 (0 -1 -1) 3 2 1
0 1 0 (0 2 1) 3 0 0 0 3 1 (0
-1 -1) 3 1 1 0 2 0 (2 0 1) 1
1 0 2 2 1 (-1 -1 -1) 2 2 1 1 1
0 (2 0 1) 0 2 0 3 1 1 (0 -1 -1)
0 3 1 3 0 0 (0 2 1) 0 1 0 3
2 1 (-1 0 -1) 1 1 1 2 2 0 (1 1
1) 0 0 0 3 3 1 DONE CL-USER 2 gt
12Tower of Hamoi
- ??? ?? A? ???? ?? 3?? ?? C? ??? ???. ? ??? ???
??? ??. - ? ?? ??? ??? ??? ? ??
- ? ?? ?? ??? ??? ? ??
- ??? ?? ???? ? ??? ?? ? ??.
- ??? ??? ????? ??? ? ??? ?? ???? ??? ??.
133?? ??? ??? ??
14n?? ??? ??
- ??? ???
- n-1?? ??? A?? B? ???
- n?? ??? A?? C? ?? ??,
- n-1?? ??? B?? C? ???.
- ??? ??
- ??? ?? n
- ???? ???? from ? to
- ???? 1? tmp
- ???, ??? prototype
void hanoi_tower(int n, char from, char tmp, char
to)
15void hanoi_tower(int n, char from, char tmp, char
to) ?
- if (n1)
- 1. 1? ??? from?to ?, A?C? ???.
- else ngt1 ??
- 2. from? ? ?? ??? ??? (n-1)?? ??? from?tmp, ?
A?B? ???. - 3. 2??? ??? ?? from? 1???? from?to, A?C? ???.
- 4. ????? tmp? ???? to? ???.
-
16???? ??
include ltstdio.hgt void hanoi_tower(int n, char
from, char tmp, char to) if( n1 )
printf("?? 1? c ?? c?? ???.\n",from,to)
else hanoi_tower(n-1, from, to,
tmp) printf("?? d? c?? c?? ???.\n",n, from,
to) hanoi_tower(n-1, tmp, from, to) int
main(void) hanoi_tower(4, 'A', 'B', 'C')
17Characteristics of State Space
- Decomposable?
- goal -- subgoals
- Blind vs Informed (heuristic) Search
- Revocable vs Irrevocable
- backtracking
- Predictable vs Stochastic
- Optimal vs Plausible Solution
18Search Algorithm
- Guarantee to find a solution ?
- Always terminate ?
- Guarantee the solution to be optimal
- -- admissible
- Complexity - time and space
- How can the complexity be reduced ?
- Characteristics of the space?
State Space Search
19Strategies of SS Search
- Data-Driven vs. Goal Driven
- Forward chaining / Backward chaining
- Symptom ? diagnosis Forward
- eg. Fever, cough ? flu
- Goal/Sub-goal Backward
- eg. Good Husband?
- Rich? Or Handsome Edu
20Forward vs Backward
- When is forward (backward) better?
- Bi-Directional
- Factors deciding Forward/Backward
- - Size of the known states initial, goal
- small to big
- - Branching Factor converging
- - computational simplicity
- eg. Water jug problem
21Weak Methods
- General Purpose Control Strategies
- Simplicity is the KEY of Weak Methods
- Usually
- - Not Optimal
- - No Backtracking
- - No Guarantee to the Solution
- Generate Test
- Hill Climbing
- Best First Search
- Means-Ends Analysis
- Etc.
22Heuristic Function
- Heuristics Knowledge obtained from human
experience - Heuristic function h SS ? number
- a preference measure for each position
of the state space - What is the heuristic function for 8-puzzle?
- Necessary for Informed Search (cf. Blind)
23Generate Test
- British Museum Algorithm
- Guess a Solution Test if it is right
- DENDRAL molecular structure analysis
- plan-generate-test (constraint)
- AM (Automated Mathematician)
- Number Theory generate a conjecture
- Generate Test is especially useful when
combined with other strategy
24Hill Climbing
- Greed Method
- Iterative Improvement Algorithm
- Select the Best Looking Candidate
- Use heuristic function
- Stop when no more better looking candidates ?
Assume it is the Goal! - No Backtracking Simplicity!!
25Hill Climbing Algorithm
- Hill-climbing Attempt to maximize Eval(X) by
moving to - the highest configuration in our moveset.
-
- 1. Let X initial config
- 2. Let E Eval(X)
- 3. Let N moveset_size(X)
- 4. For ( i 0 iltN i i1)
- Let Ei Eval(move(X,i))
- 5. If all Eis are E, terminate, return X
- 6. Else let i argmaxi Ei
- 7. X move(X,i)
- 8. E Ei
- 9. Goto 3
26Hill Climbing Issues
- Low Memory Requirement
- -- No backtracking
- Moveset (next move) design is critical
- Evaluation Function design is critical
- If the number of move is too big
- -- Inefficient
- Too small?
- -- Easily stuck (plateau)
27Problems of Hill Climbing
- Local Maxima (?????)
- Plateau(??)
- Ridge(????)
28Avoiding Problems in H.C.
- Many Starting Points
- Random Jump Mutation
- (Genetic Algorithm)
- Simulated Annealing
- Stochastic Method
29Simulated Annealing Algorithm
- 1. Let X initial config
- 2. Let E Eval(X)
- 3. Let i random move from the moveset
- 4. Let Ei Eval(move(X,i))
- 5. If E lt Ei then
- X move(X,i)
- E Ei
- Else with some probability, accept the move
even though - things get worse
- X move(X,i)
- E Ei
- 6. Goto 3 unless bored.
30Simulated Annealing
- If Ei gt E then definitely accept the change.
- If Ei lt E then accept the change with
probability - exp (-(E - Ei)/Ti) (called
the Boltzman distribution) - where Ti is a temperature
parameter that - gradually decreases.
- High temp accept all moves (Random Walk)
- Low temp Stochastic Hill-Climbing
- When enough iterations have passed without
improvement, - terminate.
31Means Ends Analysis(1)
- Bi-Directional Method
- GPS(General Problem Solver) Newell
- Operator, Pre-condition, Result Table
- Choose the operator that reduce the difference
most - Recursively reduce the first and rest parts with
other operators
32Means Ends Analysis(2)
- GPS(I, G, O) Iinitial, G goal, O op table
- select o from O which reduce the
difference - GPS(I, pre-condition(o), O)
- print(o)
- GPS(result(o), G, O)
- end
- Going to Central Park, N.Y
- GPS(Konkuk, C.Park, O-Table)
- Operation Table
- op pre
result - -------------------------------------------
----- - AirPlane Inchon J.F.K
- AirBus ????? Inchon
- Taxi Konkuk
Shinchon - Subway J.F.K
C.Park
33Implementing Search(1)
- Blind Search
- -- Search does not depend on the nature of
the positional value - -- Systematic Search Method
- Breadth-First Search
- Depth-First Search
- Iterative Deepening Search
- Lowest Cost First Search
- Heuristic Search
34Implementing Search(2)
- Blind Search
- Heuristic Search
- Hill Climbing
- Best First Search
- A Algorithm
35Blind Search
- Abstract Data Type - Tree
- (defun make-TREE(label value children)
- (list tree label value children))
- (defun TREE-label (tree) (second tree))
- (defun TREE-value(tree) (third tree))
- (defun TREE-children(tree)(fourth tree))
- (defun TREE-print(tree)
- (princ(TREE-label tree)))
36Depth First Search
- (defun dfs(nodes goalp next)
- (cond ((null nodes) nil)
- ((funcall goalp (first nodes)) (first
nodes)) - (t (dfs (append (funcall next (first
nodes)) - (rest
nodes)) - goalp
- next))))
37Function DFS
- gt (setq tree
- (make-TREE a 6
- (list (make-TREE b 3
- (list(make-TREE d 5 nil)
- (make-TREE e 4
nil)))))) - //? ???? ??? ??? ???
- gt (dfs (list tree)
- (lambda(x) (TREE-print x)
- (eq g
(TREE-label x))) - TREE-children)
38Depth-First Search Example
39Breadth First Search
- (defun bfs(nodes goalp next)
- (cond ((null nodes) nil)
- ((funcall goalp (first nodes)) (first
nodes)) - (t (bfs (append (rest nodes)
- (funcall next
(first nodes))) - goalp
- next))))
40Breadth-First Search Example
41Comparison of BFS and DFS
- BFS always terminate if goal exist
- cf. DFS on infinite tree
- BFS Guarantee shortest path to the goal
(admissible) - eg. Multiple Goals
- Space requirement
- BFS - Exponential
- DFS - Linear
- Which is better ? BFS or DFS ?
42Iterative Deepening Search
- Compromise of BFS and DFS
- Branch and Bound
- Set the Limit then DFS
- If Fail, extend the limit (one step)
- Save on Storage, guarantee shortest path
43Function ids
- (defun ids (start goalp next depth)
- (or (dfs-fd start goalp next 0 depth)
- (ids start goalp next (1 depth))))
- (defun dfs-fd(node goalp next depth max)
- (cond((funcall goalp node) node)
- (( depth max) nil)
- (t (some (lambda (n)
- (dfs-fd n goalp next
(1 depth) max)) - (funcall next node)))))
44Heuristic Search
- Blind search assumes no information about the
domain Too expensive - Heuristic Information can be helpful
- Heuristic Function
- f(n) g(n) h(n)
- f total cost
- g cost from start to node n (real
value) - h cost from node n to goal
- (usually unknown)
45Lowest Cost First Search
- Borderline between Blind and Heuristic Search
- Cost value is assigned to each arc
- Moveset (candidate list) is a priority queue
ordered by path cost - Eg. Dijkstras algorithm
- When Arc cost is unit value, BFS
46Best First Search
- Moveset (candidate list) is a priority queue
ordered by heuristic cost h(n) - Does not guarantee the Shortest Path
- Does not always terminate even though there is a
solution - Performance depends on the h function
47Best First Search - Program
- (defun best(nodes goalp next comparep)
- (cond ((null nodes) nil)
- ((funcall goalp (first nodes)) (first
nodes)) - (t (best (sort (append
- (funcall next
(first nodes)) - (rest nodes))
- comparep)
- goalp
- next
- comparep))))
48A Algorithm
- Best First Search only considers h
- Lowest Cost First only considers g
- A algorithm takes f (note. f g h)
- A is optimal if h never overestimates the cost
(optimistic) - - h is admissible
- See the extra slide about A algorithm
49And/Or Graph
- Divide a problem into sub-problems and solve
them individually - divide and conquer
- And/Or Graph
- Node sub-problems
- Links And Link, Or Link
- And connect parent and sub-problems
- Or represents alternatives
50Searching And/Or Graph(1)
- Objective of Search
- To show whether start node is Solvable?
- or Unsolvable ?
- Definition of Solvable
- Terminal node is solvable
- A non-terminal OR node is solvable if at least
one of its successor is solvable - A non-terminal AND node is solvable iff all of
its successors are solvable
51Searching And/Or Graph(2)
- Definition of UnSolvable
- Non-Terminal node with no successor is unsolvable
- A non-terminal OR node is unsolvable iff all of
its successors are unsolvable - A non-terminal AND node is unsolvable if at least
one of its successors is unsolvable - Search terminate when start node is labeled
either solvable or unsolvable
52Island Driven Search
- Idea Find a set of islands between s, g
- s ? i1 ? i2 ? . ? im-1 ? g
- Problem is to identify the islands
- Optimal solution not guaranteed
- Hierarchical abstraction can be used
53Game Tree Search
- Game Tree Special case of AND/OR Graph
- Objective of Game Tree Search
- To find good first move
- Static Evaluation Funtion e
- Measure the worth of a tip node
- if p is win for MAX, then e(p) infinity
- if p is win for MIN, then e(p) - infinity
54MiniMax Procedure
- Select the maximum worth alternative
- Under Assumption of that the opponent do his best
- Back-Up Value(BUV)
- 1. At tip node p, BUV is e(p)
- 2. At max node, BUV is max BUV of children
- 3. At min node, BUV is min BUV of children
55MiniMax for Tic-Tac-Toe
- Two player, X and O, X play first
- Static Evaluation function
- If p is not a winning position
- e(p) (complete rows, columns, or diagonal that
are still open for X) - (complete rows,
columns, or diagonal that are still open for O) - If p is a win for X, e(p) infinity
- if p is a win for O, e(p) -infinity
561
-1
X
X
1
X
O
O
-2
6 - 5
X
X
X
O
X
X
O
5 - 4
6 - 4
O
X
5 - 5
6 - 5
O
X
5 - 5
O
O
O
O
X
X
O
X
X
X
O
4 - 5
5 - 6
5 - 5
5 - 6
5 - 5
4 - 6
57Alpha-Beta Procedure
- Improvement of MiniMax Procedure
- combining search procedure evaluation
max
S
A
B
C
1
-2
-1
0
1
58Production System
- Model of Search and Human Problem Solving
- Heavily used in AI (Expert) system
- Production System components
- Production rules (or production)
- also called Long-term memory
- Working Memory
- also called Short-term memory
- Recognize-act cycle
- also called control structure, engine
59Production Rules
- Production
- condition part and action part
- premise and conclusion
- LHS and RHS
- Condition Part - pattern matching
- Action Part - problem solving step
- Single chunk of knowledge
- Good for representing Judgmental knowledge
60Control of Production System
- Data Driven vs Goal Driven
- Forward vs Backward Search
- Bidirectional Search