|
| 1 | +--- |
| 2 | +id: api |
| 3 | +sidebar_position: 1 |
| 4 | +title: API Reference |
| 5 | +--- |
| 6 | + |
| 7 | +# API Reference |
| 8 | + |
| 9 | +KoopmanRL is organised into two top-level packages. |
| 10 | + |
| 11 | +## `koopmanrl` — core algorithms and environments |
| 12 | + |
| 13 | +| Module | Contents | |
| 14 | +|--------|----------| |
| 15 | +| `koopmanrl.environments` | Four benchmark Gym environments | |
| 16 | +| `koopmanrl.soft_koopman_value_iteration` | SKVI training script | |
| 17 | +| `koopmanrl.soft_actor_koopman_critic` | SAKC training script | |
| 18 | +| `koopmanrl.linear_quadratic_regulator` | LQR baseline | |
| 19 | +| `koopmanrl.sac_continuous_action` | SAC (Q-value) baseline | |
| 20 | +| `koopmanrl.value_based_sac_continuous_action` | SAC (value-function) baseline | |
| 21 | +| `koopmanrl.koopman_observables` | Observable (lifting) functions | |
| 22 | +| `koopmanrl.koopman_tensor` | Koopman tensor construction and fitting | |
| 23 | +| `koopmanrl.opt_wrappers` | Wrappers for Optuna/Ray Tune integration | |
| 24 | +| `koopmanrl.utils` | Shared utilities (config loading, seeding) | |
| 25 | +| `koopmanrl.sakc_optuna_opt` | SAKC hyperparameter optimization | |
| 26 | +| `koopmanrl.skvi_optuna_opt` | SKVI hyperparameter optimization | |
| 27 | + |
| 28 | +## `koopmanrl_utils` — post-processing and visualisation |
| 29 | + |
| 30 | +| Module | Contents | |
| 31 | +|--------|----------| |
| 32 | +| `koopmanrl_utils.movies.generate_trajectories` | Roll out policies and save trajectory `.npy` files | |
| 33 | +| `koopmanrl_utils.movies.generate_trajectory_figure` | Static PNG trajectory plots with optional vector field | |
| 34 | +| `koopmanrl_utils.movies.generate_gifs` | Animated GIF generation from saved trajectories | |
| 35 | +| `koopmanrl_utils.run_optimized_experiments` | Re-run best configs across seeds | |
| 36 | +| `koopmanrl_utils.plot_csv_from_tensorboards` | Plot training curves from TensorBoard CSVs | |
| 37 | + |
| 38 | +## Environments |
| 39 | + |
| 40 | +All four environments follow the [OpenAI Gym](https://gymnasium.farama.org/) interface (`gym==0.23.1`). They are registered at import time and can be instantiated with: |
| 41 | + |
| 42 | +```python |
| 43 | +import gym |
| 44 | +import koopmanrl.environments # registers all environments |
| 45 | + |
| 46 | +env = gym.make("FluidFlow-v0") |
| 47 | +obs = env.reset() |
| 48 | +obs, reward, done, info = env.step(env.action_space.sample()) |
| 49 | +``` |
| 50 | + |
| 51 | +### Environment IDs |
| 52 | + |
| 53 | +| ID | Class | Source | |
| 54 | +|----|-------|--------| |
| 55 | +| `LinearSystem-v0` | `LinearSystem` | `koopmanrl/environments/linear_system.py` | |
| 56 | +| `FluidFlow-v0` | `FluidFlow` | `koopmanrl/environments/fluid_flow.py` | |
| 57 | +| `Lorenz-v0` | `Lorenz` | `koopmanrl/environments/lorenz.py` | |
| 58 | +| `DoubleWell-v0` | `DoubleWell` | `koopmanrl/environments/double_well.py` | |
| 59 | + |
| 60 | +## Koopman tensor |
| 61 | + |
| 62 | +The `koopmanrl.koopman_tensor` module provides the core Koopman tensor fitting routine used by both SKVI and SAKC. It accepts batches of transition tuples $(x_t, u_t, x_{t+1})$ and returns a tensor $\mathcal{K}$ such that |
| 63 | + |
| 64 | +$$ |
| 65 | +\phi(x_{t+1}) \approx \mathcal{K}(u_t) \, \phi(x_t) |
| 66 | +$$ |
| 67 | + |
| 68 | +where $\phi$ is the observable (lifting) function chosen via `koopmanrl.koopman_observables`. |
| 69 | + |
| 70 | +## Config loading |
| 71 | + |
| 72 | +`koopmanrl.utils.load_and_apply_config` provides layered configuration merging: a JSON file sets defaults, and any CLI flag explicitly provided takes precedence. |
| 73 | + |
| 74 | +```python |
| 75 | +from koopmanrl.utils import load_and_apply_config |
| 76 | + |
| 77 | +args = MyArgs().parse_args() |
| 78 | +args = load_and_apply_config(args, "configurations/sakc_fluid_flow_hparams.json") |
| 79 | +``` |
0 commit comments