Esempio n. 1
0
 def test_sec_p(self):
     x = np.array([[0], [0], [0], [0], [0], [0]])
     x_opt = opt.secant(self.p, x, tol=1e-6)
     print('sec')
     print(x_opt)
     print(self.x_opt)
     print()
     self.assertTrue(np.linalg.norm(x_opt - self.x_opt) < 1e-3)
Esempio n. 2
0
 def test_sec(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_sec = opt.secant(p, x)
     self.assertTrue(np.linalg.norm(x_sec - x_opt) < 1e-6)
Esempio n. 3
0
File: demo.py Progetto: sdahdah/opt
 def test_sec(self):
     x = np.array([[0], [0], [0], [0], [0], [0]])
     start = time.time()
     x_opt = opt.secant(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/sec-pA-grad.eps', format='eps')
     print('\nProblem A, Secant (Exact Gradient)')
     print('arg min v(x) =\n', x_opt[-1])
     print('time =\n', end - start, 's')
Esempio n. 4
0
 def test_sec(self):
     x = np.array([[10], [10]])
     x_opt = opt.secant(self.p, x, tol=1e-4)
     self.assertTrue(np.linalg.norm(x_opt - self.x_opt) < 1e-3)
Esempio n. 5
0
 def test_sec(self):
     x = np.array([[0], [0], [0], [0], [0], [0]])
     x_opt = opt.secant(self.p, x)
     self.assertTrue(np.linalg.norm(x_opt - self.x_opt) < 1e-6)