예제 #1
0
 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)')
예제 #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)
     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')