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')
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]))
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')
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')
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')
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')
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')
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))
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')
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')
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))
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')
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))