-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMonkeyAndBananas.pl
96 lines (50 loc) · 2.46 KB
/
MonkeyAndBananas.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
/*
https://cs.fit.edu/~pkc/classes/ai/swi-prolog/monkey.pl
http://www.dailyfreecode.com/code/prolog-monkey-banana-3010.aspx
http://cse.iitkgp.ac.in/~pallab/ai.slides/lec9.pdf
https://www.daniweb.com/programming/computer-science/threads/354781/how-to-solve-monkey-and-banana-problem
https://en.wikipedia.org/wiki/Monkey_and_banana_problem
*/
% To the following to run this program
% ?- canget(state(atdoor, onfloor, atwindow, hasnot), Plan).
% Plan = [walk(atdoor, atwindow), push(atwindow, middle), climb, grasp]
% Yes
% ?- canget(state(atwindow, onbox, atwindow, hasnot), Plan ).
% No
% ?- canget(state(Monkey, onfloor, atwindow, hasnot), Plan).
% Monkey = atwindow
% Plan = [push(atwindow, middle), climb, grasp]
% Yes
% adapted from Bratko, I. (1986) "Prolog: Programming for Artificial Intelligene." Addison-Wesley.
% initial state: Monkey is at door,
% Monkey is on floor,
% Box is at window,
% Monkey doesn't have banana.
%
% prolog structure: structName(val1, val2, ... )
% state(Monkey location in the room, Monkey onbox/onfloor, box location, has/hasnot banana)
% legal actions
do( state(middle, onbox, middle, hasnot), % grab banana
grab,
state(middle, onbox, middle, has) ).
do( state(L, onfloor, L, Banana), % climb box
climb,
state(L, onbox, L, Banana) ).
do( state(L1, onfloor, L1, Banana), % push box from L1 to L2
push(L1, L2),
state(L2, onfloor, L2, Banana) ).
do( state(L1, onfloor, Box, Banana), % walk from L1 to L2
walk(L1, L2),
state(L2, onfloor, Box, Banana) ).
% canget(State): monkey can get banana in State
canget(state(_, _, _, has)). % Monkey already has it, goal state
canget(State1) :- % not goal state, do some work to get it
do(State1, Action, State2), % do something (grab, climb, push, walk)
canget(State2). % canget from State2
% get plan = list of actions
canget(state(_, _, _, has), []). % Monkey already has it, goal state
canget(State1, Plan) :- % not goal state, do some work to get it
do(State1, Action, State2), % do something (grab, climb, push, walk)
canget(State2, PartialPlan), % canget from State2
add(Action, PartialPlan, Plan). % add action to Plan
add(X,L,[X|L]).