Download src/dynamics/base.py from codewithyug/certified-safe-neural-control: direct link, hf CLI and curl.
- Browser
- Download file 2.81 kB
-
https://huggingface.co/codewithyug/certified-safe-neural-control/resolve/main/src/dynamics/base.py
- Command line
-
hf download hf://codewithyug/certified-safe-neural-control/src/dynamics/base.py
-
curl -L -o base.py https://huggingface.co/codewithyug/certified-safe-neural-control/resolve/main/src/dynamics/base.py
2.81 kB
| """ | |
| Abstract Base Class for Control-Affine Dynamical Systems: | |
| dx/dt = f(x) + g(x)u + w(t) | |
| """ | |
| from abc import ABC, abstractmethod | |
| from typing import Tuple, Optional | |
| import numpy as np | |
| class ControlAffineSystem(ABC): | |
| """ | |
| Continuous-time control-affine dynamical system: | |
| dot{x} = f(x) + g(x) * u + w(t) | |
| where: | |
| x in R^n (state) | |
| u in R^m (control input, subject to u_min <= u <= u_max) | |
| w in R^n (unmodeled disturbance / distribution shift) | |
| """ | |
| def __init__( | |
| self, | |
| state_dim: int, | |
| control_dim: int, | |
| u_min: np.ndarray, | |
| u_max: np.ndarray, | |
| dt: float = 0.01, | |
| ): | |
| self.state_dim = state_dim | |
| self.control_dim = control_dim | |
| self.u_min = np.asarray(u_min, dtype=np.float32) | |
| self.u_max = np.asarray(u_max, dtype=np.float32) | |
| self.dt = dt | |
| def f(self, x: np.ndarray) -> np.ndarray: | |
| """Drift vector field f(x) in R^n.""" | |
| pass | |
| def g(self, x: np.ndarray) -> np.ndarray: | |
| """Control actuation matrix g(x) in R^{n x m}.""" | |
| pass | |
| def x_dot( | |
| self, | |
| x: np.ndarray, | |
| u: np.ndarray, | |
| disturbance: Optional[np.ndarray] = None, | |
| ) -> np.ndarray: | |
| """ | |
| Computes continuous state derivative: dot{x} = f(x) + g(x)u + w. | |
| """ | |
| u_clamped = np.clip(u, self.u_min, self.u_max) | |
| f_val = self.f(x) | |
| g_val = self.g(x) | |
| dx = f_val + g_val @ u_clamped | |
| if disturbance is not None: | |
| dx = dx + disturbance | |
| return dx | |
| def step_rk4( | |
| self, | |
| x: np.ndarray, | |
| u: np.ndarray, | |
| disturbance: Optional[np.ndarray] = None, | |
| ) -> np.ndarray: | |
| """ | |
| 4th-order Runge-Kutta numerical integration step: x_{t+1} = RK4(x_t, u_t, dt). | |
| """ | |
| dt = self.dt | |
| k1 = self.x_dot(x, u, disturbance) | |
| k2 = self.x_dot(x + 0.5 * dt * k1, u, disturbance) | |
| k3 = self.x_dot(x + 0.5 * dt * k2, u, disturbance) | |
| k4 = self.x_dot(x + dt * k3, u, disturbance) | |
| x_next = x + (dt / 6.0) * (k1 + 2.0 * k2 + 2.0 * k3 + k4) | |
| return x_next | |
| def step( | |
| self, | |
| x: np.ndarray, | |
| u: np.ndarray, | |
| disturbance: Optional[np.ndarray] = None, | |
| ) -> np.ndarray: | |
| """ | |
| Standard numerical integration step forwarding to RK4. | |
| """ | |
| return self.step_rk4(x, u, disturbance) | |
| def step_euler( | |
| self, | |
| x: np.ndarray, | |
| u: np.ndarray, | |
| disturbance: Optional[np.ndarray] = None, | |
| ) -> np.ndarray: | |
| """ | |
| Forward Euler integration step: x_{t+1} = x_t + dt * x_dot(x_t, u_t). | |
| """ | |
| dx = self.x_dot(x, u, disturbance) | |
| return x + self.dt * dx | |