-
Notifications
You must be signed in to change notification settings - Fork 0
/
planningProblem.py
220 lines (186 loc) · 7.39 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
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
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
self.createNoOps() # creates noOps that are used to propagate existing propositions from one layer to the next
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
"""
for goal in self.goal:
if goal not in state:
return False
return True
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
"""
self._expanded += 1
successors = []
for action in self.actions:
# if an action is not an noOp, and all preconditions are met in the current state
if not action.isNoOp() and action.allPrecondsInList(state):
# Add ourself and all the additions from the actions
successor = state + [x for x in action.getAdd() if x not in state]
# Remove all the deletions
successor = [x for x in successor if x not in action.getDelete()]
# stepCost is 1
successors.append((successor, action, 1))
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:
propLayerInit.addProposition(prop) #update the proposition layer with the propositions of the state
pgInit = PlanGraphLevel() #create a new plan graph level (level is the action layer and the propositions layer)
pgInit.setPropositionLayer(propLayerInit) #update the new plan graph level with the the proposition layer
"""
propLayerInit = PropositionLayer()
for p in state:
propLayerInit.addProposition(p)
pgInit = PlanGraphLevel()
pgInit.setPropositionLayer(propLayerInit)
graph = [] # list of PlanGraphLevel objects
graph.append(pgInit)
level = 0
# keep expanding as long as we don't hit the goal state
while problem.goalStateNotInPropLayer(graph[level].getPropositionLayer().getPropositions()):
if isFixed(graph, level):
# if the graph is fixed and expansions didn't change in the last level, it means that we can't reach
# the goal state, and we return infinity
return float('inf')
pg = PlanGraphLevel()
# expanding using a easier version of the problem - without mutexes
pg.expandWithoutMutex(graph[level])
graph.append(pg)
level += 1
return level
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')
"""
propLayerInit = PropositionLayer()
for p in state:
propLayerInit.addProposition(p)
pgInit = PlanGraphLevel()
pgInit.setPropositionLayer(propLayerInit)
graph = [] # list of PlanGraphLevel objects
graph.append(pgInit)
goals = problem.goal[:]
level = 0
sum_ = 0
# keep expanding as long as we still have goal states we didn't see
while goals:
if isFixed(graph, level):
# if the graph is fixed and expansions didn't change in the last level, it means that we can't reach
# the goal state, and we return infinity
return float('inf')
props = graph[level].getPropositionLayer().getPropositions()
for goal in goals:
if goal in props:
# each goal state that we run into, we should add to the sum, and remove it from the goals we need to see
sum_ += level
goals.remove(goal)
pg = PlanGraphLevel()
# expanding using a easier version of the problem - without mutexes
pg.expandWithoutMutex(graph[level])
graph.append(pg)
level += 1
sum_ += level
return sum_
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.time()
plan = aStarSearch(prob, heuristic)
elapsed = time.time() - 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)