From e61335e45e2dc4e5ccf8acf19fc8198fa3b08f13 Mon Sep 17 00:00:00 2001 From: Felix Zimmermann Date: Thu, 8 Jul 2021 13:39:10 +0200 Subject: [PATCH 1/2] Return result with warning if maximum number of iterations is reached On LBFGSERR_MAXIMUMITERATION the result will be returned and a warming emmited Fixes #18 --- lbfgs/_lowlevel.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lbfgs/_lowlevel.pyx b/lbfgs/_lowlevel.pyx index e767128..699f3f2 100644 --- a/lbfgs/_lowlevel.pyx +++ b/lbfgs/_lowlevel.pyx @@ -401,7 +401,7 @@ cdef class LBFGS(object): x_a).copy() return x_array.reshape(x0.shape) - elif r in (LBFGSERR_ROUNDING_ERROR, LBFGSERR_MAXIMUMLINESEARCH) : + elif r in (LBFGSERR_ROUNDING_ERROR, LBFGSERR_MAXIMUMLINESEARCH, LBFGSERR_MAXIMUMITERATION) : warnings.warn(_ERROR_MESSAGES[r]) x_array = np.PyArray_SimpleNewFromData(1, tshape, np.NPY_DOUBLE, x_a).copy() From ac95975496eb12b1fcf9f8939de0e2973b11705f Mon Sep 17 00:00:00 2001 From: Felix Zimmermann Date: Thu, 8 Jul 2021 13:57:10 +0200 Subject: [PATCH 2/2] Change test to use LBFGSERR_INVALID_FTOL as forced exception Change the test to trigger LBFGSERR_INVALID_FTOL to test the emisison of an exception instead of LBFGSERR_MAXIMUMITERATION and instead use the latter to test the emisison of a warning --- tests/test_lbfgs.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_lbfgs.py b/tests/test_lbfgs.py index 7be3d10..afe93f4 100644 --- a/tests/test_lbfgs.py +++ b/tests/test_lbfgs.py @@ -98,8 +98,12 @@ def f(x, g, *args): opt.max_iterations = 3 assert_array_equal(opt.minimize(f, 1e6), [0]) - + opt.max_iterations = 1 + with pytest.warns(UserWarning): + opt.minimize(f, 1e7) + + opt.ftol = -1 with pytest.raises(LBFGSError): opt.minimize(f, 1e7)