Skip to content

Commit

Permalink
perf: remove redundant computations
Browse files Browse the repository at this point in the history
  • Loading branch information
incebellipipo committed May 27, 2024
1 parent 610c998 commit 184e007
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 29 deletions.
6 changes: 3 additions & 3 deletions notebooks/09_compare_allocators_azimuth_rate.ipynb

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ authors = [
description = "A python package for Marine Vehicle Control"
readme = "README.md"
requires-python = ">=3.8"
dynamic = ["version", "dependencies"]
dynamic = ["version", "dependencies", "optional-dependencies"]
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
]
urls = { "Source Code" = "https://github.com/incebellipipo/skadipy" }
license = { file = "LICENSE" }

[tool.setuptools.dynamic]
dependencies = {file = ["requirements.txt"]}
optional-dependencies.examples = { file = ["requirements.examples.txt"]}


[tool.setuptools_scm]
version_file = "src/skadipy/__version__.py"
Expand Down
21 changes: 21 additions & 0 deletions src/skadipy/allocator/reference_filters/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

import typing
import numpy as np
import scipy


from ... import toolbox
from ... import safety
from ... import allocator
from ... import actuator
Expand Down Expand Up @@ -58,6 +61,9 @@ def __init__(
# Zeta, regularization parameter
self._zeta = zeta

# B matrix
self._b_matrix_weighted_inverse = None

# Q matrix, nullspace of B
self._q_matrix = None
# Theta, to be multiplied with Q, where
Expand Down Expand Up @@ -143,3 +149,18 @@ def _kappa(self, xi, xi_error, d_xi_particular, upsilon) -> np.ndarray:

# kappa := xi_dot
return kappa

def compute_configuration_matrix(self) -> None:
super().compute_configuration_matrix()

# Indices for degrees of freedom
dof_indices = [i.value for i in self.force_torque_components]

# Prepare the required variables
b_matrix = self._b_matrix[dof_indices, :]
w_matrix = self._w_matrix
self._b_matrix_weighted_inverse = toolbox.weighted_pseudo_inverse(
matrix=b_matrix, weights=w_matrix)

# Nullspace of B, $Q \in \mathcal{N}(B)$
self._q_matrix = scipy.linalg.null_space(b_matrix)
18 changes: 4 additions & 14 deletions src/skadipy/allocator/reference_filters/_minimum_magnitude.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,16 @@ def allocate(self, tau: np.ndarray, d_tau: np.ndarray = None) -> typing.Tuple[np
# Indices for degrees of freedom
dof_indices = [i.value for i in self.force_torque_components]

# Prepare the required variables
b_matrix = self._b_matrix[dof_indices, :]
w_matrix = self._w_matrix
b_weighted_inverse = toolbox.weighted_pseudo_inverse(
matrix=b_matrix,
weights=w_matrix
)

# Nullspace of B, $Q \in \mathcal{N}(B)$
self._q_matrix = scipy.linalg.null_space(b_matrix)

if self._theta is None:
# Initialization of variables
n, p = b_matrix.shape
n, p = self._b_matrix[dof_indices, :].shape
q = p - n
self._theta = np.zeros((q, 1), dtype=np.float32)

if self._xi is None:
self._xi = np.zeros(
(b_matrix.shape[1], 1), dtype=np.float32)
(self._b_matrix[dof_indices, :].shape[1], 1), dtype=np.float32)

# if self._tau_previous is None:
# Initialize the previous tau with zeros
Expand All @@ -104,11 +94,11 @@ def allocate(self, tau: np.ndarray, d_tau: np.ndarray = None) -> typing.Tuple[np
d_tau = self._derivative_solver(tau) / self._t_s

# Compute the particular solution
xi_p = b_weighted_inverse @ tau[dof_indices, :]
xi_p = self._b_matrix_weighted_inverse @ tau[dof_indices, :]

# Compute the derivative of particular solution using derivative of
# requested force
d_xi_p = b_weighted_inverse @ d_tau[dof_indices, :]
d_xi_p = self._b_matrix_weighted_inverse @ d_tau[dof_indices, :]

# Get the desired allocated forces on the manifold
xi_d = xi_p + self._q_matrix @ self._theta
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,37 +76,30 @@ def allocate(self, tau: np.ndarray, d_tau: np.ndarray = None) -> typing.Tuple[np
dof_indices = [i.value for i in self.force_torque_components]

# Prepare the required variables
b_matrix = self._b_matrix[dof_indices, :]
w_matrix = self._w_matrix
b_weighted_inverse = toolbox.weighted_pseudo_inverse(
matrix=b_matrix, weights=w_matrix)

# Nullspace of B, $Q \in \mathcal{N}(B)$
self._q_matrix = scipy.linalg.null_space(b_matrix)

if self._theta is None:
# Initialization of variables
n, p = np.shape(b_matrix)
n, p = np.shape(self._b_matrix[dof_indices, :])
# Theta is the parameter vector for the maneuvering gradient
q = p - n
self._theta = np.zeros((q, 1), dtype=np.float32)

# Initialize the virtual force/torque
if self._xi is None:
self._xi = np.zeros(
(b_matrix.shape[1], 1), dtype=np.float32)
(self._b_matrix[dof_indices, :].shape[1], 1), dtype=np.float32)

# Compute $\dot{xi}_p and use exponential smoothing
# Check if it has been provided to the function
if type(d_tau) == type(None):
d_tau = self._derivative_solver(tau) / self._t_s

# Compute the particular solution
xi_p = b_weighted_inverse @ tau[dof_indices, :]
xi_p = self._b_matrix_weighted_inverse @ tau[dof_indices, :]

# Compute the derivative of particular solution using derivative of
# requested force
d_xi_p = b_weighted_inverse @ d_tau[dof_indices, :]
d_xi_p = self._b_matrix_weighted_inverse @ d_tau[dof_indices, :]

# Get the desired allocated forces on the manifold
xi_d = xi_p + self._q_matrix @ self._theta
Expand Down

0 comments on commit 184e007

Please sign in to comment.