def runTest(self): n = 2 npt = (n + 1) * (n + 2) // 2 x0 = np.array([1.0, 1.0]) xl = -1e2 * np.ones((n, )) xu = 1e2 * np.ones((n, )) model = Model(npt, x0, objfun(x0), xl, xu, 1) x1 = x0 + np.array([1.0, 0.0]) model.change_point(1, x1 - model.xbase, objfun(x1)) x2 = x0 + np.array([0.1, 0.9]) model.change_point(2, x2 - model.xbase, objfun(x2)) x3 = x0 + np.array([-0.1, 0.0]) model.change_point(3, x3 - model.xbase, objfun(x3)) x4 = x0 + np.array([-0.1, 2.0]) model.change_point(4, x4 - model.xbase, objfun(x4)) x5 = x0 + np.array([-1.1, 1.0]) model.change_point(5, x5 - model.xbase, objfun(x5)) # For reference: model based around model.xbase interp_ok, interp_cond_num, norm_chg_grad, norm_chg_hess, interp_error = model.interpolate_model( verbose=True) self.assertTrue(interp_ok, 'Interpolation failed') self.assertAlmostEqual(interp_error, 0.0, msg='Expect exact interpolation') self.assertAlmostEqual(norm_chg_grad, np.linalg.norm(model.model_grad)) self.assertAlmostEqual( norm_chg_hess, np.linalg.norm(model.model_hess.as_full(), ord='fro')) self.assertAlmostEqual(model.model_const, objfun(model.xbase), msg='Wrong constant term') for xi in [x0, x1, x2, x3, x4, x5]: self.assertAlmostEqual(model.model_value(xi - model.xbase, d_based_at_xopt=False, with_const_term=True), objfun(xi), msg='Wrong interp value at %s' % str(xi)) # Test some other parameter settings for model.model_value() g, hess = model.build_full_model() self.assertTrue( np.allclose( g, model.model_grad + model.model_hess.vec_mul(model.xopt(abs_coordinates=False))), 'Bad gradient') self.assertTrue( np.allclose(hess.as_full(), model.model_hess.as_full()), 'Bad Hessian')
def runTest(self): n = 2 npt = 2 * n + 1 x0 = np.array([1.0, 1.0]) xl = -1e2 * np.ones((n, )) xu = 1e2 * np.ones((n, )) model = Model(npt, x0, objfun(x0), xl, xu, 1, precondition=False) x1 = x0 + np.array([1.0, 0.0]) model.change_point(1, x1 - model.xbase, objfun(x1)) x2 = x0 + np.array([0.1, 0.9]) model.change_point(2, x2 - model.xbase, objfun(x2)) x3 = x0 + np.array([-0.1, 0.0]) model.change_point(3, x3 - model.xbase, objfun(x3)) x4 = x0 + np.array([-0.1, 2.0]) model.change_point(4, x4 - model.xbase, objfun(x4)) # x2 is xopt in this situation self.assertTrue(model.kopt == 2, 'Wrong xopt') xs = [x0, x1, x3, x4] xopt = x2 nxs = len(xs) A = np.zeros((nxs + n, nxs + n)) for i in range(nxs): for j in range(nxs): A[i, j] = 0.5 * np.dot(xs[i] - xopt, xs[j] - xopt)**2 A[i, nxs:] = xs[i] - xopt A[nxs:, i] = xs[i] - xopt A2, left_scaling, right_scaling = model.interpolation_matrix() # print("Expect", A) # print("Got", A2) self.assertTrue(np.allclose(A, A2), 'Interp matrix 1') # For reference: model based around model.xbase interp_ok, interp_cond_num, norm_chg_grad, norm_chg_hess, interp_error = model.interpolate_model( verbose=True) self.assertTrue(interp_ok, 'Interpolation failed') self.assertAlmostEqual(interp_error, 0.0, msg='Expect exact interpolation') self.assertAlmostEqual(norm_chg_grad, np.linalg.norm(model.model_grad)) self.assertAlmostEqual( norm_chg_hess, np.linalg.norm(model.model_hess.as_full(), ord='fro')) self.assertAlmostEqual(model.model_const, objfun(model.xbase), msg='Wrong constant term') for xi in [x0, x1, x2, x3, x4]: self.assertAlmostEqual(model.model_value(xi - model.xbase, d_based_at_xopt=False, with_const_term=True), objfun(xi), msg='Wrong interp value at %s' % str(xi)) # Test some other parameter settings for model.model_value() g, hess = model.build_full_model() self.assertTrue( np.allclose( g, model.model_grad + model.model_hess.vec_mul(model.xopt(abs_coordinates=False))), 'Bad gradient') self.assertTrue( np.allclose(hess.as_full(), model.model_hess.as_full()), 'Bad Hessian') # Build a new model model2 = Model(npt, x0, objfun(x0), xl, xu, 1, precondition=False) model2.change_point(1, x1 - model.xbase, objfun(x1)) model2.change_point(2, x2 - model.xbase, objfun(x2)) model2.change_point(3, x3 - model.xbase, objfun(x3)) model2.change_point(4, x4 - model.xbase, objfun(x4)) # Force Hessian to be something else model2.model_hess = Hessian(n, vals=np.eye(n)) A2, left_scaling, right_scaling = model2.interpolation_matrix() self.assertTrue(np.allclose(A, A2), 'Interp matrix 2') interp_ok, interp_cond_num, norm_chg_grad, norm_chg_hess, interp_error = model2.interpolate_model( ) self.assertTrue(interp_ok, 'Interpolation failed') self.assertAlmostEqual(interp_error, 0.0, msg='Expect exact interpolation') self.assertAlmostEqual(model2.model_const, objfun(model2.xbase), msg='Wrong constant term') for xi in [x0, x1, x2, x3, x4]: self.assertAlmostEqual(model2.model_value(xi - model2.xbase, d_based_at_xopt=False, with_const_term=True), objfun(xi), msg='Wrong interp value at %s' % str(xi)) # Compare distance of hessians h1 = Hessian(n).as_full() h2 = Hessian(n, vals=np.eye(n)).as_full() self.assertLessEqual( np.linalg.norm(model.model_hess.as_full() - h1, ord='fro'), np.linalg.norm(model2.model_hess.as_full() - h1, ord='fro'), 'Not min frob Hess 1') self.assertLessEqual( np.linalg.norm(model2.model_hess.as_full() - h2, ord='fro'), np.linalg.norm(model.model_hess.as_full() - h2, ord='fro'), 'Not min frob Hess 2') # print(model.model_hess.as_full()) # print(model2.model_hess.as_full()) # Build a new model model3 = Model(npt, x0, objfun(x0), xl, xu, 1, precondition=False) model3.change_point(1, x1 - model.xbase, objfun(x1)) model3.change_point(2, x2 - model.xbase, objfun(x2)) model3.change_point(3, x3 - model.xbase, objfun(x3)) model3.change_point(4, x4 - model.xbase, objfun(x4)) # Force Hessian to be something else model3.model_hess = Hessian(n, vals=np.eye(n)) A2, left_scaling, right_scaling = model3.interpolation_matrix() self.assertTrue(np.allclose(A, A2), 'Interp matrix 3') interp_ok, interp_cond_num, norm_chg_grad, norm_chg_hess, interp_error = model3.interpolate_model( min_chg_hess=False) self.assertTrue(interp_ok, 'Interpolation failed') self.assertAlmostEqual(interp_error, 0.0, msg='Expect exact interpolation') self.assertAlmostEqual(model3.model_const, objfun(model3.xbase), msg='Wrong constant term') for xi in [x0, x1, x2, x3, x4]: self.assertAlmostEqual(model3.model_value(xi - model3.xbase, d_based_at_xopt=False, with_const_term=True), objfun(xi), msg='Wrong interp value at %s' % str(xi)) self.assertTrue( np.allclose(model.model_hess.as_full(), model3.model_hess.as_full()), 'min_chg_hess=False not working')
def runTest(self): n, m = 2, 2 npt = n + 1 x0 = np.array([-1.2, 1.0]) xl = -1e2 * np.ones((n, )) xu = 1e2 * np.ones((n, )) model = Model(npt, x0, rosenbrock(x0), xl, xu, 1, precondition=False) x1 = np.array([1.0, 0.9]) model.change_point(1, x1 - model.xbase, rosenbrock(x1)) x2 = np.array([2.0, 0.9]) model.change_point(2, x2 - model.xbase, rosenbrock(x2)) A, left_scaling, right_scaling = model.interpolation_matrix() A_expect = np.zeros((2, 2)) A_expect[0, :] = x0 - x1 # x1 is xopt in this situation A_expect[1, :] = x2 - x1 self.assertTrue(array_compare(A, A_expect), 'Interp matrix 1') # For reference: model based around model.xbase interp_ok, interp_cond_num, norm_chg_grad, norm_chg_hess, interp_error = model.interpolate_model( ) self.assertTrue(interp_ok, 'Interpolation failed') self.assertAlmostEqual(interp_error, 0.0, msg='Expect exact interpolation') self.assertAlmostEqual(model.model_const, rosenbrock(model.xbase), msg='Wrong constant term') self.assertTrue( array_compare(model.model_value(x1 - model.xbase, d_based_at_xopt=False, with_const_term=True), rosenbrock(x1), thresh=1e-10), 'Wrong x1') # allow some inexactness self.assertTrue( array_compare(model.model_value(x2 - model.xbase, d_based_at_xopt=False, with_const_term=True), rosenbrock(x2), thresh=1e-10), 'Wrong x2') # Test some other parameter settings for model.model_value() self.assertTrue( array_compare(model.model_value(x2 - x1, d_based_at_xopt=True, with_const_term=True), rosenbrock(x2), thresh=1e-10), 'Wrong x2 (from xopt)') self.assertTrue( array_compare(model.model_value(x2 - x1, d_based_at_xopt=True, with_const_term=False), rosenbrock(x2) - rosenbrock(model.xbase), thresh=1e-10), 'Wrong x2 (no constant)') self.assertTrue( array_compare(model.model_value(x2 - model.xbase, d_based_at_xopt=False, with_const_term=False), rosenbrock(x2) - rosenbrock(model.xbase), thresh=1e-10), 'Wrong x2 (no constant v2)') g, hess = model.build_full_model() self.assertTrue( np.allclose( g, model.model_grad + model.model_hess.vec_mul(model.xopt(abs_coordinates=False))), 'Bad gradient') self.assertTrue( np.allclose(hess.as_full(), model.model_hess.as_full()), 'Bad Hessian')