-
Notifications
You must be signed in to change notification settings - Fork 1
/
gp_nmpc_cstr_output_feedback.py
280 lines (237 loc) · 8.5 KB
/
gp_nmpc_cstr_output_feedback.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#
# HILO-MPC is developed by Johannes Pohlodek and Bruno Morabito under the supervision of Prof. Rolf Findeisen
# at the Control and cyber-physical systems laboratory, TU Darmstadt (https://www.ccps.tu-darmstadt.de/ccp) and at the
# Laboratory for Systems Theory and Control, Otto von Guericke University (http://ifatwww.et.uni-magdeburg.de/syst/).
#
import numpy as np
from hilo_mpc import NMPC, SimpleControlLoop, GP, SquaredExponentialKernel, Model, set_plot_backend
from hilo_mpc.library import cstr_seborg
# === Process / Plant Model =======================================================================================
# Equilibria
x0_plant = [0.6205, 348, 347.5] # initial point
y0 = x0_plant[0]
u0 = x0_plant[2]
x_ref = [0.4674, 355.9, 356]
y_ref = x_ref[0]
u_ref = x_ref[2]
# Constraints
y_lb = 0.35
y_ub = 0.65
u_lb = 335
u_ub = 372
# Simulation parameters
Ts = 0.5 # sampling time (min)
T = 25 # simulation time (min)
N_step = int(T / Ts) # simulation time steps
# Set up plant
set_plot_backend('bokeh')
plant = cstr_seborg()
# Discretize model
plant.discretize('erk', inplace=True)
# Initialize model
plant.setup(dt=Ts)
# Plant Parameters
p0 = {
'C_Af': 1,
'C_p': 0.3,
'DeltaH_r': 10000,
'E': 9750 * 8.314,
'k_0': 6e10,
'q_0': 10,
'rho': 1100,
'tau': 1.5,
'UA': 70000,
'V': 150,
'T_f': 370
}
# Initial condition
plant.set_initial_conditions(x0=x0_plant)
plant.set_initial_parameter_values(p0)
# === MPC with true dynamics ======================================================================================
nmpc = NMPC(plant)
# Set horizon
nmpc.horizon = 5
# Set cost function ------------------------------
# ...with scaling
# nmpc.set_scaling(x_scaling=[0.5, 350, 350], u_scaling=[350])
# nmpc.quad_stage_cost.add_states(names=['C_A'], weights=[100], trajectory_tracking=True)
# nmpc.quad_stage_cost.add_inputs(names=['T_cr'], weights=[100], trajectory_tracking=True)
# nmpc.quad_terminal_cost.add_states(names=['C_A'], weights=[100], trajectory_tracking=True)
# ...without scaling
nmpc.quad_stage_cost.add_states(names=['C_A'], weights=[100], trajectory_tracking=True)
nmpc.quad_stage_cost.add_inputs(names=['T_cr'], weights=[0.0002], trajectory_tracking=True)
nmpc.quad_terminal_cost.add_states(names=['C_A'], weights=[100], trajectory_tracking=True)
# -------------------------------------------------
# Set constraints
nmpc.set_box_constraints(x_lb=[y_lb, 0, 0], x_ub=[y_ub, 500, 500], u_lb=u_lb, u_ub=u_ub)
# Initial guess
nmpc.set_initial_guess(x_guess=x0_plant, u_guess=u0)
# Set up controller
nmpc.setup(options={'print_level': 0, 'objective_function': 'discrete', 'integration_method': 'discrete'})
# Reference trajectory
ref_trajectory = np.zeros([2, N_step + nmpc.horizon + 1])
ref_trajectory[0, :10] = y0
ref_trajectory[0, 10:] = y_ref
ref_trajectory[1, :10] = u0
ref_trajectory[1, 10:] = u_ref
ref_trajectory_y = ref_trajectory[0, :]
ref_trajectory_u = ref_trajectory[1, :]
# Run the simulation
scl = SimpleControlLoop(plant, nmpc)
scl.run(N_step, p=plant.solution.get_by_id('p:0'), ref_sc={'T_cr': ref_trajectory_u, 'C_A': ref_trajectory_y},
ref_tc={'C_A': ref_trajectory_y})
# Plots
scl.plot(
('t', 'T_cr'),
('t', 'C_A'),
title=('input', 'output'),
figsize=(900, 300),
layout=(2, 1),
background_fill_color='#fafafa',
output_file='gp_nmpc_cstr_output_feedback/nominal_controller.html'
)
# === GP training data ============================================================================================
# Input sequence composition:
# - 5 small chirps
# - 2 large chirps
chirps = {
'type': 'linear',
'amplitude': [6, 6, 6, 6, 6, 18, 18],
'length': [996, 996, 996, 996, 996, 5 * 996, 5 * 996],
'mean': [341, 347, 353, 359, 365, 353, 353],
'chirp_rate': [0.00015, 0.00015, 0.00015, 0.00015, 0.00015, 0.000015, 0.000035]
}
# Standard deviation for noise added to C_A
sigma_n = 5e-3
# Generate data
data_set = plant.generate_data('chirp', shift=2, add_noise={'std': sigma_n, 'seed': 10}, chirps={'T_cr': chirps},
skip=('T', 'T_c'))
data_set.plot_raw_data(
('t', 'T_cr'),
('t', 'C_A_k+1', 'C_A_k', 'C_A_k-1', 'C_A_k-2'),
title=('input', 'output'),
figsize=(900, 300),
layout=(2, 1),
background_fill_color="#fafafa",
output_file='gp_nmpc_cstr_output_feedback/raw_data.html'
)
# data_set.add_noise('C_A_k+1', seed=10, std=sigma_n)
data_set.plot_raw_data(
('t', 'C_A_k+1_noisy'),
title="output (noisy)",
figsize=(900, 300),
background_fill_color="#fafafa",
output_file='gp_nmpc_cstr_output_feedback/raw_noisy_data.html'
)
# Training data reduction
data_set.select_train_data('euclidean_distance', distance_threshold=.59)
data_set.plot_train_data(
('t', 'T_cr'),
('t', 'C_A_k+1_noisy'),
title=('input', 'output'),
figsize=(900, 300),
layout=(2, 1),
background_fill_color="#fafafa",
output_file='gp_nmpc_cstr_output_feedback/train_data.html'
)
# Select test data
data_set.select_test_data('downsample', downsample_factor=20)
data_set.plot_test_data(
('t', 'T_cr'),
('t', 'C_A_k+1_noisy'),
title=('input', 'output'),
figsize=(900, 300),
layout=(2, 1),
background_fill_color="#fafafa",
output_file='gp_nmpc_cstr_output_feedback/test_data.html'
)
# Initialize GP
kernel = SquaredExponentialKernel(active_dims=[1, 2, 3], ard=True)
gp = GP(['x_0', 'x_1', 'x_2', 'u'], ['y'], kernel=kernel, noise_variance=sigma_n ** 2)
gp.noise_variance.fixed = True
# Training of GP
train_in, train_out = data_set.train_data
gp.set_training_data(train_in, train_out)
gp.setup()
gp.fit_model()
# Validate GP model
test_in, test_out = data_set.test_data
gp.plot_prediction_error(
test_in,
test_out,
figsize=(900, 300),
layout=(2, 1),
background_fill_color="#fafafa",
output_file='gp_nmpc_cstr_output_feedback/gp_prediction_error.html'
)
gp.plot(
test_in,
background_fill_color="#fafafa",
xlabel=('y(k)', 'y(k-1)', 'y(k-2)', 'u(k)'),
ylabel=4 * ('y(k+1)', ),
output_file='gp_nmpc_cstr_output_feedback/gp_train_test_scatter.html'
)
# === GP prediction models ========================================================================================
# Initialize empty model
gp_model = Model(plot_backend='bokeh', name='GP_Model', discrete=True)
# Set states and inputs
gp_model.set_dynamical_states('x', 3)
gp_model.set_inputs('u', 1)
# States
# x_0(k) := y(k)
# x_1(k) := y(k-1)
# x_2(k) := y(k-2)
# Set difference equations
# x_0(k+1) = y(k+1) = GP
# x_1(k+1) = y(k) = x_0(k)
# x_2(k+1) = y(k-1) = x_1(k)
gp_model.set_dynamical_equations(['0', 'x_0', 'x_1'])
# Add GP to the ODE's model --> y(k+1) = GP
gp_model += [gp, 0, 0]
# Set up the model
gp_model.setup(dt=Ts)
# Set initial conditions
x0_GP = [y0, y0, y0]
gp_model.set_initial_conditions(x0=x0_GP)
# === GP-MPC (model-plant mismatch) ===============================================================================
# Build controller
gp_nmpc = NMPC(gp_model)
# Set horizon
gp_nmpc.horizon = 5
# Set cost function
weight_x0 = 10
weight_u = weight_x0 / 15e3
gp_nmpc.quad_stage_cost.add_states(names=['x_0'], weights=[weight_x0], trajectory_tracking=True)
gp_nmpc.quad_stage_cost.add_inputs(names=['u'], weights=[weight_u], trajectory_tracking=True)
gp_nmpc.quad_terminal_cost.add_states(names=['x_0'], weights=[weight_x0], trajectory_tracking=True)
# Set constraints
gp_nmpc.set_box_constraints(x_lb=[y_lb, y_lb, y_lb], x_ub=[y_ub, y_ub, y_ub], u_lb=u_lb, u_ub=u_ub)
# Initial guess
gp_nmpc.set_initial_guess(x_guess=x0_GP, u_guess=u0)
# Set up controller
gp_nmpc.setup(options={'print_level': 1, 'objective_function': 'discrete', 'integration_method': 'discrete'})
# Copy previous solution for comparison
nominal_solution = plant.solution.copy()
# Run the simulation
# NOTE: SimpleControlLoop won't work here, since the model used in the MPC differs too much from the plant model.
# Maybe in future version an advanced control loop class will be available.
plant.reset_solution()
xk = x0_GP
for _ in range(N_step):
u = gp_nmpc.optimize(x0=xk, ref_sc={'u': ref_trajectory[1, :], 'x_0': ref_trajectory[0, :]},
ref_tc={'x_0': ref_trajectory[0, :]})
plant.simulate(u=u)
xk = [plant.solution.get_by_id('x:f')[0], xk[0], xk[1]] # xk = [y_k, y_{k-1}, y_{k-2}]
# Plots
plant.solution.plot(
('t', 'T_cr'),
('t', 'C_A'),
data=nominal_solution,
data_suffix='nominal',
title=('input', 'output'),
figsize=(900, 300),
layout=(2, 1),
line_width=2,
background_fill_color='#fafafa',
output_file='gp_nmpc_cstr_output_feedback/gp_mpc_vs_nominal_mpc.html'
)