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 | class ILGEncoder(EncoderBase[HeteroData]):
"""Instance-Learning Graph encoder with per-instance planner backends."""
def __init__(
self,
domain: Any,
*,
backend: ILGBackendName | str | None = None,
symbol_type_id: str = DEFAULT_SYMBOL_TYPE_ID,
action_type_id: str = "action",
nullary_object_name: str = "![nullary_symbol]!",
add_nullary_predicates: bool = False,
include_lgan_edges: bool = False,
lgan_nn_edge_pos: str = DEFAULT_LGAN_NN_EDGE_POS,
) -> None:
"""Create an ILG encoder for one Pymimir domain or PyTyr task."""
self._domain = domain
self._runtime = create_ilg_runtime(domain, backend=backend)
self.backend = self._runtime.backend_name
self.symbol_type_id = symbol_type_id
self.action_type_id = action_type_id
self.nullary_object_name = nullary_object_name
self.add_nullary_predicates = add_nullary_predicates
self.include_lgan_edges = include_lgan_edges
self.lgan_nn_edge_pos = lgan_nn_edge_pos
self._relation_arity = dict(self._runtime.predicate_arities)
self._action_feature_dim = int(self._runtime.action_feature_dim)
@staticmethod
def _compute_statuses(
facts: Collection[ILGAtom],
goals: Sequence[ILGLiteral],
goal_level_map: dict[ILGLiteral, int],
) -> tuple[list[ILGAtom], dict[tuple[str, tuple[str, ...]], AtomStatus]]:
fact_signatures = {fact.signature for fact in facts}
goal_matches = {
goal.atom.signature
for goal in goals
if (goal.atom.signature in fact_signatures) == goal.positive
}
statuses: dict[tuple[str, tuple[str, ...]], AtomStatus] = {}
missing_goal_facts: list[ILGAtom] = []
for goal in goals:
signature = goal.atom.signature
is_satisfied = signature in goal_matches
previous = statuses.get(signature, AtomStatus())
new_levels = tuple(
sorted(set(previous.goal_levels) | {goal_level_map[goal]})
)
statuses[signature] = AtomStatus(
is_regular=False,
is_negated=not goal.positive,
is_satisfied=is_satisfied,
goal_levels=new_levels,
)
if not is_satisfied:
missing_goal_facts.append(goal.atom)
return missing_goal_facts, statuses
def _encode_input_to_builder(
self,
builder: BatchBuilder,
input_value: ILGInput,
) -> None:
facts = list(input_value.facts)
goals = list(input_value.goals)
actions = list(input_value.actions)
goal_level_map = _goal_levels(goals, input_value.subgoal_layers)
missing_goal_facts, statuses = self._compute_statuses(
facts, goals, goal_level_map
)
symbol_names = list(input_value.objects)
if self.add_nullary_predicates and self.nullary_object_name not in symbol_names:
symbol_names.append(self.nullary_object_name)
builder.add_node_features(
self.symbol_type_id,
"x",
np.zeros((len(symbol_names), 2), dtype=np.float32),
)
builder.set_node_names(self.symbol_type_id, symbol_names)
builder.set_object_names(symbol_names)
encoded_atoms = [*facts, *missing_goal_facts]
pred_atoms: dict[str, list[ILGAtom]] = {}
for atom in encoded_atoms:
pred_atoms.setdefault(atom.predicate, []).append(atom)
for pred_name, atoms in pred_atoms.items():
feature_dim = self._relation_arity.get(pred_name, 0) + 1
rows = [
[float(statuses.get(atom.signature, AtomStatus()).encode())]
* feature_dim
for atom in atoms
]
builder.add_node_features(
pred_name,
"x",
np.asarray(rows, dtype=np.float32),
)
builder.set_node_names(pred_name, [atom.display_name for atom in atoms])
if actions:
builder.add_node_features(
self.action_type_id,
"x",
np.zeros((len(actions), self._action_feature_dim), dtype=np.float32),
)
builder.set_node_names(
self.action_type_id,
[action.display_name for action in actions],
)
object_index = {name: index for index, name in enumerate(symbol_names)}
atom_index = {
pred_name: {atom.display_name: index for index, atom in enumerate(atoms)}
for pred_name, atoms in pred_atoms.items()
}
edge_map: dict[tuple[str, str, str], tuple[list[int], list[int]]] = {}
def add_edge(
src_type: str,
relation: str,
dst_type: str,
source: int,
target: int,
) -> None:
sources, targets = edge_map.setdefault(
(src_type, relation, dst_type), ([], [])
)
sources.append(source)
targets.append(target)
for pred_name, atoms in pred_atoms.items():
for atom in atoms:
arguments = atom.arguments
if not arguments and self._relation_arity.get(pred_name, 0) == 0:
arguments = (
(self.nullary_object_name,)
if self.add_nullary_predicates
else ()
)
atom_idx = atom_index[pred_name][atom.display_name]
for position, object_value in enumerate(arguments):
object_idx = object_index[object_value]
add_edge(
self.symbol_type_id,
str(position),
pred_name,
object_idx,
atom_idx,
)
add_edge(
pred_name,
str(position),
self.symbol_type_id,
atom_idx,
object_idx,
)
for action_idx, action in enumerate(actions):
for position, object_value in enumerate(action.arguments):
object_idx = object_index[object_value]
add_edge(
self.symbol_type_id,
str(position),
self.action_type_id,
object_idx,
action_idx,
)
add_edge(
self.action_type_id,
str(position),
self.symbol_type_id,
action_idx,
object_idx,
)
if self.include_lgan_edges:
object_neighbors: dict[str, set[str]] = {}
object_atoms: dict[str, list[ILGAtom]] = {}
for atom in encoded_atoms:
for first in atom.arguments:
object_atoms.setdefault(first, []).append(atom)
for second in atom.arguments:
if first != second:
object_neighbors.setdefault(first, set()).add(second)
for target_object in object_atoms:
neighbors = object_neighbors.get(target_object, set())
target_idx = object_index[target_object]
for atom in encoded_atoms:
if (
not atom.arguments
or target_object in atom.arguments
or not all(value in neighbors for value in atom.arguments)
):
continue
atom_idx = atom_index[atom.predicate][atom.display_name]
add_edge(
self.symbol_type_id,
self.lgan_nn_edge_pos,
atom.predicate,
target_idx,
atom_idx,
)
add_edge(
atom.predicate,
self.lgan_nn_edge_pos,
self.symbol_type_id,
atom_idx,
target_idx,
)
for (src_type, relation, dst_type), (sources, targets) in edge_map.items():
if sources:
builder.add_edges(
src_type,
relation,
dst_type,
np.asarray(sources, dtype=np.int64),
np.asarray(targets, dtype=np.int64),
)
def _encode_to_builder(
self,
builder: BatchBuilder,
state: object,
*,
goals: object = None,
actions: object = None,
subgoal_layers: object = None,
) -> None:
self._encode_input_to_builder(
builder,
self._runtime.make_input(
state,
goals=goals,
actions=actions,
subgoal_layers=subgoal_layers,
),
)
def _encode(
self,
state: StateInput | Iterable[Any],
*,
goals: GoalBatchInput = None,
actions: ActionBatchInput = None,
subgoal_layers: SubgoalLayersInput = None,
**kwargs: Any,
) -> BatchEncoding:
builder = BatchBuilder()
builder.set_graph_kind("hetero")
self._encode_to_builder(
builder,
state,
goals=goals,
actions=actions,
subgoal_layers=subgoal_layers,
)
return builder.build()
def _encode_batch(
self,
states: StateBatchInput,
*,
goals: GoalBatchParam = None,
actions: ActionBatchParam = None,
subgoal_layers: SubgoalLayersBatchParam = None,
**kwargs: Any,
) -> BatchEncoding:
inputs = self._runtime.make_batch_inputs(
states,
goals=goals,
actions=actions,
subgoal_layers=subgoal_layers,
)
builder = BatchBuilder()
builder.set_graph_kind("hetero")
for input_value in inputs:
self._encode_input_to_builder(builder, input_value)
builder.next_graph()
return builder.build()
def stream(self) -> ILGEncoderStream:
"""Create a streaming ILG encoder."""
return ILGEncoderStream(self)
|