Professional Documents
Culture Documents
H H T
Example moves:
after before
flip left
H H T T H T
flip middle
T T H T H H
and so on.
General properties:
all of them end up with HHH
why? after 1 move have even # of Ts
after 2 moves have odd # of Ts
after 3 moves have even # of Ts
Nice try...
end
grab the
bananas
climb onto
the box
go to where
the box is
start
TTH TTT
where
flip
HTH HTT middle
flip
left
THH THT
flip right
It should tell us
a list of moves that solve the problem
How?
provided that
X = X1,
Y and Y1 are flip sides,
Z = Z1.
which gives us...
initial_state([h,h,t]).
goal_state([X,X,X]).
legal_move([X,Y,Z],flip_left,[X1,Y,Z]) :-
flip(X,X1).
legal_move([X,Y,Z],flip_middle,[X,Y1,Z]) :-
flip(Y,Y1).
legal_move([X,Y,Z],flip_right,[X,Y,Z1]) :-
flip(Z,Z1).
flip(h,t).
flip(t,h).
want exactly
The query: 3 moves
solve_problem([M3,M2,M1]).
The result:
M3 = flip_right
M2 = flip_left
M1 = flip_left first move
reachable(Sa,[M2,M1])
legal_move(S,M3,Sa),
goal_state(S)
reachable(Sb,[M1]),
legal_move(Sa,M2,Sb),
legal_move(S,M3,Sa),
goal_state(S)
reachable(Sc,[]),
legal_move(Sb,M1,Sc),
legal_move(Sa,M2,Sb),
legal_move(S,M3,Sa),
goal_state(S)
initial_state(Sc),
legal_move(Sb,M1,Sc),
legal_move(Sa,M2,Sb),
legal_move(S,M3,Sa),
goal_state(S)
Sc= [h,h,t]
legal_move(Sb,M1,[h,h,t]),
legal_move(Sa,M2,Sb),
legal_move(S,M3,Sa),
goal_state(S)
cps721 Artificial Intelligence Hector Levesque Problem Solving 14
Tracing continued
M1=flip_left, Sb=[Xa,h,t]
flip(Xa,h),
legal_move(Sa,M2,[Xa,h,t]),
legal_move(S,M3,Sa),
goal_state(S)
Xa=t
legal_move(Sa,M2,[t,h,t]),
legal_move(S,M3,Sa),
goal_state(S)
M2=flip_left, Sa=[Xb,h,t]
flip(Xb,t),
legal_move(S,M3,[Xb,h,t]),
goal_state(S)
Xb=h
legal_move(S,M3,[h,h,t]),
goal_state(S)
M3=flip_left, S=[X,h,t] M3=flip_right, S=[h,h,Z]
goal_state([h,t,t]) success
fail
fail
initial_state(S1),
legal_move(S2,M1,S1),
legal_move(S3,M2,S2),
legal_move(S,M3,S3),
goal_state(S)
The program:
initial_state([loc3,loc4,loc1,n,n]).
1 2 3 4
goal_state([_,_,_,_,y]).
legal_move([B,L,L,y,H],climb_on,[B,L,L,n,H]).
legal_move(S1,climb_off,S) :-
legal_move(S,climb_on,S1).
legal_move([L,L,L,y,y],grab,[L,L,L,y,n]).
legal_move([B,X,L,n,H],go(X),[B,M,L,n,H]) :-
loc(X), not(X=M).
legal_move([B,X,X,n,H],push(X),[B,M,M,n,H]) :-
loc(X), not(X=M).
Responses to queries:
solve_problem([M3,M2,M1]). no 3 step
solution
no
solve_problem([M4,M3,M2,M1]).
M4 = grab
M3 = climb_on a unique
4 step
M2 = push(loc3)
solution
M1 = go(loc1) ; first
move
no
solve_problem([M5,M4,M3,M2,M1]).
M5 = grab
M4 = climb_on several
M3 = push(loc3) 5 step
M2 = push(loc2) solutions
M1 = go(loc1) ;
... (many other solutions)
reachable(G,L),
goal_state(G)
is there a 0 initial_state(G),
move solution? goal_state(G) reachable(S1,L1),
X legal_move(G,A1,S1),
goal_state(G)
initial_state(S1),
is there a 1
legal_move(G,A1,S1),
move solution?
goal_state(G)
reachable(S2,L2),
X legal_move(S1,A2,S2),
legal_move(G,A1,S1),
goal_state(G)
initial_state(S2),
is there a 2 legal_move(S1,A2,S2),
move solution? legal_move(G,A1,S1),
goal_state(G) reachable(S3,L3),
X legal_move(S2,A3,S3),
legal_move(S1,A2,S2),
legal_move(G,A1,S1),
goal_state(G)
initial_state(S3),
legal_move(S2,A3,S3),
is there a 3
legal_move(S1,A2,S2),
move solution? reachable(S4,L4),
legal_move(G,A1,S1),
goal_state(G) legal_move(S3,A4,S4),
legal_move(S2,A3,S3),
X legal_move(S1,A2,S2),
legal_move(G,A1,S1),
initial_state(S4), goal_state(G)
legal_move(S3,A4,S4),
is there a 4 legal_move(S2,A3,S3),
move solution? legal_move(S1,A2,S2),
legal_move(G,A1,S1),
goal_state(G)
reverse(L1,L2) :- rev(L1,[],L2).
rev([],L,L).
rev([X|L1],L2,L3) :- rev(L1,[X|L2],L3).
reverse([1,2,3], L)
rev([1,2,3], [ ], L)
rev([2,3], [1], L)
rev([3], [2,1], L)
rev([ ], [3,2,1], L)
L = [3,2,1]
!
Then we pose a query like:
solve_problem(L), reverse(L,In_order).
cps721 Artificial Intelligence Hector Levesque Problem Solving 21
Next problem: 15-puzzle
5 6 7 8 10 2 7 15
9 10 11 12 13 5 4
13 14 15 9 14 11 12
goal_state([1,2,3,4,5,0]).
legal_move([C,A,B,0,D,E],up(C),[0,A,B,C,D,E]).
legal_move([A,D,B,C,0,E],up(D),[A,0,B,C,D,E]).
legal_move([A,B,E,C,D,0],up(E),[A,B,0,C,D,E]).
legal_move(S1,down(X),S2) :-
legal_move(S2,up(X),S1).
legal_move([A,0,B,C,D,E],left(A),[0,A,B,C,D,E]).
legal_move([A,B,0,C,D,E],left(B),[A,0,B,C,D,E]).
legal_move([A,B,C,D,0,E],left(D),[A,B,C,0,D,E]).
legal_move([A,B,C,D,E,0],left(E),[A,B,C,D,0,E]).
legal_move(S1,right(X),S2) :-
legal_move(S2,left(X),S1).
2. Searching
Blindly searching (in phase two) for n moves
becomes infeasible.
legal_move( ), ..., legal_move( )
the number of states that may
need to be examined grows
exponentially in the number 3
of required moves n
9
Weve been lucky so far! 27
... ... ...
81
5 - - - 6 7 8
9 - - - 10 - - 11 12
13 - - 14 - 15
initial state
possibly the entire tree
below S# will be explored S#
before even looking at S##
what is below S##
Typical use:
present a planning problem: some goal we are
interested in achieving
solve the problem: find a sequence of actions
that achieves the goal
send the actions to the robot for execution
initial
a2 a1 a1
this one
poss(grab,S) :- on_box(S),
banana_location(L), location(box,L,S).
poss(climb_on,S) :- location(monkey,L,S),
location(box,L,S), not(on_box(S)).
poss(climb_off,S) :- on_box(S).
poss(go(X),S) :- loc(X), location(monkey,L,S),
not(X=L), not(on_box(S)).
poss(push(X),S) :- loc(X), location(monkey,L,S),
location(box,L,S), not(X=L), not(on_box(S)).
on_box([climb_on|S]).
on_box([A|S]) :- not(A=climb_off), on_box(S).
has_bananas([grab|S]).
has_bananas([A|S]) :- has_bananas(S).
location(monkey,X,[go(X)|S]).
location(monkey,X,[push(X)|S]).
location(monkey,X,[A|S]) :- not(A=go(_)),
not(A=push(_)), location(monkey,X,S).
location(box,X,[push(X)|S]).
location(box,X,[A|S]) :- not(A=push(_)),
location(box,X,S).
location(monkey,loc4,[]).
location(box,loc1,[]).
/* all the remaining fluents fail for [] */
loc(loc1). loc(loc2). loc(loc3). loc(loc4).
banana_location(loc3).
continuous actions
dropping a ball
filling a container with water
exogenous actions
performed by other agents (or nature)
sensing actions
some actions, like reading a thermometer, are
not used to change the world, but only to change
what is known about the world