-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlaws.py
258 lines (200 loc) · 6.27 KB
/
Flaws.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
#from pddlToGraphs import *
import collections
import bisect
from uuid import uuid1, uuid4
from Graph import isConsistentEdgeSet
import itertools
from clockdeco import clock
#from PlanElementGraph import Condition
#import PlanElementGraph
"""
Flaws for plan element graphs
"""
class Flaw:
def __init__(self, f, name):
self.name = name
self.flaw = f
self.cndts = 0
self.risks = 0
self.criteria = self.cndts
self.heuristic = 0
if name == 'opf':
self.tiebreaker = hash(f[1].replaced_ID)
def __hash__(self):
return hash(self.flaw)
def __eq__(self, other):
return hash(self) == hash(other)
#For comparison via bisect
def __lt__(self, other):
if self.criteria != other.criteria:
return self.criteria < other.criteria
else:
return self.tiebreaker < other.tiebreaker
def setCriteria(self, flaw_type):
if self.name == 'tclf':
self.criteria = self.flaw[0].stepnumber
self.tiebreaker = hash(self.flaw[1].label.replaced_ID) + self.flaw[1].sink.stepnumber
elif flaw_type == 'unsafe':
self.criteria = self.risks
def __repr__(self):
return 'Flaw({}, h={}, criteria={}, tb={})'.format(self.flaw, self.heuristic, self.criteria, self.tiebreaker)
class Flawque:
""" A deque which pretends to be a set, and keeps everything sorted"""
def __init__(self, name=None):
self._flaws = collections.deque()
self._name = name
def add(self, flaw):
flaw.setCriteria(self._name)
self.insert(flaw)
#self._flaws.append(item)
def update(self, iter):
for flaw in iter:
self.add(flaw)
def __contains__(self, item):
return item in self._flaws
def __len__(self):
return len(self._flaws)
def head(self):
return self._flaws.popleft()
def tail(self):
return self._flaws.pop()
def pop(self):
return self._flaws.pop()
def peek(self):
return self._flaws[-1]
def insert(self, flaw):
index = bisect.bisect_left(self._flaws, flaw)
self._flaws.rotate(-index)
self._flaws.appendleft(flaw)
self._flaws.rotate(index)
def __getitem__(self, position):
return self._flaws[position]
def __repr__(self):
return str(self._flaws)
class simpleQueueWrapper(collections.deque):
#def __init__(self, name):
#super(simpleQueueWrapper, self).__init__()
#self._name = name
def add(self, item):
self.append(item)
def pop(self):
return self.popleft()
def update(self, iter):
for it in iter:
self.append(it)
class FlawLib():
non_static_preds = set()
def __init__(self):
#static = unchangeable (should do oldest first.)
self.statics = Flawque()
#init = established by initial state
self.inits = Flawque()
#threat = causal link dependency undone
self.threats = Flawque()
#unsafe = existing effect would undo sorted by number of cndts
self.unsafe = Flawque('unsafe')
#reusable = open conditions consistent with at least one existing effect sorted by number of cndts
self.reusable = Flawque()
#nonreusable = open conditions inconsistent with existing effect sorted by number of cndts
self.nonreusable = Flawque()
self.typs = [self.statics, self.inits, self.threats, self.unsafe, self.reusable, self.nonreusable]
@property
def heuristic(self):
value = 0
for i,flaw_set in enumerate(self.typs):
if i == 2:
continue
value+=i*len(flaw_set)
return value
def __len__(self):
return sum(len(flaw_set) for flaw_set in self.typs)
# return len(self.threats) + len(self.unsafe) + len(self.statics) + len(self.reusable) + len(self.nonreusable)
def __contains__(self, flaw):
for flaw_set in self.typs:
if flaw in flaw_set:
return True
return False
@property
def flaws(self):
return [flaw for i, flaw_set in enumerate(self.typs) for flaw in flaw_set if i != 2]
def OCs(self):
''' Generator for open conditions'''
for i, flaw_set in enumerate(self.typs):
if len(flaw_set) == 0:
continue
if i == 2:
continue
g = (flaw for flaw in flaw_set)
yield(next(g))
def next(self):
''' Returns flaw with highest priority, and removes'''
for flaw_set in self.typs:
if len(flaw_set) > 0:
return flaw_set.pop()
return None
#@clock
def addCndtsAndRisks(self, GL, action):
""" For each effect of Action, add to open-condition mapping if consistent"""
for oc in self.OCs():
s_need, pre = oc.flaw
# step numbers of antecdent types
if action.stepnumber in GL.id_dict[pre.replaced_ID]:
oc.cndts += 1
# step numbers of threatening steps
elif action.stepnumber in GL.threat_dict[s_need.stepnumber]:
oc.risks += 1
#@clock
def insert(self, GL, plan, flaw):
''' for each effect of an existing step, check and update mapping to consistent effects'''
if flaw.name == 'tclf':
self.threats.add(flaw)
return
#unpack flaw
s_need, pre = flaw.flaw
#if pre.predicate is static
if (pre.name, pre.truth) not in FlawLib.non_static_preds:
self.statics.add(flaw)
return
#Eval number of existing candidates
ante_nums = GL.id_dict[pre.replaced_ID]
risk_nums = GL.threat_dict[s_need.replaced_ID]
for step in plan.Steps:
#defense
if step == s_need:
continue
if plan.OrderingGraph.isPath(s_need, step):
continue
if step.stepnumber in ante_nums:
flaw.cndts += 1
if step.name == 'dummy_init':
self.inits.add(flaw)
if step.replaced_ID in risk_nums:
flaw.risks += 1
if flaw in self.inits:
return
if flaw.risks > 0:
self.unsafe.add(flaw)
return
#if not static but has cndts, then reusable
if flaw.cndts > 0:
self.reusable.add(flaw)
return
#last, must be nonreusable
self.nonreusable.add(flaw)
def __repr__(self):
#flaw_str_list = [str([flaw for flaw in flaw_set]) for flaw_set in self.typs]
statics = str([flaw for flaw in self.statics])
inits = str([flaw for flaw in self.inits])
threats = str([flaw for flaw in self.threats])
unsafe = str([flaw for flaw in self.unsafe])
reusable = str([flaw for flaw in self.reusable])
nonreusable = str([flaw for flaw in self.nonreusable])
# return '\nFLAW LIBRARY: \n' + [flaw_set for flaw_set in flaw_str_list] + '\n'
return '\nFLAW LIBRARY: \nstatics: \n' + statics + '\ninits: \n' + inits + '\nthreats: \n' + threats + \
'\nunsafe: \n' + unsafe + '\nreusable: \n' + reusable + '\nnonreusable: \n' + nonreusable + '\n'
import unittest
class TestOrderingGraphMethods(unittest.TestCase):
def test_flaw_counter(self):
assert True
if __name__ == '__main__':
unittest.main()