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)
OED instance creation and optimization¶
First, we need to signal OED which parameters will be used to create an objective function by unfixing them and selecting control variables that will be a decision variable for optimization.
variable_list["A"].fixed = False
variable_list["p"].fixed = False
oed = mopeds.OptimalExperimentalDesign_NLE(m, [variable_list])
res_oed = oed.optimize()
print("Pressure value for next experiment", res_oed["x_dict"])
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.............: 0
Total number of variables............................: 1
variables with only lower bounds: 0
variables with lower and upper bounds: 1
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 3.0575120e-05 0.00e+00 1.49e-05 0.0 0.00e+00 - 0.00e+00 0.00e+00 0
1 3.0575048e-05 0.00e+00 4.90e-06 -5.9 4.80e-06 - 9.90e-01 1.00e+00f 1
2 2.4138808e-05 0.00e+00 5.13e-06 -2.0 6.00e-01 - 1.00e+00 1.00e+00f 1
3 2.4134122e-05 0.00e+00 5.70e-08 -8.0 6.00e-04 - 9.89e-01 1.00e+00f 1
4 2.3670966e-05 0.00e+00 1.61e-08 -5.4 6.09e-02 - 9.95e-01 1.00e+00f 1
5 2.0016192e-05 0.00e+00 1.35e-06 -7.4 6.17e-01 - 1.00e+00 1.00e+00f 1
6 1.7863610e-05 0.00e+00 1.04e-06 -7.8 5.23e-01 - 1.00e+00 1.00e+00f 1
7 1.7193449e-05 0.00e+00 2.15e-07 -8.7 2.36e-01 - 1.00e+00 8.39e-01f 1
8 1.7193459e-05 0.00e+00 4.02e-13 -11.0 3.10e-06 - 1.00e+00 1.00e+00f 1
Number of Iterations....: 8
(scaled) (unscaled)
Objective...............: 1.7193458798262951e-05 1.7193458798262951e-05
Dual infeasibility......: 4.0162616113764119e-13 4.0162616113764119e-13
Constraint violation....: 0.0000000000000000e+00 0.0000000000000000e+00
Variable bound violation: 0.0000000000000000e+00 0.0000000000000000e+00
Complementarity.........: 1.0662728357094591e-11 1.0662728357094591e-11
Overall NLP error.......: 1.0662728357094591e-11 1.0662728357094591e-11
Number of objective function evaluations = 9
Number of objective gradient evaluations = 9
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 = 0
Total seconds in IPOPT = 0.162
EXIT: Optimal Solution Found.
solver : t_proc (avg) t_wall (avg) n_eval
nlp_f | 41.52ms ( 4.61ms) 42.25ms ( 4.69ms) 9
nlp_grad_f | 104.30ms ( 10.43ms) 106.06ms ( 10.61ms) 10
total | 159.95ms (159.95ms) 162.48ms (162.48ms) 1
Pressure value for next experiment {'p': 2.999996728740012}
The optimizer chose the highest possible pressure, which was defined by a upper bound of the variable p.
To change the bounds, you can create a new OED with a new variable list:
variable_list["A"].fixed = False
variable_list["p"].fixed = False
variable_list["p"].upper_bound = 10
oed = mopeds.OptimalExperimentalDesign_NLE(m, [variable_list])
res_oed = oed.optimize()
print("Pressure value for next experiment", res_oed["x_dict"])
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.............: 0
Total number of variables............................: 1
variables with only lower bounds: 0
variables with lower and upper bounds: 1
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 3.0575120e-05 0.00e+00 1.49e-05 0.0 0.00e+00 - 0.00e+00 0.00e+00 0
1 3.0575005e-05 0.00e+00 7.75e-06 -5.3 7.68e-06 - 9.90e-01 1.00e+00f 1
2 1.3772832e-05 0.00e+00 5.62e-05 -1.4 3.40e+00 - 1.00e+00 1.00e+00f 1
3 1.3771837e-05 0.00e+00 5.72e-07 -7.4 5.34e-04 - 9.90e-01 1.00e+00f 1
4 1.2566841e-05 0.00e+00 7.36e-08 -3.5 7.22e-01 - 1.00e+00 1.00e+00f 1
5 1.2485856e-05 0.00e+00 5.15e-09 -5.6 5.46e-02 - 9.96e-01 1.00e+00f 1
6 1.0167321e-05 0.00e+00 2.81e-07 -7.3 2.01e+00 - 1.00e+00 1.00e+00f 1
7 8.7834459e-06 0.00e+00 2.47e-07 -8.1 1.80e+00 - 1.00e+00 1.00e+00f 1
8 8.1742871e-06 0.00e+00 7.45e-08 -8.8 1.20e+00 - 1.00e+00 8.47e-01f 1
9 8.1742984e-06 0.00e+00 2.39e-13 -11.0 2.03e-05 - 1.00e+00 1.00e+00f 1
Number of Iterations....: 9
(scaled) (unscaled)
Objective...............: 8.1742983768165506e-06 8.1742983768165506e-06
Dual infeasibility......: 2.3905522265057165e-13 2.3905522265057165e-13
Constraint violation....: 0.0000000000000000e+00 0.0000000000000000e+00
Variable bound violation: 0.0000000000000000e+00 0.0000000000000000e+00
Complementarity.........: 1.1513821816310703e-11 1.1513821816310703e-11
Overall NLP error.......: 1.1513821816310703e-11 1.1513821816310703e-11
Number of objective function evaluations = 10
Number of objective gradient evaluations = 10
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 = 0
Total seconds in IPOPT = 0.220
EXIT: Optimal Solution Found.
solver : t_proc (avg) t_wall (avg) n_eval
nlp_f | 59.61ms ( 5.96ms) 61.06ms ( 6.11ms) 10
nlp_grad_f | 120.45ms ( 10.95ms) 135.93ms ( 12.36ms) 11
total | 204.12ms (204.12ms) 221.03ms (221.03ms) 1
Pressure value for next experiment {'p': 9.999979346403725}
Or modify the oed.upper_bound attribute directly
oed.upper_bound[0] = 2
print(oed.guess)
res_oed = oed.optimize()
print("Pressure value for next experiment", res_oed["x_dict"])
[1.]
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.............: 0
Total number of variables............................: 1
variables with only lower bounds: 0
variables with lower and upper bounds: 1
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 3.0575120e-05 0.00e+00 1.49e-05 0.0 0.00e+00 - 0.00e+00 0.00e+00 0
1 3.0575062e-05 0.00e+00 4.03e-06 -6.1 3.92e-06 - 9.90e-01 1.00e+00f 1
2 2.7938996e-05 0.00e+00 7.81e-07 -2.2 2.00e-01 - 1.00e+00 1.00e+00f 1
3 2.7932708e-05 0.00e+00 7.27e-09 -8.2 5.41e-04 - 9.93e-01 1.00e+00f 1
4 2.7107690e-05 0.00e+00 5.89e-08 -5.5 7.41e-02 - 9.92e-01 1.00e+00f 1
5 2.3611575e-05 0.00e+00 1.56e-06 -7.5 3.95e-01 - 1.00e+00 1.00e+00f 1
6 2.1761381e-05 0.00e+00 9.33e-07 -7.9 2.80e-01 - 1.00e+00 1.00e+00f 1
7 2.1471933e-05 0.00e+00 8.06e-08 -9.0 5.59e-02 - 1.00e+00 8.89e-01f 1
8 2.1471943e-05 0.00e+00 2.08e-13 -11.0 1.72e-06 - 1.00e+00 1.00e+00f 1
Number of Iterations....: 8
(scaled) (unscaled)
Objective...............: 2.1471943304937937e-05 2.1471943304937937e-05
Dual infeasibility......: 2.0849722857631476e-13 2.0849722857631476e-13
Constraint violation....: 0.0000000000000000e+00 0.0000000000000000e+00
Variable bound violation: 0.0000000000000000e+00 0.0000000000000000e+00
Complementarity.........: 1.0137897540225856e-11 1.0137897540225856e-11
Overall NLP error.......: 1.0137897540225856e-11 1.0137897540225856e-11
Number of objective function evaluations = 9
Number of objective gradient evaluations = 9
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 = 0
Total seconds in IPOPT = 0.199
EXIT: Optimal Solution Found.
solver : t_proc (avg) t_wall (avg) n_eval
nlp_f | 54.64ms ( 6.07ms) 55.58ms ( 6.18ms) 9
nlp_grad_f | 124.90ms ( 12.49ms) 126.12ms ( 12.61ms) 10
total | 198.25ms (198.25ms) 200.29ms (200.29ms) 1
Pressure value for next experiment {'p': 1.999998248720443}
Adding Previous Experiments¶
If we choose to use more parameters, OED will fail. The objective function cannot be calculated if the number of parameters is higher than the number of measurements. So you have to supply previous measurements. They are constant from the point of view of the objective function, so if no previous measurements exist, randomly chosen values will work.
variable_list["A"].fixed = False
variable_list["B"].fixed = False
variable_list["C"].fixed = False
variable_list["p"].fixed = False
oed = mopeds.OptimalExperimentalDesign_NLE(
m,
[variable_list],
previous_measurements=[
{"p": 1},
{"p": 2},
{"p": 3},
],
)
res_oed = oed.optimize()
print("Pressure value for next experiment", res_oed["x_dict"])
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.............: 0
Total number of variables............................: 1
variables with only lower bounds: 0
variables with lower and upper bounds: 1
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 8.8545497e+00 0.00e+00 1.15e+01 0.0 0.00e+00 - 0.00e+00 0.00e+00 0
1 4.3399085e+00 0.00e+00 8.10e+00 0.4 2.27e+00 - 1.00e+00 1.09e-01f 2
2 1.4933192e+00 0.00e+00 1.04e+01 -2.0 4.60e-01 - 1.00e+00 5.44e-01f 1
3 1.4871134e+00 0.00e+00 1.46e-02 -3.7 9.56e-04 - 1.00e+00 1.00e+00f 1
4 1.4770784e+00 0.00e+00 3.02e-04 -4.4 1.56e-03 - 1.00e+00 9.99e-01f 1
5 1.4769774e+00 0.00e+00 1.60e-06 -10.1 1.57e-05 - 1.00e+00 1.00e+00f 1
6 1.4769774e+00 0.00e+00 1.97e-10 -11.0 4.75e-09 - 1.00e+00 1.00e+00f 1
Number of Iterations....: 6
(scaled) (unscaled)
Objective...............: 1.4769773759842009e+00 1.4769773759842009e+00
Dual infeasibility......: 1.9748899667718156e-10 1.9748899667718156e-10
Constraint violation....: 0.0000000000000000e+00 0.0000000000000000e+00
Variable bound violation: 9.9984475143699569e-09 9.9984475143699569e-09
Complementarity.........: 9.9999937203655524e-12 9.9999937203655524e-12
Overall NLP error.......: 1.9748899667718156e-10 1.9748899667718156e-10
Number of objective function evaluations = 12
Number of objective gradient evaluations = 7
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 = 0
Total seconds in IPOPT = 0.140
EXIT: Optimal Solution Found.
solver : t_proc (avg) t_wall (avg) n_eval
nlp_f | 54.70ms ( 4.56ms) 55.08ms ( 4.59ms) 12
nlp_grad_f | 72.36ms ( 9.04ms) 72.95ms ( 9.12ms) 8
total | 140.25ms (140.25ms) 140.98ms (140.98ms) 1
Pressure value for next experiment {'p': 0.4999999900015525}