-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimplify.py
332 lines (293 loc) · 12.3 KB
/
simplify.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
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
# -*- coding: utf-8 -*-
from __future__ import print_function
from collections import defaultdict
from itertools import count
DEBUG = False
# TODO:
# This is all quite hackish and would be easier if the translator were
# restructured so that more information is immediately available for
# the propositions, and if propositions had more structure. Directly
# working with int pairs is awkward.
class DomainTransitionGraph(object):
def __init__(self, init, size):
self.init = init
self.size = size
self.arcs = defaultdict(set)
def add_arc(self, u, v):
self.arcs[u].add(v)
def reachable(self):
queue = [self.init]
reachable = set(queue)
while queue:
node = queue.pop()
new_neighbors = self.arcs.get(node, set()) - reachable
reachable |= new_neighbors
queue.extend(new_neighbors)
return reachable
def dump(self):
print("SIZE", self.size)
print("INIT", self.init)
print("ARCS:")
for source, destinations in sorted(self.arcs.items()):
for destination in sorted(destinations):
print(" %d => %d" % (source, destination))
def build_dtgs(task):
init_vals = task.init.values
sizes = task.variables.ranges
dtgs = [DomainTransitionGraph(init, size)
for (init, size) in zip(init_vals, sizes)]
def add_arc(var_no, pre_spec, post):
if pre_spec == -1:
pre_values = set(range(sizes[var_no])).difference([post])
else:
pre_values = [pre_spec]
for pre in pre_values:
dtgs[var_no].add_arc(pre, post)
def get_effective_pre(var_no, pre_spec, cond):
# Search pre_spec and cond for all conditions on var_no
# and return:
# -1 if there is no such condition
# val if there is a condition of the form var_no=val
# (possibly repeated multiple times)
# None if there are contradictory conditions on var_no
result = pre_spec
for cond_var_no, cond_val in cond:
if cond_var_no == var_no:
if result == -1:
# This is the first condition on var_no.
result = cond_val
elif cond_val != result:
# We have contradictory conditions on var_no.
return None
return result
for op in task.operators:
# FOND
for nondet_choice in op.pre_post:
for var_no, pre_spec, post, cond in nondet_choice:
# Check all conditions on var_no for this effect.
# These can come from the pre_spec or from cond.
effective_pre = get_effective_pre(var_no, pre_spec, cond)
if effective_pre is not None:
add_arc(var_no, pre_spec, post)
for axiom in task.axioms:
var_no, val = axiom.effect
add_arc(var_no, -1, val)
return dtgs
always_false = object()
always_true = object()
class Impossible(Exception):
pass
class DoesNothing(Exception):
pass
class VarValueRenaming(object):
def __init__(self):
self.new_var_nos = [] # indexed by old var_no
self.new_values = [] # indexed by old var_no and old value
self.new_sizes = [] # indexed by new var_no
self.new_var_count = 0
self.num_removed_values = 0
def register_variable(self, old_domain_size, init_value, new_domain):
assert 1 <= len(new_domain) <= old_domain_size
assert init_value in new_domain
if len(new_domain) == 1:
# Remove this variable completely.
new_values_for_var = [always_false] * old_domain_size
new_values_for_var[init_value] = always_true
self.new_var_nos.append(None)
self.new_values.append(new_values_for_var)
self.num_removed_values += old_domain_size
else:
new_value_counter = count()
new_values_for_var = []
for value in range(old_domain_size):
if value in new_domain:
new_values_for_var.append(next(new_value_counter))
else:
self.num_removed_values += 1
new_values_for_var.append(always_false)
new_size = next(new_value_counter)
assert new_size == len(new_domain)
self.new_var_nos.append(self.new_var_count)
self.new_values.append(new_values_for_var)
self.new_sizes.append(new_size)
self.new_var_count += 1
def apply_to_task(self, task):
self.apply_to_variables(task.variables)
self.apply_to_mutexes(task.mutexes)
self.apply_to_init(task.init)
self.apply_to_goals(task.goal.pairs)
self.apply_to_operators(task.operators)
self.apply_to_axioms(task.axioms)
def apply_to_variables(self, variables):
variables.ranges = self.new_sizes
new_axiom_layers = [None] * self.new_var_count
for old_no, new_no in enumerate(self.new_var_nos):
if new_no is not None:
new_axiom_layers[new_no] = variables.axiom_layers[old_no]
assert None not in new_axiom_layers
variables.axiom_layers = new_axiom_layers
self._apply_to_value_names(variables.value_names)
def _apply_to_value_names(self, value_names):
new_value_names = [[None] * size for size in self.new_sizes]
for var_no, values in enumerate(value_names):
for value, value_name in enumerate(values):
new_var_no, new_value = self.translate_pair((var_no, value))
if new_value is always_true:
if DEBUG:
print("Removed true proposition: %s" % value_name)
elif new_value is always_false:
if DEBUG:
print("Removed false proposition: %s" % value_name)
else:
new_value_names[new_var_no][new_value] = value_name
assert all((None not in value_names) for value_names in new_value_names)
value_names[:] = new_value_names
def apply_to_mutexes(self, mutexes):
new_mutexes = []
for mutex in mutexes:
new_facts = []
for var, val in mutex.facts:
new_var_no, new_value = self.translate_pair((var, val))
if (new_value is not always_true and
new_value is not always_false):
new_facts.append((new_var_no, new_value))
if len(new_facts) >= 2:
mutex.facts = new_facts
new_mutexes.append(mutex)
mutexes[:] = new_mutexes
def apply_to_init(self, init):
init_pairs = list(enumerate(init.values))
try:
self.translate_pairs_in_place(init_pairs)
except Impossible:
assert False, "Initial state impossible? Inconceivable!"
new_values = [None] * self.new_var_count
for new_var_no, new_value in init_pairs:
new_values[new_var_no] = new_value
assert None not in new_values
init.values = new_values
def apply_to_goals(self, goals):
# This may propagate Impossible up.
self.translate_pairs_in_place(goals)
def apply_to_operators(self, operators):
new_operators = []
num_removed = 0
for op in operators:
try:
self.apply_to_operator(op)
new_operators.append(op)
except (Impossible, DoesNothing):
num_removed += 1
if DEBUG:
print("Removed operator: %s" % op.name)
print("%d operators removed" % num_removed)
operators[:] = new_operators
def apply_to_axioms(self, axioms):
new_axioms = []
for axiom in axioms:
try:
self.apply_to_axiom(axiom)
new_axioms.append(axiom)
except (Impossible, DoesNothing):
if DEBUG:
print("Removed axiom:")
axiom.dump()
axioms[:] = new_axioms
def apply_to_operator(self, op):
# The prevail translation may generate an Impossible exception,
# which is propagated up.
self.translate_pairs_in_place(op.prevail)
new_pre_post = []
# FOND
for nondet_choice in op.pre_post:
new_nondet_choice = []
for entry in nondet_choice:
try:
new_nondet_choice.append(self.translate_pre_post(entry))
# This may raise Impossible if "pre" is always false.
# This is then propagated up.
except DoesNothing:
# Conditional effect that is impossible to trigger, or
# effect that sets an always true value. Swallow this.
pass
new_pre_post.append(new_nondet_choice)
op.pre_post = new_pre_post
if not new_pre_post:
raise DoesNothing
def apply_to_axiom(self, axiom):
# The following line may generate an Impossible exception,
# which is propagated up.
self.translate_pairs_in_place(axiom.condition)
new_var, new_value = self.translate_pair(axiom.effect)
# If the new_value is always false, then the condition must
# have been impossible.
assert not new_value is always_false
if new_value is always_true:
raise DoesNothing
axiom.effect = new_var, new_value
def translate_pre_post(self, pre_post_tuple):
(var_no, pre, post, cond) = pre_post_tuple
new_var_no, new_post = self.translate_pair((var_no, post))
if pre == -1:
new_pre = -1
else:
_, new_pre = self.translate_pair((var_no, pre))
if new_pre is always_false:
raise Impossible
try:
self.translate_pairs_in_place(cond)
except Impossible:
raise DoesNothing
assert new_post is not always_false
if new_pre is always_true:
assert new_post is always_true
raise DoesNothing
elif new_post is always_true:
assert new_pre == -1
raise DoesNothing
return new_var_no, new_pre, new_post, cond
def translate_pair(self, fact_pair):
(var_no, value) = fact_pair
new_var_no = self.new_var_nos[var_no]
new_value = self.new_values[var_no][value]
return new_var_no, new_value
def translate_pairs_in_place(self, pairs):
new_pairs = []
for pair in pairs:
new_var_no, new_value = self.translate_pair(pair)
if new_value is always_false:
raise Impossible
elif new_value is not always_true:
assert new_var_no is not None
new_pairs.append((new_var_no, new_value))
pairs[:] = new_pairs
def build_renaming(dtgs):
renaming = VarValueRenaming()
for dtg in dtgs:
renaming.register_variable(dtg.size, dtg.init, dtg.reachable())
return renaming
def filter_unreachable_propositions(sas_task):
# This procedure is a bit of an afterthought, and doesn't fit the
# overall architecture of the translator too well. We filter away
# unreachable propositions here, and then prune away variables
# with only one value left.
#
# Examples of things that are filtered away:
# - Constant propositions that are not detected in instantiate.py
# because it only reasons at the predicate level, and some
# predicates such as "at" in Depot is constant for some objects
# (hoists), but not others (trucks).
# Example: "at(hoist1, distributor0)" and the associated
# variable in depots-01.
# - "none of those" values that are unreachable.
# Example: at(truck1, ?x) = <none of those> in depots-01.
# - Certain values that are relaxed reachable but detected as
# unreachable after SAS instantiation because the only operators
# that set them have inconsistent preconditions.
# Example: on(crate0, crate0) in depots-01.
dtgs = build_dtgs(sas_task)
renaming = build_renaming(dtgs)
# apply_to_task may propagate up Impossible if the goal is simplified
# to False.
renaming.apply_to_task(sas_task)
print("%d propositions removed" % renaming.num_removed_values)