solving ai problems- knowledge representation plus search · problem solving as graph search...
TRANSCRIPT
Solving AI Problems- KnowledgeRepresentation plus Search
2
Revision of some conceptsSubstitution: An assignment of a constant,functional expression or variable to the variablesin a given expression.
Instance: the form an expression takes after asubstitution has been applied to it.
E. g. The substitution {X=mary,Y=W}
applied to the clause:
grandparent(X,Y):- parent(X,Z),
parent(Z,Y).
yields the clause instance:
grandparent(mary,Y):- parent(X,Z),
parent(Z,W).
3
Revision (cont.)
Resolvent of a rule and a query: new queryobtained by unifying the head of the rule with thefirst subgoal in the query, and retaining all otherpredicates affected by the substitution used tounify.
?- grandparent(mary,W). grandparent(X,Y):-
parent(X,Z),
parent(Z,Y).
?- parent(mary,Z), parent(Z,W).
{X=mary,
Y=W}
4
Revision: BacktrackingConsider a query:
?- p(X), q(Y), r(X,Y).
Backtracking goes back to the first subgoal thatcan try a different (as yet untried) value for avariable. So if the program is:
p(1). p(2). q(a). ….. q(z). r(2,b).
for X=1, all 26 possible values of Y will be triedbefore backtracking to p/1. For each, r(1,Y) fails,but Prolog’s search is exhaustive, cannotintelligently backtrack to the variable whosecurrent value causes the failure.
5
Revision: backtracking
For our expert system:
woman(eve). man(tom).
woman(claire). man(adam).
compatible(eve,adam).
match(X,Y):- woman(X), man(Y),
compatible(X,Y).
go:- match(eve,X), write(X), nl, fail.
The query ?- go.
binds X=eve and Y=tom before failing, upon which
Prolog backtracks to man(Y) (the first subgoal that
can try a different value for Y), and tries Y=adam.
6
This is what the trace looks like?- trace
| ?- go.
1 1 Call: go ?
2 2 Call: match(eve,_926) ?
3 3 Call: woman(eve) ?
3 3 Exit: woman(eve) ?
4 3 Call: man(_926) ?
? 4 3 Exit: man(tom) ?
5 3 Call: compatible(eve,tom) ?
5 3 Fail: compatible(eve,tom) ?
4 3 Redo: man(tom) ?
4 3 Exit: man(adam) ?
6 3 Call: compatible(eve,adam) ?
6 3 Exit: compatible(eve,adam) ?
2 2 Exit: match(eve,adam) ?
7 2 Call: write(adam) ?
7 2 Exit: write(adam) ? adam
7
Readings from the textbook
Structures and Strategies for State Space Search:
Introduction
Graph Theory
Strategies for State Space Search: up to Depth-FirstSearch with Iterative Deepening included
Heuristic Search
Introduction
The Best-First Search Algorithm
Admissibility, Monotonicity, Informedness
Complexity Issues
8
How do we go about having
computers solve AI problems ?
We need:
a convenient way to represent the information relevant
to the problem to be solved
a procedure to search for a solution from the
representation given
A first AI paradigm:
KNOWLEDGE REPRESENTATION + SEARCH
9
Importance of a good representation
Not all possible representations of the sameproblem will lend themselves to goodsolutions of it.
Example 1- Hanoi towers
Example 2- Konigsberg bridges- illustratesthe power of graph theory to analyze thestructure and complexity of some problems.
10
Problem Solving as Graph
Search
similar to some kinds of human problem-
solving- e.g., possible moves in a chess game.
We need:
1. an initial state(s) (e.g. empty board)
2. operations to change one state into another
3. final state(s)
These define a state space graph to be searched:
solutions are found by finding a path between an
initial state and a final state in the graph.
11
How to search
exhaustively: through the entire search
space (called uninformed search)
heuristically: using judgmental rules that
guide our search to those portions of the
state space that seem “promising” (called
informed or heuristic search)
12
Problem Solving as Graph SearchExample: flip two adjacent tiles of same colour(flipping changes red to blue and viceversa) until youget four blue tiles, starting from the configuration
r b b r (b=blue, r=red)
Initial state= r b b r
Final state= b b b b
Operations= b b -> r r
r r -> b b
State Graph= ? Any loops in it?
Possible heuristics? Give preference to 2nd rule
13
A direct correspondence: graph
formulations/ Prolog formulationsFor our example:
Initial state as a fact= state([r,b, b, r]).
Final state as a query= ?- state([b, b, b, b]).
Operations as rules= state([S1,S1,Z,W]):- state([S,S,Z,W]), flip(S,S1).
state([Z,S1,S1,W]):- state([Z,S,S,W]), flip(S,S1).
state([Z,W,S1,S1]):- state([Z,W,S,S]), flip(S,S1).
State Graph= automatically generated Will it loop?
Search Strategy= top-down, left-to-right w/backtracking
Possible heuristics? Place flippings into blue first:flip(r,b). flip(b,r).
14
Recording the path from initial to
goal nodes: more generalFor our example: (problem dependent parts in violet)
Initial and final states as parameters in a query to solve/3:
?- solve([r,b, b, r], [b, b, b, b],Path).
Operations as rules= s([S1,S1,Z,W], [S,S,Z,W]):-
flip(S,S1).
s ([Z,S1,S1,W],[Z,S,S,W]):- flip(S,S1).
s ([Z,W,S1,S1],[Z,W,S,S]):- flip(S,S1).
State Graph determined by the general procedure =
solve(G,G,[G]).
solve(N,Goal,[N|Path]):- s(N,N1), solve(N1,Goal,Path).
Search Strategy= top-down, left-to-right w/backtracking
Possible heuristics? Place flippings into blue first:flip(r,b). flip(b,r).
15
Avoiding loops
We keep track of the path so far and check that the state we’reabout to move into has not occurred already in the path:
Initial and final states as before:
?- solve([r,b, b, r], [b, b, b, b],Path).
A technicality: initialize the path so far to [[r,b,b,r]]:
solve(Initial,Final,Path):- solve1(Initial,Final,[Initial],Path).
Operations as before; State graph determined by:
solve1(State,State,Path,Path).
solve1(State,Sf,Psofar,P):-s(State,S2), not(member(S2,Psofar)),
solve1(S2,Sf,[S2|Psofar],P).
State Graph= automatically generated. Build it by hand!
Search Strategy= top-down, left-to-right w/backtracking
Possible heuristics?Give preference to changes into blue:flip(r,b). flip(b,r).
16
State Space Search: more
formally[N,A,S,GS]
where N are the nodes, or states in the graph, Aare the arcs, or links between nodes in the graph, Sis (are) the start node(s), and GS is (are) the goalstate(s) or node(s).
A solution path is a path through the graph from anode in S to a node in GS.
Example for practice: formulate the followingblocks world problem in terms of state spacesearch: c a
a --> b
b c
17
Homework
write a Prolog definition for the predicatereverse(L,L1) which should succeed with L1being the reverse of list L, e.g.
?- reverse([a,b,c],R).
R= [c,b,a]
yes
Modify the definition of solve/4 so the pathobtained is ordered from the initial state to thefinal, as we would expect. Test all predicates.
If no definition of member/2 is available, use:
member(X,[X|_).
member(X,[_|L]):- member(X,L).
18
Further examples
Eight queens problem: place 8 queens in an
8 by 8 chessboard so that none attacks any
other
Tic-tac-toe
The 8-puzzle
Traveling salesman
19
To determine a search strategy
Determine a search direction: either
forward-chaining: from facts and rules, derive a goal
backward-chaining: from goal and rules, derive thefacts
Determine the order for examining nodes: withleft-to-right or right-to-left (decide which), either:
depth first: consider siblings only when no furtherdescendant can be found
breadth first: examine al nodes at level n beforeproceeding to level n+1
20
Choice of search direction affects
search complexityBranching factor: number of states generated onaverage in each direction (forward- vs. backward-)
Example- To prove that Marie Curie is my ancestor:
ancestor(X,Y):- parent(X,Y).
ancestor(X,Y):- parent(X,Z), ancestor(Z,Y).
?- ancestor(marie_curie,X), X=veronica. % forward
?- ancestor(A,veronica), A=marie_curie. % backward
In forward-chaining mode (from Marie Curie toVeronica), with 3 children average, across 5generations, there’s an average of 35 nodes tocheck; whereas in backward-chaining mode, only25 !
21
N.B.
With appropriate choice of input/output roleparameters in a query, we can instruct the sameProlog program to search either “forward” or“backwards”. Syntactically speaking, however,Prolog’s strategy is backward-chaining: it alwaysstarts from a Prolog goal or query, rather thanfrom Prolog facts.
Morale: do not confuse the notion of goal in agiven problem with the notion of goal in Prolog:in Prolog the (syntactic) goal is always the query;in our example we can construe the goal to beMarie Curie and the starting point Veronica, orviceversa.
22
General guidelines for deciding
on a search direction
Choose backward chaining if:
goal is given (e.g. a theorem to prove)
lots of rules match the facts
data must be acquired (not given)
Choose forward chaining if:
all or most data are given (e.g. data interpretationproblems)
number of goals is large, but the number of ways to usethe facts is small
it’s difficult to formulate a goal precisely
prunes the space
by guiding the
search
guides along lines
known to be true
23
Choice of the order to examine the
nodes affects search complexity, and
even feasibility
Example: Recall our definition of “flip”: in general,less nodes will be examined if we first try to turnreds into blues, than vice-versa.
In general:
Depth-first may fail to find an answer if infiniteloops exist
Breadth-first always finds the shortest path to agoal node (if a path exists)
BOTH ARE COMBINATORIALLY EXPLOSIVE
24
Today’s question
In a 3 by 3 board, where each square isrepresented by a number as follows:
1 2 3
4 5 6
7 8 9
a knight ‘s allowed moves consist in moving twosquares either horizontally or vertically followedby one square in an orthogonal direction. Showthe problem-dependent clauses needed to solve theproblem of getting a knight from X to Y withoutloops, using our Prolog procedure solve/3.