def test_display(self): """Check if something is printed when `display` is True.""" with Capturing() as output: nonlinear_conjugate_gradients(self.oracle, self.x0, display=True) self.assertTrue( len(output) > 0, 'You should print the progress when `display` is True.')
def test_line_search_options(self): """Check if argument `line_search_options` is supported.""" nonlinear_conjugate_gradients(self.oracle, self.x0, line_search_options={ 'method': 'Wolfe', 'c1': 0.01, 'c2': 0.01 })
def test_history(self): x0 = -np.array([1.3, 2.7]) x_min, message, history = nonlinear_conjugate_gradients(self.oracle, x0, trace=True, line_search_options={'method': 'Constant', 'c': 0.6}, tolerance=1e-3) func_steps = [25.635000000000005, -7.777199999999997, -7.8463333368073584, -9.439531896665148, -9.41251357036308, -9.4977531453388693] grad_norm_steps = [11.629703349613008, 2.4586174977006889, 2.5711348318091276, 0.48633577943739081, 0.58855118960609565, 0.092585266951596912] time_steps = [0.0] * 6 # Dummy values x_steps = [np.array([-1.3, -2.7]), np.array([0.08, 4.14]), np.array([0.93729171, 4.28518501]), np.array([1.07314317, 2.75959796]), np.array([1.05960886, 2.7072376]), np.array([1.02038104, 3.04515707])] true_history = dict(grad_norm=grad_norm_steps, time=time_steps, x=x_steps, func=func_steps) check_equal_histories(history, true_history)
def test_default(self): """Check if everything works correctly with default parameters.""" with Capturing() as output: x_min, message, _ = nonlinear_conjugate_gradients(self.oracle, self.x0) assert_equal(message, 'success') self.assertEqual(len(output), 0, 'You should not print anything by default.')
def test_quality(self): x_min, message, _ = nonlinear_conjugate_gradients(self.oracle, self.x0, tolerance=1e-5) f_min = self.oracle.func(x_min) g_k_norm_sqr = norm(self.A.dot(x_min) - self.b, 2)**2 g_0_norm_sqr = norm(self.A.dot(self.x0) - self.b, 2)**2 self.assertLessEqual(g_k_norm_sqr, 1e-5 * g_0_norm_sqr) self.assertLessEqual(abs(f_min - self.f_star), 1e-5 * g_0_norm_sqr)
def test_max_iter(self): """Check if argument `max_iter` is supported.""" nonlinear_conjugate_gradients(self.oracle, self.x0, max_iter=15)
def test_tolerance(self): """Check if argument `tolerance` is supported.""" nonlinear_conjugate_gradients(self.oracle, self.x0, tolerance=1e-5)
def test_restart_nu(self): """Check if argument `restart_nu` is supported.""" nonlinear_conjugate_gradients(self.oracle, self.x0, restart_nu=0.3)
def test_scheme(self): """Check if argument `scheme` is supported.""" nonlinear_conjugate_gradients(self.oracle, self.x0, scheme='Fletcher-Reeves')