Пример #1
0
 def runTest(self):
     n, m = 2, 2
     npt = n + 1
     x0 = np.array([-1.2, 1.0])
     # x0 = np.array([1.0, 2.9])
     xl = -1e2 * np.ones((n,))
     xu = 1e2 * np.ones((n,))
     model = Model(npt, x0, rosenbrock(x0), xl, xu, 1)
     model.add_new_sample(0, rosenbrock(x0))
     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))
     c0, g0 = model.lagrange_gradient(0)
     lag_thresh = 1e-10
     xopt = model.xopt(abs_coordinates=True)
     # print(c0 + np.dot(g0, x0 - xopt), c0, g0)
     self.assertTrue(abs(c0 + np.dot(g0, x0 - xopt) - 1.0) < lag_thresh, 'Bad L0(x0)')
     self.assertTrue(abs(c0 + np.dot(g0, x1 - xopt) - 0.0) < lag_thresh, 'Bad L0(x1)')
     self.assertTrue(abs(c0 + np.dot(g0, x2 - xopt) - 0.0) < lag_thresh, 'Bad L0(x2)')
     c1, g1 = model.lagrange_gradient(1)
     # print(c1 + np.dot(g1, x0 - xopt), c1, g1)
     self.assertTrue(abs(c1 + np.dot(g1, x0 - xopt) - 0.0) < lag_thresh, 'Bad L1(x0)')
     self.assertTrue(abs(c1 + np.dot(g1, x1 - xopt) - 1.0) < lag_thresh, 'Bad L1(x1)')
     self.assertTrue(abs(c1 + np.dot(g1, x2 - xopt) - 0.0) < lag_thresh, 'Bad L1(x2)')
     c2, g2 = model.lagrange_gradient(2)
     self.assertTrue(abs(c2 + np.dot(g2, x0 - xopt) - 0.0) < lag_thresh, 'Bad L2(x0)')
     self.assertTrue(abs(c2 + np.dot(g2, x1 - xopt) - 0.0) < lag_thresh, 'Bad L2(x1)')
     self.assertTrue(abs(c2 + np.dot(g2, x2 - xopt) - 1.0) < lag_thresh, 'Bad L2(x2)')
Пример #2
0
 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)
     model.add_new_sample(0, rosenbrock(x0))
     x1 = np.array([-1.2, 0.9])
     model.change_point(1, x1 - model.xbase, rosenbrock(x1))
     # Here, x0 is base
     # A = model.interpolation_matrix()
     # self.assertTrue(array_compare(A[1, :], delta * np.array([1.0, 0.0])), 'Bad padded interp matrix')
     old_jac = model.model_jac.copy()
     model.interpolate_mini_models_svd(make_full_rank=False)
     J = model.model_jac.copy()
     model.model_jac = old_jac  # reset
     sng_vals = np.linalg.svd(J)[1]
     model.interpolate_mini_models_svd(make_full_rank=True)
     J2 = model.model_jac.copy()
     sng_vals2 = np.linalg.svd(J2)[1]
     print("Original", sng_vals)
     print("After making full rank", sng_vals2)
     self.assertTrue(10 * sng_vals[-1] < sng_vals2[-1],
                     'New singular value too small')
Пример #3
0
 def runTest(self):
     n, m = 2, 2
     npt = n + 1
     x0 = np.array([-1.2, 1.0])
     delta = 0.5
     xl = -1e2 * np.ones((n,))
     xu = 1e2 * np.ones((n,))
     model = Model(npt, x0, rosenbrock(x0), xl, xu, 1)
     model.add_new_sample(0, rosenbrock(x0))
     x1 = x0 + delta * np.array([1.0, 0.0])
     model.change_point(1, x1 - model.xbase, rosenbrock(x1))
     x2 = x0 + delta * np.array([0.0, 1.0])
     model.change_point(2, x2 - model.xbase, rosenbrock(x2))
     model.kopt = 0  # force this
     # Here (use delta=1), Lagrange polynomials are (1-x-y), 1-x and 1-y
     # Maximum value in ball is for (1-x-y) at (x,y)=(1/sqrt2, 1/sqrt2) --> max value = 1 + sqrt(2)
     self.assertTrue(abs(model.poisedness_constant(delta) - 1.0 - sqrt(2.0)) < 1e-6, 'Poisedness constant wrong')
