outline for today logic programming
play

Outline for today Logic Programming Problem representation - PowerPoint PPT Presentation

Outline for today Logic Programming Problem representation Depth-First Search Iterative Deepening Lecture 7: Search Strategies: Breadth-First Search Problem representations Depth-first, breadth-first, and AND/OR search AND/OR


  1. Outline for today Logic Programming • Problem representation • Depth-First Search • Iterative Deepening Lecture 7: Search Strategies: • Breadth-First Search Problem representations Depth-first, breadth-first, and AND/OR search • AND/OR (alternating/game tree) search James Cheney Logic Programming November 3, 2014 Search problems Search spaces • Many classical (AI/CS) problems can be • Set of states s 1 , s 2 ,... formulated as search problems • Goal predicate goal(X) • Examples: • Step predicate s(X,Y) that says we can • Graph searching go from state X to state Y • Blocks world • A solution is a path leading from some • Missionaries and cannibals start state S to a goal state G satisfying goal(G) . • Planning (e.g. robotics) James Cheney Logic Programming November 3, 2014 James Cheney Logic Programming November 3, 2014

  2. Example: Blocks world Example: Blocks world C B B A A C [[c,b,a],[],[]] [[b,a],[],[c]] James Cheney Logic Programming November 3, 2014 James Cheney Logic Programming November 3, 2014 Example: Blocks world Example: Blocks world A A B C B C [[a],[b],[c]] [[],[a,b],[c]] James Cheney Logic Programming November 3, 2014 James Cheney Logic Programming November 3, 2014

  3. An abstract problem Representation in Prolog space • State is a list of stacks of blocks. s(a,b). s(b,c). [[a,b,c],[],[]] a • Transitions move a block from the top of one stack to s(c,a). b s(c,f(d)). the top of another c s(f(N),f(g(N))). s([[A|As],Bs,Cs], [As,[A|Bs],Cs]). f(d) s(f(g(X)),X). s([[A|As],Bs,Cs], [As,Bs,[A|Cs]]). f(g(d)) ... goal(d). d f(g(g(d))) ... goal([[],[],[a,b,c]]). g(d) James Cheney Logic Programming November 3, 2014 James Cheney Logic Programming November 3, 2014 Depth-first search Problem 1: Cycles • dfs(Node,Path) C • Path is a path to a goal starting from Node dfs(S,[S]) :- goal(S). B dfs(S,[S|P]) :- s(S,T), A dfs(T,P). • This should look familiar James Cheney Logic Programming November 3, 2014 James Cheney Logic Programming November 3, 2014

  4. Problem 1: Cycles Problem 1: Cycles C B B C A A James Cheney Logic Programming November 3, 2014 James Cheney Logic Programming November 3, 2014 Solution 1: Remember Problem 2: Infinite state where you've been space • Avoid cycles by avoiding previously visited states dfs_noloop(Path,Node,[Node|Path]) :- goal(Node). ... • DFS has similar problems to Prolog proof search dfs_noloop(Path,Node,Path1) :- • We may miss solutions because state space is infinite s(Node,Node1), • Even if state space is finite, may wind up finding � \+(member(Node1,Path)), "easy" solution only after a long exploration of � dfs_noloop([Node|Path],Node1,Path1). pointless part of search space James Cheney Logic Programming November 3, 2014 James Cheney Logic Programming November 3, 2014

  5. Solution 2: Problem 3: What is a Depth bounding good bound? • Don't know this in advance, in general • Keep track of depth, stop if bound exceeded • Note: does not avoid loops (can do this too) • Too low? dfs_bound(_,Node,[Node]) :- • Might miss solutions goal(Node). • Too high? dfs_bound(N,Node,[Node|Path]) :- N > 0, • Might spend a long time searching pointlessly s(Node,Node1), M is N-1, dfs_bound(M,Node1,Path). James Cheney Logic Programming November 3, 2014 James Cheney Logic Programming November 3, 2014 Solution 3: Iterative Breadth-first search deepening • Keep track of all possible solutions, try shortest ones first dfs_id(N,Node,Path) :- • Maintain a "queue" of solutions dfs_bound(N,Node,Path) bfs([[Node|Path]|_], [Node|Path]) :- goal(Node). ; bfs([Path|Paths], S) :- M is N+1, � extend(Path,NewPaths), dfs_id(M,Node,Path). � append(Paths,NewPaths,Paths1), � bfs(Paths1,S). bfs_start(N,P) :- bfs([[N]],P). James Cheney Logic Programming November 3, 2014 James Cheney Logic Programming November 3, 2014

  6. Extending paths Problem: Speed • Concatenating new paths to end of list is extend([Node|Path],NewPaths) :- slow � bagof([NewNode,Node|Path], � (s(Node,NewNode), • Avoid this using difference lists? � \+ (member(NewNode,[Node|Path]))), • Will revisit next week � NewPaths), � !. %% if there are no next steps, %% bagof will fail and we'll fall through. extend(_Path,[]). James Cheney Logic Programming November 3, 2014 James Cheney Logic Programming November 3, 2014 AND/OR search AND/OR search • So far we've considered graph search • Search space has 2 kinds of states: problems • OR: "we get to choose next state" • Just want to find some path from start to end • AND: "opponent gets to choose" • Other problems have more structure • we need to be able to handle any opponent • e.g. 2-player games move • AND/OR search is a useful abstraction James Cheney Logic Programming November 3, 2014 James Cheney Logic Programming November 3, 2014

  7. Example: Tic tac toe Representation • or(S,Nodes) • S is an OR node with possible next states Nodes • "Our move" ... • and(S,Nodes) x x x • S is an AND node with possible next states Nodes x • "Opponent moves" • goal(S) ... ... ... ... • S is a "win" for us James Cheney Logic Programming November 3, 2014 James Cheney Logic Programming November 3, 2014 Example: A simple game Basic idea andor(Node) :- goal(Node). and(a,[b,c]). a andor(Node) :- or(b,[d,a]). � or(Node,Nodes), b c or(c,[d,e]). � member(Node1,Nodes), � andor(Node1). goal(e). d e andor(Node) :- � and(Node,Nodes), � solveall(Nodes). James Cheney Logic Programming November 3, 2014 James Cheney Logic Programming November 3, 2014

  8. Solutions • Further reading: • For each AND state, we need solutions for all possible next states • Bratko, Prolog Programming for Artificial • For each OR state, we just need one choice Intelligence • A "solution" is thus a tree , or strategy • ch. 8 (difference lists), ch. 11 (DFS/BFS) • Can adapt previous program to produce solution tree. • also Ch. 12 (BestFS), 13 (AND/OR) • Can also incorporate iterative deepening, loop • Next time: avoidance, BFS • heuristic measures of "good" positions - min/max • Higher-order logic programming James Cheney Logic Programming November 3, 2014 James Cheney Logic Programming November 3, 2014

Recommend


More recommend