Search
description
Transcript of Search
SearchSearch
Problem State SpaceProblem 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
What is Problem State What is Problem State Space? Space?
8-Puzzel
23
45 67
8 111
11
22
22
3
3 3
344
44
5
55
566
66
77
77
8
8
8 8
a b c
State Space of 8-State Space of 8-Puzzle(1)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) …..
State Space of 8-State Space of 8-Puzzle(2)Puzzle(2)
Position Function Pos(1, 1) = 2 Pos(1, 2) = 8 ….Or Predicate? Pos(1, 1, 2) = True Empty(3, 2) = True
Valid Moves of 8-PuzzleValid 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)
Water Jug ProblemWater 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)
ExamplesExamples
Missionaries & Cannibals Problem
Blocks world
Tower of Hamoi
Missionaries & Missionaries & Cannibals ProblemCannibals Problem
3 명의 선교사와 3 명의 식인괴물 (cannibal)이 강 한 쪽편에
있어요 .
보트를 이용해 강을 건너야 하는데 , 보트는 1 개이고 1 번 건너갈
때 최대 2 명을 태울수 있어요
그러니까 1 명은 노를 저어야하죠 ......
만약 식인괴물 수가 선교사보다 많으면 ( 같으면 괜찮아요 )
선교사가 괴물한테 잡아 먹힙니다 . 선교사를 무사히 살려서 6 명
모두 강을 건너는 퀴즈 .
(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) (format t "~&~10A ~{~4S~}" "Action" '(m1 c1 b1 m2 c2 b2)) (mapc #'(lambda (p) (format t "~&~10A~{~4d~}" (car p) (cdr p))) path))
(defun suffle (lst) (sort (copy-list lst) #'> :key #'(lambda (x) (declare (ignore x)) (random 1.0))))
;;;; 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))
(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 3+3 people to M+C. 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) (> (b1 state) 0)) (and (= delta-b -1) (> (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 (>= (m1 new) 0) (>= (m2 new) 0) (>= (c1 new) 0) (>= (c2 new) 0)) new nil)) nil)))
(defun cannibals-can-eat? (state) "The cannibals feast if they outnumber the missionaries on either side." (or (> (c1 state) (m1 state) 0) (> (c2 state) (m2 state) 0)))
CL-USER 1 > (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 1DONE
CL-USER 2 >
Tower of HamoiTower of Hamoi
문제는 막대 A 에 쌓여있는 원판 3 개를 막대 C 로 옮기는 것이다 . 단 다음의
조건을 지켜야 한다 .
한 번에 하나의 원판만 이동할 수 있다
맨 위에 있는 원판만 이동할 수 있다
크기가 작은 원판위에 큰 원판이 쌓일 수 없다 .
중간의 막대를 임시적으로 이용할 수 있으나 앞의 조건들을 지켜야 한다 .
A B C
A B C
A B C
A B C
A B C
33 개의 원판인 경우의 해답개의 원판인 경우의 해답
A B C
A B C
A B C
A B C
1. 3 개중 위 2 개의 원판을 A->B
2. A 의 제일 큰 원판을 A->C
3. B 에 있는 2 개의 원판을 B->C
nn 개의 원판인 경우개의 원판인 경우 문제의 일반화
n-1 개의 원판을 A 에서 B 로 옮기고
n 번째 원판을 A 에서 C 로 옮긴 다음 ,
n-1 개의 원판을 B 에서 C 로 옮긴다 .
필요한 변수 원판의 개수 : n
어디에서 어디로 ? : from to
임시막대 1 개 : tmp
따라서 , 함수의 prototype
A B C
n-1 개의 원판
1 개의 원판
A B C
A B C
A B C
void hanoi_tower(int n, char from, char tmp,
char to)
if (n==1)
1. 1 번 원판을 fromto 즉 , AC 로 옮긴다 .
else n>=1 이면 {
2. from 의 맨 아래 원판을 제외한 (n-1) 개의 원판을 fromtmp, 즉 AB 로 옮긴다 .
3. 2 번에서 옮기지 않은 from 의 1 번원판을 fromto, AC 로 옮긴다 .
4. 임시막대인 tmp 의 원판들을 to 로 옮긴다 .
}
15
void hanoi_tower(void hanoi_tower(intint n, n, charchar from, from, charchar tmp, tmp, charchar to) ? to) ?
2. hanoi_tower(n-1, from, to, tmp);
4. hanoi_tower(n-1, tmp, from, to);
하노이탑 실습 하노이탑 실습
16
#include <stdio.h>
void hanoi_tower(int n, char from, char tmp, char to)
{
if( n==1 ) 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');
}
Characteristics of State Characteristics of State SpaceSpace
Decomposable? goal -- subgoals Blind vs Informed (heuristic) Search Revocable vs Irrevocable backtracking Predictable vs Stochastic Optimal vs Plausible Solution
Search AlgorithmSearch 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
Strategies of SS SearchStrategies 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
Forward vs BackwardForward 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
Weak MethodsWeak 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.
Heuristic FunctionHeuristic 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)
Generate & TestGenerate & 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
Hill ClimbingHill 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!!
Hill Climbing AlgorithmHill 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 ; i<N ; i := i+1)
Let Ei := Eval(move(X,i))
5. If all Ei’s are ≤ E, terminate, return X
6. Else let i* = argmaxi Ei
7. X := move(X,i*)
8. E := Ei*
9. Goto 3
Hill Climbing IssuesHill 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)
Problems of Hill Problems of Hill ClimbingClimbing
Local Maxima ( 단기최적화 )
Plateau( 고원 )
Ridge( 산등성이 )
Avoiding Problems in Avoiding Problems in H.C.H.C.
Many Starting Points Random Jump : Mutation (Genetic
Algorithm) Simulated Annealing Stochastic Method
Simulated Annealing Simulated Annealing AlgorithmAlgorithm
1. Let X := initial config2. Let E := Eval(X) 3. Let i = random move from the moveset4. Let Ei := Eval(move(X,i))5. If E < 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 := Ei6. Goto 3 unless bored.
Simulated AnnealingSimulated Annealing
If Ei >= E then definitely accept the change. If Ei < 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.
Means Ends Analysis(1)Means 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
Means Ends Analysis(2)Means Ends Analysis(2)
GPS(I, G, O); I:initial, 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
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
Implementing Search(1)Implementing 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
Implementing Search(2)Implementing Search(2)
Blind Search Heuristic Search * Hill Climbing * Best First Search * A* Algorithm
Blind SearchBlind 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)))
Depth First SearchDepth 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))))
Function DFSFunction DFS
> (setq tree (make-TREE ‘a 6 (list (make-TREE ‘b 3 (list(make-TREE ‘d 5 nil) (make-TREE ‘e 4
nil)))))) // 더 추가해서 만들어 보도록 하세요
> (dfs (list tree) #’(lambda(x) (TREE-print x) (eq ‘g (TREE-label
x))) #’TREE-children)
1
2 3 4
5 6 7 8 9
Depth-First Search ExampleDepth-First Search Example
Breadth First SearchBreadth 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))))
1
2 3 4
5 6 7 8 9
Breadth-First Search ExampleBreadth-First Search Example
Comparison of BFS and Comparison of BFS and DFSDFS
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 ?
Iterative Deepening Iterative Deepening SearchSearch
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
Function idsFunction 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)))))
Heuristic SearchHeuristic 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)
Lowest Cost First SearchLowest 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. Dijkstra’s algorithm When Arc cost is unit value, BFS
Best First SearchBest 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
Best First Search - Best First Search - ProgramProgram
(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))))
A* AlgorithmA* 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
And/Or GraphAnd/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
Searching And/Or Searching And/Or Graph(1)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
Searching 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
Island Driven SearchIsland 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
Game Tree SearchGame 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
MiniMax ProcedureMiniMax 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 children3. At min node, BUV is min BUV of children
MiniMax for Tic-Tac-ToeMiniMax for Tic-Tac-Toe
Two player, X and O, X play first Static Evaluation function
If p is not a winning positione(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
OO
O
O
O
O
X
X
X
X
X
X
XX
XX
XXXXX
O
O
OOO
O
6 - 5
5 - 5
6 - 5
5 - 5
4 - 5
-1
-2
1
5 - 6 5 - 5 5 - 6 5 - 5 4 - 6
5 - 4 6 - 4
1
Alpha-Beta ProcedureAlpha-Beta Procedure
Improvement of MiniMax Procedure combining search procedure &
evaluation
AB
S
1 0 -1 1 -2
max
C
Production SystemProduction 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
Production RulesProduction 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
Control of Production Control of Production SystemSystem
Data Driven vs Goal Driven Forward vs Backward Search Bidirectional Search