예제 #1
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, 5.0)
     self.assertEqual(model.kopt, 1, 'Wrong kopt after resampling')
예제 #2
0
 def runTest(self):
     n = 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.assertAlmostEqual(model.poisedness_constant(delta),
                            1.0 + sqrt(2.0),
                            places=6,
                            msg="Poisedness wrong")
예제 #3
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),
                            msg='Wrong distance 0')
     self.assertAlmostEqual(sqdists[1],
                            sumsq(x1 - x1),
                            msg='Wrong distance 1')
     self.assertAlmostEqual(sqdists[2],
                            sumsq(x2 - x1),
                            msg='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, 0.0)
     self.assertEqual(model.kopt, 0, 'Wrong kopt after bad resampling')
     self.assertTrue(array_compare(model.fopt(), 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, f, gradmin, hessmin, nsamples = model.get_final_results()
     self.assertTrue(array_compare(x, x1), 'Wrong final x')
     self.assertAlmostEqual(rosenbrock(x1), f, msg='Wrong final f')
     self.assertTrue(array_compare(np.zeros((2, )), gradmin),
                     'Wrong final gradmin')
     self.assertTrue(array_compare(np.zeros((2, 2)), hessmin),
                     'Wrong final hessmin')
     self.assertEqual(1, nsamples, 'Wrong final nsamples')
     self.assertIsNone(model.xsave, 'xsave 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.assertAlmostEqual(model.fsave,
                            rosenbrock(x0),
                            msg='Wrong fsave after saving')
     self.assertEqual(model.nsamples_save, 1,
                      'Wrong nsamples_save after saving')
     x, f, gradmin, hessmin, nsamples = model.get_final_results()
     self.assertTrue(array_compare(x, x1), 'Wrong final x after saving')
     self.assertAlmostEqual(rosenbrock(x1),
                            f,
                            msg='Wrong final f after saving')
     self.assertEqual(1, nsamples, 'Wrong final nsamples after saving')
     model.save_point(x2 - model.xbase, 0.0, 2, x_in_abs_coords=False)
     self.assertTrue(array_compare(model.xsave, x2),
                     'Wrong xsave after saving 2')
     self.assertAlmostEqual(model.fsave,
                            0.0,
                            msg='Wrong fsave after saving 2')
     self.assertEqual(model.nsamples_save, 2,
                      'Wrong nsamples_save after saving 2')
     x, f, gradmin, hessmin, nsamples = model.get_final_results()
     self.assertTrue(array_compare(x, x2), 'Wrong final x after saving 2')
     self.assertAlmostEqual(f, 0.0, msg='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.assertAlmostEqual(model.fsave,
                            0.0,
                            msg='Wrong fsave after saving 3')
     self.assertEqual(model.nsamples_save, 2,
                      'Wrong nsamples_save after saving 3')