Logic Programming: Search Strategies Alan Smaill Nov 5, 2012 Alan - - PowerPoint PPT Presentation

logic programming search strategies
SMART_READER_LITE
LIVE PREVIEW

Logic Programming: Search Strategies Alan Smaill Nov 5, 2012 Alan - - PowerPoint PPT Presentation

N I V E U R S E I H T T Y O H F G R E U D I B N Logic Programming: Search Strategies Alan Smaill Nov 5, 2012 Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 1/1 Today N I V E U R S E I H T T


slide-1
SLIDE 1

T H E U N I V E R S I T Y O F E D I N B U R G H

Logic Programming: Search Strategies

Alan Smaill Nov 5, 2012

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 1/1

slide-2
SLIDE 2

T H E U N I V E R S I T Y O F E D I N B U R G H

Today

Problem representation Search

Depth First Iterative Deepening Breadth First

AND/OR (alternating/game tree) search

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 2/1

slide-3
SLIDE 3

T H E U N I V E R S I T Y O F E D I N B U R G H

Search Problems

Many classical AI/CS problems can be formulated as search problems. Examples: Graph searching Blocks world Missionaries and cannibals Planning (e.g. robotics)

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 3/1

slide-4
SLIDE 4

T H E U N I V E R S I T Y O F E D I N B U R G H

Search Spaces

Given by: 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 start state (or states) A solution is a path leading from the S to a goal state G satisfying goal(G).

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 4/1

slide-5
SLIDE 5

T H E U N I V E R S I T Y O F E D I N B U R G H

Example: Blocks world

Take configuration of blocks as a list of three towers, each tower being a list of blocks in a tower from top to bottom.

A B C

[[c,b,a],[],[c])

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 5/1

slide-6
SLIDE 6

T H E U N I V E R S I T Y O F E D I N B U R G H

Example: Blocks world

Move a block from top of a tower to top of another tower:

A B C

[[b,a],[],[c])

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 6/1

slide-7
SLIDE 7

T H E U N I V E R S I T Y O F E D I N B U R G H

Example: Blocks world

Next move:

A B C

[[a],[b],[c])

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 7/1

slide-8
SLIDE 8

T H E U N I V E R S I T Y O F E D I N B U R G H

Example: Blocks world

Then —

A B C

[[],[a,b],[c])

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 8/1

slide-9
SLIDE 9

T H E U N I V E R S I T Y O F E D I N B U R G H

Prolog representation

State is a list of stacks of blocks: [[a,b,c],[],[]] Transitions move a block from the top of one stack to the top

  • f another:

s([[A|As],Bs,Cs], [As,[A|Bs],Cs]). s([[A|As],Bs,Cs], [As,Bs,[A|Cs]]). ... Can specify particular goal position: goal([[],[],[1,b,c]].

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 9/1

slide-10
SLIDE 10

T H E U N I V E R S I T Y O F E D I N B U R G H

An abstract problem space

s(a,b). s(b,c). s(c,a). s(c,f(d)). s(f(N),f(g(N))). s(f(g(X)),X). goal(d). Think of the graph generated by these declarations. In this case: the graph is infinite there is a loop near the top of the graph

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 10/1

slide-11
SLIDE 11

T H E U N I V E R S I T Y O F E D I N B U R G H

problem 1: cycles

We can already see in the blocks world example and in the abstract search space that it is easy to follow actions around in cycles, and not find the goal, even if there is a path to the goal. There are two main approaches to deal with this: remember where you’ve been work with depth bound

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 11/1

slide-12
SLIDE 12

T H E U N I V E R S I T Y O F E D I N B U R G H

Solution 1: remember where you’ve been

dfs_noloop(Path,Node,[Node|Path]) :- goal(Node). dfs_noloop(Path,Node,Path1) :- s(Node,Node1), \+(member(Node1,Path)), dfs_noloop([Node|Path],Node1,Path1).

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 12/1

slide-13
SLIDE 13

T H E U N I V E R S I T Y O F E D I N B U R G H

Problem 2: Infinite State Space

Compare the graph from the abstract search space. Depth First Search has similar problems to Prolog proof search: 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

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 13/1

slide-14
SLIDE 14

T H E U N I V E R S I T Y O F E D I N B U R G H

Solution 2: depth bounding

Keep track of depth, stop if bound exceeded

Note: does not avoid loops (can do this too)

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)

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 14/1

