-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlanElementGraph.py
More file actions
424 lines (344 loc) · 11.3 KB
/
PlanElementGraph.py
File metadata and controls
424 lines (344 loc) · 11.3 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
from OrderingGraph import OrderingGraph, CausalLinkGraph
from Flaws import Flaw, FlawLib
from uuid import uuid4
from Element import Argument, Element, Operator, Literal
from Graph import Edge
from ElementGraph import ElementGraph
from GlobalContainer import GC
import copy
import collections
from clockdeco import clock
class Action(ElementGraph):
# stepnumber = 2
def __init__(self, ID=None, type_graph=None, name=None, Elements=None, root_element=None, Edges=None):
if type_graph is None:
type_graph = 'Action'
if Edges is None:
Edges = set()
if root_element is None:
root_element = Operator()
if Elements is None:
Elements = {root_element}
self.nonequals = set()
super(Action, self).__init__(ID, type_graph, name, Elements, root_element, Edges)
def __hash__(self):
return hash(arg for arg in self.Args) ^ hash(self.root.name)
def __eq__(self, other):
if not isinstance(other, ElementGraph):
return False
if self.root.name == other.root.name:
if self.Args == other.Args:
return True
return False
# @property
# def executed(self):
# return self.root.executed
def RemoveSubgraph(self, elm):
edges = list(self.edges)
elm = self.getElementById(elm.ID)
if isinstance(elm, Literal):
self.elements.remove(elm)
link = None
for edge in list(self.edges):
if edge.source == elm:
edges.remove(edge)
if link is None:
if edge.sink == elm:
link = edge
self.edges = set(edges)
return link
def getPreconditionsOrEffects(self, label):
return {edge.sink for edge in self.edges if edge.label == label}
def __getattr__(self, name):
if name == 'preconditions':
self.preconditions = self.getPreconditionsOrEffects('precond-of')
return self.preconditions
elif name == 'effects':
self.effects = self.getPreconditionsOrEffects('effect-of')
return self.effects
elif name == 'Args':
self.updateArgs()
return self.Args
else:
raise AttributeError('no attribute {}'.format(name))
@property
def stepnumber(self):
return self.root.stepnumber
def replaceInternals(self):
self.ID = uuid4()
for elm in self.elements:
if not isinstance(elm, Argument):
elm.ID = uuid4()
def _replaceInternals(self):
self.ID = uuid4()
for elm in self.elements:
if not isinstance(elm, Argument):
elm.replaced_ID = uuid4()
def deepcopy(self, replace_internals=False):
new_self = copy.deepcopy(self)
if replace_internals:
new_self.replaceInternals()
return new_self
# '''for debugging'''
# def getConditions(self):
# pres = {edge for edge in self.edges if edge.label == 'precond-of'}
# effs = {edge for edge in self.edges if edge.label == 'effect-of'}
# print('Preconditions:\n')
# for pre in pres:
# pre.sink
# print('Effects:\n')
# for eff in effs:
# eff.sink
def isConsistent(self, other):
""" an action is consistent just when one can absolve the other"""
if isinstance(other, ElementGraph):
return self.isConsistentSubgraph(other)
if isinstance(other, Operator):
return self.root.isConsistent(other)
def __repr__(self):
self.updateArgs()
args = str([arg.name for arg in self.Args])
if hasattr(self.root, 'executed'):
exe = self.root.executed
if exe is None:
exe = ''
else:
exe = exe + '-'
else:
exe = 'ex'
id = str(self.root.ID)[19:23]
return '{}{}-{}-{}'.format(exe, self.root.name, self.root.stepnumber, id) + args
class Condition(ElementGraph):
""" A Literal used in causal link"""
def __init__(self, ID=None, type_graph=None, name=None, Elements=None, root_element=None, Edges=None,
Restrictions=None):
if type_graph is None:
type_graph = 'Condition'
if ID is None:
ID = root_element.ID
if root_element is None:
root_element = Literal()
if Elements is None:
Elements = {root_element}
if name is None:
name = root_element.name
super(Condition, self).__init__(ID, type_graph, name, Elements, root_element, Edges, Restrictions)
self.replaced_ID = root_element.replaced_ID
def __hash__(self):
return hash(self.ID) ^ hash(self.root.name) ^ hash(self.root.truth) ^ hash(self.root.replaced_ID)
@property
def truth(self):
return self.root.truth
def __eq__(self, other):
if not isinstance(other, ElementGraph):
return False
if self.root.name == other.root.name and self.root.truth == other.root.truth:
if self.Args == other.Args:
return True
return False
def isConsistent(self, other):
if isinstance(other, ElementGraph):
return self.isConsistentSubgraph(other)
if isinstance(other, Literal):
return self.root.isConsistent(other)
def numArgs(self):
if not hasattr(self, 'Args'):
self.updateArgs()
return len({arg for arg in self.Args if not arg.name is None})
def __repr__(self):
self.updateArgs()
args = str([arg.name for arg in self.Args])
t = ''
if not self.root.truth:
t = 'not-'
return '{}{}'.format(t, self.root.name) + args
class PlanElementGraph(ElementGraph):
def __init__(self, ID=None, type_graph=None, name=None, Elements=None, plan_elm=None, Edges=None,
Restrictions=None):
if ID is None:
ID = uuid4()
if type_graph is None:
type_graph = 'PlanElementGraph'
if Elements is None:
Elements = set()
if Edges is None:
Edges = set()
if Restrictions is None:
Restrictions = set()
self.OrderingGraph = OrderingGraph()
self.CausalLinkGraph = CausalLinkGraph()
self.flaws = FlawLib()
self.solved = False
self.initial_dummy_step = None
self.final_dummy_step = None
if plan_elm is None:
plan_elm = Element(ID=ID, typ=type_graph, name=name)
super(PlanElementGraph, self).__init__(ID, type_graph, name, Elements, plan_elm, Edges, Restrictions)
def __hash__(self):
return hash(self.name) ^ hash(self.typ) ^ hash(self.ID)
@classmethod
def Actions_2_Plan(cls, Actions):
# Used by Plannify
elements = set().union(*[A.elements for A in Actions])
edges = set().union(*[A.edges for A in Actions])
Plan = cls(name='Action_2_Plan', Elements=elements, Edges=edges)
for edge in Plan.edges:
if edge.label == 'effect-of':
elm = Plan.getElementById(edge.sink.ID)
elm.replaced_ID = edge.sink.replaced_ID
Plan.OrderingGraph = OrderingGraph()
Plan.CausalLinkGraph = CausalLinkGraph()
# Plan.Steps = [A.root for A in Actions]
return Plan
def UnifyActions(self, P, G):
# Used by Plannify
NG = G.deepcopy(replace_internals=True)
for edge in list(NG.edges):
if edge.sink.replaced_ID == -1:
sink = copy.deepcopy(edge.sink)
sink.replaced_ID = edge.sink.ID
self.elements.add(sink)
else:
sink = P.getElmByRID(edge.sink.replaced_ID)
if sink is None:
sink = copy.deepcopy(edge.sink)
self.elements.add(sink)
source = P.getElmByRID(edge.source.replaced_ID)
if source is None:
source = copy.deepcopy(edge.source)
self.elements.add(source)
self.edges.add(Edge(source, sink, edge.label))
#@clock
def __lt__(self, other):
if self.cost + self.heuristic != other.cost + other.heuristic:
return (self.cost + self.heuristic) < (other.cost + other.heuristic)
elif self.heuristic != other.heuristic:
return self.heuristic < other.heuristic
elif self.cost != other.cost:
return self.cost < other.cost
elif len(self.Steps) != len(other.Steps):
return len(self.Steps) < len(other.Steps)
elif len(self.flaws) != len(other.flaws):
return len(self.flaws) < len(other.flaws)
else:
S = list(self.Steps)
S.sort(key=lambda x: x.stepnumber)
O = list(other.Steps)
O.sort(key=lambda x: x.stepnumber)
for s, o in zip(S, O):
if s.stepnumber != o.stepnumber:
return s.stepnumber < o.stepnumber
def deepcopy(self):
new_self = copy.deepcopy(self)
new_self.ID = uuid4()
return new_self
def RemoveSubgraph(self, literal):
edges = list(self.edges)
elm = self.getElementById(literal.ID)
link = None
self.elements.remove(elm)
for edge in list(self.edges):
if edge.source == elm:
edges.remove(edge)
if link is None:
if edge.sink == elm:
link = edge
self.edges = set(edges)
return link
def AddSubgraph(self, subgraph):
self.elements.update(subgraph.elements)
self.edges.update(subgraph.edges)
def relaxedStep(self, GL, step, visited):
cost = 0
for pre in step.preconditions:
v = self.relaxedPre(GL, pre, visited)
cost += v
return cost
def relaxedPre(self, GL, pre, visited=None):
if visited is None:
visited = collections.defaultdict(int)
antecedents = GL.id_dict[pre.replaced_ID]
if len(antecedents) == 0:
return 1000
for ant in antecedents:
if len(GL[ant].preconditions) == 0:
visited[ant] = 1
return 1
if self.initial_dummy_step.stepnumber not in antecedents:
least = 1000
for ante in antecedents:
if ante in visited.keys():
v = visited[ante]
else:
visited[ante] = 1000
v = self.relaxedStep(GL, GL[ante], visited)
visited[ante] = v
if v < least:
least = v
return least + 1
return 0
def calculateHeuristic(self, GL):
value = 0
for oc in self.flaws.flaws:
_, pre = oc.flaw
c = self.relaxedPre(GL, pre)
oc.heuristic = c
# print('flaw: {} , heuristic = {}'.format(oc,c))
value += c
return value
@property
def heuristic(self):
return self.calculateHeuristic(GC.SGL)
@property
def cost(self):
return len(self.Steps) - 2
def isInternallyConsistent(self):
return self.OrderingGraph.isInternallyConsistent() and self.CausalLinkGraph.isInternallyConsistent() and \
super(PlanElementGraph, self).isInternallyConsistent()
@property
def Steps(self):
return [element for element in self.elements if type(element) is Operator]
@property
def Step_Graphs(self):
return [Action.subgraph(self, step) for step in self.Steps]
# @clock
def detectThreatenedCausalLinks(self, GL):
"""
A threatened causal link flaw is a tuple <causal link edge, threatening step element>
where if s --p--> t is a causal link edge and s_threat is the threatening step element,
then there is no ordering path from t to s_threat,
no ordering path from s_threat to s,
there is an effect edge from s_threat to a literal false-p',
and p' is consistent with p after flipping the truth attribute
"""
detectedThreatenedCausalLinks = set()
nonThreats = self.CausalLinkGraph.nonThreats
for causal_link in self.CausalLinkGraph.edges:
for step in self.Steps:
# defense 1
if step in nonThreats[causal_link]:
continue
# defense 2-4 - First, ignore steps which either are the source and sink of causal link, or which cannot
# be ordered between them
if step == causal_link.source or step == causal_link.sink:
nonThreats[causal_link].add(step)
continue
if self.OrderingGraph.isPath(causal_link.sink, step):
nonThreats[causal_link].add(step)
continue
if self.OrderingGraph.isPath(step, causal_link.source):
nonThreats[causal_link].add(step)
continue
if step.stepnumber not in GL.threat_dict[causal_link.sink.stepnumber]:
nonThreats[causal_link].add(step)
continue
detectedThreatenedCausalLinks.add(Flaw((step, causal_link), 'tclf'))
# nonThreats[causal_link].add(step)
return detectedThreatenedCausalLinks
def __repr__(self):
c = '\ncost {} + heuristic {}'.format(self.cost, self.heuristic)
steps = str([Action.subgraph(self, step) for step in self.Steps])
orderings = self.OrderingGraph.__repr__()
links = self.CausalLinkGraph.__repr__()
return 'PLAN: ' + str(
self.ID) + c + '\n*Steps: \n{' + steps + '}\n*Orderings:\n {' + orderings + '}\n*CausalLinks:\n {' + links + '}'