-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplanGraphLevel.py
281 lines (228 loc) · 11 KB
/
planGraphLevel.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
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
280
281
from action import Action
from actionLayer import ActionLayer
from util import Pair
from proposition import Proposition
from propositionLayer import PropositionLayer
# Helpers
def isDifferent(pair):
a1, a2 = pair
return a1 != a2
def lmap(func, *iterable):
return list(map(func, *iterable))
def lfilter(func, *iterable):
return list(filter(func, *iterable))
class PlanGraphLevel(object):
"""
A class for representing a level in the plan graph.
For each level i, the PlanGraphLevel consists of the actionLayer and propositionLayer at this level in this order!
"""
independentActions = [] # updated to the independentActions of the propblem GraphPlan.py line 31
# updated to the actions of the problem GraphPlan.py line 32 and
# planningProblem.py line 25
actions = []
# updated to the propositions of the problem GraphPlan.py line 33 and
# planningProblem.py line 26
props = []
@staticmethod
def setIndependentActions(independentActions):
PlanGraphLevel.independentActions = independentActions
@staticmethod
def setActions(actions):
PlanGraphLevel.actions = actions
@staticmethod
def setProps(props):
PlanGraphLevel.props = props
def __init__(self):
"""
Constructor
"""
self.actionLayer = ActionLayer() # see actionLayer.py
self.propositionLayer = PropositionLayer() # see propositionLayer.py
def getPropositionLayer(self):
# returns the proposition layer
return self.propositionLayer
def setPropositionLayer(self, propLayer):
# sets the proposition layer
self.propositionLayer = propLayer
def getActionLayer(self):
# returns the action layer
return self.actionLayer
def setActionLayer(self, actionLayer):
# sets the action layer
self.actionLayer = actionLayer
def updateActionLayer(self, previousPropositionLayer):
"""
Updates the action layer given the previous proposition layer (see propositionLayer.py)
You should add an action to the layer if its preconditions are in the previous propositions layer,
and the preconditions are not pairwise mutex.
allAction is the list of all the action (include noOp) in the domain
You might want to use those functions:
previousPropositionLayer.isMutex(prop1, prop2) returns true if prop1 and prop2 are mutex at the previous propositions layer
previousPropositionLayer.allPrecondsInLayer(action) returns true if all the preconditions of action are in the previous propositions layer
self.actionLayer.addAction(action) adds action to the current action layer
"""
allActions = PlanGraphLevel.actions
def isAllPrecondsInLayer(action):
return previousPropositionLayer.allPrecondsInLayer(action)
def isPropositionMutex(action):
# create all the combinations of the actions preconditions
all_combinations = [(cond1, cond2)
for cond1 in action.getPre()
for cond2 in action.getPre()]
# check if exists at least one mutex
are_any_mutex = any(
lmap(lambda conds: previousPropositionLayer.isMutex(conds[0], conds[1]),
lfilter(isDifferent, all_combinations)))
# retun if rthere are no mutex
return not are_any_mutex
addActionToLayer = self.actionLayer.addAction
lmap(addActionToLayer,
lfilter(isPropositionMutex,
lfilter(isAllPrecondsInLayer,
allActions)))
def updateMutexActions(self, previousLayerMutexProposition):
"""
Updates the mutex list in self.actionLayer,
given the mutex proposition from the previous layer.
currentLayerActions are the actions in the current action layer
You might want to use this function:
self.actionLayer.addMutexActions(action1, action2)
adds the pair (action1, action2) to the mutex list in the current action layer
Note that action is *not* mutex with itself
"""
currentLayerActions = self.actionLayer.getActions()
# create all the combinations of the actions
all_combinations = [(action1, action2)
for action1 in currentLayerActions
for action2 in currentLayerActions]
def isMutexActions(actions):
a1, a2 = actions
return mutexActions(a1, a2, previousLayerMutexProposition)
def isNotIn(actions):
a1, a2 = actions
return Pair(a1, a2) not in self.actionLayer.mutexActions
def addMutexActions(actions):
a1, a2 = actions
self.actionLayer.addMutexActions(a1, a2)
lmap(addMutexActions,
lfilter(isNotIn,
lfilter(isMutexActions,
lfilter(isDifferent, all_combinations))))
def updatePropositionLayer(self):
"""
Updates the propositions in the current proposition layer,
given the current action layer.
don't forget to update the producers list!
Note that same proposition in different layers might have different producers lists,
hence you should create two different instances.
currentLayerActions is the list of all the actions in the current layer.
You might want to use those functions:
dict() creates a new dictionary that might help to keep track on the propositions that you've
already added to the layer
self.propositionLayer.addProposition(prop) adds the proposition prop to the current layer
"""
currentLayerActions = self.actionLayer.getActions()
def isNotIn(prop):
return prop not in self.propositionLayer.getPropositions()
addProposition = self.propositionLayer.addProposition
for action in currentLayerActions:
for prop in action.getAdd():
if isNotIn(prop):
addProposition(prop)
prop.addProducer(action)
def updateMutexProposition(self):
"""
updates the mutex propositions in the current proposition layer
You might want to use those functions:
mutexPropositions(prop1, prop2, currentLayerMutexActions) returns true if prop1 and prop2 are mutex in the current layer
self.propositionLayer.addMutexProp(prop1, prop2) adds the pair (prop1, prop2) to the mutex list of the current layer
"""
currentLayerPropositions = self.propositionLayer.getPropositions()
currentLayerMutexActions = self.actionLayer.getMutexActions()
# create all the combinations of the propositions
all_combinations = [(prop1, prop2)
for prop1 in currentLayerPropositions
for prop2 in currentLayerPropositions]
def isMutexPropositions(propositions):
p1, p2 = propositions
return mutexPropositions(p1, p2, currentLayerMutexActions)
def isNotIn(propositions):
p1, p2 = propositions
return Pair(p1, p2) not in self.propositionLayer.mutexPropositions
def addMutexPropositions(propositions):
p1, p2 = propositions
return self.propositionLayer.mutexPropositions.append(Pair(p1, p2))
lmap(addMutexPropositions,
lfilter(isNotIn,
lfilter(isMutexPropositions,
lfilter(isDifferent, all_combinations))))
def expand(self, previousLayer):
"""
Your algorithm should work as follows:
First, given the propositions and the list of mutex propositions from the previous layer,
set the actions in the action layer.
Then, set the mutex action in the action layer.
Finally, given all the actions in the current layer,
set the propositions and their mutex relations in the proposition layer.
"""
previousPropositionLayer = previousLayer.getPropositionLayer()
previousLayerMutexProposition = previousPropositionLayer.getMutexProps()
self.updateActionLayer(previousPropositionLayer)
self.updateMutexActions(previousLayerMutexProposition)
self.updatePropositionLayer()
self.updateMutexProposition()
def expandWithoutMutex(self, previousLayer):
"""
Questions 11 and 12
You don't have to use this function
"""
previousLayerProposition = previousLayer.getPropositionLayer()
self.updateActionLayer(previousLayerProposition)
self.updatePropositionLayer()
def mutexActions(a1, a2, mutexProps):
"""
This function returns true if a1 and a2 are mutex actions.
We first check whether a1 and a2 are in PlanGraphLevel.independentActions,
this is the list of all the independent pair of actions (according to your implementation in question 1).
If not, we check whether a1 and a2 have competing needs
"""
if Pair(a1, a2) not in PlanGraphLevel.independentActions:
return True
return haveCompetingNeeds(a1, a2, mutexProps)
def haveCompetingNeeds(a1, a2, mutexProps):
"""
Complete code for deciding whether actions a1 and a2 have competing needs,
given the mutex proposition from previous level (list of pairs of propositions).
Hint: for propositions p and q, the command "Pair(p, q) in mutexProps"
returns true if p and q are mutex in the previous level
"""
def is_mutex(p1, p2):
return Pair(p1, p2) in mutexProps
preconditions1 = a1.getPre()
preconditions2 = a2.getPre()
# for all the preconditions check if they are mutex
for cond1 in preconditions1:
for cond2 in preconditions2:
if is_mutex(cond1, cond2):
return True
# if one of them is mutex so the props is mutex
return False
def mutexPropositions(prop1, prop2, mutexActions):
"""
complete code for deciding whether two propositions are mutex,
given the mutex action from the current level (list of pairs of actions).
Your updateMutexProposition function should call this function
You might want to use this function:
prop1.getProducers() returns the list of all the possible actions in the layer that have prop1 on their add list
"""
def is_pairwise_mutex(action1, action2):
return (Pair(action1, action2) in mutexActions)
producers1 = prop1.getProducers()
producers2 = prop2.getProducers()
# for all the producers(action) check if they are pairwise mutex
for action1 in producers1:
for action2 in producers2:
if not is_pairwise_mutex(action1, action2):
return False
# if all of them are mutex so the props is mutex
return True