CS 385 Fall 2006 Chapter 6 Building Control Algorithms for State Space Search Read 6.0- 6.2 1 Summary of Chapters 3 and 4 1. Problem solving is search through a set of problem states 2. The state space model uses graph theory to represent the problem as states and transitions 3. Characteristics of state space search a. Solution is a path from start state to a goal. b. Search systematically tests alternative paths to the goal c. Backtracking allows recovery from paths that fail to reach the goal. d. Lists track states under consideration. e. DFS uses a stack, BFS uses a queue, BFS uses a priority queue 2 Formal Tools Predicate Calculus: 1. A language for capturing complex semantic information and for reasoning based on that information. (formal semantics and inference rules). 2. One could formulate a problem and perform reasoning by hand. State-Space Search: 1. States of a problem are nodes, problem transitions are arcs, a solution is a path from start node to goal. 2. Methods of searching state spaces (dfs, bfs, heuristics). 3. Data-driven versus goal-driven (where on the graph do you start). 3 Tools (cont) Now: We have a formal way to describe a problem using predicate calculus. Next: Automate the search process via an algorithm (patterndirected search) which will be applied to a space of logical inferences. • Recursion-based search • Production systems • Blackboard architecture (omit) 4 6.1.1 Recursion A natural for search Algorithms are easier to write easier to analyze formally Recursion consists of a base case a recursive component that resolves into a smaller version of the problem and ultimately to the base case 5 Recursion for List Membership function member(item, tail) //Pascal syntax begin if list is empty then return(fail) else if item = first item of list then return(success) else begin tail=list with first item removed member(item, tail) end end member(X, [X|T]). member(X,[Y|T]) :- member(X,T). //PROLOG 6 DFS without/with Recursion procedure depth-first search function depthsearch(current_state) open = [start] if current_state is a goal closed = [ ] while open ≠ [ ] do remove leftmost state of open, call it X if X is goal, return success. generate all children of X put X on closed put children of X not already on open or closed on the left end of open then return success add current_state to closed while current_state has unexmd children child := next unexmd child if depthsearch(child) = success then return(success) return(fail) 7 Trace DFS without/with Recursion goal= 6: call closed depthsearch(1) [1] open closed [1] [] [2,3] [1] depthsearch(4) [4,2,1] [4,5,3] [2,1] depthsearch(5) [5,4,2,1] [5,3] [4,2,1] [3] [5,4,2,1] [6, 7] depthsearch(2) 1 2 4 [2,1] depthsearch(3) 3 5 6 depthsearch(6) 7 ←success ←success Notes: 1. The path in maintained in the nested procedure calls. 2. To keep track of the path, retrieve at each successful exit. 3. The open list on left is kept by the procedure activations (e.g. you retain the information necessary to go back to pick up other children). 4. Children are not generated until needed. This is good if it is expensive to 8 generate them. DFS in PROLOG? Represent the state space: Trace: % a binary tree move(1,5)? move(1, 2). (1): move(1,2) move(1, 3). dfs(2,5)? move(2, 4). (3): move(2,4) move(2, 5). move(3, 6). move(3, 7). %depthfirst with no output dfs1(X, X). dfs1(X, Y) :move(X, Z), dfs1(Z, Y). dfs(4,5) fail (4): move(2,5) dfs(5,5) succeed Problems: no output %depthfirst with output dfs2(X, Y) :move(X, Z), write('trying node '), write(Z), nl, dfs2(Z, Y). 9 6.1.2 Pattern-Directed Search Apply recursive search to develop a general search procedure for predicate calculus. How is goal-directed search for predicate calculus recursive? identify subgoal(s) that imply the goal recur on the subgoal(s) success if a subgoal matches a fact in the knowledge base 10 This leaves out two possibilities: • current goal is negated ( p) - -success if the goal fails • current goal is a disjunction (p..) -- success if all are satisfied with consistent bindings And directions for consistent bindings Rewritten on p.198 11 Prove h using pattern_search: a b c a b→d a c→e b d→f f→g a e→h (unifies with a conclusion apply to premise e a) goal(h) goal(e) goal(c) (unifies with a conclusion apply to premise c a) (unifies with a fact) true goal(a) (unifies with a fact) true ← e is true goal(a) (unifies with a fact) true ← h is true 12 Production Systems Pattern-directed search: based on a set of production rules (similar to but different from a rep. in predicate calculus). Production system: a system for pattern-directed control of problem solving. A production system consists of 1. A set of production rules (productions) 2. Working memory 3. Recognize/act cycle 13 6.2 Production Systems 1. A set of production rules (productions) (condition/action) pairs condition: when the action can be applied action: the problem-solving step (add something to known facts or do something) if it is cold or rainy then drive to work p(x) q(x) 2. Working memory the current state of the world what's true production rules can alter this 14 Production Systems 3. Recognize/act cycle match patterns in working memory against conditions of production rules. conflict set = those rules with matches select one member of conflict set to fire (perform the action) this will change the contents of the working memory Conflict resolution: pick which rule to fire. Often the first whose condition matches current state. Pure production systems: no way to recover from dead ends (backtrack) 15 Rewrite Rules for Sorting a String Rewrite rules for sorting a string (productions) 1. ba ab 2. ca ac 3. cb bc Task: sort cbaca Iteration Working memory Conflict set Rule fired 0 cbaca 123 3 1 bcaca 2 2 2 bacca 12 2 3 bacac 12 2 4 baacc 1 1 5 abacc 1 1 6 aabcc 16 Using the Rules in in Reverse Order Rewrite rules for sorting a string (productions) 1. ba ab 2. ca ac 3. cb bc Iteration Working memory Conflict set Rule fired 0 cbaca 123 3 1 bcaca 2 2 2 bacca 12 2 3 bacac 12 2 4 baacc 1 1 5 abacc 1 1 6 aabcc 17 Production System: the Knights Tour Can a knight (chess) visit each square on a board once? productions: knight on 1 move knight to 8, ... Is there a path from one square to another? control algorithm: X path(X,X) X,Y path(X,Y) ← Z [move(X,Z) path(Z,Y)] 18 Knights Tour Is there a path from 1 to 3? Iteration Current Goal Conflict set Fire square square 0 1 3 3, 4 3 1 8 3 13, 14 13 2 3 3 rule done! working memory 19 Knights Tour Is there a path from 2 to 3? Iteration Current Goal Conflict set Fire square square 0 2 3 3, 4 3 1 9 3 15, 16 15 2 2 3 3, 4 3 3 9 3 13, 14 13 rule working memory 20 Production Systems Newell and Simon: production systems are a model for how humans solve problems. Why? rules long term memory, working memory (state of the world) short term memory 21 Control of Search in Production Systems Review: production systems more general than pattern directed search sometimes easier to formulate. This section: production systems allow opportunities to add heuristic control to search. (pattern-directed as implemented by pure PROLOG allowed arrangement and structure of rules, but not much else) 1. Data driven versus goal driven: compare Figures 6.9 and 6.10 on next slides Actually many strategies are combinations. MYCIN: starts with symptoms and forward chains (data driven). If at some point it suspects a disease, backward chain (goal driven). 22 Figure 6.9: Data-driven search in a production system. 23 Figure 6.10: Goal-driven search in a production system. 24 Control of Search in Production Systems 2. Through the rule structure. E.g. the order of premises. Compare gpa_over_3(X) application_finished(X) candidate(X) application_finished(X) gpa_over_3(X) candidate(X) Harder to finish application than to check the gpa Checking gpa first saves effort. 3. Through conflict resolution. Note, move(1,8) is before move(1.6) in knight’s tour. Often conflict resolution is by order of the rules so that matters. knight0: no control, path(1,7) works, path(1,4) and path(1,9) loop knight1: put a footstep where you have been. knight2: keep a list. 25 Knights Tour in PROLOG knight0.pro: %productions path(1,7): move(1,6). move(1,6) move(1,8). try path(6,7) move(2,7). move(2,9). ... move(6,1) try path(7,2) %control move(7,6) path(Z,Z). try path(6,2) path(X,Y):move(X,W), path(W,Y). Whoops. Trace path(1,7): 26 Knights Tour in PROLOG Resolution (knight1.pro): path(X,Y):- move(X,W), not(been(W)), assert(been(W)), path(W,Y). But this adds been(6) to the database Try path(1,6) twice and the second time fails knight2.pro: path(Z,Z,L). path(X,Y,L):move(X,Z), not(member(Z, L)), path(Z, Y, [Z|L]). 27 Trace path(1,7, [1]): path(1, 7, [1]). move(1, 6) not member(6, [1]) succeeds path(6, 7, [6, 1] move(6, 1) not member(1, [1]) fails move(6,7) knight2.pro move(1,6). move(1,8). move(2,7). move(2,9). move(3,4). move(3,8). move(4,3). move(4,9). move(6,1). move(6,7). move(7,2). move(7,6). move(8,1) move(8,3). move(9,2). move(9,4). not member(7, [6,1]) succeeds path(7, 7, [7, 6, 1]) writelist([7, 6, 1]) path(Z,Z,L). path(X,Y,L):move(X,Z), not(member(Z, L)), path(Z, Y, [Z|L]). Ch 15 has a longer one. Try yourself. How does the result get printed in knight2.pro? 28 Blackboard Architecture (omit) Cooperating knowledge sources that share information and results Communication through a blackboard. Really a way of breaking a problem into smaller ones 29 Homework 1a 4. Draw the nodes and arcs for the legal moves. Each node will be a 4-tuple indicating who performs each of the four tasks. The tuples should be ordered by decreasing importance (fire, cakes, tea, and poetry). For example, (c s s e) indicates the child is feeding the fire, the servant is serving cakes and pouring tea, and the elder is reading poetry, 5. Complete part a in full detail. For b and c it suffices to list the order in which states are visited and the current list of closed states. Redo 5a. using the production system notation, producing a trace of execution similar to the tables on p. 169 7 for 2, not 8 of the moves. 9 10 30