forked from SWI-Prolog-Education/talespin-annie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plannerb4refactor.pl
executable file
·156 lines (137 loc) · 4.11 KB
/
plannerb4refactor.pl
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
:- module(planner, [
plan/4
]).
/** <module> A planner.
*
* Released under the terms of the SWI-Prolog license
*
* The planner depends on action modules, called 'genres'
*
*
*/
% ! plan(+InitConditions:list, +Genre:atom, -Plan:list, +Options:list)
% ! is nondet
%
% Given a set of initial conditions and a Genre, generate random plans
% on backtracking
%
% @arg InitConditions is a list of conditions at the start. Goals
% should be listed as goal(X), and when all X appear in the conditions,
% planning terminates
% @arg Genre is an atomic genre, allowing actions to be defined for
% different genres.
% @arg Plan - a list of action names
% @arg Options - the only available options are order(random)
% (default), which randomizes at each step, and order(first), which
% takes actions in lexical order
%
plan(InitConditions, Genre, Plan, Options) :-
( member(order(Order), Options)
; Order = random
),
empty_queue,
list_to_ord_set(InitConditions, OrdCond),
plan_(Genre,
Order,
/*Open*/ [OrdCond],
/* Closed*/[OrdCond],
RPlan),
reverse(RPlan, Plan).
% Genre is the module of the actions
% Order is the argument of the order/1 option
% Open is a list of nodes to explore, as State-Story pairs
% Closed is a list of nodes to never again put on Open
% RPlan is the plan, with the last action first
%
% if the Open list is exhausted,
plan_(_, _, [], _, _) :-
!,
fail.
%
% if we've reached a goal state we have a solution.
plan_(_Genre,
_Order,
[State-Story |_],
_Closed,
Story) :-
at_goal(State).
plan_(Genre,
Order,
[State-Story | Open],
Closed,
FullStory) :-
ordered_possible_action_states(State-Story, Genre, Order, Possible),
add_unique_children_to_open(State-Story, Genre, Possible, Open, Open1),
plan_(Genre, Order, Open1, [State | Closed], FullStory).
plan_(Cond, _Genre, Prev, _, _, _) :-
member(Cond, Prev), % we've been here before
!,
fail.
plan_(Cond, Genre, _Prev, _SoFar, _Plan, _Order) :-
possible_actions(Cond, Genre, []), % no move from here
!,
fail.
plan_(Cond, _, _, _, _, _) :-
member(dead, Cond), % dead end
!,
fail.
plan_(Cond, Genre, Prev, SoFar, Plan, Order) :-
add_queue(SoFar, Cond),
pop_queue(FirstSoFar, FirstCond),
possible_actions(FirstCond, Genre, RawPossible),
( Order = random
-> once(random_permutation(RawPossible, Possible))
; RawPossible = Possible
),
member(Action, Possible),
apply_action(Action, FirstCond, Genre, NewCond),
debug(planner(action), '~w', [Action]),
debug(planner(step), '~w ~w ~w', [FirstCond, Action, NewCond]),
plan_(NewCond, Genre, [Cond | Prev], [Action | FirstSoFar], Plan, Order).
at_goal(Cond) :-
check_goals(Cond, Cond),
!. % green
check_goals(_, []).
check_goals([goal(Goal)|T], Cond) :-
memberchk(Goal, Cond),
check_goals(T, Cond).
check_goals([H|T], Cond) :-
H \= goal(_),
check_goals(T, Cond).
possible_actions(Cond, Genre, Possible) :-
findall(Name, possible_action(Cond, Genre, Name), Possible).
possible_action(Cond, Genre, Name) :-
Genre:action(Name, Act),
action{ pre:Pre,
negpre: NegPre
} :< Act,
maplist(is_in(Cond), Pre),
maplist(not_in(Cond), NegPre).
is_in(List, Member) :-
memberchk(Member, List).
not_in(List, Member) :-
\+ memberchk(Member, List).
:- dynamic q/2. % q(PlanSoFar, CurrentCondition)
empty_queue :-
retractall(q(_,_)).
add_queue(PartialPlan, Cond) :-
assertz(q(PartialPlan, Cond)).
pop_queue(PartialPlan, Cond) :-
once(q(PartialPlan, Cond)),
retractall(q(PartialPlan, Cond)).
apply_action(Name, Cond, Genre, NewCond) :-
Genre:action(Name, Action),
action{
add: Add,
remove: Remove
} :< Action,
my_subtract(Cond, Remove, C1),
list_to_ord_set(Add, OrdAdd),
ord_union(C1, OrdAdd, NewCond).
my_subtract([], _, []).
my_subtract([H|T], Remove, TOut) :-
memberchk(H, Remove),
!,
my_subtract(T, Remove, TOut).
my_subtract([H|T], Remove, [H|TOut]) :-
my_subtract(T, Remove, TOut).