Parameter Estimation DAE¶
To do parameter estimation, we use yeast growth model from previous example. For parameter estimation we need: model and experimental data. Time grid is not needed, it will be build automatically based on provided data. Model is defined exactly as in previous section.
Model creation¶
InĀ [1]:
Copied!
import mopeds
import numpy as np
import matplotlib.pyplot as plt
variable_list = mopeds.variables.VariableList()
variable_list.add_variable(mopeds.VariableState("x1", 5, 0, 10))
variable_list.add_variable(mopeds.VariableState("x2", 0.01))
variable_list.add_variable(mopeds.VariableAlgebraic("r", 1.7))
variable_list.add_variable(mopeds.VariableControl("u1", 0.125, 0.05, 0.2))
variable_list.add_variable(mopeds.VariableControl("u2", 35, 5, 35))
variable_list.add_variable(mopeds.VariableParameter("theta1", 0.310, 1e-2, 2))
variable_list.add_variable(mopeds.VariableParameter("theta2", 0.180, 1e-2, 20))
variable_list.add_variable(mopeds.VariableParameter("theta3", 0.550, 1e-2, 2))
variable_list.add_variable(mopeds.VariableParameter("theta4", 0.050, 1e-2, 2))
m = mopeds.Model(variable_list)
x1 = m.varlist_all["x1"].casadi_var
x2 = m.varlist_all["x2"].casadi_var
r = m.varlist_all["r"].casadi_var
u1 = m.varlist_all["u1"].casadi_var
u2 = m.varlist_all["u2"].casadi_var
theta1 = m.varlist_all["theta1"].casadi_var
theta2 = m.varlist_all["theta2"].casadi_var
theta3 = m.varlist_all["theta3"].casadi_var
theta4 = m.varlist_all["theta4"].casadi_var
eq_alg1 = r - (theta1 * x2 / (theta2 + x2))
eq1 = (r - u1 - theta4) * x1
eq2 = -(r * x1 / theta3) + u1 * (u2 - x2)
m.add_equations_differential([eq1, eq2])
m.add_equations_algebraic([eq_alg1])
import mopeds
import numpy as np
import matplotlib.pyplot as plt
variable_list = mopeds.variables.VariableList()
variable_list.add_variable(mopeds.VariableState("x1", 5, 0, 10))
variable_list.add_variable(mopeds.VariableState("x2", 0.01))
variable_list.add_variable(mopeds.VariableAlgebraic("r", 1.7))
variable_list.add_variable(mopeds.VariableControl("u1", 0.125, 0.05, 0.2))
variable_list.add_variable(mopeds.VariableControl("u2", 35, 5, 35))
variable_list.add_variable(mopeds.VariableParameter("theta1", 0.310, 1e-2, 2))
variable_list.add_variable(mopeds.VariableParameter("theta2", 0.180, 1e-2, 20))
variable_list.add_variable(mopeds.VariableParameter("theta3", 0.550, 1e-2, 2))
variable_list.add_variable(mopeds.VariableParameter("theta4", 0.050, 1e-2, 2))
m = mopeds.Model(variable_list)
x1 = m.varlist_all["x1"].casadi_var
x2 = m.varlist_all["x2"].casadi_var
r = m.varlist_all["r"].casadi_var
u1 = m.varlist_all["u1"].casadi_var
u2 = m.varlist_all["u2"].casadi_var
theta1 = m.varlist_all["theta1"].casadi_var
theta2 = m.varlist_all["theta2"].casadi_var
theta3 = m.varlist_all["theta3"].casadi_var
theta4 = m.varlist_all["theta4"].casadi_var
eq_alg1 = r - (theta1 * x2 / (theta2 + x2))
eq1 = (r - u1 - theta4) * x1
eq2 = -(r * x1 / theta3) + u1 * (u2 - x2)
m.add_equations_differential([eq1, eq2])
m.add_equations_algebraic([eq_alg1])
InĀ [2]:
Copied!
data = [[5, 7.098, 10.135, 12.108, 12.491], [0.01, 6.683, 5.860, 3.209, 2.993]]
exp_data = []
time_grid = [0, 5, 10, 15, 20]
variable_list["x1"].set_dataframe_from_value_and_time(data[0], time_grid)
variable_list["x2"].set_dataframe_from_value_and_time(data[1], time_grid)
variable_list["theta1"].fixed = False
variable_list["theta2"].fixed = False
variable_list["theta3"].fixed = False
variable_list["theta4"].fixed = False
pe = mopeds.ParameterEstimation(m, [variable_list])
res_pe = pe.optimize()
print("Estimated parameters", res_pe["x_dict"])
data = [[5, 7.098, 10.135, 12.108, 12.491], [0.01, 6.683, 5.860, 3.209, 2.993]]
exp_data = []
time_grid = [0, 5, 10, 15, 20]
variable_list["x1"].set_dataframe_from_value_and_time(data[0], time_grid)
variable_list["x2"].set_dataframe_from_value_and_time(data[1], time_grid)
variable_list["theta1"].fixed = False
variable_list["theta2"].fixed = False
variable_list["theta3"].fixed = False
variable_list["theta4"].fixed = False
pe = mopeds.ParameterEstimation(m, [variable_list])
res_pe = pe.optimize()
print("Estimated parameters", res_pe["x_dict"])
******************************************************************************
This program contains Ipopt, a library for large-scale nonlinear optimization.
Ipopt is released as open source code under the Eclipse Public License (EPL).
For more information visit https://github.com/coin-or/Ipopt
******************************************************************************
This is Ipopt version 3.14.11, running with linear solver MUMPS 5.4.1.
Number of nonzeros in equality constraint Jacobian...: 0
Number of nonzeros in inequality constraint Jacobian.: 0
Number of nonzeros in Lagrangian Hessian.............: 10
Total number of variables............................: 4
variables with only lower bounds: 0
variables with lower and upper bounds: 4
variables with only upper bounds: 0
Total number of equality constraints.................: 0
Total number of inequality constraints...............: 0
inequality constraints with only lower bounds: 0
inequality constraints with lower and upper bounds: 0
inequality constraints with only upper bounds: 0
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
0 6.8392878e+01 0.00e+00 1.00e+02 -1.0 0.00e+00 - 0.00e+00 0.00e+00 0
1 2.2486784e+01 0.00e+00 1.01e+02 -1.0 1.81e-02 2.0 1.00e+00 1.00e+00f 1
2 1.5744626e+01 0.00e+00 6.89e+01 -1.0 2.37e-01 1.5 1.00e+00 1.74e-01f 2
3 1.0138404e+01 0.00e+00 2.36e+01 -1.0 2.70e-01 1.9 1.86e-01 2.81e-02f 6
4 4.9311371e+00 0.00e+00 3.03e+01 -1.0 1.78e-01 - 4.56e-01 1.00e+00f 1
5 2.6086530e+00 0.00e+00 7.72e+00 -1.0 5.84e-02 1.5 9.30e-01 1.00e+00f 1
6 2.3652818e+00 0.00e+00 8.87e-01 -1.0 6.86e-02 - 1.00e+00 1.00e+00f 1
7 1.9326672e+00 0.00e+00 1.11e+01 -1.7 1.55e-01 - 8.65e-01 1.00e+00f 1
8 1.7797263e+00 0.00e+00 6.02e+00 -1.7 4.20e-02 - 1.00e+00 1.00e+00f 1
9 1.5510593e+00 0.00e+00 1.81e+00 -1.7 1.68e-01 - 1.00e+00 3.84e-01f 2
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
10 1.5364632e+00 0.00e+00 1.04e+00 -1.7 3.39e-02 - 1.00e+00 1.00e+00f 1
11 1.5991152e+00 0.00e+00 1.50e-01 -1.7 2.81e-02 - 1.00e+00 1.00e+00f 1
12 1.5196970e+00 0.00e+00 6.87e-01 -2.5 3.75e-02 - 1.00e+00 1.00e+00f 1
13 1.5046313e+00 0.00e+00 1.43e-01 -2.5 1.19e-02 - 1.00e+00 1.00e+00f 1
14 1.5015580e+00 0.00e+00 2.90e-02 -2.5 7.18e-03 - 1.00e+00 1.00e+00f 1
15 1.5013817e+00 0.00e+00 2.24e-04 -2.5 7.08e-04 - 1.00e+00 1.00e+00f 1
16 1.4948217e+00 0.00e+00 1.10e-01 -3.8 1.22e-02 - 1.00e+00 1.00e+00f 1
17 1.4939967e+00 0.00e+00 2.06e-02 -3.8 4.80e-03 - 1.00e+00 1.00e+00f 1
18 1.4939218e+00 0.00e+00 1.02e-03 -3.8 1.21e-03 - 1.00e+00 1.00e+00f 1
19 1.4938543e+00 0.00e+00 2.86e-03 -5.7 1.70e-03 - 1.00e+00 1.00e+00f 1
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
20 1.4938538e+00 0.00e+00 2.38e-05 -5.7 1.69e-04 - 1.00e+00 1.00e+00f 1
21 1.4938538e+00 0.00e+00 1.48e-05 -5.7 2.19e-06 - 1.00e+00 6.10e-05f 15
22 1.4938538e+00 0.00e+00 7.90e-07 -8.6 2.72e-05 - 1.00e+00 1.00e+00f 1
23 1.4938538e+00 0.00e+00 1.04e-08 -8.6 3.16e-07 - 1.00e+00 1.00e+00f 1
24 1.4938538e+00 0.00e+00 2.11e-07 -9.0 2.27e-08 - 1.00e+00 3.91e-03f 9
25 1.4938538e+00 0.00e+00 2.11e-07 -9.0 2.25e-08 - 1.00e+00 1.22e-04f 14
26 1.4938538e+00 0.00e+00 2.10e-07 -9.0 2.26e-08 - 1.00e+00 1.95e-03f 10
27 1.4938538e+00 0.00e+00 2.10e-07 -9.0 2.24e-08 - 1.00e+00 1.95e-03f 10
28 1.4938538e+00 0.00e+00 2.10e-07 -9.0 2.25e-08 - 1.00e+00 1.22e-04f 14
29 1.4938538e+00 0.00e+00 2.10e-07 -9.0 2.24e-08 - 1.00e+00 3.91e-03f 9
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
30 1.4938538e+00 0.00e+00 2.10e-07 -9.0 2.24e-08 - 1.00e+00 1.86e-09f 30
31 1.4938538e+00 0.00e+00 2.10e-07 -9.0 2.24e-08 - 1.00e+00 1.86e-09f 30
32 1.4938538e+00 0.00e+00 2.10e-07 -9.0 2.24e-08 - 1.00e+00 1.86e-09f 30
33 1.4938538e+00 0.00e+00 2.10e-07 -9.0 2.24e-08 - 1.00e+00 1.86e-09f 30
34 1.4938538e+00 0.00e+00 2.98e-09 -9.0 2.24e-08 - 1.00e+00 1.00e+00w 1
Number of Iterations....: 34
(scaled) (unscaled)
Objective...............: 1.4157633711858381e-01 1.4938537842181558e+00
Dual infeasibility......: 2.9785229662781988e-09 3.1428117827547043e-08
Constraint violation....: 0.0000000000000000e+00 0.0000000000000000e+00
Variable bound violation: 0.0000000000000000e+00 0.0000000000000000e+00
Complementarity.........: 9.0909090909091303e-10 9.5923437657967330e-09
Overall NLP error.......: 2.9785229662781988e-09 3.1428117827547043e-08
Number of objective function evaluations = 288
Number of objective gradient evaluations = 35
Number of equality constraint evaluations = 0
Number of inequality constraint evaluations = 0
Number of equality constraint Jacobian evaluations = 0
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations = 34
Total seconds in IPOPT = 0.637
EXIT: Optimal Solution Found.
solver : t_proc (avg) t_wall (avg) n_eval
nlp_f | 118.03ms (409.83us) 118.12ms (410.15us) 288
nlp_grad_f | 86.90ms ( 2.41ms) 86.80ms ( 2.41ms) 36
nlp_hess_l | 423.88ms ( 12.47ms) 424.20ms ( 12.48ms) 34
total | 637.49ms (637.49ms) 637.54ms (637.54ms) 1
Estimated parameters {'theta1': 0.37625893247606423, 'theta2': 4.27385159837609, 'theta3': 0.4680759752834037, 'theta4': 0.01781821022084129}
Plotting results¶
There are different ways to plot the results of parameter estimation. For example, one can use only information stored in pe object.
InĀ [3]:
Copied!
sim_data = pe.calculate_objective_and_residual(res_pe["x_dict"])["y"]
exp_data = pe.array_data
for var_index in [0, 1]:
plt.figure()
time_grid = pe.list_simulators[0].time_grid_relative
value_at_time0 = pe.list_simulators[0]._initial_state[var_index]
all_simulated_values = np.insert(sim_data[:, var_index], 0, value_at_time0)
fig = plt.plot(
time_grid, all_simulated_values, label=pe.names_of_measurements[var_index]
)
plt.scatter(time_grid[1:], exp_data[:, var_index], c="r", label="exp. data")
plt.figlegend()
sim_data = pe.calculate_objective_and_residual(res_pe["x_dict"])["y"]
exp_data = pe.array_data
for var_index in [0, 1]:
plt.figure()
time_grid = pe.list_simulators[0].time_grid_relative
value_at_time0 = pe.list_simulators[0]._initial_state[var_index]
all_simulated_values = np.insert(sim_data[:, var_index], 0, value_at_time0)
fig = plt.plot(
time_grid, all_simulated_values, label=pe.names_of_measurements[var_index]
)
plt.scatter(time_grid[1:], exp_data[:, var_index], c="r", label="exp. data")
plt.figlegend()
To have more control, one can create a simulation object with given time grid, for that one needs to supply correct parameter variables.
InĀ [4]:
Copied!
sim = mopeds.Simulator(m, np.linspace(0, 20, 100), variable_list)
# sim.change_independent_variables(res_pe["x_dict"])
res_sim = sim.simulate(unfixed_variables=res_pe["x_dict"])[2]
axes = res_sim.plot(show=False)
for meas_index, meas_name in enumerate(pe.names_of_measurements):
axes[meas_index].scatter(
x=variable_list[meas_name].time_absolute, y=variable_list[meas_name].value
)
plt.show()
sim = mopeds.Simulator(m, np.linspace(0, 20, 100), variable_list)
# sim.change_independent_variables(res_pe["x_dict"])
res_sim = sim.simulate(unfixed_variables=res_pe["x_dict"])[2]
axes = res_sim.plot(show=False)
for meas_index, meas_name in enumerate(pe.names_of_measurements):
axes[meas_index].scatter(
x=variable_list[meas_name].time_absolute, y=variable_list[meas_name].value
)
plt.show()