% Blocks world coding by Ilkka Niemela % % typical command lines % cat bw.lp anomaly | lparse -1 -d none -c steps=3 | smodels % cat bw.lp bw-large.c | lparse -1 -d none -c steps=8 | smodels | awk -f plan.awk % initial conditions % on(a,b,0). % ... % goal conditions % goal(T) :- time(T), on(a,c,T),...,on(f,g,T). % see, e.g., anomaly for an example % goal predicate which holds when the goal conditions have be reached goal :- time(T), goal(T). :- not goal. % goal(T) holds for all subsequent situations % This is used for blocking all operators after the goal has been reached goal(T2) :- nextstate(T2,T1), goal(T1). % operator preconditions moveop(X,Y,T):- time(T), block(X), object(Y), X != Y, on_something(X,T), available(Y,T), not covered(X,T), not covered(Y,T), not blocked_move(X,Y,T). % operator effects on(X,Y,T2) :- block(X), object(Y), nextstate(T2,T1), moveop(X,Y,T1). on_something(X,T) :- block(X), object(Z), time(T), on(X,Z,T). available(table,T) :- time(T). available(X,T) :- block(X), time(T), on_something(X,T). covered(X,T) :- block(Z), block(X), time(T), on(Z,X,T). % frame axioms on(X,Y,T2) :- nextstate(T2,T1), block(X), object(Y), on(X,Y,T1), not moving(X,T1). moving(X,T) :- time(T), block(X), object(Y), moveop(X,Y,T). % Stop applying operators when the goal has been reached blocked_move(X,Y,T):- block(X), object(Y), time(T), goal(T). % moveop(X,Y,T) is blocked if its not chosed (not moveop(X,Y,T) holds) blocked_move(X,Y,T) :- time(T), block(X), object(Y), not moveop(X,Y,T). % An object can be moved by one move operation at a time blocked_move(X,Y,T) :- block(X), object(Y), object(Z), time(T), moveop(X,Z,T), Y != Z. % An object cannot be moved on top of an object that is moved blocked_move(X,Y,T) :- block(X), object(Y), time(T), moving(Y,T). % Two objects cannot be moved on top of the same object by % one move operation at a time blocked_move(X,Y,T) :- block(X), block(Y), block(Z), time(T), moveop(Z,Y,T), X != Z. % pruning rules % do not move blocks from table to table :- block(X), time(T), moveop(X,table,T), on(X,table,T). % do not move a block on top of something and then on the table :- nextstate(T2,T1), block(X), object(Y), moveop(X,Y,T1), moveop(X,table,T2). % facts time(0..steps). nextstate(Y,X) :- time(X), time(Y), Y = X + 1. object(table). object(X) :- block(X).