Exemple #1
0
 def test_cg_p(self):
     x = np.array([[0], [0], [0], [0], [0], [0]])
     x_opt = opt.conjugate_gradient(self.p, x, tol=1e-6)
     print('cg')
     print(x_opt)
     print(self.x_opt)
     print()
     self.assertTrue(np.linalg.norm(x_opt - self.x_opt) < 1e-3)
Exemple #2
0
 def test_cg(self):
     P = np.array([[1, 0, 0], [0, 2, 0], [0, 0, 4]])
     v = lambda x: 0.5 * x.T @ P @ x
     del_v = lambda x: x.T @ P
     p = opt.Problem(v, del_v)
     x = np.array([[1], [1], [1]])
     x_opt = np.array([[0], [0], [0]])
     x_cg = opt.conjugate_gradient(p, x)
     self.assertTrue(np.linalg.norm(x_cg - x_opt) < 1e-6)
Exemple #3
0
 def test_cg(self):
     x = np.array([[0], [0], [0], [0], [0], [0]])
     start = time.time()
     x_opt = opt.conjugate_gradient(self.p, x, hist=True)
     end = time.time()
     g = np.array(
         [np.linalg.norm(self.p.grad(x_opt[i])) for i in range(len(x_opt))])
     fig = plt.figure()
     plt.plot(np.arange(len(x_opt)), g)
     plt.xlabel('Iteration')
     plt.ylabel('Norm of Gradient')
     fig.savefig('./fig/cg-pA-grad.eps', format='eps')
     print('\nProblem A, Conjugate Gradient (Exact Gradient)')
     print('arg min v(x) =\n', x_opt[-1])
     print('time =\n', end - start, 's')
Exemple #4
0
 def test_cg(self):
     x = np.array([[10], [10]])
     x_opt = opt.conjugate_gradient(self.p, x, tol=1e-4)
     self.assertTrue(np.linalg.norm(x_opt - self.x_opt) < 1e-3)