Пример #4
0
 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)
     model.add_new_sample(0, rosenbrock(x0))
     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.ones((3, 3))
     A_expect[0, 1:] = x0 - x1
     A_expect[1, 1:] = x1 - x1  # x1 is xopt in this situation
     A_expect[2, 1:] = x2 - x1
     A_after_scaling = np.dot(np.diag(1.0 / left_scaling), np.dot(A, np.diag(1.0 / right_scaling)))
     self.assertTrue(array_compare(A_after_scaling, A_expect), 'Interp matrix 1')
     # For reference: model based around model.xbase
     interp_ok, interp_error, norm_J_error, linalg_resid, ls_interp_cond_num = model.interpolate_mini_models_svd()
     self.assertTrue(interp_ok, 'Interpolation failed')
     # print(x0, x1, x2)
     # print(rosenbrock(x0), rosenbrock(x1), rosenbrock(x2))
     # print("xbase =", model.xbase)
     # print("xopt =", model.xopt(abs_coordinates=True))
     # print(model.model_const, rosenbrock(model.xbase))
     self.assertTrue(array_compare(model.model_const, rosenbrock(model.xbase), thresh=1e-10), 'Wrong constant term')
     self.assertTrue(array_compare(model.model_value(x0 - model.xbase, d_based_at_xopt=False, with_const_term=True),
                                   rosenbrock(x0), thresh=1e-10), 'Wrong x0')  # allow some inexactness
     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, H = model.build_full_model()
     r = rosenbrock(x1)
     J = model.model_jac
     self.assertTrue(array_compare(g, 2.0*np.dot(J.T, r), thresh=1e-10), 'Bad gradient')
     self.assertTrue(array_compare(H, 2.0*np.dot(J.T, J)), 'Bad Hessian')
Пример #5
0
 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)
     x1 = np.array([1.0, 0.9])
     model.change_point(1, x1 - model.xbase, rosenbrock(x1))
     x2 = np.array([1.0, 1.0])
     model.change_point(2, x2 - model.xbase, rosenbrock(x2))
     self.assertEqual(model.kopt, 2, 'Wrong kopt before resampling')
     # Originally, x2 is the ideal point
     # Here, testing that kopt moves back to x1 after adding heaps of bad x2 samples
     for i in range(10):
         model.add_new_sample(2, np.array([5.0, 5.0]))
     self.assertEqual(model.kopt, 1, 'Wrong kopt after resampling')
