Title: Search as a problem solving technique.
1Search as a problem solving technique.
- Consider an AI program that is capable of
formulating a desired goal - based on the analysis of the current world state.
To reach its goal, - the program must be able to formulate a search
problem by - providing a description of the current world
state, - providing a description of its own actions that
can transform one world state into another one, - providing a description of the goal state where
the desired goal holds. - Problem solution consists of finding a path from
the current state to - the goal state. To define how good the solution
is, a path cost - function can be assigned to the path. Different
solutions to the same - problem can be compared by means of the
corresponding path cost - functions.
2Example the missionaries and cannibals problem.
- Description of the current state a sequence of
six numbers, representing the number of
missionaries, cannibals and boats on each bank of
the river. Assuming 3 missionaries, 3 cannibals
and one boat, the initial state is - (setf start '(3 3 1 0 0 0))
- Description of possible actions (or operators)
take either one missionary, one cannibal, two
missionaries, two cannibals, or one of each
across the river in the boat, i.e. - (setf list-of-actions '((1 0 1) (0 1 1) (2 0 1)
(0 2 1) (1 1 1))) - Description of the goal state, i.e.
- (setf finish '(0 0 0 3 3 1))
- Note that some world states are illegal (the
number of cannibals must always - be less or equal to the number of missionaries on
each side of the river. - Therefore, we must impose certain constraints on
the search to avoid illegal - states. We also must guarantee that search will
not fall in a loop (some actions - may undo the result of a previous action).
3Problem space is a complete description of the
domain, and is only procedurally defined.
3,3,1,0,0,0
3,1,1,0,2,0
3,1,0,0,2,1
3,2,1,0,1,0
3,2,0,0,1,1
3,0,0,0,3,1
3,0,1,0,3,0
2,2,0,1,1,1
2,3,0,1,0,1
1,3,0,2,0,1
2,2,1,1,1,0
2,1,0,1,2,1
2,0,1,1,3,0
2,1,1,1,2,0
2,3,1,1,0,0
1,3,1,2,0,0
1,1,1,2,2,0
2,0,0,1,3,1
1,2,1,2,1,0
1,0,1,2,3,0
1,1,0,2,2,1
1,2,0,2,1,1
0,3,0,3,0,1
0,2,1,3,1,0
0,1,0,3,2,1
0,1,1,3,2,0
3,3,0,0,0,1
0,3,1,3,0,0
0,2,0,3,1,1
3,3,1,0,0,0
4Search (or solution) space is a part of the
problem space which is actually examined
3,3,1,0,0,0
1,1,1
0,2,1
0,1,1
2,2,0,1,1,1
3,2,0,0,1,1
3,1,0,0,2,1
1,0,1
0,1,1
Dead end
3,2,1,0,1,0
3,2,1,0,1,0
1,0,1
0,1,1
0,2,1
0,2,1
3,0,0,0,3,1
3,1,0,0,2,1
3,0,0,0,3,1
2,2,0,1,1,1
Dead end
Dead end
...
...
5Depth-first search always expand the path to one
of the nodes at the deepest level of the search
tree
- Each path is a list of states on that path, where
each state is a list of six - elements (m1 c1 b1 m2 c2 b2). Initially, the only
path contains only the start - state, i.e. ((3 3 1 0 0 0)).
- (defun depth-first (start finish optional (queue
(list (list start)))) - (cond ((endp queue) nil)
- ((equal finish (first (first queue)))
- (reverse (first queue)))
- (t (depth-first start finish
- (append (extend (first queue))
(rest queue)))))) - (defun extend (path)
- (print (reverse path))
- (setf extensions (get-extensions path))
- (mapcar '(lambda (new-node) (cons new-node
path)) - (filter-extensions extensions
path)))
6Breadth-first search always expand all nodes at
a given level, before expanding any node at the
next level
- (defun breadth-first (start finish optional
- (queue (list (list
start)))) - (cond ((endp queue) nil)
- ((equal finish (first (first queue)))
- (reverse (first
queue))) - (t (breadth-first start finish
- (append (rest queue)
(extend (first queue))))))) - (defun extend (path)
- (print (reverse path))
- (setf extensions (get-extensions path))
- (mapcar '(lambda (new-node) (cons new-node
path)) - (filter-extensions extensions path)))
7Depth-first vs breadth-first search
- Depth-first search
- 1. Space complexity O(bd), where b is the
branching factor, and d is the depth of the
search. - 2. Time complexity O(bd).
- 3. Not guaranteed to find the shortest path (not
optimal). - 4. Not guaranted to find a solution (not
complete) - 5. Polinomial space complexity makes it
applicable for non-toy problems.
- Breadth-first search
- 1. Space complexity O(bd)
- 2. Time complexity O(bd).
- 3. Guaranted to find the shortest path (optimal).
- 4. Guaranted to find a solution (complete).
- 5. Exponential space complexity makes it
impractical even for toy problems.
8Informed search strategies best-first greedy
search
- Best-first search always expends the node that is
believed to be the closest to - the goal state. This is defined by means of the
selected evaluation function. - Example consider the following graph whose nodes
are represented by means - of their property lists
- (setf (get 's 'neighbors) '(a d)
- (get 'a 'neighbors) '(s b d)
- (get 'b 'neighbors) '(a c e)
- (get 'c 'neighbors) '(b)
- (get 'd 'neighbors) '(s a e)
- (get 'e 'neighbors) '(b d f)
- (get 'f 'neighbors) '(e))
- (setf (get 's 'coordinates) '(0 3)
- (get 'a 'coordinates) '(4 6)
- (get 'b 'coordinates) '(7 6)
- (get 'c 'coordinates) '(11 6)
- (get 'd 'coordinates) '(3 0)
- (get 'e 'coordinates) '(6 0)
9- To see the description of a node, we can say
- (describe 'a)
- ........
- Property COORDINATES, Value (4 6)
- Property NEIGHBORS, Value (S B D)
- To find how close a given node is to the goal, we
can use the formula computing - the straight line distance between the two nodes
- defun distance (node-1 node-2)
- (let ((coordinates-1 (get node-1 'coordinates))
- (coordinates-2 (get node-2
'coordinates))) - (sqrt ( (expt (- (first
coordinates-1) - (first
coordinates-2)) - 2)
- (expt (- (second
coordinates-1) - (second
coordinates-2)) - 2)))))
10- Given two partial paths, whose final node is
closest to the goal, can be - defined by means of the following closerp
predicate - (defun closerp (path-1 path-2 finish)
- (lt (distance (first path-1) finish)
- (distance (first path-2) finish)))
- The best-first search now means expand the path
believed to be the closest to - the goal, i.e.
- (defun best-first (start finish optional (queue
(list (list start)))) - (cond ((endp queue) nil)
- ((equal finish (first (first queue)))
(reverse (first queue))) - (t (best-first start finish
- (sort (append (extend
(first queue)) (rest queue)) '(lambda (p1 p2)
(closerp p1 p2 finish))))))) - (defun extend (path)
- (print (reverse path))
- (mapcar '(lambda (new-node) (cons new-node
path))
11Iterative improvement methods hill-climbing
search
- If the current state contains all the information
needed to solve the problem, - then we try the best modification possible to
transform the current state into - the goal state.
- (defun hill-climb (start finish optional (queue
(list (list start)))) - (cond ((endp queue) nil)
- ((equal finish (first (first queue)))
(reverse (first queue))) - (t (hill-climb start finish
- (append (sort (extend
(first queue)) - '(lambda (p1
p2) (closerp p1 p2 finish))) (rest queue))))))
12Iterative improvement methods beam search
- Beam search is similar to breadth-first, but it
only extends a specified number - of best paths, where best means closest to
the goal. That is, beam search - looks at a small number of ways to continue.
- (defun beam (start finish width optional
- (queue (list (list
start)))) - (setf queue (butlast queue (max (- (length
queue) width) 0))) trim the queue to the
required width - (cond ((endp queue) nil)
- ((equal finish (first (first
queue))) (reverse (first queue))) - (t (beam start finish width (sort
(apply 'append (mapcar 'extend queue)) - '(lambda (p1 p2)
(closerp p1 p2 finish)))))))
13Iterative breadth-first example from the book
- Although the most natural implementation of the
breadth-first search is the - recursive one, sometimes because of the
efficiency considerations it can - be implemented iteratively.
- (defun bsolve (initial)
- (do ((queue (list (list initial initial))
first parameter - (append (cdr queue)
new-paths)) - (new-paths nil nil))
second parameter - ((null queue))
termination test - (when (goal-recognizer (caar queue))
- (return (values (caar queue)
(cdar queue)))) - (setq new-paths (expand-path (car
queue))))) - (defun goal-recognizer (state)
- (if (eql state goal) t nil))