b340a92c63a0f1d8baff66ebc0da99e0981559d4
[ker1415-1.git] / report / src / planner.pl
1 % -------------= Simple planner for situation calculus =---------------
2 %
3 % Executes an iterative deepening search, by default limited to a depth
4 % of 15 levels
5 %
6 % plan. will search for a plan of maximum length
7 % plan(M). will search for a plan of maximum length up to M
8 % plan(N,M). will search for a plan of mimimum length N and maximum
9 % length M
10 %
11 %
12 % Checklist for the definition of a domain and problem:
13 %
14 % - initial state initialize fluents in s0
15 %
16 % - possibility axioms poss( <action>(...), S ) :- <precond>
17 %
18 % - successor-state axioms <fluent>( ..., result(A,S) ) :-
19 % <some events made it true>; % or
20 % <it was true all along>, % and
21 % not(<some events made it false>).
22 %
23 % - goal predicate goal(S) :- <goal conditions>
24 %
25
26 % --------------------------= High Level =-----------------------------
27
28 % default number of iterations (15)
29 plan :- plan(0, 15).
30
31 % given max number of iterations
32 plan(MaxDepth) :- plan(0, MaxDepth).
33
34 % given min and max number of iterations
35 plan(MinDepth, MaxDepth) :-
36 M is MaxDepth+1, write('Trying plans of length: '),
37 plan(Plan, MinDepth, M), nl, nl, writePlan(Plan).
38
39
40
41 % same as above, to be called from shell; include additional output
42 % messages and halt to quit pl
43 planner :- planner(0, 15).
44 planner(MaxDepth) :- planner(0, MaxDepth).
45 planner(MinDepth, MaxDepth) :-
46 nl, plan(MinDepth, MaxDepth), nl, halt;
47 write('\n\nNo plan has been found!'), nl, nl, halt.
48
49
50
51 % ---------------------------= Low Level =-----------------------------
52
53 % start from depth N, up to M
54
55 plan(P, N, M) :-
56 N < M,
57 debugoutput(write('\n\nPlans of length ')),
58 writef('%w %f',[N]),
59 seqplan(P, N, s0), !.
60
61 plan(P, N, M) :- N < M, O is N+1, plan(P, O, M).
62
63 seqplan( [], N, S ) :- N = 0, debugoutput(writeSituation(S)), goal(S).
64 seqplan( [A|L], N, S) :- N > 0, M is N-1, result(A,S,S1), seqplan(L, M, S1).
65
66 result(E,S,result(E,S)) :- primitive_action(E), poss(E,S).
67
68 % -----------------------= Support Function =--------------------------
69
70 % debugoutput will be printed only if debug(on) is defined in the KB
71 :- dynamic debug/1.
72 :- multifile debug/1.
73
74 debug(on).
75 debugoutput(A) :- debug(on), A, !; true.
76
77
78 % prints a situation as a sequence of actions, starting from s0
79 writeSituation(s0) :- write('\n s0').
80 writeSituation(result(A,S)) :- writeSituation(S), writef(" > %w", [A]).
81
82 % prints a plan as a list of actions, numbered
83
84 writePlan(List) :-
85 debugoutput(write('\nA plan has been found:\n\n')),
86 writePlan(List,1).
87
88 writePlan([],_) :-
89 nl.
90
91 writePlan([Action|List], Counter) :-
92 writef('%3R %w\n',[Counter,Action]),
93 Next is Counter+1,
94 writePlan(List,Next).
95
96
97 % hides some (unimportant) warnings from the pl interpreter
98 :- style_check([-discontiguous, -singleton]).
99
100
101
102 % ---------------------------------------------------------------------
103 % ---------------------------------------------------------------------