def test_hessian_SimpleModel2(self): model = SimpleModel2() m = model.make_model() x_init_list = [ [-5.0], [-4.0], [-3.0], [-1.5], [0.5], [1.0], [2.0], [3.5] ] external_model = ExternalPyomoModel( [m.x], [m.y], [m.residual_eqn], [m.external_eqn], ) for x in x_init_list: external_model.set_input_values(x) hess = external_model.evaluate_hessians_of_residuals() self.assertAlmostEqual( hess[0][0, 0], model.evaluate_hessian(x[0]), delta=1e-7, )
def test_evaluate_hessian_equality_constraints_order(self): model = Model2by2() m = model.make_model() m.x[0].set_value(1.0) m.x[1].set_value(2.0) m.y[0].set_value(3.0) m.y[1].set_value(4.0) x0_init_list = [-5.0, -3.0, 0.5, 1.0, 2.5] x1_init_list = [0.5, 1.0, 1.5, 2.5, 4.1] lam_init_list = [-2.5, -0.5, 0.0, 1.0, 2.0] init_list = list( itertools.product(x0_init_list, x1_init_list, lam_init_list)) external_model = ExternalPyomoModel( list(m.x.values()), list(m.y.values()), list(m.residual_eqn.values()), list(m.external_eqn.values()), ) for x0, x1, lam in init_list: x = [x0, x1] lam = [lam] external_model.set_equality_constraint_multipliers(lam) external_model.set_input_values(x) # Using evaluate_hessian_equality_constraints, which calculates # external multiplier values, we can calculate the correct Hessian # regardless of the order in which primal and dual variables are # set. hess = external_model.evaluate_hessian_equality_constraints() pred_hess = model.calculate_reduced_lagrangian_hessian(lam, x) # This test asserts that we are doing the block reduction properly. np.testing.assert_allclose(hess.toarray(), np.tril(pred_hess), rtol=1e-8) from_individual = external_model.evaluate_hessians_of_residuals() hl_from_individual = sum(l * h for l, h in zip(lam, from_individual)) # This test asserts that the block reduction is correct. np.testing.assert_allclose(hess.toarray(), np.tril(hl_from_individual), rtol=1e-8)
def test_reduced_hessian_lagrangian(self): model = Model2by2() m = model.make_model() m.x[0].set_value(1.0) m.x[1].set_value(2.0) m.y[0].set_value(3.0) m.y[1].set_value(4.0) x0_init_list = [-5.0, -3.0, 0.5, 1.0, 2.5] x1_init_list = [0.5, 1.0, 1.5, 2.5, 4.1] lam_init_list = [-2.5, -0.5, 0.0, 1.0, 2.0] init_list = list( itertools.product(x0_init_list, x1_init_list, lam_init_list)) external_model = ExternalPyomoModel( list(m.x.values()), list(m.y.values()), list(m.residual_eqn.values()), list(m.external_eqn.values()), ) for x0, x1, lam in init_list: x = [x0, x1] lam = [lam] external_model.set_input_values(x) # Same comment as previous test regarding calculation order external_model.set_external_constraint_multipliers(lam) hlxx, hlxy, hlyy = \ external_model.get_full_space_lagrangian_hessians() hess = external_model.calculate_reduced_hessian_lagrangian( hlxx, hlxy, hlyy) pred_hess = model.calculate_reduced_lagrangian_hessian(lam, x) # This test asserts that we are doing the block reduction properly. np.testing.assert_allclose(np.array(hess), pred_hess, rtol=1e-8) from_individual = external_model.evaluate_hessians_of_residuals() hl_from_individual = sum(l * h for l, h in zip(lam, from_individual)) # This test asserts that the block reduction is correct. np.testing.assert_allclose(np.array(hess), hl_from_individual, rtol=1e-8)
def test_hessian_SimpleModel2x2_1(self): model = SimpleModel2by2_1() m = model.make_model() m.x[0].set_value(1.0) m.x[1].set_value(2.0) m.y[0].set_value(3.0) m.y[1].set_value(4.0) x0_init_list = [-5.0, -3.0, 0.5, 1.0, 2.5] x1_init_list = [-4.5, -2.3, 0.0, 1.0, 4.1] x_init_list = list(itertools.product(x0_init_list, x1_init_list)) external_model = ExternalPyomoModel( list(m.x.values()), list(m.y.values()), list(m.residual_eqn.values()), list(m.external_eqn.values()), ) for x in x_init_list: external_model.set_input_values(x) hess = external_model.evaluate_hessians_of_residuals() expected_hess = model.evaluate_hessian(x) np.testing.assert_allclose(hess, expected_hess, rtol=1e-8)