sysid package

SysID Base Classes

The ModelFactory Class

class autompc.sysid.model.ModelFactory(system, **kwargs)[source]

The ModelFactory creates and trains a System ID model and provides information about the model hyperparameters.

abstract get_configuration_space()[source]

Returns the model configuration space.

The Model Class

class autompc.sysid.model.Model(system)[source]
abstract traj_to_state(traj)[source]
Parameters:

traj (Trajectory) – State and control history up to present time

Returns:

state – Corresponding model state

Return type:

numpy array of size self.state_dim

abstract update_state(state, new_ctrl, new_obs)[source]
Parameters:
  • state (numpy array of size self.state_dim) – Current model state

  • new_ctrl (numpy array of size self.system.ctrl_dim) – New control input

  • new_obs (numpy array of size self.system.obs_dim) – New observation

Returns:

state – Model state after observation and control

Return type:

numpy array of size self.state_dim

abstract pred(state, ctrl)[source]

Run model prediction.

Parameters:
  • state (Numpy array of size self.state_dim) – Model state at time t

  • ctrl (Numpy array of size self.system.ctrl_dim) – Control applied at time t

Returns:

state – Predicted model state at time t+1

Return type:

Numpy array of size self.state_dim

pred_batch(states, ctrls)[source]

Run batch model predictions. Depending on the model, this can be much faster than repeatedly calling pred.

Parameters:
  • state (Numpy array of size (N, self.state_dim)) – N model input states

  • ctrl (Numpy array of size (N, self.system.ctrl_dim)) – N controls

Returns:

state – N predicted states

Return type:

Numpy array of size (N, self.state_dim)

pred_diff(state, ctrl)[source]

Run model prediction and compute gradients.

Parameters:
  • state (Numpy array of size self.state_dim) – Model state at time t

  • ctrl (Numpy array of size self.system.ctrl_dim) – Control at time t

Returns:

  • state (Numpy array of size self.state_dim) – Predicted model state at time t+1

  • state_jac (Numpy array of shape (self.state_dim,) – self.state_dim) Gradient of predicted model state wrt to state

  • ctrl_jac (Numpy array of shape (self.state_dim,) – self.ctrl_dim) Gradient of predicted model state wrt to ctrl

pred_diff_batch(states, ctrls)[source]

Run model prediction and compute gradients in batch.

Parameters:
  • state (Numpy array of shape (N, self.state_dim)) – N input model states

  • ctrl (Numpy array of size (N, self.system.ctrl_dim)) – N input controls

Returns:

  • state (Numpy array of size (N, self.state_dim)) – N predicted model states

  • state_jac (Numpy array of shape (N, self.state_dim,) – self.state_dim) Gradient of predicted model states wrt to state

  • ctrl_jac (Numpy array of shape (N, self.state_dim,) – self.ctrl_dim) Gradient of predicted model states wrt to ctrl

to_linear()[source]
Returns: (A, B, state_func, cost_func)

A, B – Linear system matrices as Numpy arrays.

Only implemented for linear models.

train(trajs, silent=False)[source]
Parameters:
  • trajs (List of pairs (xs, us)) – Training set of trajectories

  • silent (bool) – Silence progress bar output

Only implemented for trainable models.

get_parameters()[source]

Returns a dict containing trained model parameters.

Only implemented for trainable models.

set_parameters(params)[source]

Sets trainable model parameters from dict.

Only implemented for trainable parameters.

abstract property state_dim

Returns the size of the model state

property is_linear

Returns true for linear models

property is_diff

Returns true for differentiable models.

Supported System ID Models

Multi-layer Perceptron

class autompc.sysid.MLPFactory(*args, **kwargs)[source]

The multi-layer perceptron (MLP) model uses a feed-forward neural network architecutret to predict the system dynamics. The network size, activation function, and learning rate are tunable hyperparameters.

Parameters

  • n_batch (Type: int, Default: 64): Training batch size of the neural net.

  • n_train_iters (Type: int, Default: 50): Number of training epochs

Hyperparameters:

  • n_hidden_layers (Type: str, Choices: [“1”, “2”, “3”, “4”], Default: “2”): The number of hidden layers in the network

  • hidden_size_1 (Type int, Low: 16, High: 256, Default: 32): Size of hidden layer 1.

  • hidden_size_2 (Type int, Low: 16, High: 256, Default: 32): Size of hidden layer 2. (Conditioned on n_hidden_layers >=2).

  • hidden_size_3 (Type int, Low: 16, High: 256, Default: 32): Size of hidden layer 3. (Conditioned on n_hidden_layers >=3).

  • hidden_size_4 (Type int, Low: 16, High: 256, Default: 32): Size of hidden layer 4. (Conditioned on n_hidden_layers >=4).

  • nonlintype (Type: str, choices: [“relu”, “tanh”, “sigmoid”, “selu”], Default: “relu): Type of activation function.

  • lr (Type: float, Low: 1e-5, High: 1, Default: 1e-3): Adam learning rate for the network.

