From 50ecc25b67734215957959c41f99869c9ebdcdd3 Mon Sep 17 00:00:00 2001 From: Lindon Roberts Date: Thu, 10 Oct 2024 12:27:46 +1100 Subject: [PATCH] Added unit tests for eval num return values --- dfols/model.py | 6 +++--- dfols/tests/test_model.py | 18 ++++++++++++++++++ dfols/tests/test_regu_model.py | 17 +++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/dfols/model.py b/dfols/model.py index d6a4d25..e10c5e0 100644 --- a/dfols/model.py +++ b/dfols/model.py @@ -95,7 +95,7 @@ def __init__(self, npt, x0, r0, xl, xu, projections, r0_nsamples, h=None, argsh= # Model information self.model_const = np.zeros((m, )) # constant term for model m(s) = c + J*s self.model_jac = np.zeros((m, n)) # Jacobian term for model m(s) = c + J*s - self.model_jac_eval_nums = np.zeros((npt,), dtype=int) # which evaluation numbers (1-indexed, nx not nf) were used to build model_jac + self.model_jac_eval_nums = None # which evaluation numbers (1-indexed, nx not nf) were used to build model_jac # Saved point (in absolute coordinates) - always check this value before quitting solver self.xsave = None @@ -266,10 +266,10 @@ def save_point(self, x, rvec, nsamples, eval_num, x_in_abs_coords=True): self.xsave = xabs self.rsave = rvec.copy() self.objsave = obj - self.jacsave = self.model_jac.copy() + self.jacsave = self.model_jac.copy() if self.model_jac is not None else None self.nsamples_save = nsamples self.eval_num_save = eval_num - self.jacsave_eval_nums = self.model_jac_eval_nums.copy() + self.jacsave_eval_nums = self.model_jac_eval_nums.copy() if self.model_jac_eval_nums is not None else None return True else: return False # this value is worse than what we have already - didn't save diff --git a/dfols/tests/test_model.py b/dfols/tests/test_model.py index b164306..bb127f7 100644 --- a/dfols/tests/test_model.py +++ b/dfols/tests/test_model.py @@ -51,6 +51,7 @@ def runTest(self): self.assertTrue(array_compare(model.xopt(abs_coordinates=True), x0), 'Wrong xopt after initialisation') self.assertTrue(array_compare(model.ropt(), rosenbrock(x0)), 'Wrong ropt after initialisation') self.assertAlmostEqual(model.objopt(), sumsq(rosenbrock(x0)), 'Wrong fopt after initialisation') + self.assertTrue(array_compare(model.eval_num, np.array([1,0,0], dtype=int)), 'Wrong eval_num after initialisation') # Now add better point x1 = np.array([1.0, 0.9]) rvec = rosenbrock(x1) @@ -59,6 +60,7 @@ def runTest(self): self.assertTrue(array_compare(model.xopt(abs_coordinates=True), x1), 'Wrong xopt after x1') self.assertTrue(array_compare(model.ropt(), rosenbrock(x1)), 'Wrong ropt after x1') self.assertAlmostEqual(model.objopt(), sumsq(rosenbrock(x1)), 'Wrong fopt after x1') + self.assertTrue(array_compare(model.eval_num, np.array([1,2,0], dtype=int)), 'Wrong eval_num after x1') # Now add worse point x2 = np.array([2.0, 0.9]) rvec = rosenbrock(x2) @@ -70,6 +72,7 @@ def runTest(self): self.assertTrue(array_compare(model.xopt(abs_coordinates=True), x1), 'Wrong xopt after x2') self.assertTrue(array_compare(model.ropt(), rosenbrock(x1)), 'Wrong ropt after x2') self.assertAlmostEqual(model.objopt(), sumsq(rosenbrock(x1)), 'Wrong fopt after x2') + self.assertTrue(array_compare(model.eval_num, np.array([1,2,3], dtype=int)), 'Wrong eval_num after x2') # Now add best point (but don't update kopt) x3 = np.array([1.0, 1.0]) rvec = rosenbrock(x3) @@ -80,6 +83,7 @@ def runTest(self): self.assertAlmostEqual(model.objopt(), sumsq(rosenbrock(x1)), 'Wrong fopt after x3') self.assertTrue(array_compare(model.xopt(abs_coordinates=True), model.as_absolute_coordinates(model.xopt())), 'Comparison wrong after x3') + self.assertTrue(array_compare(model.eval_num, np.array([4,2,3], dtype=int)), 'Wrong eval_num 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') @@ -111,11 +115,13 @@ def runTest(self): self.assertTrue(array_compare(model.xpt(1, abs_coordinates=True), x1), 'Wrong xpt(1) after swap 1') self.assertTrue(array_compare(model.xpt(2, abs_coordinates=True), x0), 'Wrong xpt(2) after swap 1') self.assertTrue(array_compare(model.xopt(abs_coordinates=True), x1), 'Wrong xopt after swap 1') + self.assertTrue(array_compare(model.eval_num, np.array([3,2,1], dtype=int)), 'Wrong eval_num after swap 1') model.swap_points(1, 2) self.assertTrue(array_compare(model.xpt(0, abs_coordinates=True), x2), 'Wrong xpt(0) after swap 2') self.assertTrue(array_compare(model.xpt(1, abs_coordinates=True), x0), 'Wrong xpt(1) after swap 2') self.assertTrue(array_compare(model.xpt(2, abs_coordinates=True), x1), 'Wrong xpt(2) after swap 2') self.assertTrue(array_compare(model.xopt(abs_coordinates=True), x1), 'Wrong xopt after swap 2') + self.assertTrue(array_compare(model.eval_num, np.array([3,1,2], dtype=int)), 'Wrong eval_num after swap 2') class TestBasicManipulation(unittest.TestCase): def runTest(self): @@ -170,31 +176,43 @@ def runTest(self): self.assertIsNone(model.rsave, 'rsave not none after initialisation') self.assertIsNone(model.objsave, 'fsave not none after initialisation') self.assertIsNone(model.nsamples_save, 'nsamples_save not none after initialisation') + self.assertIsNone(model.eval_num_save, 'eval_num_save not none after initialisation') + self.assertIsNone(model.jacsave_eval_nums, 'jacsave_eval_nums not none after initialisation') model.save_point(x0, rosenbrock(x0), 1, 6, 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.objsave, sumsq(rosenbrock(x0)), 'Wrong fsave after saving') self.assertEqual(model.nsamples_save, 1, 'Wrong nsamples_save after saving') + self.assertEqual(model.eval_num_save, 6, 'Wrong eval_num_save after saving') + self.assertIsNone(model.jacsave_eval_nums, 'jacsave_eval_nums not none after saving') x, rvec, f, jacmin, nsamples, x_eval_num, jacmin_eval_nums = 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') + self.assertEqual(x_eval_num, 5, 'Wrong final x_eval_num after saving') + self.assertIsNone(jacmin_eval_nums, 'Final jacmin_eval_nums not none after saving') model.save_point(x2 - model.xbase, np.array([0.0, 0.0]), 2, 7, 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.objsave, 0.0, 'Wrong fsave after saving 2') self.assertEqual(model.nsamples_save, 2, 'Wrong nsamples_save after saving 2') + self.assertEqual(model.eval_num_save, 7, 'Wrong eval_num_save after saving 2') + self.assertIsNone(model.jacsave_eval_nums, 'jacsave_eval_nums not none after saving 2') x, rvec, f, jacmin, nsamples, x_eval_num, jacmin_eval_nums = 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') + self.assertEqual(x_eval_num, 7, 'Wrong final x_eval_num after saving 2') + self.assertIsNone(jacmin_eval_nums, 'Final jacmin_eval_nums not none after saving 2') model.save_point(x0, rosenbrock(x0), 3, 8, 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.objsave, 0.0, 'Wrong fsave after saving 3') self.assertEqual(model.nsamples_save, 2, 'Wrong nsamples_save after saving 3') + self.assertEqual(model.eval_num_save, 7, 'Wrong eval_num_save after saving 2') + self.assertIsNone(model.jacsave_eval_nums, 'jacsave_eval_nums not none after saving 2') class TestAveraging(unittest.TestCase): diff --git a/dfols/tests/test_regu_model.py b/dfols/tests/test_regu_model.py index b9d43f7..b469554 100644 --- a/dfols/tests/test_regu_model.py +++ b/dfols/tests/test_regu_model.py @@ -52,6 +52,7 @@ def runTest(self): self.assertTrue(array_compare(model.xopt(abs_coordinates=True), x0), 'Wrong xopt after initialisation') self.assertTrue(array_compare(model.ropt(), rosenbrock(x0)), 'Wrong ropt after initialisation') self.assertAlmostEqual(model.objopt(), sumsq(rosenbrock(x0))+h(x0), None, 'Wrong objopt after initialisation') + self.assertTrue(array_compare(model.eval_num, np.array([1,0,0], dtype=int)), 'Wrong eval_num after initialisation') # Now add better point x1 = np.array([1.0, 0.9]) rvec = rosenbrock(x1) @@ -60,6 +61,7 @@ def runTest(self): self.assertTrue(array_compare(model.xopt(abs_coordinates=True), x1), 'Wrong xopt after x1') self.assertTrue(array_compare(model.ropt(), rosenbrock(x1)), 'Wrong ropt after x1') self.assertAlmostEqual(model.objopt(), sumsq(rosenbrock(x1))+h(x1), None, 'Wrong objopt after x1') + self.assertTrue(array_compare(model.eval_num, np.array([1,2,0], dtype=int)), 'Wrong eval_num after x1') # Now add worse point x2 = np.array([2.0, 0.9]) rvec = rosenbrock(x2) @@ -71,6 +73,7 @@ def runTest(self): self.assertTrue(array_compare(model.xopt(abs_coordinates=True), x1), 'Wrong xopt after x2') self.assertTrue(array_compare(model.ropt(), rosenbrock(x1)), 'Wrong ropt after x2') self.assertAlmostEqual(model.objopt(), sumsq(rosenbrock(x1))+h(x1), None, 'Wrong objopt after x2') + self.assertTrue(array_compare(model.eval_num, np.array([1,2,3], dtype=int)), 'Wrong eval_num after x2') # Now add best point (but don't update kopt) x3 = np.array([1.0, 1.0]) rvec = rosenbrock(x3) @@ -113,11 +116,13 @@ def runTest(self): self.assertTrue(array_compare(model.xpt(1, abs_coordinates=True), x1), 'Wrong xpt(1) after swap 1') self.assertTrue(array_compare(model.xpt(2, abs_coordinates=True), x0), 'Wrong xpt(2) after swap 1') self.assertTrue(array_compare(model.xopt(abs_coordinates=True), x1), 'Wrong xopt after swap 1') + self.assertTrue(array_compare(model.eval_num, np.array([3,2,1], dtype=int)), 'Wrong eval_num after swap 1') model.swap_points(1, 2) self.assertTrue(array_compare(model.xpt(0, abs_coordinates=True), x2), 'Wrong xpt(0) after swap 2') self.assertTrue(array_compare(model.xpt(1, abs_coordinates=True), x0), 'Wrong xpt(1) after swap 2') self.assertTrue(array_compare(model.xpt(2, abs_coordinates=True), x1), 'Wrong xpt(2) after swap 2') self.assertTrue(array_compare(model.xopt(abs_coordinates=True), x1), 'Wrong xopt after swap 2') + self.assertTrue(array_compare(model.eval_num, np.array([3,1,2], dtype=int)), 'Wrong eval_num after swap 2') class TestBasicManipulation(unittest.TestCase): def runTest(self): @@ -174,31 +179,43 @@ def runTest(self): self.assertIsNone(model.rsave, 'rsave not none after initialisation') self.assertIsNone(model.objsave, 'objsave not none after initialisation') self.assertIsNone(model.nsamples_save, 'nsamples_save not none after initialisation') + self.assertIsNone(model.eval_num_save, 'eval_num_save not none after initialisation') + self.assertIsNone(model.jacsave_eval_nums, 'jacsave_eval_nums not none after initialisation') model.save_point(x0, rosenbrock(x0), 1, 6, 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.objsave, sumsq(rosenbrock(x0))+h(x0), None, 'Wrong objsave after saving') self.assertEqual(model.nsamples_save, 1, 'Wrong nsamples_save after saving') + self.assertEqual(model.eval_num_save, 6, 'Wrong eval_num_save after saving') + self.assertIsNone(model.jacsave_eval_nums, 'jacsave_eval_nums not none after saving') x, rvec, obj, jacmin, nsamples, x_eval_num, jacmin_eval_nums = 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))+h(x1), obj, None, 'Wrong final obj after saving') self.assertEqual(1, nsamples, 'Wrong final nsamples after saving') + self.assertEqual(x_eval_num, 5, 'Wrong final x_eval_num after saving') + self.assertIsNone(jacmin_eval_nums, 'Final jacmin_eval_nums not none after saving') model.save_point(x2 - model.xbase, np.array([0.0, 0.0]), 2, 7, 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.objsave, 0.0 + h(x2), None, 'Wrong objsave after saving 2') self.assertEqual(model.nsamples_save, 2, 'Wrong nsamples_save after saving 2') + self.assertEqual(model.eval_num_save, 7, 'Wrong eval_num_save after saving 2') + self.assertIsNone(model.jacsave_eval_nums, 'jacsave_eval_nums not none after saving 2') x, rvec, obj, jacmin, nsamples, x_eval_num, jacmin_eval_nums = 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(obj, 0.0 + h(x2), None, 'Wrong final obj after saving 2') self.assertEqual(2, nsamples, 'Wrong final nsamples after saving 2') + self.assertEqual(x_eval_num, 7, 'Wrong final x_eval_num after saving 2') + self.assertIsNone(jacmin_eval_nums, 'Final jacmin_eval_nums not none after saving 2') model.save_point(x0, rosenbrock(x0), 3, 8, 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.objsave, 0.0 + h(x2), None, 'Wrong objsave after saving 3') self.assertEqual(model.nsamples_save, 2, 'Wrong nsamples_save after saving 3') + self.assertEqual(model.eval_num_save, 7, 'Wrong eval_num_save after saving 3') + self.assertIsNone(model.jacsave_eval_nums, 'jacsave_eval_nums not none after saving 3') class TestAveraging(unittest.TestCase):