This repository has been archived by the owner on Jan 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocks-world.lp
78 lines (60 loc) · 1.85 KB
/
blocks-world.lp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
% This is a demo ASP program for blocks world planning.
% This helps you to understand how the asp planning works.
% The incmode introduce atom t/0 as planning step.
#include <incmode>.
% The code in the base section does not contain 't'.
#program base.
% Definition
block(b0).
block(b1).
block(b2).
location(table).
location(X) :- block(X).
holds(F, 0) :- init(F).
% Initial state:
%
% +---+
% | 2 |
% +---+ +---+
% | 0 | | 1 |
% +---+ +---+
%%%%%%%%%%%%%%%%%%%%%%
init(on(b1, table)).
init(on(b2, b0)).
init(on(b0, table)).
% Goal state:
%
% +---+
% | 2 |
% +---+
% | 1 |
% +---+
% | 0 |
% +---+
%%%%%%%%%%%%%%%%%%%%%%
goal(on(b1, b0)).
goal(on(b2, b1)).
goal(on(b0, table)).
% The code in the step section will be repeated for each planning step t > 0.
% The recursion logic is defined in this section.
#program step(t).
% This is called the 'aggregate'. It requires that there is one and only one
% move(X, Y, t) should be hold at each t. Moreover, X should be a block, Y
% should be a location, and X is not equal to Y.
{ move(X, Y, t) : block(X), location(Y), X != Y } = 1.
% This defines the effect of move: if move(X, Y, t) is hold, X is on Y at step t.
holds(on(X, Y), t) :- move(X, Y, t).
% A constraint of move: it should be clear on X to move X.
:- move(X, Y, t), holds(on(A, X), t-1).
% Another constraint of move: cannot move to a place where there is a block, except for table.
:- move(X, Y, t), holds(on(B, Y), t-1), B != X, Y != table.
% Definition of atom moved/2
moved(X, t) :- move(X, Y, t).
% The inertia rule: a block stays on the same place if it is not moved.
holds(on(X, Z), t) :- holds(on(X, Z), t-1), not moved(X, t).
% The code in the test section will only be checked at the final step t >= 0.
#program check(t).
% To check if the goal is achieved.
:- query(t), goal(F), not holds(F, t).
% Control which atoms will be printed out.
#show move/3.