lnn#

Classes#

class prt_rl.common.components.networks.lnn.LagrangianNetwork(state_dim: int, hidden_dim: int = 64, action_dim: int | None = None)[source]#

Neural network for modeling the Lagrangian and generalized forces of a dynamical system.

Parameters:
  • state_dim (int) – Dimension of the state (q, dq) input.

  • hidden_dim (int, optional) – Number of hidden units in each layer. Default is 64.

  • action_dim (int | None, optional) – Dimension of the action input. If provided, enables the Q_net for force computation.

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

compute_ddq(dL_dq: Tensor, dL_dqd: Tensor, q: Tensor, qd: Tensor, tau: Tensor | None = None, eps: float = 1e-06) Tensor[source]#

Compute the acceleration (ddq) using the Euler-Lagrange equation.

Parameters:
  • dL_dq (Tensor) – Gradient of Lagrangian w.r.t. q, shape (B, q_dim)

  • dL_dqd (Tensor) – Gradient of Lagrangian w.r.t. dq, shape (B, dq_dim)

  • q (Tensor) – Generalized coordinates, shape (B, q_dim)

  • qd (Tensor) – Generalized velocities, shape (B, dq_dim)

  • tau (Tensor | None, optional) – Generalized forces/torques, shape (B, q_dim). If None, zeros are used.

  • eps (float, optional) – Small value for numerical stability. Default is 1e-6.

Returns:

Generalized accelerations (ddq), shape (B, q_dim)

Return type:

Tensor

compute_gradients(q: Tensor, qd: Tensor) Tuple[Tensor, Tensor][source]#

Compute the gradients of the Lagrangian with respect to q and dq.

Parameters:
  • q (Tensor) – Generalized coordinates, shape (B, q_dim)

  • qd (Tensor) – Generalized velocities, shape (B, dq_dim)

Returns:

  • dL_dq: Gradient of Lagrangian w.r.t. q, shape (B, q_dim)

  • dL_ddq: Gradient of Lagrangian w.r.t. dq, shape (B, dq_dim)

Return type:

Tuple[Tensor, Tensor]

forces(q: Tensor, dq: Tensor, a: Tensor) Tensor[source]#

Compute the generalized forces for given state, velocity, and action.

Parameters:
  • q (Tensor) – Generalized coordinates, shape (B, q_dim)

  • dq (Tensor) – Generalized velocities, shape (B, dq_dim)

  • a (Tensor) – Actions, shape (B, action_dim)

Returns:

Generalized forces, shape (B, action_dim)

Return type:

Tensor

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

Compute the Lagrangian for given state and velocity.

Parameters:
  • q (Tensor) – Generalized coordinates, shape (B, q_dim)

  • dq (Tensor) – Generalized velocities, shape (B, dq_dim)

Returns:

Scalar Lagrangian values, shape (B, 1)

Return type:

Tensor