Skip to content

Commit

Permalink
Merge pull request #18 from decargroup/feature/17-implement-order-sel…
Browse files Browse the repository at this point in the history
…ection-method-with-inheritance

Implement order selection method with inheritance
  • Loading branch information
sdahdah authored Dec 5, 2024
2 parents 72a7689 + de1accd commit 56d1db1
Show file tree
Hide file tree
Showing 9 changed files with 408 additions and 479 deletions.
4 changes: 2 additions & 2 deletions examples/1_example_dk_iter_fixed_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ def example_dk_iter_fixed_order():

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}"))
dkpy.plot_mu(ds, 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}"))
_, ax = dkpy.plot_D(ds, ax=ax, plot_kw=dict(label=f"iter{i}"))

plt.show()

Expand Down
4 changes: 2 additions & 2 deletions examples/2_example_dk_iter_list_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ def example_dk_iter_fixed_order():

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}"))
dkpy.plot_mu(ds, 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}"))
_, ax = dkpy.plot_D(ds, ax=ax, plot_kw=dict(label=f"iter{i}"))

plt.show()

Expand Down
92 changes: 49 additions & 43 deletions examples/3_example_dk_iter_order_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,52 @@
import dkpy


class MyDkIter(dkpy.DkIteration):
"""Custom D-K iteration class with interactive order selection."""

def _get_fit_order(
self,
iteration,
omega,
mu_omega,
D_omega,
P,
K,
block_structure,
):
d_info = []
for fit_order in range(5):
D_fit, D_fit_inv = self.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()
dkpy.plot_mu(d_info[0], ax=ax, plot_kw=dict(label="true"), hide="mu_fit_omega")
for i, ds in enumerate(d_info):
dkpy.plot_mu(ds, ax=ax, plot_kw=dict(label=f"order={i}"), hide="mu_omega")
print("Close plot to continue...")
plt.show()
selected_order_str = input("Select order (<Enter> to end iteration): ")
if selected_order_str == "":
return None
else:
return int(selected_order_str)


def example_dk_iter_fixed_order():
"""D-K iteration with fixed number of iterations and fit order."""
# Plant
Expand Down Expand Up @@ -66,46 +112,7 @@ def example_dk_iter_fixed_order():
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(
dk_iter = MyDkIter(
controller_synthesis=dkpy.HinfSynLmi(
lmi_strictness=1e-7,
solver_params=dict(
Expand All @@ -124,7 +131,6 @@ def callback(
),
),
transfer_function_fit=dkpy.TfFitSlicot(),
fit_order_callback=callback,
)

omega = np.logspace(-3, 3, 61)
Expand All @@ -141,11 +147,11 @@ def callback(

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}"))
dkpy.plot_mu(ds, 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}"))
_, ax = dkpy.plot_D(ds, ax=ax, plot_kw=dict(label=f"iter{i}"))

plt.show()

Expand Down
1 change: 1 addition & 0 deletions src/dkpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
from .controller_synthesis import *
from .dk_iteration import *
from .fit_transfer_functions import *
from .plotting import *
from .structured_singular_value import *
from .utilities import *
Loading

0 comments on commit 56d1db1

Please sign in to comment.