def test_bad_arg(self): m = ConcreteModel() m.t = ContinuousSet(bounds=(0, 1)) m.a = Param(initialize=1, mutable=True) m.b = Param(initialize=2, mutable=True) m.c = Param(initialize=3, mutable=False) m.x = Var(m.t) list_one = [m.a, m.b] list_two = [m.a, m.b, m.c] list_three = [m.a, m.x] list_four = [m.a, m.c] # verify ValueError thrown when param and perturb list are different # lengths msg = ("Length of paramList argument does" " not equal length of perturbList") with self.assertRaisesRegex(ValueError, msg): Result = sensitivity_calculation('sipopt', m, list_one, list_two) # verify ValueError thrown when param list has an unmutable param msg = ("Parameters within paramList must be mutable") with self.assertRaisesRegex(ValueError, msg): Result = sensitivity_calculation('sipopt', m, list_four, list_one) # verify ValueError thrown when param list has an unfixed var. msg = ("Specified \"parameter\" variables must be fixed") with self.assertRaisesRegex(ValueError, msg) as context: Result = sensitivity_calculation('sipopt', m, list_three, list_one)
def test_constraintSub(self): m = ri.create_model() m.pert_a = Param(initialize=0.01) m.pert_b = Param(initialize=1.01) m_sipopt = sensitivity_calculation('sipopt', m, [m.a, m.b], [m.pert_a, m.pert_b]) # verify substitutions in equality constraint self.assertTrue(m_sipopt.C_equal.lower.ctype is Param and m_sipopt.C_equal.upper.ctype is Param) self.assertFalse(m_sipopt.C_equal.active) self.assertTrue( m_sipopt._SENSITIVITY_TOOLBOX_DATA.constList[3].lower == 0.0 and m_sipopt._SENSITIVITY_TOOLBOX_DATA.constList[3].upper == 0.0 and len( list( identify_variables( m_sipopt._SENSITIVITY_TOOLBOX_DATA.constList[3].body))) == 2) # verify substitutions in one-sided bounded constraint self.assertTrue(m_sipopt.C_singleBnd.lower is None and m_sipopt.C_singleBnd.upper.ctype is Param) self.assertFalse(m_sipopt.C_singleBnd.active) self.assertTrue( m_sipopt._SENSITIVITY_TOOLBOX_DATA.constList[4].lower is None and m_sipopt._SENSITIVITY_TOOLBOX_DATA.constList[4].upper == 0.0 and len( list( identify_variables( m_sipopt._SENSITIVITY_TOOLBOX_DATA.constList[4].body))) == 2) # verify substitutions in ranged inequality constraint self.assertTrue(m_sipopt.C_rangedIn.lower.ctype is Param and m_sipopt.C_rangedIn.upper.ctype is Param) self.assertFalse(m_sipopt.C_rangedIn.active) self.assertTrue( m_sipopt._SENSITIVITY_TOOLBOX_DATA.constList[1].lower is None and m_sipopt._SENSITIVITY_TOOLBOX_DATA.constList[1].upper == 0.0 and len( list( identify_variables( m_sipopt._SENSITIVITY_TOOLBOX_DATA.constList[1].body))) == 2) self.assertTrue( m_sipopt._SENSITIVITY_TOOLBOX_DATA.constList[2].lower is None and m_sipopt._SENSITIVITY_TOOLBOX_DATA.constList[2].upper == 0.0 and len( list( identify_variables( m_sipopt._SENSITIVITY_TOOLBOX_DATA.constList[2].body))) == 2)
def run_example(print_flag=True): ''' Execute the example Arguments: print_flag: Toggle on/off printing Returns sln_dict: Dictionary containing solution (used for automated testing) ''' m = create_model() m.perturbed_eta1 = Param(initialize=4.0) m.perturbed_eta2 = Param(initialize=1.0) m_kaug_dsdp = sensitivity_calculation('k_aug', m, [m.eta1, m.eta2], [m.perturbed_eta1, m.perturbed_eta2], tee=True) if print_flag: print("\nOriginal parameter values:") print("\teta1 =", m.eta1()) print("\teta2 =", m.eta2()) print("Initial point:") print("\tObjective =", value(m.cost)) print("\tx1 =", m.x1()) print("\tx2 =", m.x2()) print("\tx3 =", m.x3()) # Kaug saves only approximated solutions not original solutions print("\nNew parameter values:") print("\teta1 =", m_kaug_dsdp.perturbed_eta1()) print("\teta2 =", m_kaug_dsdp.perturbed_eta2()) print("(Approximate) solution with the new parameter values:") print("\tObjective =", m_kaug_dsdp.cost()) print("\tx1 =", m_kaug_dsdp.x1()) print("\tx2 =", m_kaug_dsdp.x2()) print("\tx3 =", m_kaug_dsdp.x3()) # Save the results in a dictionary. # This is optional and makes automated testing convenient. # This code is not required for a Minimum Working Example (MWE) d = dict() d['eta1'] = m.eta1() d['eta2'] = m.eta2() d['x1_init'] = m.x1() d['x2_init'] = m.x2() d['x3_init'] = m.x3() d['eta1_pert'] = m_kaug_dsdp.perturbed_eta1() d['eta2_pert'] = m_kaug_dsdp.perturbed_eta2() d['cost_pert'] = m_kaug_dsdp.cost() d['x1_pert'] = m_kaug_dsdp.x1() d['x2_pert'] = m_kaug_dsdp.x2() d['x3_pert'] = m_kaug_dsdp.x3() return d
def test_indexedParamsMapping_kaug(self): m = hiv.create_model() hiv.initialize_model(m, 10, 5, 1) m.epsDelta = Param(initialize=0.75001) q_del = {} q_del[(0, 0)] = 1.001 q_del[(0, 1)] = 1.002 q_del[(1, 0)] = 1.003 q_del[(1, 1)] = 1.004 q_del[(2, 0)] = 0.83001 q_del[(2, 1)] = 0.83002 q_del[(3, 0)] = 0.42001 q_del[(4, 0)] = 0.17001 m.qqDelta = Param(m.ij, initialize=q_del) m.aaDelta = Param(initialize=0.0001001) m_kaug = sensitivity_calculation('k_aug', m, [m.eps, m.qq, m.aa], [m.epsDelta, m.qqDelta, m.aaDelta]) # Make sure Param constraints have the correct form, i.e. # 0 <= _SENSITIVITY_TOOLBOX_DATA.PARAM_NAME - PARAM_NAME <= 0 self.assertEqual(m_kaug._SENSITIVITY_TOOLBOX_DATA.paramConst[1].lower, 0.0) self.assertEqual(m_kaug._SENSITIVITY_TOOLBOX_DATA.paramConst[1].upper, 0.0) self.assertEqual( m_kaug._SENSITIVITY_TOOLBOX_DATA.paramConst[1].body.to_string(), '_SENSITIVITY_TOOLBOX_DATA.eps - eps') self.assertEqual(m_kaug._SENSITIVITY_TOOLBOX_DATA.paramConst[6].lower, 0.0) self.assertEqual(m_kaug._SENSITIVITY_TOOLBOX_DATA.paramConst[6].upper, 0.0) self.assertEqual( m_kaug._SENSITIVITY_TOOLBOX_DATA.paramConst[6].body.to_string(), '_SENSITIVITY_TOOLBOX_DATA.qq[2,0] - qq[2,0]') self.assertEqual(m_kaug._SENSITIVITY_TOOLBOX_DATA.paramConst[10].lower, 0.0) self.assertEqual(m_kaug._SENSITIVITY_TOOLBOX_DATA.paramConst[10].upper, 0.0) self.assertEqual( m_kaug._SENSITIVITY_TOOLBOX_DATA.paramConst[10].body.to_string(), '_SENSITIVITY_TOOLBOX_DATA.aa - aa')
def test_sipopt_equivalent(self): m1 = param_ex.create_model() m1.perturbed_eta1 = Param(initialize = 4.0) m1.perturbed_eta2 = Param(initialize = 1.0) m2 = param_ex.create_model() m2.perturbed_eta1 = Param(initialize = 4.0) m2.perturbed_eta2 = Param(initialize = 1.0) m11 = sipopt(m1,[m1.eta1,m1.eta2], [m1.perturbed_eta1,m1.perturbed_eta2], cloneModel=True) m22 = sensitivity_calculation('sipopt',m2,[m2.eta1,m2.eta2], [m2.perturbed_eta1,m2.perturbed_eta2], cloneModel=True) out1 = StringIO() out2 = StringIO() m11._SENSITIVITY_TOOLBOX_DATA.constList.pprint(ostream=out1) m22._SENSITIVITY_TOOLBOX_DATA.constList.pprint(ostream=out2) self.assertMultiLineEqual(out1.getvalue(), out2.getvalue())
discretizer = TransformationFactory('dae.collocation') discretizer.apply_to(m, nfe=n_nfe, ncp=n_ncp, scheme='LAGRANGE-RADAU') sim.initialize_model() if __name__ == '__main__': m = create_model() initialize_model(m, 10, 5, 1) m.epsDelta = Param(initialize=0.75001) q_del = {} q_del[(0, 0)] = 1.001 q_del[(0, 1)] = 1.002 q_del[(1, 0)] = 1.003 q_del[(1, 1)] = 1.004 q_del[(2, 0)] = 0.83001 q_del[(2, 1)] = 0.83002 q_del[(3, 0)] = 0.42001 q_del[(4, 0)] = 0.17001 m.qqDelta = Param(m.ij, initialize=q_del) m.aaDelta = Param(initialize=.0001001) m_sipopt = sensitivity_calculation('sipopt', m, [m.eps, m.qq, m.aa], [m.epsDelta, m.qqDelta, m.aaDelta], tee=True)
def test_noClone_soln_kaug(self): m_orig = fc.create_model() fc.initialize_model(m_orig, 100) m_orig.perturbed_a = Param(initialize=-0.25) m_orig.perturbed_H = Param(initialize=0.55) m_kaug = sensitivity_calculation( 'k_aug', m_orig, [m_orig.a, m_orig.H], [m_orig.perturbed_a, m_orig.perturbed_H], cloneModel=False) ptb_map = ComponentMap() ptb_map[m_kaug.a] = value(-(m_kaug.perturbed_a - m_kaug.a)) ptb_map[m_kaug.H] = value(-(m_kaug.perturbed_H - m_kaug.H)) self.assertTrue(m_kaug == m_orig) # verify suffixes self.assertTrue( hasattr(m_kaug, 'sens_state_0') and m_kaug.sens_state_0.ctype is Suffix and m_kaug.sens_state_0[m_kaug._SENSITIVITY_TOOLBOX_DATA.H] == 2 and m_kaug.sens_state_0[m_kaug._SENSITIVITY_TOOLBOX_DATA.a] == 1) self.assertTrue( hasattr(m_kaug, 'sens_state_1') and m_kaug.sens_state_1.ctype is Suffix and m_kaug.sens_state_1[m_kaug._SENSITIVITY_TOOLBOX_DATA.H] == 2 and m_kaug.sens_state_1[m_kaug._SENSITIVITY_TOOLBOX_DATA.a] == 1) self.assertTrue( hasattr(m_kaug, 'sens_state_value_1') and m_kaug.sens_state_value_1.ctype is Suffix and m_kaug.sens_state_value_1[m_kaug._SENSITIVITY_TOOLBOX_DATA.H] == 0.55 and m_kaug.sens_state_value_1[m_kaug._SENSITIVITY_TOOLBOX_DATA.a] == -0.25) self.assertTrue( hasattr(m_kaug, 'sens_init_constr') and m_kaug.sens_init_constr.ctype is Suffix and m_kaug.sens_init_constr[ m_kaug._SENSITIVITY_TOOLBOX_DATA.paramConst[1]] == 1 and m_kaug.sens_init_constr[ m_kaug._SENSITIVITY_TOOLBOX_DATA.paramConst[2]] == 2) self.assertTrue(hasattr(m_kaug, 'DeltaP')) self.assertIs(m_kaug.DeltaP.ctype, Suffix) self.assertEqual( m_kaug.DeltaP[m_kaug._SENSITIVITY_TOOLBOX_DATA.paramConst[1]], ptb_map[m_kaug.a]) self.assertEqual( m_kaug.DeltaP[m_kaug._SENSITIVITY_TOOLBOX_DATA.paramConst[2]], ptb_map[m_kaug.H]) self.assertTrue( hasattr(m_kaug, 'dcdp') and m_kaug.dcdp.ctype is Suffix and m_kaug.dcdp[m_kaug._SENSITIVITY_TOOLBOX_DATA.paramConst[1]] == 1 and m_kaug.dcdp[m_kaug._SENSITIVITY_TOOLBOX_DATA.paramConst[2]] == 2) self.assertTrue( hasattr(m_kaug, 'sens_sol_state_1') and m_kaug.sens_sol_state_1.ctype is Suffix) self.assertTrue( hasattr(m_kaug, 'ipopt_zL_in') and m_kaug.ipopt_zL_in.ctype is Suffix) self.assertAlmostEqual(m_kaug.ipopt_zL_in[m_kaug.u[15]], 7.162686166847096e-09, 13) self.assertTrue( hasattr(m_kaug, 'ipopt_zU_in') and m_kaug.ipopt_zU_in.ctype is Suffix) self.assertAlmostEqual(m_kaug.ipopt_zU_in[m_kaug.u[15]], -1.2439730261288605e-08, 13) # verify deactivated constraints for cloned model self.assertFalse(m_kaug.FDiffCon[0].active and m_kaug.FDiffCon[7.5].active and m_kaug.FDiffCon[15].active) self.assertFalse(m_kaug.x_dot[0].active and m_kaug.x_dot[7.5].active and m_kaug.x_dot[15].active) # verify solution # This is the only test that verifies the solution. Here we # verify the objective function value, which is a weak test. self.assertAlmostEqual(value(m_kaug.J), 0.002633263921107476, 8)
def test_noClone_soln(self): m_orig = fc.create_model() fc.initialize_model(m_orig, 100) m_orig.perturbed_a = Param(initialize=-0.25) m_orig.perturbed_H = Param(initialize=0.55) m_sipopt = sensitivity_calculation( 'sipopt', m_orig, [m_orig.a, m_orig.H], [m_orig.perturbed_a, m_orig.perturbed_H], cloneModel=False) self.assertTrue(m_sipopt == m_orig) # test _SENSITIVITY_TOOLBOX_DATA block exists self.assertTrue( hasattr(m_orig, '_SENSITIVITY_TOOLBOX_DATA') and m_orig._SENSITIVITY_TOOLBOX_DATA.ctype is Block) # test variable declaration self.assertTrue( hasattr(m_sipopt._SENSITIVITY_TOOLBOX_DATA, 'a') and m_sipopt._SENSITIVITY_TOOLBOX_DATA.a.ctype is Var) self.assertTrue( hasattr(m_sipopt._SENSITIVITY_TOOLBOX_DATA, 'H') and m_sipopt._SENSITIVITY_TOOLBOX_DATA.H.ctype is Var) # test for suffixes self.assertTrue( hasattr(m_sipopt, 'sens_state_0') and m_sipopt.sens_state_0.ctype is Suffix and m_sipopt.sens_state_0[m_sipopt._SENSITIVITY_TOOLBOX_DATA.H] == 2 and m_sipopt.sens_state_0[m_sipopt._SENSITIVITY_TOOLBOX_DATA.a] == 1) self.assertTrue( hasattr(m_sipopt, 'sens_state_1') and m_sipopt.sens_state_1.ctype is Suffix and m_sipopt.sens_state_1[m_sipopt._SENSITIVITY_TOOLBOX_DATA.H] == 2 and m_sipopt.sens_state_1[m_sipopt._SENSITIVITY_TOOLBOX_DATA.a] == 1) self.assertTrue( hasattr(m_sipopt, 'sens_state_value_1') and m_sipopt.sens_state_value_1.ctype is Suffix and m_sipopt.sens_state_value_1[m_sipopt._SENSITIVITY_TOOLBOX_DATA.H] == 0.55 and m_sipopt.sens_state_value_1[m_sipopt._SENSITIVITY_TOOLBOX_DATA.a] == -0.25) self.assertTrue( hasattr(m_sipopt, 'sens_init_constr') and m_sipopt.sens_init_constr.ctype is Suffix and m_sipopt.sens_init_constr[ m_sipopt._SENSITIVITY_TOOLBOX_DATA.paramConst[1]] == 1 and m_sipopt.sens_init_constr[ m_sipopt._SENSITIVITY_TOOLBOX_DATA.paramConst[2]] == 2) self.assertTrue( hasattr(m_sipopt, 'sens_sol_state_1') and m_sipopt.sens_sol_state_1.ctype is Suffix) self.assertAlmostEqual(m_sipopt.sens_sol_state_1[m_sipopt.F[15]], -0.00102016765, 8) self.assertTrue( hasattr(m_sipopt, 'sens_sol_state_1_z_L') and m_sipopt.sens_sol_state_1_z_L.ctype is Suffix) self.assertAlmostEqual(m_sipopt.sens_sol_state_1_z_L[m_sipopt.u[15]], -2.181712e-09, 13) self.assertTrue( hasattr(m_sipopt, 'sens_sol_state_1_z_U') and m_sipopt.sens_sol_state_1_z_U.ctype is Suffix) self.assertAlmostEqual(m_sipopt.sens_sol_state_1_z_U[m_sipopt.u[15]], 6.580899e-09, 13) # verify deactivated constraints on model self.assertFalse(m_sipopt.FDiffCon[0].active and m_sipopt.FDiffCon[7.5].active and m_sipopt.FDiffCon[15].active) self.assertFalse(m_sipopt.x_dot[0].active and m_sipopt.x_dot[7.5].active and m_sipopt.x_dot[15].active) # test model solution # NOTE: # ipopt_sens does not alter the values in the model, # so all this test is doing is making sure that the # objective value doesn't change. This test does nothing to # check values of the perturbed solution. self.assertAlmostEqual(value(m_sipopt.J), 0.0048956783, 8)
def test_clonedModel_soln(self): m_orig = fc.create_model() fc.initialize_model(m_orig, 100) m_orig.perturbed_a = Param(initialize=-0.25) m_orig.perturbed_H = Param(initialize=0.55) m_sipopt = sensitivity_calculation( 'sipopt', m_orig, [m_orig.a, m_orig.H], [m_orig.perturbed_a, m_orig.perturbed_H], cloneModel=True) # verify cloned model has _SENSITIVITY_TOOLBOX_DATA block # and original model is untouched self.assertFalse(m_sipopt == m_orig) self.assertTrue( hasattr(m_sipopt, '_SENSITIVITY_TOOLBOX_DATA') and m_sipopt._SENSITIVITY_TOOLBOX_DATA.ctype is Block) self.assertFalse(hasattr(m_orig, '_SENSITIVITY_TOOLBOX_DATA')) self.assertFalse(hasattr(m_orig, 'b')) # verify variable declaration self.assertTrue( hasattr(m_sipopt._SENSITIVITY_TOOLBOX_DATA, 'a') and m_sipopt._SENSITIVITY_TOOLBOX_DATA.a.ctype is Var) self.assertTrue( hasattr(m_sipopt._SENSITIVITY_TOOLBOX_DATA, 'H') and m_sipopt._SENSITIVITY_TOOLBOX_DATA.H.ctype is Var) # verify suffixes self.assertTrue( hasattr(m_sipopt, 'sens_state_0') and m_sipopt.sens_state_0.ctype is Suffix and m_sipopt.sens_state_0[m_sipopt._SENSITIVITY_TOOLBOX_DATA.H] == 2 and m_sipopt.sens_state_0[m_sipopt._SENSITIVITY_TOOLBOX_DATA.a] == 1) self.assertTrue( hasattr(m_sipopt, 'sens_state_1') and m_sipopt.sens_state_1.ctype is Suffix and m_sipopt.sens_state_1[m_sipopt._SENSITIVITY_TOOLBOX_DATA.H] == 2 and m_sipopt.sens_state_1[m_sipopt._SENSITIVITY_TOOLBOX_DATA.a] == 1) self.assertTrue( hasattr(m_sipopt, 'sens_state_value_1') and m_sipopt.sens_state_value_1.ctype is Suffix and m_sipopt.sens_state_value_1[m_sipopt._SENSITIVITY_TOOLBOX_DATA.H] == 0.55 and m_sipopt.sens_state_value_1[m_sipopt._SENSITIVITY_TOOLBOX_DATA.a] == -0.25) self.assertTrue( hasattr(m_sipopt, 'sens_init_constr') and m_sipopt.sens_init_constr.ctype is Suffix and m_sipopt.sens_init_constr[ m_sipopt._SENSITIVITY_TOOLBOX_DATA.paramConst[1]] == 1 and m_sipopt.sens_init_constr[ m_sipopt._SENSITIVITY_TOOLBOX_DATA.paramConst[2]] == 2) self.assertTrue( hasattr(m_sipopt, 'sens_sol_state_1') and m_sipopt.sens_sol_state_1.ctype is Suffix) self.assertAlmostEqual(m_sipopt.sens_sol_state_1[m_sipopt.F[15]], -0.00102016765, 8) # These tests require way too much precision for something that # just needs to enforce that bounds are not active... self.assertTrue( hasattr(m_sipopt, 'sens_sol_state_1_z_L') and m_sipopt.sens_sol_state_1_z_L.ctype is Suffix) self.assertAlmostEqual(m_sipopt.sens_sol_state_1_z_L[m_sipopt.u[15]], -2.181712e-09, 13) self.assertTrue( hasattr(m_sipopt, 'sens_sol_state_1_z_U') and m_sipopt.sens_sol_state_1_z_U.ctype is Suffix) self.assertAlmostEqual(m_sipopt.sens_sol_state_1_z_U[m_sipopt.u[15]], 6.580899e-09, 13) # verify deactivated constraints for cloned model self.assertFalse(m_sipopt.FDiffCon[0].active and m_sipopt.FDiffCon[7.5].active and m_sipopt.FDiffCon[15].active) self.assertFalse(m_sipopt.x_dot[0].active and m_sipopt.x_dot[7.5].active and m_sipopt.x_dot[15].active) # verify constraints on original model are still active self.assertTrue(m_orig.FDiffCon[0].active and m_orig.FDiffCon[7.5].active and m_orig.FDiffCon[15].active) self.assertTrue(m_orig.x_dot[0].active and m_orig.x_dot[7.5].active and m_orig.x_dot[15].active) # verify solution # NOTE: This is the solution to the original problem, # not the result of any sensitivity update. self.assertAlmostEqual(value(m_sipopt.J), 0.0048956783, 8)
def run_example(print_flag=True): ''' Execute the example Arguments: print_flag: Toggle on/off printing Returns: sln_dict: Dictionary containing solution (used for automated testing) ''' m = create_model() m.perturbed_eta1 = Param(initialize=4.0) m.perturbed_eta2 = Param(initialize=1.0) m_sipopt = sensitivity_calculation('sipopt', m, [m.eta1, m.eta2], [m.perturbed_eta1, m.perturbed_eta2], tee=True) if print_flag: print("\nOriginal parameter values:") print("\teta1 =", m.eta1()) print("\teta2 =", m.eta2()) print("Initial point:") print("\tObjective =", value(m.cost)) print("\tx1 =", m.x1()) print("\tx2 =", m.x2()) print("\tx3 =", m.x3()) print("Solution with the original parameter values:") print("\tObjective =", m_sipopt.cost()) print("\tx1 =", m_sipopt.x1()) print("\tx2 =", m_sipopt.x2()) print("\tx3 =", m_sipopt.x3()) print("\nNew parameter values:") print("\teta1 =", m_sipopt.perturbed_eta1()) print("\teta2 =", m_sipopt.perturbed_eta2()) # This highlights one limitation of sipopt. It will only return the # perturbed solution. The user needs to calculate relevant values such as # the objective or expressions x1 = m_sipopt.sens_sol_state_1[m_sipopt.x1] x2 = m_sipopt.sens_sol_state_1[m_sipopt.x2] x3 = m_sipopt.sens_sol_state_1[m_sipopt.x3] obj = x1**2 + x2**2 + x3**2 if print_flag: print("(Approximate) solution with the new parameter values:") print("\tObjective =", obj) print("\tx1 =", m_sipopt.sens_sol_state_1[m_sipopt.x1]) print("\tx2 =", m_sipopt.sens_sol_state_1[m_sipopt.x2]) print("\tx3 =", m_sipopt.sens_sol_state_1[m_sipopt.x3]) # Save the results in a dictionary. # This is optional and makes automated testing convenient. # This code is not important for a Minimum Working Example (MWE) of sipopt d = dict() d['eta1'] = m.eta1() d['eta2'] = m.eta2() d['x1_init'] = m.x1() d['x2_init'] = m.x2() d['x3_init'] = m.x3() d['x1_sln'] = m_sipopt.x1() d['x2_sln'] = m_sipopt.x2() d['x3_sln'] = m_sipopt.x3() d['cost_sln'] = m_sipopt.cost() d['eta1_pert'] = m_sipopt.perturbed_eta1() d['eta2_pert'] = m_sipopt.perturbed_eta2() d['x1_pert'] = x1 d['x2_pert'] = x2 d['x3_pert'] = x3 d['cost_pert'] = obj return d
###################################### if __name__ == '__main__': m = create_model() initialize_model(m, 100) # plt = plot_optimal_solution(m) # plt.show() m.perturbed_a = Param(initialize=-0.25) m.perturbed_H = Param(initialize=0.55) m_sipopt = sensitivity_calculation('sipopt', m, [m.a, m.H], [m.perturbed_a, m.perturbed_H], cloneModel=True, tee=True) for var, val in m_sipopt.sens_sol_state_1.items(): # To load updated variable values back into the model: if var.ctype is not Var: continue var.set_value(val) m_sipopt.a.set_value(value(m_sipopt.perturbed_a)) m_sipopt.H.set_value(value(m_sipopt.perturbed_H)) # To solve for the "true solution" (with the full nonlinear # model) after perturbing parameters: solver = SolverFactory('ipopt')
from pyomo.environ import ConcreteModel, Param, Var, Constraint, inequality from pyomo.contrib.sensitivity_toolbox.sens import sensitivity_calculation def create_model(): m = ConcreteModel() m.a = Param(initialize=0, mutable=True) m.b = Param(initialize=1, mutable=True) m.x = Var(initialize=1.0) m.y = Var() m.C_rangedIn = Constraint(expr=inequality(m.a, m.x, m.b)) m.C_equal = Constraint(expr=m.y == m.b) m.C_singleBnd = Constraint(expr=m.x <= m.b) return m if __name__ == '__main__': m = create_model() m.pert_a = Param(initialize=0.01) m.pert_b = Param(initialize=1.01) m_sipopt = sensitivity_calculation('sipopt', m, [m.a, m.b], [m.pert_a, m.pert_b], tee=True)