forked from csmiles000/cs182asst4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlanGraph.py
82 lines (68 loc) · 3.57 KB
/
PlanGraph.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
'''
Created on Oct 20, 2013
@author: Ofra
'''
from Action import Action
from ActionLayer import ActionLayer
from Pair import Pair
from Proposition import Proposition
from PropositionLayer import PropositionLayer
class PlanGraph(object):
'''
A class for representing a level in the plan graph. For each level i, the PlanGraph consists of the actionLayer and propositionLayer at this level
'''
def __init__(self, level, independentActions):
'''
Constructor
'''
self.level = level
self.independentActions = independentActions # a list of the independent actions (this would be the same at each level)
self.actionLayer = ActionLayer()
self.propositionLayer = PropositionLayer();
def getPropositionLayer(self):
return self.propositionLayer
def setPropositionLayer(self, propLayer):
self.propositionLayer = propLayer
def getActionLayer(self):
return self.actionLayer
def expand(self, previousLevel, allProps, allActions): # you can change the params the function takes if you like
previousPropositionLayer = previousLevel.getPropositionLayer()
newActionLayer = ActionLayer()
for action in allActions:
if previousPropositionLayer.allPrecondsInLayer(action):
newActionLayer.addAction(action)
# add mutex actions
for action1 in newActionLayer.getActions():
for action2 in newActionLayer.getActions():
actionPair = Pair(action1, action2)
if action1 != action2 and self.mutexActions(action1, action2, previousPropositionLayer.getMutexProps()) and actionPair not in newActionLayer.getMutexActions():
newActionLayer.addMutexActions(action1, action2)
self.actionLayer = newActionLayer
newPropositionLayer = PropositionLayer()
for prop in allProps:
if newActionLayer.effectExists(prop):
newPropositionLayer.addProposition(prop)
# add mutex propositions
for prop1 in newPropositionLayer.getPropositions():
for prop2 in newPropositionLayer.getPropositions():
propPair = Pair(prop1, prop2)
if prop1 != prop2 and self.mutexPropositions(prop1, prop2, newActionLayer.getMutexActions()) and propPair not in newPropositionLayer.getMutexProps():
newPropositionLayer.addMutexProp(prop1, prop2)
# set new proposition layer
self.setPropositionLayer(newPropositionLayer)
def mutexActions(self, a1, a2, mutexProps):
'''YOUR CODE HERE: complete code for deciding whether actions a1 and a2 are mutex, given the previous proposition layer. Your exapnd function should call this function'''
if Pair(a1, a2) not in self.independentActions:
return True
for pre1 in a1.getPre():
for pre2 in a2.getPre():
if Pair(pre1, pre2) in mutexProps:
return True
return False
def mutexPropositions(self, prop1, prop2, mutexActions):
'''YOUR CODE HERE: complete code for deciding whether propositions p1 and p2 are mutex, given the previous proposition layer. Your exapnd function should call this function'''
for producer1 in prop1.getProducers():
for producer2 in prop2.getProducers():
if Pair(producer1, producer2) not in mutexActions:
return False
return True