Control API Reference¶
The orc.control subpackage provides reservoir computing models for dynamical system control.
Base Classes¶
RCControllerBase ¶
RCControllerBase(driver: DriverBase, readout: ReadoutBase, embedding: EmbedBase, in_dim: int, control_dim: int, dtype: Float = float64, alpha_1: float = 100, alpha_2: float = 1, alpha_3: float = 5, seed: int = 0)
Bases: Module, ABC
Base class for reservoir computer controllers.
Defines the interface for the reservoir computer controller which includes the driver, readout and embedding layers. Unlike the forecaster, the controller handles an additional control input at each time step.
Attributes:
| Name | Type | Description |
|---|---|---|
driver |
DriverBase
|
Driver layer of the reservoir computer. |
readout |
ReadoutBase
|
Readout layer of the reservoir computer. |
embedding |
EmbedBase
|
Embedding layer of the reservoir computer. Should accept concatenated [input, control] vectors. |
in_dim |
int
|
Dimension of the system input data. |
control_dim |
int
|
Dimension of the control input. |
out_dim |
int
|
Dimension of the output data. |
res_dim |
int
|
Dimension of the reservoir. |
dtype |
type
|
Data type of the reservoir computer (jnp.float64 is highly recommended). |
alpha_1 |
float
|
Weight for trajectory deviation penalty in control optimization. |
alpha_2 |
float
|
Weight for control magnitude penalty in control optimization. |
alpha_3 |
float
|
Weight for control derivative penalty in control optimization. |
seed |
int
|
Random seed for generating the PRNG key for the reservoir computer. |
Methods:
| Name | Description |
|---|---|
force |
Teacher forces the reservoir with input and control sequences. |
apply_control |
Apply a predefined control sequence in closed-loop. |
set_readout |
Replaces the readout layer of the reservoir computer. |
set_embedding |
Replaces the embedding layer of the reservoir computer. |
compute_penalty |
Compute the control penalty for a given control sequence. |
compute_control |
Compute optimal control sequence to track a reference trajectory. |
Initialize RCController Base.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
driver
|
DriverBase
|
Driver layer of the reservoir computer. |
required |
readout
|
ReadoutBase
|
Readout layer of the reservoir computer. |
required |
embedding
|
EmbedBase
|
Embedding layer of the reservoir computer. |
required |
in_dim
|
int
|
Dimension of the system input data. |
required |
control_dim
|
int
|
Dimension of the control input. |
required |
dtype
|
type
|
Data type of the reservoir computer (jnp.float64 is highly recommended). |
float64
|
alpha_1
|
float
|
Weight for trajectory deviation penalty in control optimization. |
100
|
alpha_2
|
float
|
Weight for control magnitude penalty in control optimization. |
1
|
alpha_3
|
float
|
Weight for control derivative penalty in control optimization. |
5
|
seed
|
int
|
Random seed for generating the PRNG key for the reservoir computer. |
0
|
Source code in src/orc/control/base.py
force ¶
Teacher forces the reservoir with input and control sequences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_seq
|
Array
|
Input sequence to force the reservoir, (shape=(seq_len, in_dim)). |
required |
control_seq
|
Array
|
Control sequence to force the reservoir, (shape=(seq_len, control_dim)). |
required |
res_state
|
Array
|
Initial reservoir state, (shape=(res_dim,)). |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Forced reservoir sequence, (shape=(seq_len, res_dim)). |
Source code in src/orc/control/base.py
__call__ ¶
Teacher forces the reservoir, wrapper for force method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_seq
|
Array
|
Input sequence to force the reservoir, (shape=(seq_len, in_dim)). |
required |
control_seq
|
Array
|
Control sequence to force the reservoir, (shape=(seq_len, control_dim)). |
required |
res_state
|
Array
|
Initial reservoir state, (shape=(res_dim,)). |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Forced reservoir sequence, (shape=(seq_len, res_dim)). |
Source code in src/orc/control/base.py
apply_control ¶
Apply a predefined control sequence in closed-loop.
The readout feeds back as the next input: u(t+1) = readout(x(t)). Control c(t) comes from the provided control_seq.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
control_seq
|
Array
|
Control sequence to apply, (shape=(fcast_len, control_dim)). |
required |
res_state
|
Array
|
Initial reservoir state, (shape=(res_dim,)). |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Controlled output trajectory with shape=(fcast_len, out_dim)). |
Source code in src/orc/control/base.py
set_readout ¶
set_readout(readout: ReadoutBase) -> RCControllerBase
Replace readout layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
readout
|
ReadoutBase
|
New readout layer. |
required |
Returns:
| Type | Description |
|---|---|
RCControllerBase
|
Updated model with new readout layer. |
Source code in src/orc/control/base.py
set_embedding ¶
set_embedding(embedding: EmbedBase) -> RCControllerBase
Replace embedding layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedding
|
EmbedBase
|
New embedding layer. |
required |
Returns:
| Type | Description |
|---|---|
RCControllerBase
|
Updated model with new embedding layer. |
Source code in src/orc/control/base.py
compute_penalty ¶
Compute the control penalty for a given control sequence.
The penalty consists of three terms: - Deviation penalty: squared error between forecast and reference trajectory - Magnitude penalty: squared norm of control inputs - Derivative penalty: squared norm of control input differences
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
control_seq
|
Array
|
Control sequence to evaluate, (shape=(fcast_len, control_dim)). |
required |
res_state
|
Array
|
Initial reservoir state, (shape=(res_dim,)). |
required |
ref_traj
|
Array
|
Reference trajectory to track, (shape=(fcast_len, out_dim)). |
required |
Returns:
| Type | Description |
|---|---|
Float
|
Total penalty value (scalar). |
Source code in src/orc/control/base.py
compute_control ¶
Compute optimal control sequence to track a reference trajectory.
Uses BFGS optimization to find a control sequence that minimizes the penalty function (deviation from reference + control magnitude + control smoothness).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
control_seq
|
Array
|
Initial guess for control sequence, (shape=(fcast_len, control_dim)). |
required |
res_state
|
Array
|
Initial reservoir state, (shape=(res_dim,)). |
required |
ref_traj
|
Array
|
Reference trajectory to track, (shape=(fcast_len, out_dim)). |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Optimized control sequence, (shape=(fcast_len, control_dim)). |
Source code in src/orc/control/base.py
Models¶
ESNController ¶
ESNController(data_dim: int, control_dim: int, res_dim: int, leak_rate: float = 0.6, bias: float = 1.6, embedding_scaling: float = 0.08, Wr_density: float = 0.02, Wr_spectral_radius: float = 0.8, dtype: type = float64, alpha_1: float = 100, alpha_2: float = 1, alpha_3: float = 5, seed: int = 0, quadratic: bool = False, use_sparse_eigs: bool = True)
Bases: RCControllerBase
Basic implementation of ESN for control tasks.
Attributes:
| Name | Type | Description |
|---|---|---|
res_dim |
int
|
Reservoir dimension. |
data_dim |
int
|
System input/output dimension. |
control_dim |
int
|
Control input dimension. |
driver |
ESNDriver
|
Driver implementing the Echo State Network dynamics. |
readout |
ReadoutBase
|
Trainable linear readout layer. |
embedding |
LinearEmbedding
|
Untrainable linear embedding layer for [input, control] concatenation. |
alpha_1 |
float
|
Weight for trajectory deviation penalty in control optimization. |
alpha_2 |
float
|
Weight for control magnitude penalty in control optimization. |
alpha_3 |
float
|
Weight for control derivative penalty in control optimization. |
Methods:
| Name | Description |
|---|---|
force |
Teacher forces the reservoir with input and control sequences. |
apply_control |
Apply a predefined control sequence in closed-loop. |
set_readout |
Replace readout layer. |
set_embedding |
Replace embedding layer. |
Initialize the ESN controller.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_dim
|
int
|
Dimension of the system input/output data. |
required |
control_dim
|
int
|
Dimension of the control input. |
required |
res_dim
|
int
|
Dimension of the reservoir adjacency matrix Wr. |
required |
leak_rate
|
float
|
Integration leak rate of the reservoir dynamics. |
0.6
|
bias
|
float
|
Bias term for the reservoir dynamics. |
1.6
|
embedding_scaling
|
float
|
Scaling factor for the embedding layer. |
0.08
|
Wr_density
|
float
|
Density of the reservoir adjacency matrix Wr. |
0.02
|
Wr_spectral_radius
|
float
|
Largest eigenvalue of the reservoir adjacency matrix Wr. |
0.8
|
dtype
|
type
|
Data type of the model (jnp.float64 is highly recommended). |
float64
|
alpha_1
|
float
|
Weight for trajectory deviation penalty in control optimization. |
100
|
alpha_2
|
float
|
Weight for control magnitude penalty in control optimization. |
1
|
alpha_3
|
float
|
Weight for control derivative penalty in control optimization. |
5
|
seed
|
int
|
Random seed for generating the PRNG key for the reservoir computer. |
0
|
quadratic
|
bool
|
Use quadratic nonlinearity in output, default False. |
False
|
use_sparse_eigs
|
bool
|
Whether to use sparse eigensolver for setting the spectral radius of wr. Default is True, which is recommended to save memory and compute time. If False, will use dense eigensolver which may be more accurate. |
True
|
Source code in src/orc/control/models.py
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
Training Functions¶
train_ESNController ¶
train_ESNController(model: ESNController, train_seq: Array, control_seq: Array, target_seq: Array = None, spinup: int = 0, initial_res_state: Array = None, beta: float = 8e-08) -> tuple[ESNController, Array]
Training function for ESNController.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
ESNController
|
ESNController model to train. |
required |
train_seq
|
Array
|
Training input sequence for reservoir, (shape=(seq_len, data_dim)). |
required |
control_seq
|
Array
|
Control input sequence for reservoir, (shape=(seq_len, control_dim)). |
required |
target_seq
|
Array
|
Target sequence for training reservoir, (shape=(seq_len, data_dim)). If None, defaults to train_seq[1:]. |
None
|
initial_res_state
|
Array
|
Initial reservoir state, (shape=(res_dim,)). |
None
|
spinup
|
int
|
Initial transient of reservoir states to discard. |
0
|
beta
|
float
|
Tikhonov regularization parameter. |
8e-08
|
Returns:
| Name | Type | Description |
|---|---|---|
model |
ESNController
|
Trained ESN controller model. |
res_seq |
Array
|
Training sequence of reservoir states. |