-
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.
Merge pull request #13 from decargroup/dev
Fix `matplotlib` interaction with `joblib`
- Loading branch information
Showing
8 changed files
with
286 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
"""D-K iteration with fixed number of iterations and fit order.""" | ||
|
||
import control | ||
import numpy as np | ||
from matplotlib import pyplot as plt | ||
|
||
import dkpy | ||
|
||
|
||
def example_dk_iter_fixed_order(): | ||
"""D-K iteration with fixed number of iterations and fit order.""" | ||
# Plant | ||
G0 = np.array( | ||
[ | ||
[87.8, -86.4], | ||
[108.2, -109.6], | ||
] | ||
) | ||
G = control.append( | ||
control.TransferFunction([1], [75, 1]), | ||
control.TransferFunction([1], [75, 1]), | ||
) * control.TransferFunction( | ||
G0.reshape(2, 2, 1), | ||
np.ones((2, 2, 1)), | ||
) | ||
# Weights | ||
Wp = 0.5 * control.append( | ||
control.TransferFunction([10, 1], [10, 1e-5]), | ||
control.TransferFunction([10, 1], [10, 1e-5]), | ||
) | ||
Wi = control.append( | ||
control.TransferFunction([1, 0.2], [0.5, 1]), | ||
control.TransferFunction([1, 0.2], [0.5, 1]), | ||
) | ||
G.name = "G" | ||
Wp.name = "Wp" | ||
Wi.name = "Wi" | ||
sum_w = control.summing_junction( | ||
inputs=["u_w", "u_G"], | ||
dimension=2, | ||
name="sum_w", | ||
) | ||
sum_del = control.summing_junction( | ||
inputs=["u_del", "u_u"], | ||
dimension=2, | ||
name="sum_del", | ||
) | ||
split = control.summing_junction( | ||
inputs=["u"], | ||
dimension=2, | ||
name="split", | ||
) | ||
P = control.interconnect( | ||
syslist=[G, Wp, Wi, sum_w, sum_del, split], | ||
connections=[ | ||
["G.u", "sum_del.y"], | ||
["sum_del.u_u", "split.y"], | ||
["sum_w.u_G", "G.y"], | ||
["Wp.u", "sum_w.y"], | ||
["Wi.u", "split.y"], | ||
], | ||
inplist=["sum_del.u_del", "sum_w.u_w", "split.u"], | ||
outlist=["Wi.y", "Wp.y", "-sum_w.y"], | ||
) | ||
# Dimensions | ||
n_y = 2 | ||
n_u = 2 | ||
|
||
def callback( | ||
dk_iteration, | ||
iteration, | ||
omega, | ||
mu_omega, | ||
D_omega, | ||
P, | ||
K, | ||
block_structure, | ||
): | ||
d_info = [] | ||
for fit_order in range(5): | ||
D_fit, D_fit_inv = dk_iteration.transfer_function_fit.fit( | ||
omega, | ||
D_omega, | ||
order=fit_order, | ||
block_structure=block_structure, | ||
) | ||
d_info.append( | ||
dkpy.DScaleFitInfo.create_from_fit( | ||
omega, | ||
mu_omega, | ||
D_omega, | ||
P, | ||
K, | ||
D_fit, | ||
D_fit_inv, | ||
block_structure, | ||
) | ||
) | ||
fig, ax = plt.subplots() | ||
d_info[0].plot_mu(ax=ax, plot_kw=dict(label="true"), hide="mu_fit_omega") | ||
for i, ds in enumerate(d_info): | ||
ds.plot_mu(ax=ax, plot_kw=dict(label=f"order={i}"), hide="mu_omega") | ||
plt.show() | ||
selected_order = int(input("Selected order: ")) | ||
done = input("Done? (y/N): ") == "y" | ||
return (selected_order, done) | ||
|
||
dk_iter = dkpy.DkIterOrderCallback( | ||
controller_synthesis=dkpy.HinfSynLmi( | ||
lmi_strictness=1e-7, | ||
solver_params=dict( | ||
solver="MOSEK", | ||
eps=1e-8, | ||
), | ||
), | ||
structured_singular_value=dkpy.SsvLmiBisection( | ||
bisection_atol=1e-5, | ||
bisection_rtol=1e-5, | ||
max_iterations=1000, | ||
lmi_strictness=1e-7, | ||
solver_params=dict( | ||
solver="MOSEK", | ||
eps=1e-9, | ||
), | ||
), | ||
transfer_function_fit=dkpy.TfFitSlicot(), | ||
fit_order_callback=callback, | ||
) | ||
|
||
omega = np.logspace(-3, 3, 61) | ||
block_structure = np.array([[1, 1], [1, 1], [2, 2]]) | ||
K, N, mu, d_scale_fit_info, info = dk_iter.synthesize( | ||
P, | ||
n_y, | ||
n_u, | ||
omega, | ||
block_structure, | ||
) | ||
|
||
print(mu) | ||
|
||
fig, ax = plt.subplots() | ||
for i, ds in enumerate(d_scale_fit_info): | ||
ds.plot_mu(ax=ax, plot_kw=dict(label=f"iter{i}")) | ||
|
||
ax = None | ||
for i, ds in enumerate(d_scale_fit_info): | ||
_, ax = ds.plot_D(ax=ax, plot_kw=dict(label=f"iter{i}")) | ||
|
||
plt.show() | ||
|
||
|
||
if __name__ == "__main__": | ||
example_dk_iter_fixed_order() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,22 @@ | ||
# Dependencies | ||
numpy | ||
scipy | ||
control | ||
slycot | ||
cvxpy | ||
joblib | ||
slycot | ||
matplotlib | ||
|
||
# Extra solvers | ||
mosek>=9.2.49 | ||
|
||
# Development | ||
pytest | ||
pytest-regressions | ||
sphinx | ||
sphinx-rtd-theme | ||
ruff==0.8.0 | ||
pre-commit | ||
mosek>=9.2.49 | ||
|
||
# Examples | ||
PyQt6 |
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