Пример #1
0
 def runTest(self):
     n = 5
     A = np.arange(n ** 2, dtype=np.float).reshape((n, n))
     H = A + A.T  # force symmetric
     hess = Hessian(n, vals=H)
     neghess = -hess
     self.assertTrue(np.allclose(hess.upper_triangular(), -neghess.upper_triangular()), 'Wrong negative values')
Пример #2
0
 def runTest(self):
     n = 4
     nvals = n*(n+1)//2
     hess = Hessian(n)
     self.assertEqual(hess.shape(), (nvals,), 'Wrong shape for initialisation')
     self.assertEqual(hess.dim(), n, 'Wrong dimension')
     self.assertEqual(len(hess), nvals, 'Wrong length')
     self.assertTrue(np.all(hess.upper_triangular() == np.zeros((nvals,))), 'Wrong initialised values')
Пример #3
0
 def runTest(self):
     n = 5
     nvals = n*(n+1)//2
     x = np.arange(nvals, dtype=np.float)
     hess = Hessian(n, vals=x)
     self.assertEqual(hess.shape(), (nvals,), 'Wrong shape for initialisation')
     self.assertEqual(hess.dim(), n, 'Wrong dimension')
     self.assertEqual(len(hess), nvals, 'Wrong length')
     self.assertTrue(np.all(hess.upper_triangular() == x), 'Wrong initialised values')
Пример #4
0
 def runTest(self):
     n = 3
     A = np.arange(n ** 2, dtype=np.float).reshape((n, n))
     H = A + A.T  # force symmetric
     hess = Hessian(n, vals=H)
     for i in range(n):
         for j in range(n):
             self.assertEqual(hess.get_element(i, j), H[i,j], 'Wrong value for (i,j)=(%g,%g): got %g, expecting %g'
                              % (i, j, hess.get_element(i, j), H[i,j]))
Пример #5
0
 def runTest(self):
     n = 3
     nvals = n*(n+1)//2
     A = np.arange(n**2, dtype=np.float).reshape((n,n))
     hess = Hessian(n, vals=A+A.T)  # force symmetric
     self.assertEqual(hess.shape(), (nvals,), 'Wrong shape for initialisation')
     self.assertEqual(hess.dim(), n, 'Wrong dimension')
     self.assertEqual(len(hess), nvals, 'Wrong length')
     self.assertTrue(np.all(hess.upper_triangular() == np.array([0.0, 4.0, 8.0, 8.0, 12.0, 16.0])),
                     'Wrong initialised values')
Пример #6
0
 def runTest(self):
     n = 5
     A = np.arange(n ** 2, dtype=np.float).reshape((n, n))
     H = np.sin(A + A.T)  # force symmetric
     hess = Hessian(n, vals=H)
     vec = np.exp(np.arange(n, dtype=np.float))
     hs = np.dot(H, vec)
     self.assertTrue(array_compare(hess*vec, hs, thresh=1e-12), 'Wrong values')
Пример #7
0
 def runTest(self):
     n = 5
     A = np.arange(n ** 2, dtype=np.float).reshape((n, n))
     H = A + A.T  # force symmetric
     hess = Hessian(n, vals=H)
     # When testing for assertion errors, need lambda to stop assertion from actually happening
     self.assertRaises(AssertionError, lambda: hess * 1.0)
     self.assertRaises(AssertionError, lambda: hess * None)
     self.assertRaises(AssertionError, lambda: hess * [float(i) for i in range(n)])
     self.assertRaises(AssertionError, lambda: hess * np.arange(n-1, dtype=np.float))
     self.assertRaises(AssertionError, lambda: hess * np.arange(n+1, dtype=np.float))
Пример #8
0
 def runTest(self):
     n = 5
     A = np.arange(n**2, dtype=np.float).reshape((n, n))
     H = np.sin(A + A.T)  # force symmetric
     hess = Hessian(n, vals=H)
     vec = np.exp(np.arange(n, dtype=np.float))
     g = np.cos(3 * np.arange(n, dtype=np.float) - 2.0)
     mval = np.dot(g, vec) + 0.5 * np.dot(vec, np.dot(H, vec))
     self.assertAlmostEqual(mval,
                            model_value(g, hess, vec),
                            msg='Wrong value')
Пример #9
0
 def runTest(self):
     xbase = np.array([0.0, 0.0])
     g = np.array([1e-15, -1.0])
     a = np.array([-2.0, -2.0])
     b = np.array([1.0, 2.0])
     hess = Hessian(2)
     delta = 5.0
     c = 0.0
     x = trsbox_geometry(xbase, c, g, hess, a, b, delta)
     xtrue = np.array([0.0, 2.0])
     self.assertTrue(np.max(np.abs(x - xtrue)) < 1e-10, 'Wrong step')
Пример #10
0
 def runTest(self):
     xbase = np.array([0.0, 0.0]) + 1
     g = np.array([1.0, -1.0])
     a = np.array([-2.0, -2.0]) + 1
     b = np.array([1.0, 2.0]) + 1
     hess = Hessian(2)
     delta = 5.0
     c = 3.0  # may want to max instead
     x = trsbox_geometry(xbase, c, g, hess, a, b, delta)
     xtrue = np.array([1.0, -2.0]) + 1
     self.assertTrue(np.max(np.abs(x - xtrue)) < 1e-10, 'Wrong step')
