90 likes | 214 Views
CHA2555 Week 7: Prolog: Some Tips Planners: Some Tips. Lee McCluskey Email lee@hud.ac.uk First term: http://scom.hud.ac.uk/scomtlm/cha2555/. Prolog: for coursework.
E N D
CHA2555 Week 7:Prolog: Some TipsPlanners: Some Tips Lee McCluskey Email lee@hud.ac.uk First term: http://scom.hud.ac.uk/scomtlm/cha2555/
Prolog: for coursework.. • PROLOG: From the initial part of the course you should know basic Prolog operation, matching, basic backtracking, variable binding, use of the Cut, list processing + recursive functions. This week: ways of manipulating data stored as facts • PLANNING: changing the Prolog Planners to deal with static relations such as “connects”
Lists and facts Data can be stored in facts, using combinations of lists / terms: chess_board(1,[white_rook, white_knight, white_bishop, white_king, white_queen, white_bishop, white_knight, white_rook]). chess_board(2,[white_pawn, white_pawn, white_pawn, white_pawn, white_pawn, white_pawn, white_pawn, white_pawn ]). chess_board(3,[blank , blank , blank , blank , blank , blank , blank , blank]). Etc connects(a,[s,d,g,h]). connects(b,[f,g,h]). connects(c,[a,,f]).
RECALL: List Processing • Virtually all list processing carried out in prolog contains the following Recursive Case: 'Break the input list down into its head and tail with an expression of the form '[ X | Y ]' . Process the head X. Recursively call this procedure with input list = tail Y .‘ BUT what about processing a database of facts?
RECALL: Assert and Retract “assert(X)” will assert term X as a clause at the end of the program. “retract(X)” will succeed once it has removed a clause that matches with X. EG Retract all instances of a term and make it into a list of terms: Eg node(X,Y) get_list([node(X,Y) | T ]):- retract(node(X,Y)), get_list(T),!. get_list([ ]).
Example: One way of finding the “highest” value in a set of facts (eg nodes). node(13,tom). node(53,dick). node(51,fred). node(11,harry). node(62,tom). node(23,bill). get_highest(Val, Result) :- node(X,Y), assert(current_highest(X,Y)), get_highest, retract(current_highest(Val,Result)). get_highest :- node(X,Y), current_highest(Val,Current), X > Val, retract(current_highest(Val,Current)), assert(current_highest(X,Y)), fail. get_highest.
Example: One way of changing representations.. :- dynamic connects/2. connects(a,[e,d,f,c]). connects(c,[a,b,g,h]). connects(h,[c,i,j]). connects(i,[k]). % translate into different representation % next(x,y) translate :- retract(connects(X,L)), member(E,L), assert(next(X,E)), assert(next(E,X)), fail. translate. member(X,[X|_]). member(X,[_|T]) :- member(X,T).
Prolog Planners: using then to do route finding. Example using WSC planner. task1 :- startOCL([se(c1,[at(c1,corridor)]),se(r2,[position(r2,menstoilet)])] , [ ss(c1,[at(c1,landing)]), ss(c2,[at(c2,landing)]), ss(c3,[at(c3,landing)]), ss(c4,[at(c4,landing)]), ss(r1,[position(r1,landing),fuel_level(r1,4)]), ss(r2,[position(r2,landing),fuel_level(r2,4)]), ss(landing, [connects(landing,menstoilet),connects(landing,corridor)]), ss(menstoilet, [connects(menstoilet,landing),connects(menstoilet,corridor)]), ss(corridor, [connects(corridor,menstoilet),connects(corridor,landing)]) ] ). Solution example: goal [se(c1,[at(c1,corridor)]),se(r2,[position(r2,menstoilet)])] achieved by sequence[move(r2,landing,menstoilet),load(c1,r1,landing),move(r1,landing,corridor),unload(c1,r1,corridor)]
Prolog Planners: using then to do route finding. Example using WSC planner. op(move(R,A,B), % prevail [se(A,[connects(A,B)])], % necessary [ ssc(R,[position(R,A), fuel_level(R,X), ge(X,1), is(XX,X-1)], [position(R,B), fuel_level(R,XX)] ) ], % conditional [ ]).