slide-15
SLIDE 15

T H E U N I V E R S I T Y O F E D I N B U R G H

Problem 3: what is a good bound?

In general, we just don’t know in advance:

Too low? – Might miss solutions Too high? – Might spend a long time searching pointlessly

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 15/1

slide-16
SLIDE 16

T H E U N I V E R S I T Y O F E D I N B U R G H

Solution 3: iterative deepening

Use the following with some small start value for N dfs_id(N,Node,Path) :- dfs_bound(N,Node,Path) ; M is N+1, dfs_id(M,Node,Path). NB: if there is no solution, this will not terminate.

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 16/1

slide-17
SLIDE 17

T H E U N I V E R S I T Y O F E D I N B U R G H

Breadth first search

Keep track of all possible solutions, try shortest ones first; do this by maintaining a “queue” of solutions bfs([[Node|Path]|_], [Node|Path]) :- goal(Node). bfs([Path|Paths], S) :- extend(Path,NewPaths), append(Paths,NewPaths,Paths1), bfs(Paths1,S). bfs_start(N,P) :- bfs([[N]],P).

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 17/1

slide-18
SLIDE 18

T H E U N I V E R S I T Y O F E D I N B U R G H

extending paths

extend([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,[]).

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 18/1

slide-19
SLIDE 19

T H E U N I V E R S I T Y O F E D I N B U R G H

Problem: speed

Concatenating new paths to end of list is slow Avoid this using difference lists?

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 19/1

slide-20
SLIDE 20

T H E U N I V E R S I T Y O F E D I N B U R G H

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

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 20/1

slide-21
SLIDE 21

T H E U N I V E R S I T Y O F E D I N B U R G H

Example: Noughts and Crosses

X X X X X X X X X X X O O X O O X O X O X . . . . . . . . . . . . . . . . . . . . . X X

–1 +1

X X X X O X X O X X O O O X X X O O O O O X X

MAX (X) MIN (O) MAX (X) MIN (O) TERMINAL Utility

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 21/1

slide-22
SLIDE 22

T H E U N I V E R S I T Y O F E D I N B U R G H

Representation

  • r(S,Nodes)

S is an OR node with possible next states Nodes “Our move”

and(S,Nodes)

S is an AND node with possible next states Nodes “Opponent moves”

goal(S)

S is a “win” for us

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 22/1

slide-23
SLIDE 23

T H E U N I V E R S I T Y O F E D I N B U R G H

Example: A simple game

and(a,[b,c]).

  • r(b,[d,a]).
  • r(c,[d,e]).

goal(e). What is the graph here?

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 23/1

slide-24
SLIDE 24

T H E U N I V E R S I T Y O F E D I N B U R G H

Basic Idea

andor(Node) :- goal(Node). andor(Node) :-

  • r(Node,Nodes),

member(Node1,Nodes), andor(Node1). andor(Node) :- and(Node,Nodes), solveall(Nodes). solveall(Nodes) :- ...

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 24/1

slide-25
SLIDE 25

T H E U N I V E R S I T Y O F E D I N B U R G H

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, BFS. heuristic measures of “good” positions leads to algorithms like MiniMax.

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 25/1

slide-26
SLIDE 26

T H E U N I V E R S I T Y O F E D I N B U R G H

Further Reading

Bratko, Prolog Programming for Artificial Intelligence

  • ch. 8 (difference lists), ch. 11 (DFS/BFS)

also Ch. 12 (BestFS), 13 (AND/OR)

Alan Smaill Logic Programming: Search Strategies Nov 5, 2012 26/1