leap_c.utils.collate

Collate helpers for leap-c context objects.

These helpers are useful when batching nested data with AcadosDiffMpcCtx objects inside, for example to reuse single-sample contexts as one batched warm start.

For a full user-facing example, see notebooks/getting_started/07_imitation_learning.py.

Attributes

LEAPC_COLLATE_FN_MAP

PyTorch-style collate_fn_map for AcadosDiffMpcCtx.

Functions

collate_acados_diff_mpc_ctx(...)

Collate a batch of single-sample contexts into one batched context.

collate_torch(→ Any)

Collate a batch with PyTorch defaults plus leap-c context support.

Module Contents

leap_c.utils.collate.collate_acados_diff_mpc_ctx(batch: collections.abc.Sequence[leap_c.diff_mpc.function.AcadosDiffMpcCtx], collate_fn_map: dict[str, collections.abc.Callable] | None = None) leap_c.diff_mpc.function.AcadosDiffMpcCtx[source]

Collate a batch of single-sample contexts into one batched context.

Stacks the iterate and solver input arrays along a new batch axis and concatenates statuses into a 1-D array. log is set to None since per-sample logs do not merge meaningfully.

Parameters:
  • batch – Non-empty sequence of AcadosDiffMpcCtx objects, each holding a single-sample iterate and solver input.

  • collate_fn_map – Optional PyTorch-style collate map. Accepted for protocol compatibility but unused.

Returns:

A single AcadosDiffMpcCtx whose iterate.N_batch equals len(batch).

leap_c.utils.collate.collate_torch(batch: collections.abc.Sequence[Any]) Any[source]

Collate a batch with PyTorch defaults plus leap-c context support.

This is a convenience function for PyTorch DataLoader instances, replay buffers, or downstream training code that stores AcadosDiffMpcCtx values for batched warm starts. PyTorch handles tensors, numpy arrays, mappings, tuples, scalars, etc.; leap-c only adds the missing rule for MPC contexts.

Parameters:

batch – Sequence of samples to collate.

Returns:

The PyTorch-collated batch.

leap_c.utils.collate.LEAPC_COLLATE_FN_MAP

PyTorch-style collate_fn_map for AcadosDiffMpcCtx.

Use this when an external batching utility accepts a custom collate map and may encounter AcadosDiffMpcCtx objects. If users only need to stack contexts directly, they can call collate_acados_diff_mpc_ctx() instead.