def test_disp(self): """Check if something is printed when `disp` is True.""" with Capturing() as output: lbfgs(self.func, self.x0, disp=True) self.assertTrue( len(output) > 0, 'You should print the progress when `disp` is True.')
def test_default(self): """Check if everything works correctly with default parameters.""" with Capturing() as output: x_min, f_min, status = lbfgs(self.func, self.x0) assert_equal(status, 0) self.assertTrue(norm(self.A.dot(x_min) - self.b, np.inf) <= 1e-4) self.assertTrue(abs(f_min) <= 1e-8) self.assertTrue( len(output) == 0, 'You should not print anything by default.')
def test_trace(self): """Check if the history is returned correctly when `trace` is True.""" x_min, f_min, status, hist = lbfgs(self.func, self.x0, trace=True) self.assertTrue(isinstance(hist['f'], np.ndarray)) self.assertTrue(isinstance(hist['norm_g'], np.ndarray)) self.assertTrue(isinstance(hist['n_evals'], np.ndarray)) self.assertTrue(isinstance(hist['elaps_t'], np.ndarray)) assert_equal(len(hist['norm_g']), len(hist['f'])) assert_equal(len(hist['n_evals']), len(hist['f'])) assert_equal(len(hist['elaps_t']), len(hist['f'])) # make sure `hist['n_evals']` is a cumulative sum of integers assert_equal(np.round(hist['n_evals']), hist['n_evals']) self.assertTrue(np.all(hist['n_evals'] >= 0)) self.assertTrue(np.all(hist['n_evals'][1:] - hist['n_evals'][:-1] > 0))
def test_c2(self): """Check if argument `c2` is supported.""" lbfgs(self.func, self.x0, c2=0.1)
def test_c1(self): """Check if argument `c1` is supported.""" lbfgs(self.func, self.x0, c1=0.2)
def test_m(self): """Check if argument `m` is supported.""" lbfgs(self.func, self.x0, m=1)
def test_max_iter(self): """Check if argument `max_iter` is supported.""" lbfgs(self.func, self.x0, max_iter=15)
def test_tol(self): """Check if argument `tol` is supported.""" lbfgs(self.func, self.x0, tol=1e-6)