def test_evaluate_hessian_lagrangian_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) external_model.set_equality_constraint_multipliers([1.0, 1.0]) hess_lag = external_model.evaluate_hessian_equality_constraints() hess_lag = hess_lag.toarray() expected_hess = model.evaluate_hessian(x) expected_hess_lag = np.tril(expected_hess[0] + expected_hess[1]) np.testing.assert_allclose(hess_lag, expected_hess_lag, rtol=1e-8)
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)