-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgraphPlan.py
252 lines (224 loc) · 9.56 KB
/
graphPlan.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
from util import Pair
import copy
from propositionLayer import PropositionLayer
from planGraphLevel import PlanGraphLevel
from action import Action
from Parser import Parser
class GraphPlan(object):
"""
A class for initializing and running the graphplan algorithm
"""
def __init__(self, domain, problem):
"""
Constructor
"""
self.independentActions = []
self.noGoods = []
self.graph = []
p = Parser(domain, problem)
# list of all the actions and list of all the propositions
self.actions, self.propositions = p.parseActionsAndPropositions()
# the initial state and the goal state are lists of propositions
self.initialState, self.goal = p.pasreProblem()
# creates noOps that are used to propagate existing propositions from
# one layer to the next
self.createNoOps()
# creates independent actions list and updates self.independentActions
self.independent()
PlanGraphLevel.setIndependentActions(self.independentActions)
PlanGraphLevel.setActions(self.actions)
PlanGraphLevel.setProps(self.propositions)
def graphPlan(self):
"""
The graphplan algorithm.
The code calls the extract function which you should complete below
"""
# initialization
initState = self.initialState
level = 0
self.noGoods = [] # make sure you update noGoods in your backward search!
self.noGoods.append([])
# create first layer of the graph, note it only has a proposition layer
# which consists of the initial state.
propLayerInit = PropositionLayer()
for prop in initState:
propLayerInit.addProposition(prop)
pgInit = PlanGraphLevel()
pgInit.setPropositionLayer(propLayerInit)
self.graph.append(pgInit)
"""
While the layer does not contain all of the propositions in the goal state,
or some of these propositions are mutex in the layer we,
and we have not reached the fixed point, continue expanding the graph
"""
while self.goalStateNotInPropLayer(self.graph[level].getPropositionLayer().getPropositions()) or \
self.goalStateHasMutex(self.graph[level].getPropositionLayer()):
if self.isFixed(level):
return None # this means we stopped the while loop above because we reached a fixed point in the graph. nothing more to do, we failed!
self.noGoods.append([])
level = level + 1
pgNext = PlanGraphLevel() # create new PlanGraph object
# calls the expand function, which you are implementing in the
# PlanGraph class
pgNext.expand(self.graph[level - 1])
# appending the new level to the plan graph
self.graph.append(pgNext)
# remember size of nogood table
sizeNoGood = len(self.noGoods[level])
# try to extract a plan since all of the goal propositions are in
# current graph level, and are not mutex
plan = self.extract(self.graph, self.goal, level)
while(plan is None): # while we didn't extract a plan successfully
level = level + 1
self.noGoods.append([])
pgNext = PlanGraphLevel() # create next level of the graph by expanding
# create next level of the graph by expanding
pgNext.expand(self.graph[level - 1])
self.graph.append(pgNext)
# try to extract a plan again
plan = self.extract(self.graph, self.goal, level)
if (plan is None and self.isFixed(level)): # if failed and reached fixed point
# if size of nogood didn't change, means there's nothing more
# to do. We failed.
if sizeNoGood == len(self.noGoods[level]):
return None
# we didn't fail yet! update size of no good
sizeNoGood = len(self.noGoods[level])
return plan
def extract(self, Graph, subGoals, level):
"""
The backsearch part of graphplan that tries
to extract a plan when all goal propositions exist in a graph plan level.
"""
if level == 0:
return []
if subGoals in self.noGoods[level]:
return None
plan = self.gpSearch(Graph, subGoals, [], level)
if plan is not None:
return plan
self.noGoods[level].append([subGoals])
return None
def gpSearch(self, Graph, subGoals, plan, level):
if subGoals == []:
newGoals = []
for action in plan:
for prop in action.getPre():
if prop not in newGoals:
newGoals.append(prop)
newPlan = self.extract(Graph, newGoals, level - 1)
if newPlan is None:
return None
else:
return newPlan + plan
prop = subGoals[0]
providers = []
for action1 in [act for act in Graph[level].getActionLayer().getActions() if prop in act.getAdd()]:
noMutex = True
for action2 in plan:
if Pair(action1, action2) not in self.independentActions:
noMutex = False
break
if noMutex:
providers.append(action1)
for action in providers:
newSubGoals = [g for g in subGoals if g not in action.getAdd()]
planClone = list(plan)
planClone.append(action)
newPlan = self.gpSearch(Graph, newSubGoals, planClone, level)
if newPlan is not None:
return newPlan
return None
def goalStateNotInPropLayer(self, propositions):
"""
Helper function that receives a list of propositions (propositions) and returns true
if not all the goal propositions are in that list
"""
for goal in self.goal:
if goal not in propositions:
return True
return False
def goalStateHasMutex(self, propLayer):
"""
Helper function that checks whether all goal propositions are non mutex at the current graph level
"""
for goal1 in self.goal:
for goal2 in self.goal:
if propLayer.isMutex(goal1, goal2):
return True
return False
def isFixed(self, level):
"""
Checks if we have reached a fixed point, i.e. each level we'll expand would be the same, thus no point in continuing
"""
if level == 0:
return False
if len(self.graph[level].getPropositionLayer().getPropositions()) == len(self.graph[level - 1].getPropositionLayer().getPropositions()) and \
len(self.graph[level].getPropositionLayer().getMutexProps()) == len(self.graph[level - 1].getPropositionLayer().getMutexProps()):
return True
return False
def createNoOps(self):
"""
Creates the noOps that are used to propagate propositions from one layer to the next
"""
for prop in self.propositions:
name = prop.name
precon = []
add = []
precon.append(prop)
add.append(prop)
delete = []
act = Action(name, precon, add, delete, True)
self.actions.append(act)
prop.addProducer(act)
def independent(self):
"""
Creates a list of independent actions
"""
for act1 in self.actions:
for act2 in self.actions:
if independentPair(act1, act2):
self.independentActions.append(Pair(act1, act2))
def isIndependent(self, a1, a2):
return Pair(a1, a2) in self.independentActions
def noMutexActionInPlan(self, plan, act, actionLayer):
"""
Helper action that you may want to use when extracting plans,
returns true if there are no mutex actions in the plan
"""
for planAct in plan:
if actionLayer.isMutex(Pair(planAct, act)):
return False
return True
def independentPair(a1, a2):
if a1 == a2:
return True
def is_pre_or_pos_cond_of(action):
return lambda condition: action.isPreCond(condition) or action.isPosEffect(condition)
conds1 = a1.getDelete()
conds2 = a2.getDelete()
# check if one of action pre-conditions are pre of pos condition of other
# action
conds_a1_action_a2 = any(list(map(is_pre_or_pos_cond_of(a2), conds1)))
conds_a2_action_a1 = any(list(map(is_pre_or_pos_cond_of(a1), conds2)))
return (conds_a1_action_a2 is False) and (conds_a2_action_a1 is False)
if __name__ == '__main__':
import sys
import time
if len(sys.argv) != 1 and len(sys.argv) != 3:
print("Usage: GraphPlan.py domainName problemName")
exit()
domain = 'dwrDomain.txt'
problem = 'dwrProblem.txt'
if len(sys.argv) == 3:
domain = str(sys.argv[1])
problem = str(sys.argv[2])
gp = GraphPlan(domain, problem)
start = time.clock()
plan = gp.graphPlan()
elapsed = time.clock() - start
if plan is not None:
print("Plan found with %d actions in %.2f seconds" %
(len([act for act in plan if not act.isNoOp()]), elapsed))
else:
print("Could not find a plan in %.2f seconds" % elapsed)