In [1]:
Copied!
import mopeds
import numpy as np
import copy
variable_list = mopeds.VariableList()
variable_list.add_variable(mopeds.VariableAlgebraic("T", 373.0))
variable_list.add_variable(mopeds.VariableParameter("A", 3.55959, -1e3, 1e3))
variable_list.add_variable(mopeds.VariableParameter("B", 643.748, -1e3, 1e3))
variable_list.add_variable(mopeds.VariableParameter("C", -198.043, -1e4, 1e4))
variable_list.add_variable(mopeds.VariableControl("p", 1.0, 0.5, 3))
m = mopeds.Model(variable_list)
T = m.varlist_all["T"].casadi_var
A = m.varlist_all["A"].casadi_var
B = m.varlist_all["B"].casadi_var
C = m.varlist_all["C"].casadi_var
p = m.varlist_all["p"].casadi_var
EQ_alg1 = p - 10 ** (A - (B / (T + C)))
list_algebraic_equations = [EQ_alg1]
m.add_equations_algebraic(list_algebraic_equations)
import mopeds
import numpy as np
import copy
variable_list = mopeds.VariableList()
variable_list.add_variable(mopeds.VariableAlgebraic("T", 373.0))
variable_list.add_variable(mopeds.VariableParameter("A", 3.55959, -1e3, 1e3))
variable_list.add_variable(mopeds.VariableParameter("B", 643.748, -1e3, 1e3))
variable_list.add_variable(mopeds.VariableParameter("C", -198.043, -1e4, 1e4))
variable_list.add_variable(mopeds.VariableControl("p", 1.0, 0.5, 3))
m = mopeds.Model(variable_list)
T = m.varlist_all["T"].casadi_var
A = m.varlist_all["A"].casadi_var
B = m.varlist_all["B"].casadi_var
C = m.varlist_all["C"].casadi_var
p = m.varlist_all["p"].casadi_var
EQ_alg1 = p - 10 ** (A - (B / (T + C)))
list_algebraic_equations = [EQ_alg1]
m.add_equations_algebraic(list_algebraic_equations)
Providing data¶
Data must be provided using a variable list by specifying a value for algebraic variables that are being measured. Algebraic variables without values are ignored. Assuming we have pressure and temperature values in the lists artificial_controls and artificial_data, we need the following code to provide data.
In [2]:
Copied!
artificial_controls = [
0.5,
0.7777777777777778,
1.0555555555555556,
1.3333333333333335,
1.6111111111111112,
1.8888888888888888,
2.166666666666667,
2.4444444444444446,
2.7222222222222223,
3.0,
]
artificial_data = [
364.2997132128273,
373.87219318788704,
380.0266106144029,
385.3865747086218,
388.8613013531244,
393.87775354360167,
396.4967576258504,
400.0184640940616,
403.5469424859316,
407.15753640547916,
]
list_varlist = []
for p, T in zip(artificial_controls, artificial_data):
varlist_new = copy.deepcopy(variable_list)
varlist_new["p"].value = p
varlist_new["T"].value = T
list_varlist.append(varlist_new)
list_varlist[0]["A"].fixed = False
list_varlist[0]["C"].fixed = False
pe = mopeds.ParameterEstimationNLE(m, list_varlist)
res_pe = pe.optimize()
print("Resulting parameters: ", res_pe["x_dict"])
artificial_controls = [
0.5,
0.7777777777777778,
1.0555555555555556,
1.3333333333333335,
1.6111111111111112,
1.8888888888888888,
2.166666666666667,
2.4444444444444446,
2.7222222222222223,
3.0,
]
artificial_data = [
364.2997132128273,
373.87219318788704,
380.0266106144029,
385.3865747086218,
388.8613013531244,
393.87775354360167,
396.4967576258504,
400.0184640940616,
403.5469424859316,
407.15753640547916,
]
list_varlist = []
for p, T in zip(artificial_controls, artificial_data):
varlist_new = copy.deepcopy(variable_list)
varlist_new["p"].value = p
varlist_new["T"].value = T
list_varlist.append(varlist_new)
list_varlist[0]["A"].fixed = False
list_varlist[0]["C"].fixed = False
pe = mopeds.ParameterEstimationNLE(m, list_varlist)
res_pe = pe.optimize()
print("Resulting 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.............: 3
Total number of variables............................: 2
variables with only lower bounds: 0
variables with lower and upper bounds: 2
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 4.7634132e+00 0.00e+00 1.00e+02 -1.0 0.00e+00 - 0.00e+00 0.00e+00 0
1 2.8313867e+00 0.00e+00 1.58e+00 -1.0 3.68e-05 - 1.00e+00 1.00e+00f 1
2 2.8228900e+00 0.00e+00 5.88e-02 -1.0 2.08e-05 - 1.00e+00 1.00e+00f 1
3 2.8228805e+00 0.00e+00 6.73e-05 -2.5 6.98e-07 - 1.00e+00 1.00e+00f 1
4 2.8228805e+00 0.00e+00 3.10e-08 -3.8 1.61e-08 - 1.00e+00 1.00e+00f 1
5 2.8228805e+00 0.00e+00 8.30e-11 -5.7 8.83e-10 - 1.00e+00 1.00e+00f 1
6 2.8228805e+00 0.00e+00 7.26e-08 -8.6 1.09e-11 - 1.00e+00 7.81e-03f 8
7 2.8228805e+00 0.00e+00 7.21e-08 -8.6 1.09e-11 - 1.00e+00 7.81e-03f 8
8 2.8228805e+00 0.00e+00 7.21e-08 -8.6 1.08e-11 - 1.00e+00 1.53e-05f 17
9 2.8228805e+00 0.00e+00 6.31e-08 -8.6 1.08e-11 - 1.00e+00 1.25e-01f 4
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
10 2.8228805e+00 0.00e+00 6.11e-08 -8.6 9.43e-12 - 1.00e+00 3.12e-02f 6
11 2.8228805e+00 0.00e+00 7.35e-12 -8.6 9.13e-12 - 1.00e+00 1.00e+00f 1
Number of Iterations....: 11
(scaled) (unscaled)
Objective...............: 5.6218825028583755e-04 2.8228805112849549e+00
Dual infeasibility......: 7.3538146474445766e-12 3.6925247088174665e-08
Constraint violation....: 0.0000000000000000e+00 0.0000000000000000e+00
Variable bound violation: 0.0000000000000000e+00 0.0000000000000000e+00
Complementarity.........: 2.5059035596800622e-09 1.2582735975331793e-05
Overall NLP error.......: 2.5059035596800622e-09 1.2582735975331793e-05
Number of objective function evaluations = 70
Number of objective gradient evaluations = 12
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 = 11
Total seconds in IPOPT = 0.914
EXIT: Optimal Solution Found.
solver : t_proc (avg) t_wall (avg) n_eval
nlp_f | 676.74ms ( 9.67ms) 677.14ms ( 9.67ms) 70
nlp_grad_f | 126.72ms ( 9.75ms) 126.71ms ( 9.75ms) 13
nlp_hess_l | 106.69ms ( 9.70ms) 106.80ms ( 9.71ms) 11
total | 913.54ms (913.54ms) 913.73ms (913.73ms) 1
Resulting parameters: {'A': 3.5771581568174238, 'C': -198.6257634149538}
Parameter Uncertainty¶
Approximated parameter uncertainty can be accessed using the parameter analysis method.
In [3]:
Copied!
pe.parameter_analysis(res_pe["x_dict"])
pe.parameter_analysis(res_pe["x_dict"])
OLS std: [0.59402026] [0.04194976 2.40232875] 3.5771581568174238 +- 0.0967363150649393 | (2.704278391509673%) -198.6257634149538 +- 5.539780034013988 | (-2.7890541180404185%)
Out[3]:
array([0.09673632, 5.53978003])
Be careful, the parameter uncertainty is calculated using the measurement error provided by the `.variance' attribute of the variables. So if we change the variance, we get a different estimate of the parameter error.
In [4]:
Copied!
for varlist in list_varlist:
varlist["T"].variance = 10
pe_big_variance = mopeds.ParameterEstimationNLE(m, list_varlist)
pe_big_variance.parameter_analysis(res_pe["x_dict"])
for varlist in list_varlist:
varlist["T"].variance = 10
pe_big_variance = mopeds.ParameterEstimationNLE(m, list_varlist)
pe_big_variance.parameter_analysis(res_pe["x_dict"])
OLS std: [0.59402026] [0.13265678 7.59683054] 3.5771581568174238 +- 0.30590708805686806 | (8.551679144347137%) -198.6257634149538 +- 17.518322643809295 | (-8.819763530479856%)
Out[4]:
array([ 0.30590709, 17.51832264])
Artificial Data Generation¶
The data used above is generated using "true parameters" and some perturbations.
In [5]:
Copied!
sim = mopeds.SimulatorNLE(m, variable_list)
artificial_data = []
artificial_controls = []
for val in np.linspace(0.5, 3, 10):
artificial_controls.append(val)
sim.change_independent_variables({"p": val})
artificial_data.append(sim.simulate()[2]["T"].value[0] + np.random.normal())
print("Temperatures: ", artificial_data)
print("Pressures: ", artificial_controls)
sim = mopeds.SimulatorNLE(m, variable_list)
artificial_data = []
artificial_controls = []
for val in np.linspace(0.5, 3, 10):
artificial_controls.append(val)
sim.change_independent_variables({"p": val})
artificial_data.append(sim.simulate()[2]["T"].value[0] + np.random.normal())
print("Temperatures: ", artificial_data)
print("Pressures: ", artificial_controls)
Temperatures: [364.7582846203865, 373.47121619664534, 380.7924827298468, 386.58244250656014, 390.7867393487391, 395.5345806104972, 396.13515423714864, 400.81522162050726, 404.4786163973101, 406.1150392136371] Pressures: [0.5, 0.7777777777777778, 1.0555555555555556, 1.3333333333333335, 1.6111111111111112, 1.8888888888888888, 2.166666666666667, 2.4444444444444446, 2.7222222222222223, 3.0]