Пример #6
0
 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)
     self.assertTrue(array_compare(model.sl, xl - x0), 'Wrong sl after initialisation')
     self.assertTrue(array_compare(model.su, xu - x0), 'Wrong su after initialisation')
     x1 = np.array([1.0, 0.9])
     model.change_point(1, x1 - model.xbase, rosenbrock(x1))
     self.assertTrue(array_compare(model.as_absolute_coordinates(x1 - x0), x1), 'Wrong abs coords')
     self.assertTrue(array_compare(model.as_absolute_coordinates(np.array([-1e3, 1e3])-x0), np.array([-1e2, 1e2])),
                     'Bad abs coords with bounds')
     x2 = np.array([2.0, 0.9])
     model.change_point(2, x2 - model.xbase, rosenbrock(x2))
     sqdists = model.distances_to_xopt()
     self.assertAlmostEqual(sqdists[0], sumsq(x0 - x1), 'Wrong distance 0')
     self.assertAlmostEqual(sqdists[1], sumsq(x1 - x1), 'Wrong distance 1')
     self.assertAlmostEqual(sqdists[2], sumsq(x2 - x1), 'Wrong distance 2')
     model.add_new_sample(0, rosenbrock(x0))
     self.assertEqual(model.nsamples[0], 2, 'Wrong number of samples 0')
     self.assertEqual(model.nsamples[1], 1, 'Wrong number of samples 1')
     self.assertEqual(model.nsamples[2], 1, 'Wrong number of samples 2')
     for i in range(50):
         model.add_new_sample(0, np.array([0.0, 0.0]))
     self.assertEqual(model.kopt, 0, 'Wrong kopt after bad resampling')
     self.assertTrue(array_compare(model.ropt(), 2*rosenbrock(x0)/52), 'Wrong ropt after bad resampling')
     self.assertAlmostEqual(model.fopt(), sumsq(2 * rosenbrock(x0) / 52), 'Wrong fopt after bad resampling')
     d = np.array([10.0, 10.0])
     dirns_old = model.xpt_directions(include_kopt=True)
     model.shift_base(d)
     dirns_new = model.xpt_directions(include_kopt=True)
     self.assertTrue(array_compare(model.xbase, x0 + d), 'Wrong new base')
     self.assertEqual(model.kopt, 0, 'Wrong kopt after shift base')
     for i in range(3):
         self.assertTrue(array_compare(dirns_old[i, :], dirns_new[i, :]), 'Wrong dirn %i after shift base' % i)
     self.assertTrue(array_compare(model.sl, xl - x0 - d), 'Wrong sl after shift base')
     self.assertTrue(array_compare(model.su, xu - x0 - d), 'Wrong su after shift base')
     # save_point and get_final_results
     model.change_point(0, x0 - model.xbase, rosenbrock(x0))  # revert after resampling
     model.change_point(1, x1 - model.xbase, rosenbrock(x1))  # revert after resampling
     x, rvec, f, jacmin, nsamples = model.get_final_results()
     self.assertTrue(array_compare(x, x1), 'Wrong final x')
     self.assertTrue(array_compare(rvec, rosenbrock(x1)), 'Wrong final rvec')
     self.assertAlmostEqual(sumsq(rosenbrock(x1)), f, 'Wrong final f')
     self.assertTrue(array_compare(np.zeros((2,2)), jacmin), 'Wrong final jacmin')
     self.assertEqual(1, nsamples, 'Wrong final nsamples')
     self.assertIsNone(model.xsave, 'xsave not none after initialisation')
     self.assertIsNone(model.rsave, 'rsave not none after initialisation')
     self.assertIsNone(model.fsave, 'fsave not none after initialisation')
     self.assertIsNone(model.nsamples_save, 'nsamples_save not none after initialisation')
     model.save_point(x0, rosenbrock(x0), 1, x_in_abs_coords=True)
     self.assertTrue(array_compare(model.xsave, x0), 'Wrong xsave after saving')
     self.assertTrue(array_compare(model.rsave, rosenbrock(x0)), 'Wrong rsave after saving')
     self.assertAlmostEqual(model.fsave, sumsq(rosenbrock(x0)), 'Wrong fsave after saving')
     self.assertEqual(model.nsamples_save, 1, 'Wrong nsamples_save after saving')
     x, rvec, f, jacmin, nsamples = model.get_final_results()
     self.assertTrue(array_compare(x, x1), 'Wrong final x after saving')
     self.assertTrue(array_compare(rvec, rosenbrock(x1)), 'Wrong final rvec after saving')
     self.assertAlmostEqual(sumsq(rosenbrock(x1)), f, 'Wrong final f after saving')
     self.assertEqual(1, nsamples, 'Wrong final nsamples after saving')
     model.save_point(x2 - model.xbase, np.array([0.0, 0.0]), 2, x_in_abs_coords=False)
     self.assertTrue(array_compare(model.xsave, x2), 'Wrong xsave after saving 2')
     self.assertTrue(array_compare(model.rsave, np.array([0.0, 0.0])), 'Wrong rsave after saving 2')
     self.assertAlmostEqual(model.fsave, 0.0, 'Wrong fsave after saving 2')
     self.assertEqual(model.nsamples_save, 2, 'Wrong nsamples_save after saving 2')
     x, rvec, f, jacmin, nsamples = model.get_final_results()
     self.assertTrue(array_compare(x, x2), 'Wrong final x after saving 2')
     self.assertTrue(array_compare(rvec, np.array([0.0, 0.0])), 'Wrong final rvec after saving 2')
     self.assertAlmostEqual(f, 0.0, 'Wrong final f after saving 2')
     self.assertEqual(2, nsamples, 'Wrong final nsamples after saving 2')
     model.save_point(x0, rosenbrock(x0), 3, x_in_abs_coords=True)  # try to re-save a worse value
     self.assertTrue(array_compare(model.xsave, x2), 'Wrong xsave after saving 3')
     self.assertTrue(array_compare(model.rsave, np.array([0.0, 0.0])), 'Wrong rsave after saving 3')
     self.assertAlmostEqual(model.fsave, 0.0, 'Wrong fsave after saving 3')
     self.assertEqual(model.nsamples_save, 2, 'Wrong nsamples_save after saving 3')