lagrangian#

Classes#

LNNConfig

Configuration for a Lagrangian Neural Network.

LagrangianNN

Lagrangian Neural Network with optional action-conditioned generalized forces.

class prt_rl.model_based.models.dynamics.lagrangian.LNNConfig(q_dim: int, u_dim: int = 0, lagrangian_hidden: Tuple[int, ...] = (128, 128), force_hidden: Tuple[int, ...] = (128, 128), activation: str = 'tanh', learn_forces: bool = True, damping: bool = False, gravity: bool = False, eps: float = 1e-06, device: device | None = None, dtype: dtype | None = None)[source]#

Configuration for a Lagrangian Neural Network.

class prt_rl.model_based.models.dynamics.lagrangian.LagrangianNN(cfg: LNNConfig)[source]#

Lagrangian Neural Network with optional action-conditioned generalized forces.

  • Unforced (conservative): u_dim=0 or learn_forces=False => Q ≡ 0

  • Forced/action-conditioned: u_dim>0 and learn_forces=True => Q = Q(q, dq, u)

Intended usage:

L = model.lagrangian(q, dq) Q = model.generalized_forces(q, dq, u) # optional ddq = model.accelerations(q, dq, u) # used in loss

Initialize internal Module state, shared by both nn.Module and ScriptModule.

accelerations(q: Tensor, dq: Tensor, u: Tensor | None = None) Tensor[source]#
Compute accelerations ddq from Euler–Lagrange with forcing:

d/dt(∂L/∂dq) - ∂L/∂q = Q

Parameters:
  • q – [B, n]

  • dq – [B, n]

  • u – [B, m] (optional)

Returns:

[B, n]

Return type:

ddq

coriolis_and_gravity(q: Tensor, dq: Tensor) Tensor[source]#
Return a convenience term C(q,dq) typically representing:

C(q,dq) = ∂/∂q(∂L/∂dq) dq - ∂L/∂q

(Exact definition may vary by implementation.) Shape [B, n].

dL_ddq(q: Tensor, dq: Tensor) Tensor[source]#

Return ∂L/∂dq, shape [B, n].

dL_dq(q: Tensor, dq: Tensor) Tensor[source]#

Return ∂L/∂q, shape [B, n].

energy(q: Tensor, dq: Tensor) Tensor[source]#

Return total energy E(q,dq) = dqᵀ(∂L/∂dq) - L. Shape [B, 1] (or [B]).

generalized_forces(q: Tensor, dq: Tensor, u: Tensor | None = None) Tensor[source]#

Compute generalized external forces Q.

Parameters:
  • q – [B, n]

  • dq – [B, n]

  • u – [B, m] (optional; required if action-conditioned)

Returns:

[B, n]

Return type:

Q

is_action_conditioned() bool[source]#

Return True if model expects actions (u_dim > 0 and learn_forces).

lagrangian(q: Tensor, dq: Tensor) Tensor[source]#

Compute scalar Lagrangian L(q, dq) per batch element.

Parameters:
  • q – [B, n]

  • dq – [B, n]

Returns:

[B, 1] (or [B])

Return type:

L

mass_matrix(q: Tensor, dq: Tensor) Tensor[source]#

Return M(q, dq) = ∂²L/∂dq², shape [B, n, n]. Often used as a learned inertia / metric term.