Пример #11
0
 def runTest(self):
     n = 3
     g = np.array([1.0, 0.0, 1.0])
     H = np.array([[1.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 2.0]])
     Delta = 5.0 / 12.0
     hess = Hessian(n, vals=H)
     xopt = np.zeros((n,))
     sl = -1e20 * np.ones((n,))
     su = 1e20 * np.ones((n,))
     d, gnew, crvmin = trsbox(xopt, g, hess, sl, su, Delta)
     true_d = np.array([-1.0 / 3.0, 0.0, -0.25])
     est_min = model_value(g, hess, d)
     true_min = model_value(g, hess, true_d)
     # Hope to get actual correct answer
     # self.assertTrue(np.all(d == true_d), 'Wrong answer')
     # self.assertAlmostEqual(est_min, true_min, 'Wrong min value')
     s_cauchy, red_cauchy, crvmin_cauchy = cauchy_pt(g, hess, Delta)
     self.assertTrue(est_min <= red_cauchy, 'Cauchy reduction not achieved')
     self.assertTrue(np.all(gnew == g + hess.vec_mul(d)), 'Wrong gnew')
     self.assertAlmostEqual(crvmin, 0.0, 'Wrong crvmin')
Пример #12
0
 def runTest(self):
     xbase = np.array([0.0, 0.0, 0.0])
     g = np.array([-1.0, -1.0, -1.0])
     a = np.array([-2.0, -2.0, -2.0])
     b = np.array([0.9, 0.1, 5.0])
     hess = Hessian(3)
     delta = sqrt(3.0)
     c = -1.0  # may want to max instead
     x = trsbox_geometry(xbase, c, g, hess, a, b, delta)
     xtrue = np.array([0.9, 0.1, sqrt(3.0 - 0.81 - 0.01)])
     print(x)
     self.assertTrue(np.max(np.abs(x - xtrue)) < 1e-10, 'Wrong step')
Пример #13
0
 def runTest(self):
     n = 3
     g = np.array([1.0, 0.0, 1.0])
     H = np.array([[1.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 2.0]])
     Delta = 5.0 / 12.0
     hess = Hessian(n, vals=H)
     xopt = np.zeros((n,))
     sl = xopt + np.array([-0.3, -0.01, -0.1])
     su = xopt + np.array([10.0, 1.0, 10.0])
     d, gnew, crvmin = trsbox(xopt, g, hess, sl, su, Delta)
     true_d = np.array([-1.0 / 3.0, 0.0, -0.25])
     est_min = model_value(g, hess, d)
     true_min = model_value(g, hess, true_d)
     # Hope to get actual correct answer
     # self.assertTrue(np.all(d == true_d), 'Wrong answer')
     # self.assertAlmostEqual(est_min, true_min, 'Wrong min value')
     s_cauchy, red_cauchy, crvmin_cauchy = cauchy_pt_box(g, hess, Delta, sl - xopt, su - xopt)
     self.assertTrue(est_min <= red_cauchy, 'Cauchy reduction not achieved')
     self.assertTrue(np.max(np.abs(gnew - g - hess.vec_mul(d))) < 1e-10, 'Wrong gnew')
     print(crvmin)
     self.assertAlmostEqual(crvmin, -1.0, 'Wrong crvmin')
Пример #14
0
 def runTest(self):
     xbase = np.array([0.0, 0.0])
     g = np.array([1e-15, 0.0])
     a = np.array([-2.0, -2.0])
     b = np.array([1.0, 2.0])
     hess = Hessian(2)
     delta = 5.0
     c = 0.0
     x = trsbox_geometry(xbase, c, g, hess, a, b, delta)
     # Since objective is essentially zero, will accept any x within the defined region
     self.assertTrue(np.linalg.norm(x) <= delta)
     self.assertTrue(np.max(x - a) >= 0.0)
     self.assertTrue(np.max(x - b) <= 0.0)
Пример #15
0
 def runTest(self):
     n = 3
     g = np.array([1.0, 0.0, 1.0])
     H = np.array([[1.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 2.0]])
     Delta = 2.0
     hess = Hessian(n, vals=H)
     xopt = np.ones((n,))  # trying nonzero (since bounds inactive)
     sl = xopt + np.array([-0.5, -10.0, -10.0])
     su = xopt + np.array([10.0, 10.0, 10.0])
     d, gnew, crvmin = trsbox(xopt, g, hess, sl, su, Delta)
     true_d = np.array([-1.0, 0.0, -0.5])
     est_min = model_value(g, hess, d)
     true_min = model_value(g, hess, true_d)
     # Hope to get actual correct answer for internal minimum?
     # self.assertTrue(np.all(d == true_d), 'Wrong answer')
     # self.assertAlmostEqual(est_min, true_min, 'Wrong min value')
     s_cauchy, red_cauchy, crvmin_cauchy = cauchy_pt_box(g, hess, Delta, sl-xopt, su-xopt)
     # print(s_cauchy)
     # print(d)
     self.assertTrue(est_min <= red_cauchy, 'Cauchy reduction not achieved')
     self.assertTrue(np.all(gnew == g + hess.vec_mul(d)), 'Wrong gnew')
     print(crvmin)
     self.assertAlmostEqual(crvmin, -1.0, 'Wrong crvmin')
