-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
78 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,62 @@ | ||
"""Utilities for unit testing.""" | ||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
|
||
import numpy as np | ||
|
||
from donk.dynamics import LinearDynamics | ||
from donk.policy import LinearGaussianPolicy | ||
from donk.utils import symmetrize | ||
|
||
|
||
def random_spd(shape, rng): | ||
def random_spd(shape: tuple[int], rng: np.random.Generator): | ||
"""Generate an arbitrarily random matrix guaranteeed to be s.p.d.""" | ||
return symmetrize(rng.uniform(size=shape)) + np.eye(shape[-1]) * shape[-1] | ||
|
||
|
||
def random_lq_pol(T, dX, dU, rng): | ||
def random_lq_pol(T: int, dX: int, dU: int, rng: np.random.Generator): | ||
"""Generate an arbitrarily random linear gaussian policy.""" | ||
K = rng.normal(size=(T, dU, dX)) | ||
k = rng.normal(size=(T, dU)) | ||
pol_covar = random_spd((T, dU, dU), rng) | ||
return LinearGaussianPolicy(K, k, pol_covar) | ||
|
||
|
||
def random_tvlg(T, dX, dU, rng): | ||
def random_tvlg(T: int, dX: int, dU: int, rng: np.random.Generator): | ||
"""Generate arbitrarily random linear gaussian dynamics.""" | ||
Fm = rng.normal(size=(T, dX, dX + dU)) | ||
fv = rng.normal(size=(T, dX)) | ||
dyn_covar = random_spd((T, dX, dX), rng) | ||
return LinearDynamics(Fm, fv, dyn_covar) | ||
|
||
|
||
def load_state_controller_dataset( | ||
dataset: int, itr: int | ||
) -> tuple[np.ndarray, LinearGaussianPolicy, np.ndarray, np.ndarray]: | ||
"""Loads a state_controller dataset. | ||
Args: | ||
dataset: Id of the dataset | ||
itr: Iteration to return | ||
Returns: | ||
X: (N, T, dX) Real states | ||
pol: Fitted linear policy | ||
X_mean: (T, dX) Mean of state distribution | ||
X_covar: (T, dX, dX) Covariance of state distribution | ||
""" | ||
file = Path(f"tests/data/state_controller_{dataset:02d}.npz") | ||
if not file.is_file(): | ||
raise ValueError(f"There is no dataset 'state_controller_{dataset:02d}.npz'") | ||
|
||
with np.load(file) as data: | ||
if itr not in range(len(data["X"])): | ||
raise ValueError(f"Invalid iteration {itr}") | ||
|
||
X = data["X"][itr] | ||
pol = LinearGaussianPolicy(K=data["K"][itr], k=data["k"][itr], pol_covar=data["pol_covar"][itr]) | ||
X_mean = data["X_mean"][itr] | ||
X_covar = data["X_covar"][itr] | ||
|
||
return X, pol, X_mean, X_covar |