From 39dd3a4f669416f7b4b5be333bc459b3b1c1f8ca Mon Sep 17 00:00:00 2001 From: Yu Feng Date: Thu, 25 Feb 2021 08:57:22 -0800 Subject: [PATCH 1/2] Rename a few functions and remove an unused flag. --- abopt/algs/tests/test_lbfgs.py | 6 +++--- abopt/base.py | 2 -- abopt/testing/__init__.py | 4 ++-- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/abopt/algs/tests/test_lbfgs.py b/abopt/algs/tests/test_lbfgs.py index bf4b618..e9c265c 100644 --- a/abopt/algs/tests/test_lbfgs.py +++ b/abopt/algs/tests/test_lbfgs.py @@ -11,7 +11,7 @@ from abopt.algs.lbfgs import pre_scaled_direct_bfgs, pre_scaled_inverse_dfp from abopt.algs.lbfgs import post_scaled_direct_bfgs, post_scaled_inverse_dfp -from abopt.testing import RosenProblem, ChiSquareProblem, ChiSquareProblem_dual, ChiSquareProblem_dual2 +from abopt.testing import RosenProblem, ChiSquareProblem, ChiSquareProblemWithFG, ChiSquareProblemWithFandFG import numpy from numpy.testing import assert_allclose @@ -65,7 +65,7 @@ def test_abopt_lbfgs_quad(precond): precond = Preconditioner(Pvp=diag_scaling, vPp=diag_scaling) @pytest.mark.parametrize("precond", [None, precond]) -def test_abopt_lbfgs_quad_dual(precond): +def test_abopt_lbfgs_quad_fg(precond): lbfgs = LBFGS(linesearch=backtrace) J = numpy.array([ [0, 0, 2, 1], @@ -73,7 +73,7 @@ def test_abopt_lbfgs_quad_dual(precond): [40, 100, 0, 0], [400, 0, 0, 0]]) - problem = ChiSquareProblem_dual(J=J, precond=precond) + problem = ChiSquareProblemWithFG(J=J, precond=precond) x0 = numpy.zeros(4) r = lbfgs.minimize(problem, x0, monitor=print) diff --git a/abopt/base.py b/abopt/base.py index 69364a6..fe6e4e7 100644 --- a/abopt/base.py +++ b/abopt/base.py @@ -281,8 +281,6 @@ def __init__(self, objective=None, gradient=None, self._precond = precond self.vs = vs - self.problem_dual_eval = False - if objective_gradient is None: if not (objective is not None and gradient is not None): raise ValueError("if objective_gradient is None, gradient and objective cannot be None.") diff --git a/abopt/testing/__init__.py b/abopt/testing/__init__.py index c01f6a8..e444cd8 100644 --- a/abopt/testing/__init__.py +++ b/abopt/testing/__init__.py @@ -2,7 +2,7 @@ from scipy.optimize import rosen, rosen_der, rosen_hess_prod, rosen_hess import numpy -class ChiSquareProblem_dual(Problem): +class ChiSquareProblemWithFG(Problem): """ chisquare problem with @@ -28,7 +28,7 @@ def hessian(x, v): hessian_vector_product = hessian, precond = precond) -class ChiSquareProblem_dual2(Problem): +class ChiSquareProblemWithFandFG(Problem): """ chisquare problem with From 7f8387b3ff981a3660c1598548741e73fcd2b7e1 Mon Sep 17 00:00:00 2001 From: Yu Feng Date: Thu, 25 Feb 2021 09:03:15 -0800 Subject: [PATCH 2/2] Add test for WithFandFG. --- abopt/algs/tests/test_lbfgs.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/abopt/algs/tests/test_lbfgs.py b/abopt/algs/tests/test_lbfgs.py index e9c265c..c491280 100644 --- a/abopt/algs/tests/test_lbfgs.py +++ b/abopt/algs/tests/test_lbfgs.py @@ -80,3 +80,21 @@ def test_abopt_lbfgs_quad_fg(precond): assert r.converged assert_allclose(problem.f(r.x), 0.0, atol=1e-7) +precond = Preconditioner(Pvp=diag_scaling, vPp=diag_scaling) +@pytest.mark.parametrize("precond", +[None, precond]) +def test_abopt_lbfgs_quad_f_fg(precond): + lbfgs = LBFGS(linesearch=backtrace) + + J = numpy.array([ [0, 0, 2, 1], + [0, 10, 2, 0], + [40, 100, 0, 0], + [400, 0, 0, 0]]) + + problem = ChiSquareProblemWithFandFG(J=J, precond=precond) + + x0 = numpy.zeros(4) + r = lbfgs.minimize(problem, x0, monitor=print) + assert r.converged + assert_allclose(problem.f(r.x), 0.0, atol=1e-7) +