def runTest(self): n, m = 2, 2 npt = n + 1 x0 = np.array([-1.2, 1.0]) xl = -1e20 * np.ones((n, )) xu = 1e20 * np.ones((n, )) model = Model(npt, x0, rosenbrock(x0), xl, xu, 1) # Now add better point x1 = np.array([1.0, 0.9]) f1 = rosenbrock(x1) model.change_point(1, x1 - model.xbase, f1, allow_kopt_update=True) # Now add worse point x2 = np.array([2.0, 0.9]) f2 = rosenbrock(x2) model.change_point(2, x2 - model.xbase, f2, allow_kopt_update=True) model.swap_points(0, 2) self.assertTrue(array_compare(model.xpt(0, abs_coordinates=True), x2), 'Wrong xpt(0) after swap 1') self.assertTrue(array_compare(model.xpt(1, abs_coordinates=True), x1), 'Wrong xpt(1) after swap 1') self.assertTrue(array_compare(model.xpt(2, abs_coordinates=True), x0), 'Wrong xpt(2) after swap 1') self.assertTrue(array_compare(model.xopt(abs_coordinates=True), x1), 'Wrong xopt after swap 1') model.swap_points(1, 2) self.assertTrue(array_compare(model.xpt(0, abs_coordinates=True), x2), 'Wrong xpt(0) after swap 2') self.assertTrue(array_compare(model.xpt(1, abs_coordinates=True), x0), 'Wrong xpt(1) after swap 2') self.assertTrue(array_compare(model.xpt(2, abs_coordinates=True), x1), 'Wrong xpt(2) after swap 2') self.assertTrue(array_compare(model.xopt(abs_coordinates=True), x1), 'Wrong xopt after swap 2')
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)) xopt = model.xopt() for i in range(npt): c, g, hess = model.lagrange_polynomial(i) # based at xopt for j in range(npt): dx = model.xpt(j) - xopt lag_value = c + model_value(g, hess, dx) expected_value = 1.0 if i == j else 0.0 self.assertAlmostEqual( lag_value, expected_value, msg="Lagrange for x%g has bad value at x%g" % (i, j))
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 = 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) 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)) xopt = model.xopt() for i in range(npt): c, g, hess = model.lagrange_polynomial(i) # based at xopt for j in range(npt): dx = model.xpt(j) - xopt lag_value = c + model_value(g, hess, dx) expected_value = 1.0 if i == j else 0.0 self.assertAlmostEqual( lag_value, expected_value, msg="Lagrange for x%g has bad value at x%g" % (i, j))
def runTest(self): n, m = 2, 2 npt = n + 1 x0 = np.array([-1.2, 1.0]) xl = -1e20 * np.ones((n, )) xu = 1e20 * np.ones((n, )) model = Model(npt, x0, rosenbrock(x0), xl, xu, 1) self.assertEqual(model.npt(), npt, 'Wrong npt after initialisation') self.assertTrue(array_compare(model.xopt(abs_coordinates=True), x0), 'Wrong xopt after initialisation') self.assertTrue(array_compare(model.fopt(), rosenbrock(x0)), 'Wrong fopt after initialisation') # Now add better point x1 = np.array([1.0, 0.9]) rvec = rosenbrock(x1) model.change_point(1, x1 - model.xbase, rvec, allow_kopt_update=True) self.assertEqual(model.npt(), npt, 'Wrong npt after x1') self.assertTrue(array_compare(model.xopt(abs_coordinates=True), x1), 'Wrong xopt after x1') self.assertTrue(array_compare(model.fopt(), rosenbrock(x1)), 'Wrong fopt after x1') # Now add worse point x2 = np.array([2.0, 0.9]) rvec = rosenbrock(x2) model.change_point(2, x2 - model.xbase, rvec, allow_kopt_update=True) self.assertEqual(model.npt(), npt, 'Wrong npt after x2') self.assertTrue(array_compare(model.xpt(0, abs_coordinates=True), x0), 'Wrong xpt(0) after x2') self.assertTrue(array_compare(model.xpt(1, abs_coordinates=True), x1), 'Wrong xpt(1) after x2') self.assertTrue(array_compare(model.xpt(2, abs_coordinates=True), x2), 'Wrong xpt(2) after x2') self.assertTrue(array_compare(model.xopt(abs_coordinates=True), x1), 'Wrong xopt after x2') self.assertTrue(array_compare(model.fopt(), rosenbrock(x1)), 'Wrong fopt after x2') # Now add best point (but don't update kopt) x3 = np.array([1.0, 1.0]) rvec = rosenbrock(x3) model.change_point(0, x3 - model.xbase, rvec, allow_kopt_update=False) # full: overwrite x0 self.assertEqual(model.npt(), npt, 'Wrong npt after x3') self.assertTrue(array_compare(model.xopt(abs_coordinates=True), x1), 'Wrong xopt after x3') self.assertTrue(array_compare(model.fopt(), rosenbrock(x1)), 'Wrong fopt after x3') self.assertAlmostEqual(model.fopt(), rosenbrock(x1), msg='Wrong fopt after x3') self.assertTrue( array_compare(model.xopt(abs_coordinates=True), model.as_absolute_coordinates(model.xopt())), 'Comparison wrong after x3') dirns = model.xpt_directions(include_kopt=True) self.assertTrue(array_compare(x3 - x1, dirns[0, :]), 'Wrong dirn 0') self.assertTrue(array_compare(x1 - x1, dirns[1, :]), 'Wrong dirn 1') self.assertTrue(array_compare(x2 - x1, dirns[2, :]), 'Wrong dirn 2') dirns = model.xpt_directions(include_kopt=False) self.assertTrue(array_compare(x3 - x1, dirns[0, :]), 'Wrong dirn 0 (no kopt)') # self.assertTrue(array_compare(x1 - x1, dirns[1, :]), 'Wrong dirn 1') self.assertTrue(array_compare(x2 - x1, dirns[1, :]), 'Wrong dirn 1 (no kopt)')
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')