Sparse Identification of Nonlinear Dynamics (SINDy)

class autompc.sysid.SINDyFactory(*args, **kwargs)[source]

Sparse Identification of Nonlinear Dynamics (SINDy) is an system identification approach that works as follows. Using a library of \(k\) pre-selected functions (e.g. \(f \in \mathbb{R}^k\)), it computes numerically the derivatives of the system states (e.g. \(\dot{x} \in \mathbb{R}^n\)) iteratively solves a least-squares optimization to identify the weights \(K \in \mathbb{R}^{n \times k}\) that best fit the data: e.g. \(\|\dot{x} - Kf(x) \|^2\). In every iteration, functions whose weights are below a user-specified threshold \(\lambda\) are discarded. For more information, the reader is referred to https://arxiv.org/pdf/2004.08424.pdf

Hyperparameters:

  • time_mode (Type str, Choices: [“discrete”, “continuous”]): Whether to learn dynamics equations as discrete-time or continous-time.

  • method (Type str, Choices: [“lstsq, lasso”], Default: “lstsq”): Method for selecting model coefficients.

  • lasso_alpha (Type: str, Low: 10^-5, High: 10^2, Default: 1): α parameter for lasso regression. (Conditioned on method=”lasso”)

  • poly_basis (Type: bool): Whether to use polynomial basis functions

  • poly_degree (Type: int, Low: 2, High: 8, Default: 3): Maximum degree of polynomial terms. (Conditioned on poly_basis=”true”)

  • poly_cross_terms (Type: bool): Whether to include polynomial cross-terms. (Conditioned on poly_basis=”true”)

  • trig_basis (Type: bool): Whether to include trigonometric basis terms.

  • trig_freq (Type: int, Low: 1, High: 8, Default: 1): Maximum trig function frequency to include. (Conditioned on trig_basis=”true”)

  • trig_interaction (Type: bool): Whether to include cross-multiplication terms between trig functions and other state variables.

Autoregression (ARX)

class autompc.sysid.ARXFactory(*args, **kwargs)[source]

Autoregression with Exogenous Variable (ARX) learns the dynamics as a linear function of the last \(k\) observations and controls. That is

\[x_{t+1} = [x_t, \ldots x_{t-k+1}, u_t, \ldots, u_{t-k+1}] \theta\]

The model is trained least-squared linear regression.

Hyperparameters:

  • history (Type: int, Low: 1, High: 10, Default: 4): Size of history window for ARX model.

Koopman

class autompc.sysid.KoopmanFactory(*args, **kwargs)[source]

This class identifies Koopman models of the form \(\dot{\Psi}(x) = A\Psi(x) + Bu\). Given states \(x \in \mathbb{R}^n\), \(\Psi(x) \in \mathbb{R}^N\) —termed Koopman basis functions— are used to lift the dynamics in a higher-dimensional space where nonlinear functions of the system states evolve linearly. The identification of the Koopman model, specified by the A and B matrices, can be done in various ways, such as solving a least squares solution \(\|\dot{\Psi}(x) - [A, B][\Psi(x),u]^T\|\).

The choice of the basis functions is an open research question. In this implementation, we choose basis functions that depend only on the system states and not the control, in order to derive a representation that is amenable for LQR control.

Hyperparameters:

  • method (Type: str, Choices: [“lstsq”, “lasso”, “stable”]): Method for training Koopman operator.

  • lasso_alpha (Type: float, Low: 10^-10, High: 10^2, Defalt: 1.0): α parameter for Lasso regression. (Conditioned on method=”lasso”).

  • poly_basis (Type: bool): Whether to use polynomial basis functions.

  • poly_degree (Type: int, Low: 2, High: 8, Default: 3): Maximum degree of polynomial basis functions. (Conditioned on poly_basis=”true”).

  • trig_basis (Type: bool): Whether to use trig basis functions.

  • trig_freq (Type: int, Low: 1, High: 8, Default: 1): Maximum frequency of trig functions.

  • product_terms (Type: bool): Whether to include cross-product terms.

Approximate Gaussian Process

class autompc.sysid.ApproximateGPModelFactory(*args, **kwargs)[source]

Gaussian Processes (GPs) are a non-parametric regression method. Since GPs have trouble scaling to large training sets, this class provides a variational GP which automatically selects a subset of the training data for inference. This functionality is provided by the gPyTorch library. For more details see the original documentation, and the corresponding paper at https://arxiv.org/pdf/1411.2005.pdf

Hyperparameters:

  • induce_count (Type: int, Lower: 50, Upper: 200, Default: 100): Number of inducing points to include in the gaussian process.