| 423 |
ira |
1 |
#!/usr/bin/env python
|
|
|
2 |
|
|
|
3 |
__author__ = "Ira W. Snyder (devel@irasnyder.com)"
|
|
|
4 |
__copyright__ = "Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)"
|
|
|
5 |
__license__ = "GNU GPL v2 (or, at your option, any later version)"
|
|
|
6 |
|
|
|
7 |
from PyCompat import *
|
|
|
8 |
import re
|
|
|
9 |
|
| 429 |
ira |
10 |
LOCATIONS = (DOOR, WINDOW, MIDDLE) = ('Door', 'Window', 'Middle')
|
|
|
11 |
BOX_LOC = (UP, DOWN) = ('Up', 'Down')
|
|
|
12 |
|
| 423 |
ira |
13 |
# Solution to the Monkey and Banana Problem
|
|
|
14 |
|
|
|
15 |
class Action (object):
|
|
|
16 |
def __init__ (self, State):
|
|
|
17 |
self.State = State
|
| 429 |
ira |
18 |
self.PC = [] # Preconditions
|
|
|
19 |
self.NPC= [] # Negative Preconditions
|
|
|
20 |
self.AL = [] # Add List
|
|
|
21 |
self.DL = [] # Delete List
|
| 423 |
ira |
22 |
|
|
|
23 |
def __repr__ (self):
|
|
|
24 |
return '%s' % self.State
|
|
|
25 |
|
|
|
26 |
def meetsPreconditions (self):
|
|
|
27 |
for e in self.PC:
|
|
|
28 |
if e not in self.State:
|
|
|
29 |
return False
|
|
|
30 |
|
| 429 |
ira |
31 |
for e in self.NPC:
|
|
|
32 |
if e in self.State:
|
|
|
33 |
return False
|
|
|
34 |
|
| 423 |
ira |
35 |
return True
|
|
|
36 |
|
|
|
37 |
def takeAction (self):
|
|
|
38 |
if not self.meetsPreconditions ():
|
|
|
39 |
raise ValueError
|
|
|
40 |
|
|
|
41 |
for e in self.AL:
|
|
|
42 |
self.State.add (e)
|
|
|
43 |
|
|
|
44 |
for e in self.DL:
|
|
|
45 |
self.State.remove (e)
|
|
|
46 |
|
|
|
47 |
# Return the new State
|
|
|
48 |
return self.State
|
|
|
49 |
|
|
|
50 |
def isGoal (self):
|
|
|
51 |
return 'HasBanana()' in self.State
|
|
|
52 |
|
|
|
53 |
def findMonkeyPosition (self):
|
|
|
54 |
regex = re.compile ('At\(Monkey,(.+)\)')
|
|
|
55 |
|
|
|
56 |
for e in self.State:
|
|
|
57 |
if regex.match (e):
|
|
|
58 |
return regex.match (e).group (1)
|
|
|
59 |
|
|
|
60 |
def findBoxPosition (self):
|
|
|
61 |
regex = re.compile ('At\(Box,(.+)\)')
|
|
|
62 |
|
|
|
63 |
for e in self.State:
|
|
|
64 |
if regex.match (e):
|
|
|
65 |
return regex.match (e).group (1)
|
|
|
66 |
|
|
|
67 |
def monkeyOnBox (self):
|
|
|
68 |
return 'OnBox()' in self.State
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
class Walk (Action):
|
|
|
72 |
def __init__ (self, State, F, T):
|
|
|
73 |
Action.__init__ (self, State)
|
|
|
74 |
self.F = F
|
|
|
75 |
self.T = T
|
|
|
76 |
|
|
|
77 |
self.PC = ['At(Monkey,%s)' % (F,) , 'OnFloor()']
|
| 429 |
ira |
78 |
self.NPC= ['OnBox()', 'At(Monkey,%s)' % (T,)]
|
| 423 |
ira |
79 |
self.AL = ['At(Monkey,%s)' % (T,)]
|
|
|
80 |
self.DL = ['At(Monkey,%s)' % (F,)]
|
|
|
81 |
|
|
|
82 |
class Push (Action):
|
|
|
83 |
def __init__ (self, State, F, T):
|
|
|
84 |
Action.__init__ (self, State)
|
|
|
85 |
self.F = F
|
|
|
86 |
self.T = T
|
|
|
87 |
|
|
|
88 |
self.PC = ['At(Monkey,%s)' % (F,) , 'At(Box,%s)' % (F,) , 'OnFloor()']
|
| 429 |
ira |
89 |
self.NPC= ['At(Monkey,%s)' % (T,) , 'At(Box,%s)' % (T,), 'OnBox()']
|
| 423 |
ira |
90 |
self.AL = ['At(Monkey,%s)' % (T,) , 'At(Box,%s)' % (T,)]
|
|
|
91 |
self.DL = ['At(Monkey,%s)' % (F,) , 'At(Box,%s)' % (F,)]
|
|
|
92 |
|
|
|
93 |
class Climb (Action):
|
|
|
94 |
def __init__ (self, State, D):
|
|
|
95 |
Action.__init__ (self, State)
|
|
|
96 |
self.D = D
|
|
|
97 |
|
| 429 |
ira |
98 |
if D == UP:
|
| 423 |
ira |
99 |
self.PC = ['At(Monkey,%s)' % (self.findBoxPosition()), \
|
|
|
100 |
'At(Box,%s)' % (self.findMonkeyPosition()), 'OnFloor()']
|
| 429 |
ira |
101 |
self.NPC= ['OnBox()']
|
| 423 |
ira |
102 |
self.AL = ['OnBox()']
|
|
|
103 |
self.DL = ['OnFloor()']
|
|
|
104 |
else: # D == DOWN
|
|
|
105 |
self.PC = ['At(Monkey,%s)' % (self.findBoxPosition()), \
|
|
|
106 |
'At(Box,%s)' % (self.findMonkeyPosition()), 'OnBox()']
|
|
|
107 |
self.AL = ['OnFloor()']
|
|
|
108 |
self.DL = ['OnBox()']
|
|
|
109 |
|
|
|
110 |
class Grab (Action):
|
|
|
111 |
def __init__ (self, State):
|
|
|
112 |
Action.__init__ (self, State)
|
| 429 |
ira |
113 |
self.PC = ['OnBox()', 'At(Monkey,%s)' % (MIDDLE), 'NoBanana()']
|
|
|
114 |
self.NPC= ['OnFloor()']
|
| 423 |
ira |
115 |
self.AL = ['HasBanana()']
|
|
|
116 |
self.DL = ['NoBanana()']
|
|
|
117 |
|
|
|
118 |
# vim: set ts=4 sts=4 sw=4:
|
|
|
119 |
|