Пример #16
0
 def runTest(self):
     n = 3
     c = 1.0  # changed this value to force max rather than min
     g = np.array([1.0, 0.0, 1.0])
     H = np.array([[-2.0, 0.0, 0.0], [0.0, -1.0, 0.0], [0.0, 0.0, -1.0]])
     Delta = 5.0 / 12.0
     hess = Hessian(n, vals=H)
     xopt = np.zeros((n,))
     sl = -1e20 * np.ones((n,))
     su = 1e20 * np.ones((n,))
     x = trsbox_geometry(xopt, c, g, hess, sl, su, Delta)
     xtrue = np.array([0.25, 0.0, 1.0 / 3.0])  # max solution
     # print(x)
     # print(xtrue)
     self.assertTrue(np.allclose(x, xtrue, atol=1e-3), "Wrong step")
Пример #17
0
 def runTest(self):
     n = 3
     c = -1.0
     g = np.array([1.0, 0.0, 1.0])
     H = np.array([[-2.0, 0.0, 0.0], [0.0, -1.0, 0.0], [0.0, 0.0, -1.0]])
     Delta = 5.0 / 12.0
     hess = Hessian(n, vals=H)
     xopt = np.zeros((n,))
     sl = -1e20 * np.ones((n,))
     su = 1e20 * np.ones((n,))
     x = trsbox_geometry(xopt, c, g, hess, sl, su, Delta)
     xtrue = np.array([-1.0 / 3.0, 0.0, -0.25])
     # print(x)
     # print(xtrue)
     self.assertTrue(np.allclose(x, xtrue, atol=1e-3), "Wrong step")
Пример #18
0
 def runTest(self):
     n = 5
     A = np.arange(n ** 2, dtype=np.float).reshape((n, n))
     H = A + A.T  # force symmetric
     hess = Hessian(n, vals=H)
     # When testing for assertion errors, need lambda to stop assertion from actually happening
     self.assertRaises(AssertionError, lambda: hess.set_element(-1, 0, 1.0))
     self.assertRaises(AssertionError, lambda: hess.set_element(-1, 0, 2.0))
     self.assertRaises(AssertionError, lambda: hess.set_element(-3, n - 1, 3.0))
     self.assertRaises(AssertionError, lambda: hess.set_element(n, 0, 4.0))
     self.assertRaises(AssertionError, lambda: hess.set_element(n + 3, 0, -4.0))
     self.assertRaises(AssertionError, lambda: hess.set_element(n + 7, n - 1, 5.0))
     self.assertRaises(AssertionError, lambda: hess.set_element(0, -1, 6.0))
     self.assertRaises(AssertionError, lambda: hess.set_element(0, -1, 7.0))
     self.assertRaises(AssertionError, lambda: hess.set_element(n - 1, -3, -7.0))
     self.assertRaises(AssertionError, lambda: hess.set_element(0, n, -76.3))
     self.assertRaises(AssertionError, lambda: hess.set_element(0, n + 3, 2.8))
     self.assertRaises(AssertionError, lambda: hess.set_element(n - 1, n + 7, -1.0))
Пример #19
0
 def runTest(self):
     n = 7
     A = np.arange(n ** 2, dtype=np.float).reshape((n, n))
     H = A + A.T  # force symmetric
     hess = Hessian(n, vals=H)
     self.assertTrue(np.all(hess.as_full() == H), 'Wrong values')
Пример #20
0
    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')
Пример #21
0
 def runTest(self):
     n = 4
     A = np.arange(n ** 2, dtype=np.float).reshape((n, n))
     H = A + A.T  # force symmetric
     hess = Hessian(n, vals=H)
     # When testing for assertion errors, need lambda to stop assertion from actually happening
     self.assertRaises(AssertionError, lambda: hess.get_element(-1, 0))
     self.assertRaises(AssertionError, lambda: hess.get_element(-1, 0))
     self.assertRaises(AssertionError, lambda: hess.get_element(-3, n-1))
     self.assertRaises(AssertionError, lambda: hess.get_element(n, 0))
     self.assertRaises(AssertionError, lambda: hess.get_element(n+3, 0))
     self.assertRaises(AssertionError, lambda: hess.get_element(n+7, n-1))
     self.assertRaises(AssertionError, lambda: hess.get_element(0, -1))
     self.assertRaises(AssertionError, lambda: hess.get_element(0, -1))
     self.assertRaises(AssertionError, lambda: hess.get_element(n-1, -3))
     self.assertRaises(AssertionError, lambda: hess.get_element(0, n))
     self.assertRaises(AssertionError, lambda: hess.get_element(0, n+3))
     self.assertRaises(AssertionError, lambda: hess.get_element(n-1, n+7))