Пример #1
0
 def test_run_optimization(self):
     f, _ = newton_rhapson(
         x=a([
             [0.0, 1.0],
             [1.0, 1.0],
             [-1.0, 0.0],
             [2.0, 2.0],
             [1.5, -1.0]
         ]),
         f0=a([
             [0.0],
             [0.0],
             [0.0],
             [0.0],
             [0.0],
         ]),
         comparisons=a([
             [3, 1],
             [0, 1],
             [2, 1],
             [4, 0],
             [2, 4],
         ]),
         kernelfunc=default_kernel,
         Hfunc=compute_H,
         gfunc=compute_g,
         sigma=2,
         maxiter=20,
     )
     self.assertTrue(f[3][0] > f[1][0])
     self.assertTrue(f[0][0] > f[1][0])
     self.assertTrue(f[2][0] > f[1][0])
     self.assertTrue(f[4][0] > f[0][0])
     self.assertTrue(f[2][0] > f[4][0])
Пример #2
0
 def test_one_iteration_yields_expected_result(self):
     '''
     Taking our results from the computation of H in the last test,
     H^-1:
     [
     -1.066045352 -0.661325899 -0.301834093
     -0.661325899 -1.045461463 -0.027289758
     -0.301834093 -0.027289758 -1.066045352
     ]
     b (computed fresh):
     [
     [-.603424068]
     [0.0]
     [.603424068]
     ]
     g:
     [
     [-9.616613948]
     [4.388339650]
     [1.558974488]
     ]
     Then, we compute that H^-1 * g:
     [
     [6.879072286]
     [1.729331837]
     [1.120927715]
     ]
     '''
     f1, _ = newton_rhapson(
         x=a([
             [0.0, 1.0],
             [1.0, 1.0],
             [-1.0, 0.0],
         ]),
         f0=a([
             [6.0],
             [1.0],
             [2.0],
         ]),
         comparisons=a([
             [0, 2],
             [2, 0],
         ]),
         kernelfunc=default_kernel,
         Hfunc=compute_H,
         gfunc=compute_g,
         sigma=2.0,
         maxiter=1,
     )
     self.assertAlmostEqual(f1, a([
         [-.879072286],
         [-.729331837],
         [.879072285],
     ]))