Download - Logic Programming
![Page 1: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/1.jpg)
Logic Programming
Lecture 7: Search Strategies
![Page 2: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/2.jpg)
James Cheney Logic Programming November 12, 2009
Outline for today•Problem representation
•Depth-First Search
• Iterative Deepening
•Breadth-First Search
•Advanced techniques taster
• Best-first search
• AND/OR (alternating) search
![Page 3: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/3.jpg)
James Cheney Logic Programming November 12, 2009
Search spaces
•Set of states s1,s2,...
•Goal predicate goal(X)
•Step predicate s(X,Y) that says we can go from state X to state Y
•A solution is a path leading from some start state S to a goal state g satisfying goal(G).
![Page 4: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/4.jpg)
James Cheney Logic Programming November 12, 2009
Example: Blocks world
AA
BB
CC
[[c,b,a],[],[]]
![Page 5: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/5.jpg)
James Cheney Logic Programming November 12, 2009
Example: Blocks world
AA
BB
CC
[[b,a],[],[c]]
![Page 6: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/6.jpg)
James Cheney Logic Programming November 12, 2009
Example: Blocks world
AA BB CC
[[a],[b],[c]]
![Page 7: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/7.jpg)
James Cheney Logic Programming November 12, 2009
Example: Blocks world
AA
BB CC
[[],[a,b],[c]]
![Page 8: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/8.jpg)
James Cheney Logic Programming November 12, 2009
Representation in Prolog
• State is a list of stacks of blocks.
[[a,b,c],[],[]]
• Transitions move a block from the top of one stack to the top of another
s([[A|As],Bs,Cs], [As,[A|Bs],Cs]).
s([[A|As],Bs,Cs], [As,Bs,[A|Cs]]).
...
goal([[],[],[a,b,c]]).
![Page 9: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/9.jpg)
James Cheney Logic Programming November 12, 2009
An abstract problem space
cc
bb
aa
dd
...
s(a,b).s(b,c).s(c,a).s(c,f(d)).s(f(N), f(g(N))).s(a,d).
goal(d).
![Page 10: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/10.jpg)
James Cheney Logic Programming November 12, 2009
Depth-first search•dfs(Node,Path)
•Path is a path to a goal starting from Node
dfs(S,[S]) :- goal(S).
dfs(S,[S|P]) :- s(S,T),
dfs(T,P).
![Page 11: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/11.jpg)
James Cheney Logic Programming November 12, 2009
Problem 1: Cycles
AA
BB
CC
![Page 12: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/12.jpg)
James Cheney Logic Programming November 12, 2009
Solution 1: Remember where
you've been• Avoid cycles by avoiding previously visited states
dfs_noloop(Path,Node,[Node|Path]) :-
goal(Node).
dfs_noloop(Path,Node,Path1) :-
s(Node,Node1),
\+(member(Node,Path)),
dfs_noloop([Node|Path],Node1,Path1).
![Page 13: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/13.jpg)
James Cheney Logic Programming November 12, 2009
Problem 2: Infinite state space
•We may miss solutions because state space is infinite
•Even if state space is finite, may wind up finding "easy" solution only after a long exploration of pointless part of search space
...
![Page 14: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/14.jpg)
James Cheney Logic Programming November 12, 2009
Solution 2: Depth bounding
• Keep track of depth, stop if bound exceeded
dfs_bound(_,Node,[Node]) :-
goal(Node).
dfs_bound(N,Node,[Node|Path]) :-
N > 0,
s(Node,Node1),
M is N-1,
dfs_bound(M,Node1,Path).
![Page 15: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/15.jpg)
James Cheney Logic Programming November 12, 2009
Problem 3: What is a good bound?
•Don't know this in advance, in general
•Too low?
•Might miss solutions
•Too high?
•Might spend a long time searching pointlessly
![Page 16: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/16.jpg)
James Cheney Logic Programming November 12, 2009
Solution 3: Iterative deepening
dfs_id(N,Node,Path) :-
dfs_bound(N,Node,Path)
;
M is N+1,
dfs_id(M,Node,Path).
![Page 17: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/17.jpg)
James Cheney Logic Programming November 12, 2009
Breadth-first search
• Keep track of all possible solutions, try shortest ones first
• Maintain a "queue" of solutions
bfs([[Node|Path],_], [Node|Path]) :-
goal(N).
bfs([Path|Paths], S) :-
extend(Path,NewPaths),
append(Paths,NewPaths,Paths1),
bfs(Paths1,S).
bfs_start(N,P) :- bfs([[N]],P).
![Page 18: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/18.jpg)
James Cheney Logic Programming November 12, 2009
Extending pathsextend([Node|Path],NewPaths) :-
bagof([NewNode,Node|Path],
(s(Node,NewNode),
\+ (member(NewNode,[Node|Path]))),
NewPaths),
!.
%% if there are no next steps,
%% bagof will fail and we'll fall through.
extend(_Path,[]).
![Page 19: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/19.jpg)
James Cheney Logic Programming November 12, 2009
Problem: Speed
•Concatenating new paths to end of list is slow
•Avoid this using difference lists?
![Page 20: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/20.jpg)
James Cheney Logic Programming November 12, 2009
A difference list implementation
bfs_dl([[Node|Path]|_],_, [Node|Path]) :-
goal(Node).
bfs_dl([Path|Paths], Z, Solution) :-
extend(Path,NewPaths),
append(NewPaths,Z1,Z),
Paths \== Z1, %% (Paths,Z1) is not empty DL
bfs_dl(Paths,Z1,Solution).
bfs_dl_start(N,P) :- bfs_dl([[N]|X],X,P).
We'll come We'll come back to this back to this next weeknext week
![Page 21: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/21.jpg)
James Cheney Logic Programming November 12, 2009
Best-first search
•Often want a minimum cost solution
•and maybe a heuristic cost measure
•Idea:
•Maintain "frontier" of solutions with similar cost
•Expand least-cost unexplored node
![Page 22: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/22.jpg)
James Cheney Logic Programming November 12, 2009
•Heuristic h(x) = map distance between points
Example: Shortest paths
![Page 23: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/23.jpg)
James Cheney Logic Programming November 12, 2009
•Heuristic h(x) = map distance between points
Example: Shortest paths
![Page 24: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/24.jpg)
James Cheney Logic Programming November 12, 2009
•Heuristic h(x) = map distance between points
Example: Shortest paths
![Page 25: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/25.jpg)
James Cheney Logic Programming November 12, 2009
AND/OR search•So far we've considered graph search
problems
• Just want to find some path from start to end
•Other problems have more structure
•e.g. 2-player games
•AND/OR search is a useful abstraction
![Page 26: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/26.jpg)
James Cheney Logic Programming November 12, 2009
AND/OR search
•Search space has 2 kinds of states:
•OR: "we get to choose next state"
•AND: "opponent gets to choose"
• we need to be able to handle any opponent move
![Page 27: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/27.jpg)
James Cheney Logic Programming November 12, 2009
Example: Tic tac toe
xx xx xx
xx
...
...... ......
![Page 28: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/28.jpg)
James Cheney Logic Programming November 12, 2009
Representation• or(S,Nodes)
• S is an Or node with possible next states Nodes
• "We move"
• and(S,Nodes)
• S is an And node with possible next states Nodes
• "Opponent moves"
• goal(S)
• S is a "win" for us
![Page 29: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/29.jpg)
James Cheney Logic Programming November 12, 2009
Example: A simple game
aa
bb cc
dd ee
and(a,[b,c]).or(b,[d,a]).or(c,[d,e]).goal(e).
![Page 30: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/30.jpg)
James Cheney Logic Programming November 12, 2009
Basic ideaandor(Node) :- goal(Node).
andor(Node) :-
or(Node,Nodes),
member(Node1,Nodes),
andor(Node1).
andor(Node) :-
and(Node,Nodes),
solveall(Nodes).
![Page 31: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/31.jpg)
James Cheney Logic Programming November 12, 2009
Solutions•For each AND state, we need solutions for
all possible next states
•For each OR state, we just need one choice
•A "solution" is thus a tree, or strategy
• Can adapt previous program to produce solution tree.
• Can also incorporate iterative deepening, loop avoidance, heuristic measures of "good" positions
![Page 32: Logic Programming](https://reader034.vdocuments.site/reader034/viewer/2022051316/56813c1e550346895da595df/html5/thumbnails/32.jpg)
James Cheney Logic Programming November 12, 2009
Next time•Further reading:
• Bratko, ch. 8 (difference lists), ch. 11 (DFS/BFS)
• also Ch. 12 (BestFS), 13 (AND/OR)
•Next time:
• Advanced programming with terms
• (var/1, nonvar/1, =../2, ==/2, ...)
• Meta-programming