%STRIPS PLANNER %achieve_all(Goals,InitState,FinalState,TotalStepsAllowed,UnusedSteps) is true if every element of list Goals can be % achieved going from state InitState to state FinalState. % TotalStepsAllowed is the bound on the number of actions going into achieve_all % UnusedSteps: the number of actions not required to achieve. achieve_all([],World,World,U,U). achieve_all([G1|Gr],W0,W2,U0,U2) :- achieve(G1,W0,W1,U0,U1),write(`succeeded:`), write(G1), nl, achieve_all(Gr,W1,W2,U1,U2). % achieve(Goal,InitState,FinalState,U1,U2) is true if Goal can be achieved % going from InitState to FinalState with no more than U1 steps achieve(G,W,W,U,U) :- % goals already true true_in(G,W), write(`already true `), write(G),write(W),nl. achieve(G,W0,W1,U0,U1) :- % derived relations getComponentGoals(G,Body), achieve_all(Body,W0,W1,U0,U1). achieve(dif(A,B),W,W,U,U) :- % The goal of A different than B is only % achieved if they are already different. A \= B. achieve(G,W0,do(Act,W1),U0,U2) :- % primitive relations U0>0, U1 is U0-1, achieves(Act,G), write(Act), write(` achieves `),write(G), nl, preconditions(Act,PreAct), write(`PreAct of `) , write(Act), write(` is `), write(PreAct), nl, continue, achieve_all(PreAct,W0,W1,U1,U2). % true_in(Goal,State) is true if Goal is ALREADY true in State. true_in(G,init) :- holds(G,init). true_in(G,do(A,_)) :- achieves(A,G). true_in(G,do(A,S)) :- true_in(G,S), %write(G), write(` true in `), write(S), nl, not deletes(A,G).%, write(A), write(` does not delete `),write(G),nl. % ACTIONS % move(Ag,Pos,Pos_1) is the action of Ag moving from Pos to Pos_1 preconditions(move(Ag,Pos,Pos_1), [agent(Ag),adjacent(Pos,Pos_1), agent_at(Ag,Pos)]). achieves(move(Ag,Pos,Pos_1),agent_at(Ag,Pos_1)). deletes(move(Ag,Pos,Pos_1),agent_at(Ag,Pos)). % pickup(Ag,Obj,Pos) is the action of agent Ag picking up Obj. preconditions(pickup(Ag,Obj,Pos), [agent(Ag),dif(Ag,Obj), sitting_at(Obj,Pos), agent_at(Ag,Pos) ]). achieves(pickup(Ag,Obj,Pos), carrying(Ag,Obj)). deletes(pickup(Ag,Obj,Pos), sitting_at(Obj,Pos)). % putdown(Ag,Obj,Pos) preconditions(putdown(Ag,Obj,Pos), [agent(Ag),dif(Ag,Obj), agent_at(Ag,Pos), carrying(Ag,Obj)]). achieves(putdown(Ag,Obj,Pos),sitting_at(Obj,Pos)). deletes(putdown(Ag,Obj,Pos),carrying(Ag,Obj)). % unlock(Ag,Door) preconditions(unlock(Ag,Door), [agent(Ag),blocks(Door,P_1,_), opens(Key,Door), carrying(Ag,Key), carrying(Ag,money),agent_at(Ag,P_1)]). achieves(unlock(Ag,Door),unlocked(Door)). deletes(unlock(Ag,Door),carrying(Ag,money)). % PRIMITIVE RELATIONS – cannot be decomposed primitive(carrying(_,_)). primitive(sitting_at(_,_)). primitive(agent_at(_,_)). primitive(unlocked(_)). % DERIVED RELATIONS getComponentGoals(at(Obj,Pos), [sitting_at(Obj,Pos)]). getComponentGoals(at(Obj,Pos), [agent(Ag),dif(Ag,Obj), carrying(Ag,Obj), agent_at(Ag,Pos)]). getComponentGoals(adjacent(downtown,usu),[]). getComponentGoals(adjacent(downtown,lounge),[]). getComponentGoals(adjacent(lounge,bank),[]). getComponentGoals(adjacent(bank,mail),[]). getComponentGoals(adjacent(usu,mail),[]). getComponentGoals(adjacent(mail,usu),[]). getComponentGoals(adjacent(P_1,P_2),[blocks(Door,P_1,P_2), unlocked(Door)]). getComponentGoals(blocks(door2,usu,lab2),[]). getComponentGoals(blocks(door1,lab2,lab1),[]). getComponentGoals(opens(key1,door1),[]). getComponentGoals(opens(key2,door2),[]). getComponentGoals(agent(fred),[]). continue :- nl,write('Press y to continue, any other to stop.'),nl, respkey(R), write(R), member(R,"yY"),!. continue :- throw(die_buggy_program_die). % INITIAL SITUATION holds(agent_at(fred,downtown),init). holds(sitting_at(gift,lounge),init). holds(sitting_at(diamond,lab2),init). holds(sitting_at(key1,mail),init). holds(sitting_at(key2,mail),init). holds(sitting_at(money,bank),init). holds(sitting_at(money,mail),init). achieves(init,X) :- holds(X,init). member(A,[A|Y]). member(A,[_|Y]) :- member(A,Y). % Examples of Calls to achieve and achieve_all % achieve(carrying(fred,key1),init,S,10,_). % achieve(at(key1,lab2),init,S,10,N). % achieve(carrying(fred,gift),init,S,10,N). % achieve(carrying(fred,money),init,S,10,N). % achieve(agent_at(fred,lab2),init,S,10,N). % achieve_all([agent_at(fred,usu),carrying(fred,money)],init,S,20,N). % achieve_all([carrying(fred,gift),carrying(fred,money),agent_at(fred,lab2)],init,S,20,N). % achieve_all([carrying(fred,money),carrying(fred,key2)],init,S,8,N). %achieve(agent_at(fred,lab2),init,S,10,N).