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 = sipopt(m_orig, [m_orig.a, m_orig.H], [m_orig.perturbed_a, m_orig.perturbed_H], cloneModel=True) #verify cloned model has _sipopt_data block # and original model is untouched self.assertFalse(m_sipopt == m_orig) self.assertTrue( hasattr(m_sipopt, '_sipopt_data') and m_sipopt._sipopt_data.ctype is Block) self.assertFalse(hasattr(m_orig, '_sipopt_data')) self.assertFalse(hasattr(m_orig, 'b')) #verify variable declaration self.assertTrue( hasattr(m_sipopt._sipopt_data, 'a') and m_sipopt._sipopt_data.a.ctype is Var) self.assertTrue( hasattr(m_sipopt._sipopt_data, 'H') and m_sipopt._sipopt_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._sipopt_data.H] == 2 and m_sipopt.sens_state_0[m_sipopt._sipopt_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._sipopt_data.H] == 2 and m_sipopt.sens_state_1[m_sipopt._sipopt_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._sipopt_data.H] == 0.55 and m_sipopt.sens_state_value_1[m_sipopt._sipopt_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._sipopt_data.paramConst[1]] == 1 and m_sipopt.sens_init_constr[m_sipopt._sipopt_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 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 self.assertAlmostEqual(value(m_sipopt.J), 0.0048956783, 8)
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 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 = 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 _sipopt_data block exists self.assertTrue( hasattr(m_orig, '_sipopt_data') and m_orig._sipopt_data.type() is Block) #test variable declaration self.assertTrue( hasattr(m_sipopt._sipopt_data, 'a') and m_sipopt._sipopt_data.a.type() is Var) self.assertTrue( hasattr(m_sipopt._sipopt_data, 'H') and m_sipopt._sipopt_data.H.type() is Var) #test for suffixes self.assertTrue( hasattr(m_sipopt, 'sens_state_0') and m_sipopt.sens_state_0.type() is Suffix and m_sipopt.sens_state_0[m_sipopt._sipopt_data.H] == 2 and m_sipopt.sens_state_0[m_sipopt._sipopt_data.a] == 1) self.assertTrue( hasattr(m_sipopt, 'sens_state_1') and m_sipopt.sens_state_1.type() is Suffix and m_sipopt.sens_state_1[m_sipopt._sipopt_data.H] == 2 and m_sipopt.sens_state_1[m_sipopt._sipopt_data.a] == 1) self.assertTrue( hasattr(m_sipopt, 'sens_state_value_1') and m_sipopt.sens_state_value_1.type() is Suffix and m_sipopt.sens_state_value_1[m_sipopt._sipopt_data.H] == 0.55 and m_sipopt.sens_state_value_1[m_sipopt._sipopt_data.a] == -0.25) self.assertTrue( hasattr(m_sipopt, 'sens_init_constr') and m_sipopt.sens_init_constr.type() is Suffix and m_sipopt.sens_init_constr[m_sipopt._sipopt_data.paramConst[1]] == 1 and m_sipopt.sens_init_constr[m_sipopt._sipopt_data.paramConst[2]] == 2) self.assertTrue( hasattr(m_sipopt, 'sens_sol_state_1') and m_sipopt.sens_sol_state_1.type() 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.type() 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.type() 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 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 = sipopt(m_orig,[m_orig.a,m_orig.H], [m_orig.perturbed_a,m_orig.perturbed_H], cloneModel=True) #verify cloned model has _sipopt_data block # and original model is untouched self.assertFalse(m_sipopt == m_orig) self.assertTrue(hasattr(m_sipopt,'_sipopt_data') and m_sipopt._sipopt_data.type() is Block) self.assertFalse(hasattr(m_orig,'_sipopt_data')) self.assertFalse(hasattr(m_orig,'b')) #verify variable declaration self.assertTrue(hasattr(m_sipopt._sipopt_data,'a') and m_sipopt._sipopt_data.a.type() is Var) self.assertTrue(hasattr(m_sipopt._sipopt_data,'H') and m_sipopt._sipopt_data.H.type() is Var) #verify suffixes self.assertTrue(hasattr(m_sipopt,'sens_state_0') and m_sipopt.sens_state_0.type() is Suffix and m_sipopt.sens_state_0[m_sipopt._sipopt_data.H]==2 and m_sipopt.sens_state_0[m_sipopt._sipopt_data.a]==1) self.assertTrue(hasattr(m_sipopt,'sens_state_1') and m_sipopt.sens_state_1.type() is Suffix and m_sipopt.sens_state_1[m_sipopt._sipopt_data.H]==2 and m_sipopt.sens_state_1[m_sipopt._sipopt_data.a]==1) self.assertTrue(hasattr(m_sipopt,'sens_state_value_1') and m_sipopt.sens_state_value_1.type() is Suffix and m_sipopt.sens_state_value_1[ m_sipopt._sipopt_data.H]==0.55 and m_sipopt.sens_state_value_1[ m_sipopt._sipopt_data.a]==-0.25) self.assertTrue(hasattr(m_sipopt,'sens_init_constr') and m_sipopt.sens_init_constr.type() is Suffix and m_sipopt.sens_init_constr[ m_sipopt._sipopt_data.paramConst[1]]==1 and m_sipopt.sens_init_constr[ m_sipopt._sipopt_data.paramConst[2]]==2) self.assertTrue(hasattr(m_sipopt,'sens_sol_state_1') and m_sipopt.sens_sol_state_1.type() 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.type() 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.type() 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 self.assertAlmostEqual(value(m_sipopt.J),0.0048956783,8)