File size: 2,806 Bytes
0210a65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
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

    @abstractmethod
    def f(self, x: np.ndarray) -> np.ndarray:
        """Drift vector field f(x) in R^n."""
        pass

    @abstractmethod
    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