forked from puemos/graphplan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplanningProblem.py
257 lines (214 loc) · 8.57 KB
/
planningProblem.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
from util import Pair
import copy
from propositionLayer import PropositionLayer
from planGraphLevel import PlanGraphLevel
from Parser import Parser
from action import Action
try:
from search import SearchProblem
from search import aStarSearch
except:
from CPF.search import SearchProblem
from CPF.search import aStarSearch
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 PlanningProblem():
def __init__(self, domain, problem):
"""
Constructor
"""
p = Parser(domain, problem)
self.actions, self.propositions = p.parseActionsAndPropositions()
# list of all the actions and list of all the propositions
self.initialState, self.goal = p.pasreProblem()
# the initial state and the goal state are lists of propositions
# creates noOps that are used to propagate existing propositions from
# one layer to the next
self.createNoOps()
PlanGraphLevel.setActions(self.actions)
PlanGraphLevel.setProps(self.propositions)
self._expanded = 0
def getStartState(self):
return self.initialState
def isGoalState(self, state):
"""
Hint: you might want to take a look at goalStateNotInPropLayer function
"""
return not self.goalStateNotInPropLayer(state)
def getSuccessors(self, state):
"""
For a given state, this should return a list of triples,
(successor, action, stepCost), where 'successor' is a
successor to the current state, 'action' is the action
required to get there, and 'stepCost' is the incremental
cost of expanding to that successor, 1 in our case.
You might want to this function:
For a list of propositions l and action a,
a.allPrecondsInList(l) returns true if the preconditions of a are in l
"""
def allPrecondsInList(action, propositions):
for pre in action.getPre():
if pre not in propositions:
return False
return True
successors = []
step_cost = 1
self._expanded += 1
# get all possible actions
for action in self.actions:
if (not action.isNoOp()) and allPrecondsInList(action, state):
# add all the positives
successor = state + \
[p for p in action.getAdd() if p not in state]
# remove all the negatives
successor = [
p for p in successor if p not in action.getDelete()]
successors.append((successor, action, step_cost))
return successors
def getCostOfActions(self, actions):
return len(actions)
def goalStateNotInPropLayer(self, propositions):
"""
Helper function that returns true if all the goal propositions
are in propositions
"""
for goal in self.goal:
if goal not in propositions:
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)
def maxLevel(state, problem):
"""
The heuristic value is the number of layers required to expand all goal propositions.
If the goal is not reachable from the state your heuristic should return float('inf')
A good place to start would be:
propLayerInit = PropositionLayer() #create a new proposition layer
for prop in state:
#update the proposition layer with the propositions of the state
propLayerInit.addProposition(prop)
# create a new plan graph level (level is the action layer and the
# propositions layer)
pgInit = PlanGraphLevel()
#update the new plan graph level with the the proposition layer
pgInit.setPropositionLayer(propLayerInit)
"""
def nextPlan(plan):
next_plan = PlanGraphLevel()
next_plan.expandWithoutMutex(plan)
return next_plan, next_plan.getPropositionLayer().getPropositions()
propLayerInit = PropositionLayer()
# add all to the new proposition layer
lmap(propLayerInit.addProposition, state)
plan = PlanGraphLevel()
plan.setPropositionLayer(propLayerInit)
plan_propositions = plan.getPropositionLayer().getPropositions()
# create a graph that will store all the plan levels
graph = []
graph.append(plan)
# if we found we can rest
while not problem.isGoalState(plan_propositions):
# if fixed we won't have a solution
if isFixed(graph, len(graph) - 1):
return float('inf')
# create the next plan by the prev
plan, plan_propositions = nextPlan(plan)
# store in the graph
graph.append(plan)
return len(graph) - 1
def levelSum(state, problem):
"""
The heuristic value is the sum of sub-goals level they first appeared.
If the goal is not reachable from the state your heuristic should return float('inf')
"""
def nextPlan(plan):
next_plan = PlanGraphLevel()
next_plan.expandWithoutMutex(plan)
return next_plan, next_plan.getPropositionLayer().getPropositions()
propLayerInit = PropositionLayer()
# add all to the new proposition layer
lmap(propLayerInit.addProposition, state)
plan = PlanGraphLevel()
plan.setPropositionLayer(propLayerInit)
plan_propositions = plan.getPropositionLayer().getPropositions()
# create a graph that will store all the plan levels
graph = []
graph.append(plan)
goals_levels = dict()
goal = problem.goal
# init goals levels
for p in goal:
goals_levels[p.getName()] = None
# as long as we have for one of the goal None we didnt find the first level
while None in goals_levels.values():
# if fixed we won't have a solution
if isFixed(graph, len(graph) - 1):
return float('inf')
# for each prop in the goal check if exist on the current plan
# propositions
for p in goal:
# check that we didnt assign a value yet
if p in plan_propositions and goals_levels[p.getName()] == None:
# set the current level as the fist appearance of the prop
goals_levels[p.getName()] = len(graph) - 1
# create the next plan by the prev
plan, plan_propositions = nextPlan(plan)
# store in the graph
graph.append(plan)
return sum(goals_levels.values())
def isFixed(Graph, 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
return len(Graph[level].getPropositionLayer().getPropositions()) == len(Graph[level - 1].getPropositionLayer().getPropositions())
if __name__ == '__main__':
import sys
import time
if len(sys.argv) != 1 and len(sys.argv) != 4:
print("Usage: PlanningProblem.py domainName problemName heuristicName(max, sum or zero)")
exit()
domain = 'dwrDomain.txt'
problem = 'dwrProblem.txt'
heuristic = lambda x, y: 0
if len(sys.argv) == 4:
domain = str(sys.argv[1])
problem = str(sys.argv[2])
if str(sys.argv[3]) == 'max':
heuristic = maxLevel
elif str(sys.argv[3]) == 'sum':
heuristic = levelSum
elif str(sys.argv[3]) == 'zero':
heuristic = lambda x, y: 0
else:
print(
"Usage: PlanningProblem.py domainName problemName heuristicName(max, sum or zero)")
exit()
prob = PlanningProblem(domain, problem)
start = time.clock()
plan = aStarSearch(prob, heuristic)
elapsed = time.clock() - start
if plan is not None:
print("Plan found with %d actions in %.2f seconds" %
(len(plan), elapsed))
else:
print("Could not find a plan in %.2f seconds" % elapsed)
print("Search nodes expanded: %d" % prob._expanded)