-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexample_pipeline_vdp.py
142 lines (121 loc) · 3.55 KB
/
example_pipeline_vdp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
"""Example of how to use the Koopman pipeline."""
import numpy as np
from matplotlib import pyplot as plt
import pykoop
import pykoop.dynamic_models
def main() -> None:
"""Demonstrate how to use the Koopman pipeline."""
# Get sample data
X_vdp = pykoop.example_data_vdp()
# Create pipeline
kp = pykoop.KoopmanPipeline(
lifting_functions=[(
'sp',
pykoop.SplitPipeline(
lifting_functions_state=[
('pl', pykoop.PolynomialLiftingFn(order=3))
],
lifting_functions_input=None,
),
)],
regressor=pykoop.Edmd(),
)
# Take last episode for validation
X_train = X_vdp[X_vdp[:, 0] < 4]
X_valid = X_vdp[X_vdp[:, 0] == 4]
# Fit the pipeline
kp.fit(X_train, n_inputs=1, episode_feature=True)
# Extract initial conditions and input from validation episode
x0 = X_valid[[0], 1:3]
u = X_valid[:, 3:]
# Predict with re-lifting between timesteps (default)
X_pred_local = kp.predict_state(
x0,
u,
relift_state=True,
episode_feature=False,
)
# Predict without re-lifting between timesteps
X_pred_global = kp.predict_state(
x0,
u,
relift_state=False,
episode_feature=False,
)
# Plot trajectories in phase space
fig, ax = plt.subplots(constrained_layout=True)
ax.plot(
X_valid[:, 1],
X_valid[:, 2],
label='True trajectory',
)
ax.plot(
X_pred_local[:, 0],
X_pred_local[:, 1],
'--',
label='Local prediction',
)
ax.plot(
X_pred_global[:, 0],
X_pred_global[:, 1],
'--',
label='Global prediction',
)
ax.set_xlabel('$x_1[k]$')
ax.set_ylabel('$x_2[k]$')
ax.legend()
ax.grid(linestyle='--')
# Lift validation set
Psi_valid = kp.lift(X_valid[:, 1:], episode_feature=False)
# Predict lifted state with re-lifting between timesteps (default)
Psi_pred_local = kp.predict_state(
x0,
u,
relift_state=True,
return_lifted=True,
return_input=True,
episode_feature=False,
)
# Predict lifted state without re-lifting between timesteps
Psi_pred_global = kp.predict_state(
x0,
u,
relift_state=False,
return_lifted=True,
return_input=True,
episode_feature=False,
)
fig, ax = plt.subplots(
kp.n_states_out_,
1,
constrained_layout=True,
sharex=True,
squeeze=False,
)
for i in range(ax.shape[0]):
ax[i, 0].plot(Psi_valid[:, i], label='True trajectory')
ax[i, 0].plot(Psi_pred_local[:, i], '--', label='Local prediction')
ax[i, 0].plot(Psi_pred_global[:, i], '--', label='Global prediction')
ax[i, 0].grid(linestyle='--')
ax[i, 0].set_ylabel(rf'$\vartheta_{i + 1}[k]$')
ax[-1, 0].set_xlabel('$k$')
ax[0, 0].legend()
fig, ax = plt.subplots(
kp.n_inputs_out_,
1,
constrained_layout=True,
sharex=True,
squeeze=False,
)
for i in range(ax.shape[0]):
j = kp.n_states_out_ + i
ax[i, 0].plot(Psi_valid[:, j], label='True trajectory')
ax[i, 0].plot(Psi_pred_local[:, j], '--', label='Local prediction')
ax[i, 0].plot(Psi_pred_global[:, j], '--', label='Global prediction')
ax[i, 0].grid(linestyle='--')
ax[-1, 0].set_xlabel('$k$')
ax[0, 0].legend()
ax[0, 0].set_ylabel('$u[k]$')
plt.show()
if __name__ == '__main__':
main()