exprs = dict() exprs['A'] = -m.P['k1'] * m.Z[t, 'A'] exprs['B'] = m.P['k1'] * m.Z[t, 'A'] - m.P['k2'] * m.Z[t, 'B'] exprs['C'] = m.P['k2'] * m.Z[t, 'B'] return exprs builder.set_odes_rule(rule_odes) opt_model = builder.create_pyomo_model(0.0, 10.0) #========================================================================= #USER INPUT SECTION - VARIANCE ESTIMATION #========================================================================= # For this problem we have an input D matrix that has some noise in it # We can therefore use the variance estimator described in the Overview section # of the documentation and Section 4.3.3 v_estimator = VarianceEstimator(opt_model) v_estimator.apply_discretization('dae.collocation', nfe=60, ncp=1, scheme='LAGRANGE-RADAU') # It is often requried for larger problems to give the solver some direct instructions # These must be given in the form of a dictionary options = {} # While this problem should solve without changing the deault options, example code is # given commented out below. See Section 5.6 for more options and advice. # options['bound_push'] = 1e-8 # options['tol'] = 1e-9 # The set A_set is then decided. This set, explained in Section 4.3.3 is used to make the # variance estimation run faster and has been shown to not decrease the accuracy of the variance
exprs['B'] = m.P['k1']*m.Z[t,'A']-m.P['k2']*m.Z[t,'B'] exprs['C'] = m.P['k2']*m.Z[t,'B'] return exprs builder.set_odes_rule(rule_odes) builder.bound_profile(var = 'S', bounds = (0,200)) opt_model = builder.create_pyomo_model(0.0,10.0) #========================================================================= #USER INPUT SECTION - VARIANCE ESTIMATION #========================================================================= # For this problem we have an input D matrix that has some noise in it # We can therefore use the variance estimator described in the Overview section # of the documentation and Section 4.3.3 v_estimator = VarianceEstimator(opt_model) v_estimator.apply_discretization('dae.collocation',nfe=100,ncp=3,scheme='LAGRANGE-RADAU') # It is often requried for larger problems to give the solver some direct instructions # These must be given in the form of a dictionary options = {} # While this problem should solve without changing the deault options, example code is # given commented out below. See Section 5.6 for more options and advice. # options['bound_push'] = 1e-8 # options['tol'] = 1e-9 options['linear_solver'] = 'ma57' options['max_iter'] = 2000 # The set A_set is then decided. This set, explained in Section 4.3.3 is used to make the # variance estimation run faster and has been shown to not decrease the accuracy of the variance # prediction for large noisey data sets.
results_sim.C.plot.line() results_sim.S.plot.line() plt.show() ################################################################################# builder = TemplateBuilder() builder.add_mixture_component(concentrations) builder.add_parameter('k', bounds=(0.0, 1.0)) builder.add_spectral_data(results_sim.D) builder.set_odes_rule(rule_odes) opt_model = builder.create_pyomo_model(0.0, 200.0) v_estimator = VarianceEstimator(opt_model) v_estimator.apply_discretization('dae.collocation', nfe=4, ncp=1, scheme='LAGRANGE-RADAU') v_estimator.initialize_from_trajectory('Z', results_sim.Z) v_estimator.initialize_from_trajectory('S', results_sim.S) v_estimator.scale_variables_from_trajectory('Z', results_sim.Z) v_estimator.scale_variables_from_trajectory('S', results_sim.S) options = {} #{'mu_init': 1e-6, 'bound_push': 1e-6} results_variances = v_estimator.run_opt('ipopt', tee=True, solver_options=options,
# define explicit system of ODEs def rule_odes2(m, t): exprs = dict() exprs['A'] = -m.P['k1'] * m.Z[t, 'A'] * m.Z[t, 'B'] exprs['B'] = -m.P['k1'] * m.Z[t, 'A'] * m.Z[t, 'B'] exprs['C'] = m.P['k1'] * m.Z[t, 'A'] * m.Z[t, 'B'] return exprs builder.set_odes_rule(rule_odes2) pyomo_model2 = builder.create_pyomo_model(0.0, 1077.9) #pyomo_model2.P['k1'].value = 0.006655 #pyomo_model2.P['k1'].fixed = True v_estimator = VarianceEstimator(pyomo_model2) v_estimator.apply_discretization('dae.collocation', nfe=60, ncp=3, scheme='LAGRANGE-RADAU') # Provide good initial guess p_guess = {'k1': 0.006655} raw_results = v_estimator.run_lsq_given_P('ipopt', p_guess, tee=False) v_estimator.initialize_from_trajectory('Z', raw_results.Z) v_estimator.initialize_from_trajectory('S', raw_results.S) v_estimator.initialize_from_trajectory('dZdt', raw_results.dZdt) v_estimator.initialize_from_trajectory('C', raw_results.C)
exprs['A'] = -m.P['k1'] * m.Z[t, 'A'] exprs['B'] = m.P['k1'] * m.Z[t, 'A'] - m.P['k2'] * m.Z[t, 'B'] exprs['C'] = m.P['k2'] * m.Z[t, 'B'] return exprs builder.set_odes_rule(rule_odes) opt_model = builder.create_pyomo_model(0.0, 10.0) #========================================================================= #USER INPUT SECTION - VARIANCE ESTIMATION #========================================================================= # For this problem we have an input D matrix that has some noise in it # We can therefore use the variance estimator described in the Overview section # of the documentation and Section 4.3.3 or we can use the alternative methodology, # whereby we directly solve for the sigmas given different values of device variance v_estimator = VarianceEstimator(opt_model) v_estimator.apply_discretization('dae.collocation', nfe=50, ncp=3, scheme='LAGRANGE-RADAU') # It is often requried for larger problems to give the solver some direct instructions # These must be given in the form of a dictionary options = {} # While this problem should solve without changing the deault options, example code is # given commented out below. See Section 5.6 for more options and advice. # options['bound_push'] = 1e-8 # options['tol'] = 1e-9 options['linear_solver'] = 'ma57' # The set A_set is then decided. This set, explained in Section 4.3.3 is used to make the
plt.title("Temperature") plt.show() #print(results.Y['Temp']) # sys.exit() #========================================================================= #USER INPUT SECTION - Variance Estimation #======================================================================== model=builder.create_pyomo_model(0.0,10.0) #Now introduce parameters as non fixed model.del_component(params) builder.add_parameter('k1',init=1.0,bounds=(0.0,10.0)) builder.add_parameter('k2Tr',init=0.2265,bounds=(0.0, 10.0)) builder.add_parameter('E',2.) model = builder.create_pyomo_model(0, 10) v_estimator = VarianceEstimator(model) v_estimator.apply_discretization('dae.collocation', nfe=50, ncp=3, scheme='LAGRANGE-RADAU') v_estimator.initialize_from_trajectory('Z', results.Z) v_estimator.initialize_from_trajectory('S', results.S) options = {} A_set = [l for i, l in enumerate(model.meas_lambdas) if (i % 4 == 0)] # #: this will take a sweet-long time to solve. results_variances = v_estimator.run_opt('ipopt', tee=True, solver_options=options, tolerance=1e-5, max_iter=15, subset_lambdas=A_set,
exprs['A'] = -m.P['k1'] * m.Z[t, 'A'] exprs['B'] = m.P['k1'] * m.Z[t, 'A'] - m.P['k2'] * m.Z[t, 'B'] exprs['C'] = m.P['k2'] * m.Z[t, 'B'] return exprs builder.set_odes_rule(rule_odes) opt_model = builder.create_pyomo_model(0.0, 10.0) #========================================================================= #USER INPUT SECTION - VARIANCE ESTIMATION #========================================================================= # For this problem we have an input D matrix that has some noise in it # We can therefore use the variance estimator described in the Overview section # of the documentation and Section 4.3.3 or we can use the alternative methodology, # whereby we directly solve for the sigmas given different values of device variance v_estimator = VarianceEstimator(opt_model) v_estimator.apply_discretization('dae.collocation', nfe=50, ncp=3, scheme='LAGRANGE-RADAU') # It is often requried for larger problems to give the solver some direct instructions # These must be given in the form of a dictionary options = {} # While this problem should solve without changing the deault options, example code is # given commented out below. See Section 5.6 for more options and advice. # options['bound_push'] = 1e-8 # options['tol'] = 1e-9 options['linear_solver'] = 'ma57' # The set A_set is then decided. This set, explained in Section 4.3.3 is used to make the
exprs['B'] = m.P['k1'] * m.Z[t, 'A'] - m.P['k2'] * m.Z[t, 'B'] exprs['C'] = m.P['k2'] * m.Z[t, 'B'] return exprs builder.set_odes_rule(rule_odes) builder.bound_profile(var='S', bounds=(0, 200)) opt_model = builder.create_pyomo_model(0.0, 10.0) # ========================================================================= # USER INPUT SECTION - VARIANCE ESTIMATION # ========================================================================= # For this problem we have an input D matrix that has some noise in it # If we have the situation where model noise can be neglected, KIPET can still be used! # for this case we first calculate the variance of the device, using the function: # solve_max_device_variance v_estimator = VarianceEstimator(opt_model) v_estimator.apply_discretization('dae.collocation', nfe=100, ncp=1, scheme='LAGRANGE-RADAU') # It is often requried for larger problems to give the solver some direct instructions # These must be given in the form of a dictionary options = {} # While this problem should solve without changing the deault options, example code is # given commented out below. See Section 5.6 for more options and advice. # options['bound_push'] = 1e-8 # options['tol'] = 1e-9 # Finally we run the variance estimatator using the arguments shown in Seciton 4.3.3 worst_case_device_var = v_estimator.solve_max_device_variance(
builder.add_spectral_data(D_frame) end_time = 10 opt_model = builder.create_pyomo_model(0.0, end_time) #fD_frame = savitzky_golay(dataFrame = D_frame, window_size = 17, orderPoly = 3, orderDeriv=1) #========================================================================= #USER INPUT SECTION - VARIANCE ESTIMATION #========================================================================= # For this problem we have an input D matrix that has some noise in it # We can therefore use the variance estimator described in the Overview section # of the documentation and Section 4.3.3 nfe = 100 ncp = 3 v_estimator = VarianceEstimator(opt_model) v_estimator.apply_discretization('dae.collocation', nfe=nfe, ncp=ncp, scheme='LAGRANGE-RADAU') # It is often requried for larger problems to give the solver some direct instructions # These must be given in the form of a dictionary options = {} # While this problem should solve without changing the deault options, example code is # given commented out below. See Section 5.6 for more options and advice. # options['bound_push'] = 1e-8 options['linear_solver'] = 'ma57' # The set A_set is then decided. This set, explained in Section 4.3.3 is used to make the # variance estimation run faster and has been shown to not decrease the accuracy of the variance