Skip to main content

API Reference

KoopmanRL is organised into two top-level packages.

koopmanrl — core algorithms and environments​

ModuleContents
koopmanrl.environmentsFour benchmark Gym environments
koopmanrl.soft_koopman_value_iterationSKVI training script
koopmanrl.soft_actor_koopman_criticSAKC training script
koopmanrl.linear_quadratic_regulatorLQR baseline
koopmanrl.sac_continuous_actionSAC (Q-value) baseline
koopmanrl.value_based_sac_continuous_actionSAC (value-function) baseline
koopmanrl.koopman_observablesObservable (lifting) functions
koopmanrl.koopman_tensorStand-alone Koopman tensor classes, regressors and the generate_tensor CLI
koopmanrl.opt_wrappersWrappers for Optuna/Ray Tune integration
koopmanrl.utilsShared utilities (config loading, seeded environment construction, folder creation)
koopmanrl.sakc_optuna_optSAKC hyperparameter optimization
koopmanrl.skvi_optuna_optSKVI hyperparameter optimization

koopmanrl_utils — post-processing and visualisation​

ModuleContents
koopmanrl_utils.movies.generate_trajectoriesRoll out policies and save trajectory .npy files
koopmanrl_utils.movies.generate_trajectory_figureStatic PNG trajectory plots with optional vector field
koopmanrl_utils.movies.generate_gifsAnimate saved trajectories as trajectory and cost-ratio GIFs
koopmanrl_utils.run_optimized_experimentsRe-run best configs and the baselines across seeds (episodic-return figures)
koopmanrl_utils.run_ablationsRun the SKVI and SAKC hyperparameter grids behind the two ablation figures
koopmanrl_utils.run_skvi_optimizationRun skvi_optuna_opt on all four benchmarks (requires --storage_dir; starts the four studies when run with it)
koopmanrl_utils.run_sakc_optimizationRun sakc_optuna_opt on all four benchmarks (requires --storage_dir; starts the four studies when run with it)
koopmanrl_utils.dataframe_creatorCollect the episodic returns of a folder of TensorBoard runs into a JSON data frame
koopmanrl_utils.process_episodic_returnsReduce an episodic-return data frame to IQM curves with bootstrap confidence bands (table for plotting)
koopmanrl_utils.process_skvi_ablationsReduce an SKVI ablation data frame to the IQM surface table
koopmanrl_utils.process_sakc_ablationsReduce an SAKC ablation data frame to the IQM surface table
koopmanrl_utils.tsne_koopman_tensort-SNE embedding of Koopman tensors of the four benchmarks in a common dictionary basis
koopmanrl_utils.koopman_prediction_validationPrediction error of the fitted Koopman tensor on held-out random-agent data
koopmanrl_utils.koopman_regressor_comparisonHeld-out accuracy of the Koopman tensor under different regression algorithms
koopmanrl_utils.skvi_policy_checksRead SKVI's deployed policy off the Koopman tensor and check it against references
koopmanrl_utils.skvi_sensitivity_checksTensor accuracy along the trained SKVI policy, and sensitivity of SKVI's control
koopmanrl_utils.interpret_koopmanUnfinished stub for printing stored SKVI value-function weights; it does not run yet (it reads an argument its parser does not define)

Environments​

All four environments follow the OpenAI Gym interface (gym==0.23.1). They are registered at import time and can be instantiated with:

import gym
import koopmanrl.environments # registers all environments

env = gym.make("FluidFlow-v0")
obs = env.reset()
obs, reward, done, info = env.step(env.action_space.sample())

Environment IDs​

IDClassSource
LinearSystem-v0LinearSystemkoopmanrl/environments/linear_system.py
FluidFlow-v0FluidFlowkoopmanrl/environments/fluid_flow.py
Lorenz-v0Lorenzkoopmanrl/environments/lorenz.py
DoubleWell-v0DoubleWellkoopmanrl/environments/double_well.py

Koopman tensor​

SKVI and SAKC each define their own KoopmanTensor class inside their training script (koopmanrl.soft_koopman_value_iteration.KoopmanTensor, koopmanrl.soft_actor_koopman_critic.KoopmanTensor). Both fit a tensor K\mathcal{K} from batches of transition tuples (xt,ut,xt+1)(x_t, u_t, x_{t+1}) such that

ϕ(xt+1)≈K(ut) ϕ(xt)\phi(x_{t+1}) \approx \mathcal{K}(u_t) \, \phi(x_t)

where ϕ\phi is the monomial observable (lifting) function koopmanrl.koopman_observables.monomials. What the two scripts share is that observable module and the regularised regressors of koopmanrl.koopman_tensor.regressors.

The koopmanrl.koopman_tensor package holds stand-alone versions for use outside the training scripts:

ModuleContents
koopmanrl.koopman_tensor.torch_tensorKoopmanTensor (PyTorch)
koopmanrl.koopman_tensor.numpy_tensorKoopmanTensor (NumPy)
koopmanrl.koopman_tensor.regressorsridge, sindy, rrr, fit and tensor_regression: ridge, sequentially thresholded least squares (SINDy) and reduced-rank regression of the tensor, with the hyperparameter chosen on held-out transitions when not given
koopmanrl.koopman_tensor.utilssave_tensor / load_tensor (pickle under ./koopman_tensor/saved_models/<env_id>/)
koopmanrl.koopman_tensor.observablesNumPy and PyTorch observable dictionaries (monomials, indicators, Gaussians)
koopmanrl.koopman_tensor.generate_tensorCLI that collects random-agent data and fits a tensor

The ordinary-least-squares fit (--regressor ols, the default) is solved with torch.linalg.lstsq; ridge, sindy and rrr go through regressors.tensor_regression. To fit a tensor from the command line:

uv run -m koopmanrl.koopman_tensor.generate_tensor --env_id Lorenz-v0 --regressor ridge

--rank (for rrr), --penalty (for ridge) and --threshold (for sindy) are chosen on held-out transitions when omitted.

Config loading​

koopmanrl.utils.load_and_apply_config(args, key_map, fallbacks) provides layered configuration merging with the precedence CLI flag > config file > fallback default. It reads the JSON file named by args.config_file (if set), copies each value whose hyphenated key is in key_map onto the attribute that is still None (i.e. not given on the CLI), and then fills any attribute that is still None from fallbacks.

from koopmanrl.utils import load_and_apply_config

key_map = {"seed": "seed", "num-paths": "num_paths"} # JSON key -> args attribute
fallbacks = {"seed": 1, "num_paths": 100} # used when neither CLI nor file sets a value

args = MyArgs().parse_args() # MyArgs declares config_file and Optional[...] = None fields
args = load_and_apply_config(args, key_map, fallbacks)