-
Notifications
You must be signed in to change notification settings - Fork 1
/
riverworld.py
199 lines (144 loc) · 4.56 KB
/
riverworld.py
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
from abstracttypes import Action, Domain, State, getType, checkPredicateTrue
from blockworld import checkParams
import customerrors as err
from copy import deepcopy
#This entire state representation is messed
#This doesn't work as required yet
"""
Atually not sure how no params is gonna go...
"""
class RiverState(State):
def __init__(self):
super().__init__()
self.addObject(Item("wolf"))
self.addObject(Item("sheep"))
self.addObject(Item("cabbage"))
self.addObject(Boat("Boat"))
# self.addObject(Rocket("Rocket"))
def __str__(self):
ret = ""
for x in self.objects:
ret += str(x) + "\n"
return ret
def isGoalSatisfied(self):
for obj in self.objects:
if obj.left:
return False
for item in self.obj_types["Item"]:
if self.get(item).eaten:
return False
return True
# def __eq__(self, other):
#TO IMPLEMENT
class Item():
def __init__(self, name):
self.name = name
self.left = True
self.eaten = False
def __str__(self):
return self.name + " \n Left: " + str(self.left) + "\n Eaten: " + str(self.eaten)
class Boat():
def __init__(self, name):
self.name = name
self.left = True
self.inside = None
def __str__(self):
return self.name + "\n Left: " + str(self.left) + "\n Inside: " + str(self.inside)
class Rocket():
def __init__(self, name):
self.name = name
self.left = True
def __str__(self):
return "Rocket Left: " + str(self.left)
class cross(Action):
def __init__(self, state, name = "cross"):
super().__init__(state, name)
self.param_types = ["Boat"]
@staticmethod
def _get_obj_sides(state):
sides = {}
for obj in state.obj_types["Item"]:
try:
sides[state.get(obj).left].append(state.get(obj).name)
except KeyError as e:
#We have no record of that type yet
sides[state.get(obj).left] = [state.get(obj).name]
return sides
@checkParams
def checkPredicates(self, state, b_name: str):
#There are no predicates to having the boat cross the river
test_state = deepcopy(state)
#First move the items over
if test_state.get("Boat").inside != None:
state.get(test_state.get("Boat").inside).left = not(state.get(test_state.get("Boat").inside).left)
#The eating checking happens here
for s in self._get_obj_sides(state).values():
if ("wolf" in s and "sheep" in s):
raise err.PredicateFailed()
elif ("sheep" in s and "cabbage" in s):
raise err.PredicateFailed()
pass
@checkParams
def doAction(self, state, b_name: str):
boat = state.get(b_name)
#First move the items over
if boat.inside != None:
state.get(boat.inside).left = not(state.get(boat.inside).left)
#The only effect on the Boat that occurs is that it's .left param is switched
boat.left = not(boat.left)
#The eating checking happens here
for s in self._get_obj_sides(state).values():
if ("wolf" in s and "sheep" in s):
state.get("sheep").eaten = True
elif ("sheep" in s and "cabbage" in s):
state.get("cabbage").eaten = True
class load(Action):
def __init__(self, state, name="load"):
super().__init__(state, name)
self.param_types = ["Item"]
@checkParams
def checkPredicates(self, state, i_name: str):
#Check that the item and the boat are on the same side of the river
#Also check that the boat isn't already full
item = state.get(i_name)
if state.get("Boat").inside != None:
raise err.PredicateFailed("Boat already full")
if state.get("Boat").left != item.left:
raise err.PredicateFailed("Boat and item on different sides")
@checkParams
def doAction(self, state, i_name: str):
state.get("Boat").inside = i_name
class unload(Action):
def __init__(self, state, name="unload"):
super().__init__(state, name)
self.param_types = []
@checkParams
def checkPredicates(self, state):
if state.get("Boat").inside == None:
raise err.PredicateFailed("Boat empty")
@checkParams
def doAction(self, state):
#Set the .left of the item to be the same as the current of the boat
item = state.get(state.get("Boat").inside)
item.left = state.get("Boat").left
class launch(Action):
def __init__(self, state, name = "launch"):
super().__init__(state, name)
self.param_types = ["Rocket"]
@checkParams
def checkPredicates(self, state, rname: str):
r = state.get(rname)
#Predicates, maybe should move them
onleft = (lambda x: x.left)
checkPredicateTrue(onleft, r)
@checkParams
def doAction(self, state, rname: str):
r = state.get(rname)
r.left = False
class RiverWorld(Domain):
def __init__(self):
super().__init__(RiverState())
self.cross = cross(self)
# self.launch = launch(self)
self.load = load(self)
self.unload = unload(self)