forked from Anniepoo/prolog-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
talespin2.pl
282 lines (227 loc) · 11.1 KB
/
talespin2.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
:- module(talespin, [talespin/0]).
% File: talespin.pl
% Author: Peter Clark
% Date: Jan 1999
% Purpose: Simple and highly improvised reconstruction of Meehan's TALE-SPIN
% story generator, here applied to aviation incident "stories". This
% reconstruction undoubtedly misses out a lot of Meehan's program, and
% also adds in new parts/approaches that Meehan didn't originally use.
talespin :-
InitialSituation =
[ plocation(passengers1, gate(seattle)),
alocation(airplane1, gate(seattle)),
flight_path(seattle, chicago),
flight_path(chicago, dallas),
airplane(airplane1),
passengers(passengers1) ],
InitialGoal = plocation(passengers1, gate(dallas)),
make_best_plan(InitialGoal, InitialSituation, InitialPlan),
Prob = 0.3, % Probability of incident occurring at a particular step
execute_plan(InitialPlan, InitialSituation, InitialGoal,
Prob, StoryActions, _StorySituations),
write('Once upon a time...'), nl,
anglify(StoryActions, StoryText),
lwrite(StoryText).
% ======================================================================
% THE STRIPS-LIKE PLANNER
% ======================================================================
% make_plan/3: Simple backward-chaining planner, without a protected goal list.
make_best_plan(Goal, Situation, BestPlan) :-
findall(Quality-Plan,
( make_plan(Goal,Situation,Plan),
plan_quality(Plan,Quality) ),
RankedPlans),
sort(RankedPlans, OrderedPlans),
last(OrderedPlans, _-BestPlan).
plan_quality(Plan, Quality) :-
length(Plan, Length), % lose 10 points per step
( member(evacuate(_),Plan) -> Cost = 1 ; Cost = 0 ), % lose 1 for evacuating
Quality is 100 - Length*10 - Cost.
% ----------
make_plan(Goal, Situation, Plan) :-
make_plan(Goal, Situation, [], _FinalSituation, Plan).
make_plan(Goal, Situation, _, Situation, []) :-
satisfied(Goal, Situation). % no cut, as maybe multiple solns
make_plan(Goal, Situation, GoalStack, NewSituation, Actions) :-
\+ satisfied(Goal, Situation),
\+ member(Goal, GoalStack), % avoid looping
event_definition(action, Action, PCs, Dels, Adds),
achieves(PCs, Dels, Adds, Goal),
make_plans(PCs, Situation, [Goal|GoalStack], MidSituation, PreActions),
apply_effects(Dels, Adds, MidSituation, NewSituation),
append(PreActions, [Action], Actions).
make_plans([], Situation, _, Situation, []).
make_plans([Goal|Goals], Situation, GoalStack, NewSituation, Actions) :-
make_plan(Goal, Situation, GoalStack, MidSituation, FirstActions),
make_plans(Goals, MidSituation, GoalStack, NewSituation, RestActions),
append(FirstActions, RestActions, Actions).
achieves(_, _, Adds, Goal) :- % Goal = effect
member(Goal, Adds).
achieves(_, _, Adds, Goal) :- % Goal = ramification of effect
rule(( Goal :- Facts )),
subset(Facts, Adds).
% ======================================================================
% THE SIMULATOR
% This is similar to the planner, *except* it will also throw a spanner
% in the works (ie. a happening), requiring replanning.
% ======================================================================
% Final clause - we're done
execute_plan([], FinalSituation, _, _, [], [FinalSituation]) :-
!.
% 'happening' (incident) happens
execute_plan(_Actions, Sitn, Goal, P, [Happening|NextActions], [Sitn|NextSitns]) :-
maybe(P), % incident happens!! Abandon old Actions and Goal...
!,
findall(Happening,
( event_definition(happening,Happening,PCs,_,_),
satisfieds(PCs,Sitn) ), % is physically feasible
Happenings),
rnd_member(Happening, Happenings), % choose a random happening
do_event(Happening, Sitn, NewSitn),
revise_goal(NewSitn, Goal, NewGoal),
make_best_plan(NewGoal, NewSitn, NewActions),
execute_plan(NewActions, NewSitn, NewGoal, 0, NextActions, NextSitns).
% normal event happens
execute_plan([Action|Actions], Sitn, Goal, P, [Action|NextActions], [Sitn|NextSitns]) :-
do_event(Action, Sitn, NewSitn),
execute_plan(Actions, NewSitn, Goal, P, NextActions, NextSitns).
do_event(Event, Situation, NewSituation) :-
event_definition(_, Event, _PCs, Dels, Adds),
apply_effects(Dels, Adds, Situation, NewSituation).
% Do the deletes and adds as appropriate
apply_effects(Dels, Adds, Situation, NewSituation) :-
removes(Dels, Situation, MidSituation),
append(Adds, MidSituation, NewSituation).
% ======================================================================
% OTHER UTILITIES
% ======================================================================
satisfieds([], _).
satisfieds([F|Fs], S) :-
satisfied(F, S),
satisfieds(Fs, S).
satisfied(Fact, Situation) :-
member(Fact, Situation).
satisfied(Fact, Situation) :-
rule((Fact :- Facts)), % Fact is a ramification of the world
satisfieds(Facts, Situation).
% ---------- writing...
lwrite([]).
lwrite([X|Xs]) :- write(' '), lwrite2(X), nl, lwrite(Xs).
lwrite2([]).
lwrite2([BitX|BitXs]) :- !, write(BitX), lwrite2(BitXs).
lwrite2(X) :- write(X).
anglify([], []).
anglify([Event|Events], [English|Englishs]) :-
event_english(Event, English),
anglify(Events, Englishs).
% ======================================================================
% GENERAL UTILITIES
% ======================================================================
removes([], L, L).
removes([R|Rs], L, NewL) :-
remove(R, L, MidL),
removes(Rs, MidL, NewL).
remove(A, [A|B], B).
remove(A, [C|B], [C|NewB]) :-
remove(A, B, NewB).
subset([], _).
subset([X|Xs], Ys) :- remove(X, Ys, RestYs), subset(Xs, RestYs).
nmember(Elem, List, N) :-
nmember(Elem, List, 1, N).
nmember(Elem, [Elem|_], N, N).
nmember(Elem, [_|List], NSoFar, N) :-
NewN is NSoFar + 1,
nmember(Elem, List, NewN, N).
% ---------- Randomization utilities
:- dynamic lastrnd/1.
lastrnd(0).
maybe(P) :- random_float < P, !. % succeed with probability P
rnd_member(X, Xs) :-
length(Xs, L),
R is random_float,
N is integer(R*L) + 1,
nmember(X, Xs, N), !.
% ======================================================================
% THE FLIGHT INCIDENT KNOWLEDGE BASE
% ======================================================================
event_definition(Type, Event, PCs, Adds, Dels) :- ed(Type, Event, PCs, Adds, Dels, _).
event_english(Event, English) :- ed(_, Event, _, _, _, English).
% ---------- Routine actions... ----------
ed(action, load(Passengers,Airplane),
/*pcs*/ [plocation(Passengers,gate(Airport)),alocation(Airplane,gate(Airport))],
/*del*/ [plocation(Passengers,gate(Airport))],
/*add*/ [contains(Airplane,Passengers)],
/*txt*/ 'The passengers boarded the plane.' ).
ed(action, taxi_to_runway(Airplane),
/*pcs*/ [alocation(Airplane,gate(Airport))],
/*del*/ [alocation(Airplane,gate(Airport))],
/*add*/ [alocation(Airplane,runway(Airport))],
/*txt*/ 'The plane taxiied to the runway.' ).
ed(action, take_off(Airplane,Airport),
/*pcs*/ [alocation(Airplane,runway(Airport))],
/*del*/ [alocation(Airplane,runway(Airport))],
/*add*/ [alocation(Airplane,near(Airport))],
/*txt*/ ['The plane took off from ',Airport,'.']).
ed(action, cruise(Airplane,Airport1,Airport2),
/*pcs*/ [flight_path(Airport1,Airport2),alocation(Airplane,near(Airport1))],
/*del*/ [alocation(Airplane,near(Airport1))],
/*add*/ [alocation(Airplane,near(Airport2))],
/*txt*/ ['The plane cruised towards ',Airport2,'.']).
ed(action, land(Airplane,Airport2),
/*pcs*/ [alocation(Airplane,near(Airport2))],
/*del*/ [alocation(Airplane,near(Airport2))],
/*add*/ [alocation(Airplane,runway(Airport2))],
/*txt*/ ['The plane landed at ',Airport2,'.']).
ed(action, taxi_to_gate(Airplane),
/*pcs*/ [alocation(Airplane,runway(Airport))],
/*del*/ [alocation(Airplane,runway(Airport))],
/*add*/ [alocation(Airplane,gate(Airport))],
/*txt*/ 'The plane taxiied to the gate.' ).
ed(action, unload(Passengers,Airplane),
/*pcs*/ [contains(Airplane,Passengers),alocation(Airplane,gate(Airport))],
/*del*/ [contains(Airplane,Passengers)],
/*add*/ [plocation(Passengers,gate(Airport))],
/*txt*/ 'The passengers disembarked.' ).
% ---------- Emergency actions... ----------
ed(action, evacuate(Airplane),
/*pcs*/ [a_on_ground(Airplane),alocation(Airplane,Loc),contains(Airplane,Passengers)],
/*del*/ [contains(Airplane,Passengers)],
/*add*/ [plocation(Passengers,Loc)],
/*txt*/ 'The passengers were evacuated from the plane.' ).
ed(action, emergency_landing(Airplane),
/*pcs*/ [alocation(Airplane,near(Airport2))],
/*del*/ [alocation(Airplane,near(Airport2))],
/*add*/ [alocation(Airplane,on_ground_near(Airport2))],
/*txt*/ ['The pilot made an emergency landing near ',Airport2,'.']).
ed(action, medical_help(Passengers),
/*pcs*/ [plocation(Passengers, gate(_))], % any gate
/*del*/ [],
/*add*/ [medical_help(Passengers)],
/*txt*/ 'Medical help was provided.' ).
% ---------- Possible happenings... ----------
ed(happening, fire(engine),
/*pcs*/ [], % can happen anywhere
/*del*/ [],
/*add*/ [on_fire(engine)],
/*txt*/ 'The engine caught fire.' ).
% ---------- Possible happenings... ----------
ed(happening, ill_passenger,
/*pcs*/ [contains(Airplane,Passengers),passengers(Passengers),airplane(Airplane)],
/*del*/ [],
/*add*/ [ill_passenger],
/*txt*/ 'A passenger became very ill.' ).
% Ramifications of facts about the world...
rule(( a_on_ground(Airplane) :- [alocation(Airplane,gate(_))] )).
rule(( a_on_ground(Airplane) :- [alocation(Airplane,runway(_))] )).
rule(( a_on_ground(Airplane) :- [alocation(Airplane,on_ground_near(_))] )).
rule(( p_on_ground(Passengers) :- [plocation(Passengers,gate(_))] )).
rule(( p_on_ground(Passengers) :- [plocation(Passengers,runway(_))] )).
rule(( p_on_ground(Passengers) :- [plocation(Passengers,on_ground_near(_))] )).
% Rules for revising the goal
revise_goal(Situation, plocation(Passengers,_), Goal) :- % If the engine's on fire,
memberchk(on_fire(engine), Situation), !, % get to the ground asap!
Goal = p_on_ground(Passengers).
revise_goal(Situation, plocation(Passengers,_), Goal) :- % If a passenger's ill,
memberchk(ill_passenger, Situation), !, % get to a gate somewhere.
Goal = medical_help(Passengers).
revise_goal(_Situation, Goal, Goal).